MCPcopy Create free account
hub / github.com/NatLabRockies/OpenStudio / executeRubyScriptCommand

Function executeRubyScriptCommand

src/cli/UpdateCommand.cpp:58–103  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

56 }
57
58 void executeRubyScriptCommand(openstudio::path rubyScriptPath, ScriptEngineInstance& rubyEngine, const std::vector<std::string>& arguments) {
59 rubyScriptPath = openstudio::filesystem::system_complete(rubyScriptPath);
60 LOG_FREE(Debug, "executeRubyScriptCommand", "Path for the file to run: " << rubyScriptPath);
61 if (!openstudio::filesystem::is_regular_file(rubyScriptPath)) {
62 throw std::runtime_error(fmt::format("Unable to find the file '{}' on the filesystem", rubyScriptPath.string()));
63 }
64
65 std::string cmd = "ARGV.clear\n";
66 for (const auto& arg : arguments) {
67 cmd += fmt::format("ARGV << \"{}\"\n", arg);
68 }
69 cmd += fmt::format(R"(
70begin
71 require '{}'
72 0
73rescue SystemExit => e
74 # puts "System Exit: #{{e.status}}"
75 if !e.success?
76 STDERR.puts
77 STDERR.puts "#{{e.class}}: #{{e.message}} with return code #{{e.status}} "
78 STDERR.puts "Backtrace:\n\t" + e.backtrace.join("\n\t")
79 end
80 e.status
81rescue Exception => e
82 STDERR.puts
83 STDERR.puts "#{{e.class}}: #{{e.message}}"
84 STDERR.puts "Backtrace:\n\t" + e.backtrace.join("\n\t")
85 raise
86end
87 )",
88 rubyScriptPath.generic_string());
89 try {
90 auto ret_so = rubyEngine->eval(cmd);
91 auto ret_code = rubyEngine->getAs<int>(ret_so);
92 // If ret_code is already none zero, just exit faster
93 // Otherwise let it be, so that the at_exit(s) can run in particular (for minitest for eg)
94 if (ret_code != 0) {
95 exit(ret_code);
96 }
97 } catch (...) {
98 // Bail faster though ruby isn't slow like python
99 fmt::print(stderr, "Failed to execute '{}'\n", rubyScriptPath.generic_string());
100 exit(1);
101 // throw std::runtime_error(fmt::format("Failed to execute '{}'\n", rubyScriptPath.generic_string()));
102 }
103 }
104
105 void executePythonScriptCommand(openstudio::path pythonScriptPath, ScriptEngineInstance& pythonEngine, const std::vector<std::string>& arguments) {
106 pythonScriptPath = openstudio::filesystem::system_complete(pythonScriptPath);

Callers 1

mainFunction · 0.85

Calls 2

stringMethod · 0.45
evalMethod · 0.45

Tested by

no test coverage detected