GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 93.8% 180 / 0 / 192
Functions: 100.0% 11 / 0 / 11
Branches: 78.6% 121 / 0 / 154

ruby/bindings/InitRubyBindings.cpp
Line Branch Exec Source
1 /***********************************************************************************************************************
2 * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC.
3 * See also https://openstudio.net/license
4 ***********************************************************************************************************************/
5
6 #include "InitRubyBindings.hpp"
7 #include "ModelicaAntlrCleanup.hpp"
8 #include "../interpreter/RubyEval.hpp"
9
10 // #define HAVE_ISFINITE 1
11 #include <ruby.h>
12 // #undef int128_t
13 // #undef uint128_t
14 // #undef isfinite
15 // #undef memcpy
16 #include "../../src/utilities/core/Filesystem.hpp"
17
18 // You can't use fmt when you include ruby, awesome!
19 // #include <fmt/format.h>
20 // <conan_fmt>/include/fmt/core.h:405:7: error: expected unqualified-id
21 // using int128_t = __int128_t;
22 // ^
23 // <conan_openstudio_ruby>include/ruby-3.2.0/arm64-darwin21/ruby/config.h:202:18: note: expanded from macro 'int128_t'
24 // #define int128_t __int128
25
26 // #include <ranges>
27 #include <boost/range/adaptor/reversed.hpp>
28 #include <iostream>
29 #include <string>
30 #include <sstream>
31 #include <string_view>
32 #include <vector>
33
34 extern "C"
35 {
36 void Init_openstudioairflow(void);
37 void Init_openstudiomodelcore(void);
38 void Init_openstudiomodelsimulation(void);
39 void Init_openstudioutilitiescore(void);
40 void Init_openstudioutilitiesplot(void);
41 void Init_openstudioenergyplus(void);
42 void Init_openstudioepjson(void);
43 void Init_openstudioosversion(void);
44 void Init_openstudioutilitiesdata(void);
45 void Init_openstudioutilitiessql(void);
46 void Init_openstudiogbxml(void);
47 void Init_openstudiogltf(void);
48 void Init_openstudiomodelgenerators(void);
49 void Init_openstudioradiance(void);
50 void Init_openstudioutilitiestime(void);
51 void Init_openstudioisomodel(void);
52 void Init_openstudiomodelgeometry(void);
53 void Init_openstudiosdd(void);
54 void Init_openstudioutilitiesfiletypes(void);
55 void Init_openstudioutilitiesunits(void);
56 void Init_openstudiomodelhvac(void);
57 void Init_openstudiomodelzonehvac(void);
58 void Init_openstudiomodelavailabilitymanager(void);
59 void Init_openstudiomodelplantequipmentoperationscheme(void);
60 void Init_openstudiomodelstraightcomponent(void);
61 void Init_openstudiomodelairflow(void);
62 void Init_openstudiomodelica(void);
63 void Init_openstudioutilities(void);
64 void Init_openstudioutilitiesgeometry(void);
65 void Init_openstudiomeasure(void);
66 void Init_openstudiomodelrefrigeration(void);
67 void Init_openstudioutilitiesbcl(void);
68 void Init_openstudioutilitiesidd(void);
69 void Init_openstudiomodel(void);
70 void Init_openstudiomodelresources(void);
71 void Init_openstudioutilitiesidf(void);
72 void Init_openstudioutilitiesxml(void);
73 void Init_openstudioalfalfa(void);
74
75 ////void Init_openstudiomodeleditor(void); # happens separately in openstudio.so only, for SketchUp plug-in
76 }
77
78 namespace openstudio {
79 namespace ruby {
80
81 362 void initBasicRubyBindings() {
82 362 rb_provide("openstudio");
83 362 rb_provide("openstudio.so");
84
85 362 Init_openstudioutilitiescore();
86 362 rb_provide("openstudioutilitiescore");
87 362 rb_provide("openstudioutilitiescore.so");
88 362 Init_openstudioutilitiestime();
89 362 rb_provide("openstudioutilitiestime");
90 362 rb_provide("openstudioutilitiestime.so");
91 362 Init_openstudioutilitiesdata();
92 362 rb_provide("openstudioutilitiesdata");
93 362 rb_provide("openstudioutilitiesdata.so");
94 362 Init_openstudioutilitiesplot();
95 362 rb_provide("openstudioutilitiesplot");
96 362 rb_provide("openstudioutilitiesplot.so");
97 362 Init_openstudioutilitiesgeometry();
98 362 rb_provide("openstudioutilitiesgeometry");
99 362 rb_provide("openstudioutilitiesgeometry.so");
100 362 Init_openstudioutilitiessql();
101 362 rb_provide("openstudioutilitiessql");
102 362 rb_provide("openstudioutilitiessql.so");
103 362 Init_openstudioutilitiesbcl();
104 362 rb_provide("openstudioutilitiesbcl");
105 362 rb_provide("openstudioutilitiesbcl.so");
106 362 Init_openstudioutilitiesunits();
107 362 rb_provide("openstudioutilitiesunits");
108 362 rb_provide("openstudioutilitiesunits.so");
109 362 Init_openstudioutilitiesidd();
110 362 rb_provide("openstudioutilitiesidd");
111 362 rb_provide("openstudioutilitiesidd.so");
112 362 Init_openstudioutilitiesidf();
113 362 rb_provide("openstudioutilitiesidf");
114 362 rb_provide("openstudioutilitiesidf.so");
115 362 Init_openstudioutilitiesfiletypes();
116 362 rb_provide("openstudioutilitiesfiletypes");
117 362 rb_provide("openstudioutilitiesfiletypes.so");
118 362 Init_openstudioutilitiesxml();
119 362 rb_provide("openstudioutilitiesxml");
120 362 rb_provide("openstudioutilitiesxml.so");
121 362 Init_openstudioalfalfa();
122 362 rb_provide("openstudioalfalfa");
123 362 rb_provide("openstudioalfalfa.so");
124 362 Init_openstudioutilities();
125 362 rb_provide("openstudioutilities");
126 362 rb_provide("openstudioutilities.so");
127 362 }
128
129 355 void initExtendedRubyBindings() {
130
1/1
✓ Branch 1 taken 355 times.
355 Init_openstudiomodel();
131
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiomodel");
132
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiomodel.so");
133
1/1
✓ Branch 1 taken 355 times.
355 Init_openstudiomodelica();
134 // Ensure the ANTLR cleanup hook is armed as soon as the Modelica bindings are loaded so Ruby
135 // can unwind cleanly at interpreter shutdown without dlclose() crashes.
136
1/1
✓ Branch 1 taken 355 times.
355 openstudio::ruby::registerModelicaAntlrCleanup();
137
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiomodelica");
138
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiomodelica.so");
139
1/1
✓ Branch 1 taken 355 times.
355 Init_openstudiomodelcore();
140
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiomodelcore");
141
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiomodelcore.so");
142
1/1
✓ Branch 1 taken 355 times.
355 Init_openstudiomodelsimulation();
143
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiomodelsimulation");
144
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiomodelsimulation.so");
145
1/1
✓ Branch 1 taken 355 times.
355 Init_openstudiomodelresources();
146
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiomodelresources");
147
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiomodelresources.so");
148
1/1
✓ Branch 1 taken 355 times.
355 Init_openstudiomodelgeometry();
149
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiomodelgeometry");
150
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiomodelgeometry.so");
151
1/1
✓ Branch 1 taken 355 times.
355 Init_openstudiomodelhvac();
152
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiomodelhvac");
153
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiomodelhvac.so");
154
1/1
✓ Branch 1 taken 355 times.
355 Init_openstudiomodelzonehvac();
155
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiomodelzonehvac");
156
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiomodelzonehvac.so");
157
1/1
✓ Branch 1 taken 355 times.
355 Init_openstudiomodelavailabilitymanager();
158
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiomodelavailabilitymanager");
159
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiomodelavailabilitymanager.so");
160
1/1
✓ Branch 1 taken 355 times.
355 Init_openstudiomodelplantequipmentoperationscheme();
161
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiomodelplantequipmentoperationscheme");
162
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiomodelplantequipmentoperationscheme.so");
163
1/1
✓ Branch 1 taken 355 times.
355 Init_openstudiomodelstraightcomponent();
164
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiomodelstraightcomponent");
165
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiomodelstraightcomponent.so");
166
1/1
✓ Branch 1 taken 355 times.
355 Init_openstudiomodelairflow();
167
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiomodelairflow");
168
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiomodelairflow.so");
169
1/1
✓ Branch 1 taken 355 times.
355 Init_openstudiomodelrefrigeration();
170
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiomodelrefrigeration");
171
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiomodelrefrigeration.so");
172
1/1
✓ Branch 1 taken 355 times.
355 Init_openstudiomodelgenerators();
173
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiomodelgenerators");
174
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiomodelgenerators.so");
175
176
1/1
✓ Branch 1 taken 355 times.
355 Init_openstudioenergyplus();
177
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudioenergyplus");
178
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudioenergyplus.so");
179
1/1
✓ Branch 1 taken 355 times.
355 Init_openstudioepjson();
180
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudioepjson");
181
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudioepjson.so");
182
1/1
✓ Branch 1 taken 355 times.
355 Init_openstudioradiance();
183
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudioradiance");
184
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudioradiance.so");
185
1/1
✓ Branch 1 taken 355 times.
355 Init_openstudiogbxml();
186
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiogbxml");
187
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiogbxml.so");
188
1/1
✓ Branch 1 taken 355 times.
355 Init_openstudiogltf();
189
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiogltf");
190
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiogltf.so");
191
1/1
✓ Branch 1 taken 355 times.
355 Init_openstudioairflow();
192
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudioairflow");
193
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudioairflow.so");
194
1/1
✓ Branch 1 taken 355 times.
355 Init_openstudioosversion();
195
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudioversion");
196
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudioversion.so");
197
1/1
✓ Branch 1 taken 355 times.
355 Init_openstudiomeasure();
198
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiomeasure");
199
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiomeasure.so");
200
1/1
✓ Branch 1 taken 355 times.
355 Init_openstudioisomodel();
201
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudioisomodel");
202
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudioisomodel.so");
203
1/1
✓ Branch 1 taken 355 times.
355 Init_openstudiosdd();
204
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiosdd");
205
1/1
✓ Branch 1 taken 355 times.
355 rb_provide("openstudiosdd.so");
206
207 //Init_openstudiomodeleditor(); # happens separately in openstudio.so only, for SketchUp plug-in
208 //rb_provide("openstudiomodeleditor");
209 //rb_provide("openstudiomodeleditor.so");
210
211 // "typedefs" for backwards compatibility
212 // keep synchronized with \openstudiocore\src\utilities\core\RubyInterpreter.hpp
213
1/1
✓ Branch 1 taken 355 times.
355 std::string ruby_typedef_script = R"ruby(
214 module OpenStudio
215 module Ruleset
216
217 # support for name deprecated as of 0.10.1
218 class UserScriptArgument < OpenStudio::Measure::OSArgument
219 def initialize
220 OpenStudio::logFree(OpenStudio::Warn, "OpenStudio.Measure", "UserScriptArgument is deprecated, use OpenStudio::Measure::Argument instead.")
221 super
222 end
223 end
224
225 # support for name deprecated as of 0.10.1
226 class OptionalUserScriptArgument < OpenStudio::Measure::OptionalOSArgument
227 def initialize
228 OpenStudio::logFree(OpenStudio::Warn, "OpenStudio.Measure", "OptionalUserScriptArgument is deprecated, use OpenStudio::Measure::OptionalOSArgument instead.")
229 super
230 end
231 end
232
233 # support for name deprecated as of 0.10.1
234 class UserScriptArgumentVector < OpenStudio::Measure::OSArgumentVector
235 def initialize
236 OpenStudio::logFree(OpenStudio::Warn, "OpenStudio.Measure", "UserScriptArgumentVector is deprecated, use OpenStudio::Measure::OSArgumentVector instead.")
237 super
238 end
239 end
240
241 # support for name deprecated as of 0.10.1
242 class UserScriptArgumentMap < OpenStudio::Measure::OSArgumentMap
243 def initialize
244 OpenStudio::logFree(OpenStudio::Warn, "OpenStudio.Measure", "UserScriptArgumentMap is deprecated, use OpenStudio::Measure::OSArgumentMap instead.")
245 super
246 end
247 end
248
249 # support for name deprecated as of 2.0.0
250 class UserScript < OpenStudio::Measure::OSMeasure
251 def initialize
252 OpenStudio::logFree(OpenStudio::Warn, "OpenStudio.Measure", "UserScript is deprecated, use OpenStudio::Measure::OSMeasure instead.")
253 super
254 end
255 end
256
257 # support for name deprecated as of 2.0.0
258 class ModelUserScript < OpenStudio::Measure::ModelMeasure
259 def initialize
260 OpenStudio::logFree(OpenStudio::Warn, "OpenStudio.Measure", "ModelUserScript is deprecated, use OpenStudio::Measure::ModelMeasure instead.")
261 super
262 end
263 end
264
265 # support for name deprecated as of 2.0.0
266 class WorkspaceUserScript < OpenStudio::Measure::EnergyPlusMeasure
267 def initialize
268 OpenStudio::logFree(OpenStudio::Warn, "OpenStudio.Measure", "WorkspaceUserScript is deprecated, use OpenStudio::Measure::EnergyPlusMeasure instead.")
269 super
270 end
271 end
272
273 # support for name deprecated as of 2.0.0
274 class ReportingUserScript < OpenStudio::Measure::ReportingMeasure
275 def initialize
276 OpenStudio::logFree(OpenStudio::Warn, "OpenStudio.Measure", "ReportingUserScript is deprecated, use OpenStudio::Measure::ReportingMeasure instead.")
277 super
278 end
279 end
280
281 # support for name deprecated as of 2.0.0
282 class OSArgument < OpenStudio::Measure::OSArgument
283 def initialize
284 OpenStudio::logFree(OpenStudio::Warn, "OpenStudio.Measure", "OSArgument is deprecated, use OpenStudio::Measure::OSArgument instead.")
285 super
286 end
287 end
288
289 # support for name deprecated as of 2.0.0
290 def self.convertOSArgumentVectorToMap(argument_vector)
291 OpenStudio::logFree(OpenStudio::Warn, "OpenStudio.Measure", "OpenStudio::Ruleset::convertOSArgumentVectorToMap is deprecated, use OpenStudio::Measure::convertOSArgumentVectorToMap instead.")
292 return OpenStudio::Measure::convertOSArgumentVectorToMap(argument_vector)
293 end
294
295 # support for name deprecated as of 2.0.0
296 class OSArgumentVector < OpenStudio::Measure::OSArgumentVector
297 def initialize
298 OpenStudio::logFree(OpenStudio::Warn, "OpenStudio.Measure", "OSArgumentVector is deprecated, use OpenStudio::Measure::OSArgumentVector instead.")
299 super
300 end
301 end
302
303 # support for name deprecated as of 2.0.0
304 class OSArgumentMap < OpenStudio::Measure::OSArgumentMap
305 def initialize
306 OpenStudio::logFree(OpenStudio::Warn, "OpenStudio.Measure", "OSArgumentMap is deprecated, use OpenStudio::Measure::OSArgumentMap instead.")
307 super
308 end
309 end
310
311 # class was replaced by OpenStudio::WorkflowStepResult
312 # # support for name deprecated as of 2.0.0
313 # class OSResult < OpenStudio::Measure::OSResult
314 # def initialize
315 # OpenStudio::logFree(OpenStudio::Warn, "OpenStudio.Measure", "OSResult is deprecated, use OpenStudio::Measure::OSResult instead.")
316 # super
317 # end
318 # end
319 #
320 # # support for name deprecated as of 2.0.0
321 # class OSResultVector < OpenStudio::Measure::OSResultVector
322 # def initialize
323 # OpenStudio::logFree(OpenStudio::Warn, "OpenStudio.Measure", "OSResultVector is deprecated, use OpenStudio::Measure::OSResultVector instead.")
324 # super
325 # end
326 # end
327
328 # support for name deprecated as of 2.0.0
329 class OSRunner < OpenStudio::Measure::OSRunner
330 def initialize(workflow_json = nil)
331 OpenStudio::logFree(OpenStudio::Warn, "OpenStudio.Measure", "OSRunner is deprecated, use OpenStudio::Measure::OSRunner instead.")
332 if workflow_json.nil?
333 workflow_json = OpenStudio::WorkflowJSON.new
334 OpenStudio::logFree(OpenStudio::Warn, "OpenStudio.Measure", "No workflow provided, using empty WorkflowJSON.")
335 end
336 super(workflow_json)
337 end
338 end
339
340 # support for name deprecated as of 2.0.0
341 class RubyUserScriptInfo < OpenStudio::Measure::OSMeasureInfo
342 def initialize(*args)
343 OpenStudio::logFree(OpenStudio::Warn, "OpenStudio.Measure", "RubyUserScriptInfo is deprecated, use OpenStudio::Measure::OSMeasureInfo instead.")
344 if args.size == 1
345 super(args[0])
346 elsif args.size == 8
347 super(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8])
348 end
349 end
350 end
351
352 # support for name deprecated as of 2.0.0
353 def self.infoExtractorRubyFunction
354 OpenStudio::logFree(OpenStudio::Warn, "OpenStudio.Measure", "Ruleset is deprecated, use OpenStudio::Measure::infoExtractorRubyFunction instead.")
355 return OpenStudio::Measure.infoExtractorRubyFunction
356 end
357
358 # support for name deprecated as of 2.0.0
359 def self.getInfo(measure, model, workspace)
360 OpenStudio::logFree(OpenStudio::Warn, "OpenStudio.Measure", "Ruleset is deprecated, use OpenStudio::Measure::getInfo instead.")
361 return OpenStudio::Measure.getInfo(measure, model, workspace)
362 end
363
364 # support for name deprecated as of 2.0.0
365 def self.makeChoiceArgumentOfWorkspaceObjects(name, iddObjectType, workspace, required=true)
366 OpenStudio::logFree(OpenStudio::Warn, "OpenStudio.Measure", "Ruleset is deprecated, use OpenStudio::Measure::makeChoiceArgumentOfWorkspaceObjects instead.")
367 return OpenStudio::Measure.makeChoiceArgumentOfWorkspaceObjects(name, iddObjectType, workspace, required)
368 end
369
370 end # module Ruleset
371
372 module Measure
373 class RubyMeasureInfoBinding < OpenStudio::Measure::MeasureInfoBinding
374 def initialize
375 super() # needed
376 @info = nil
377 end
378 def setMeasureInfo(info)
379 @info = info
380 end
381 def measure_info
382 @info
383 end
384 def error
385 @info.error.to_s
386 end
387 def measureType
388 @info.measureType.valueName.to_s
389 end
390 def className
391 @info.className.to_s
392 end
393 def name
394 @info.name.to_s
395 end
396 def description
397 @info.description.to_s
398 end
399 def taxonomy
400 @info.taxonomy.to_s
401 end
402 def modelerDescription
403 @info.modelerDescription.to_s
404 end
405 def arguments
406 @info.arguments.map{|a| a.toJSON()}
407 end
408 def outputs
409 @info.outputs.map{|a| a.toJSON()}
410 end
411 def get_binding
412 result = binding()
413 return result
414 end
415 def renderFile(readme_in_path)
416 require 'erb'
417 begin
418 readme_out_path = File.join(File.dirname(readme_in_path), File.basename(readme_in_path, File.extname(readme_in_path)))
419 readme_in = File.read(readme_in_path)
420 renderer = ERB.new(readme_in)
421 b = get_binding()
422 readme_out = renderer.result(b)
423 rescue => e
424 exception_msg = "Failed to Render ERB file: #{e.class}: #{e.message}\nTraceback:\n"
425 exception_msg += e.backtrace.join("\n")
426 STDERR.puts exception_msg
427 # info = OpenStudio::Measure::OSMeasureInfo.new(exception_msg)
428 return false # @info
429 end
430 # write README.md file
431 File.open(readme_out_path, 'w') do |file|
432 file << readme_out
433 # make sure data is written to the disk one way or the other
434 begin
435 file.fsync
436 rescue StandardError
437 file.flush
438 end
439 end
440 return true # @info
441 end
442 end
443 end # module Measure
444
445 end # module OpenStudio
446
447 module OpenStudio
448 def self.getSharedResourcesPath()
449 OpenStudio::logFree(OpenStudio::Warn, "OpenStudio", "getSharedResourcesPath is deprecated.")
450 return OpenStudio::Path.new()
451 end
452 end # module OpenStudio
453
454 )ruby";
455
456
1/1
✓ Branch 1 taken 355 times.
355 openstudio::evalString(ruby_typedef_script);
457 355 }
458
459 223 void initEmbeddedGems() {
460
461 // Note: We instantiate a global $logger here
462 // It used to be a ruby Logger (require 'logger'), and we would have needed to set the log level based on the CLI parameters passed
463 // Instead, I mimic it by providing a convenience class that forwards to the OpenStudio logger, which takes care of the log level
464
465
1/1
✓ Branch 1 taken 223 times.
223 std::string initScript = R"ruby(
466 # This is the save puts to use to catch EPIPE. Uses `puts` on the given IO object and safely ignores any Errno::EPIPE
467 #
468 # @param [String] message Message to output
469 # @param [Hash] opts Options hash
470 #
471 def safe_puts(message=nil, opts=nil)
472 message ||= ''
473 opts = {
474 io: $stdout,
475 printer: :puts
476 }.merge(opts || {})
477
478 begin
479 opts[:io].send(opts[:printer], message)
480 rescue Errno::EPIPE
481 # This is what makes this a `safe` puts
482 return
483 end
484 end
485
486 require 'stringio' # TODO: remove this once json-schema version is > 3.4.0, (cf https://github.com/voxpupuli/json-schema/pull/512)
487 require 'rbconfig'
488
489 # require 'logger'
490 # $logger = Logger.new(STDOUT)
491 # $logger.level = Logger::DEBUG #WARN
492 # Mimic ruby Logger, but forward to OpenStudio, so we don't have to worry about setting the LogLevel
493 class ConvenienceLogger
494
495 attr_accessor :level
496
497 def initialize(log_level)
498 @level = log_level
499 end
500
501 def trace?
502 @level <= OpenStudio::Trace
503 end
504
505 def trace(msg)
506 OpenStudio::logFree(OpenStudio::Trace, "ruby", msg)
507 end
508
509 def debug(msg)
510 OpenStudio::logFree(OpenStudio::Debug, "ruby", msg)
511 end
512
513 def info(msg)
514 OpenStudio::logFree(OpenStudio::Info, "ruby", msg)
515 end
516
517 def warn(msg)
518 OpenStudio::logFree(OpenStudio::Warn, "ruby", msg)
519 end
520
521 def error(msg)
522 OpenStudio::logFree(OpenStudio::Error, "ruby", msg)
523 end
524
525 def fatal(msg)
526 OpenStudio::logFree(OpenStudio::Fatal, "ruby", msg)
527 end
528
529 end
530 $logger = ConvenienceLogger.new(OpenStudio::Logger.instance.standardOutLogger.logLevel.get)
531
532 # debug Gem::Resolver, must go before resolver is required
533 if $logger.trace?
534 ENV['DEBUG_RESOLVER'] = "1"
535 end
536
537 original_arch = nil
538 if RbConfig::CONFIG['arch'] =~ /x64-mswin64/
539 # assume that system ruby of 'x64-mingw32' architecture was used to create bundle
540 original_arch = RbConfig::CONFIG['arch']
541 RbConfig::CONFIG['arch'] = 'x64-mingw32'
542 end
543
544 # load embedded ruby gems
545 # This loads :/ruby/gems/3.2.0/specifications/default
546 require 'rubygems'
547 require 'rubygems/version'
548 Gem::Platform.local
549
550 # Note that we have three standard locations where we have gemspecs
551 #
552 # 1. This is the Ruby-built in stuff (eg; csv, fileutils, irb, json, reline, etc)
553 #':/ruby/gems/3.2.0/specifications/default',
554 #
555 # 2. This is the Ruby-built in stuff that's brought in via gems/bundled_gems (https://github.com/ruby/ruby/blob/f91480d7a671b1b114270a4b5e4d3c5aa6dabce9/gems/bundled_gems)
556 # (rbs, debug, matrix, rexml, etc)
557 # ':/ruby/gems/3.2.0/specifications',
558 #
559 # 3. Where we have the openstudio-gems gemspecs (openstudio-standards, etc)
560 #':/ruby/3.2.0/specifications',
561
562 if original_arch
563 RbConfig::CONFIG['arch'] = original_arch
564 end
565 )ruby";
566
567
1/1
✓ Branch 1 taken 223 times.
223 openstudio::evalString(initScript);
568
569 #ifndef _WIN32 // no io/console available
570
1/1
✓ Branch 1 taken 223 times.
223 const std::string irbPatch = R"ruby(if $logger.trace?
571 require 'irb'
572 require 'irb/lc/error'
573
574 # ENV["IRB_LANG"] = "C"
575 # puts "Reline encoding_system_needs: #{Reline.encoding_system_needs.name}"
576 # puts "Reline::IOGate.encoding: #{Reline::IOGate.encoding}"
577
578 # Workaround issue in line_editor.rb where it fails to read a full block
579 # unicode character. Probably because of the way we embbed it in C++
580 # The full block is \xe2\x96\x88
581 ENV['RELINE_ALT_SCROLLBAR'] = "1"
582
583 class Reline::LineEditor
584 def reset(prompt = '', encoding:)
585 @rest_height = (Reline::IOGate.get_screen_size.first - 1) - Reline::IOGate.cursor_pos.y
586 @screen_size = Reline::IOGate.get_screen_size
587 @screen_height = @screen_size.first
588 reset_variables(prompt, encoding: encoding)
589 Reline::IOGate.set_winch_handler do
590 @resized = true
591 end
592 if ENV.key?('RELINE_ALT_SCROLLBAR')
593 @full_block = '::'
594 @upper_half_block = "''"
595 @lower_half_block = '..'
596 @block_elem_width = 2
597 elsif Reline::IOGate.win?
598 @full_block = 'â–ˆ'
599 @upper_half_block = 'â–€'
600 @lower_half_block = 'â–„'
601 @block_elem_width = 1
602 elsif @encoding == Encoding::UTF_8
603 @full_block = 'â–ˆ'
604 @upper_half_block = 'â–€'
605 @lower_half_block = 'â–„'
606 @block_elem_width = Reline::Unicode.calculate_width('â–ˆ')
607 else
608 @full_block = '::'
609 @upper_half_block = "''"
610 @lower_half_block = '..'
611 @block_elem_width = 2
612 end
613 end
614 end
615
616 module IRB # :nodoc:
617 class Locale
618
619 alias :original_load :load
620 alias :original_require :require
621
622 def require(file, priv = nil)
623 # puts "require: #{file}"
624 original_require(file, priv)
625 end
626
627 # Some shenanigans being done to detect localized files, and that relies ton File.readable? and co...
628 def load(file, priv=nil)
629 # puts "file=#{file}"
630 if file == 'irb/error.rb'
631 $".push file
632 @@loaded << 'irb/lc/error.rb'
633 return
634 end
635 original_load(file, priv)
636 end
637
638 end
639
640
641 def self.start_session(binding)
642 unless @__initialized
643 args = ARGV
644 ARGV.replace(ARGV.dup)
645 IRB.setup(nil)
646 # locale = @CONF[:LC_MESSAGES]
647 # p locale
648
649 ARGV.replace(args)
650 @__initialized = true
651 end
652
653 @CONF[:IRB_RC].call(irb.context) if @CONF[:IRB_RC]
654 # puts @CONF[:PROMPT_MODE]
655 os_version = "3.10.0" # OpenStudio::openStudioVersion
656 @CONF[:PROMPT][:OPENSTUDIO] = {
657 :PROMPT_I=>"(os #{os_version}) :%03n > ",
658 :PROMPT_S=>"(os #{os_version}) :%03n%l> ",
659 :PROMPT_C=>"(os #{os_version}) :%03n > ",
660 :PROMPT_N=>"(os #{os_version}) :%03n?> ",
661 :RETURN=>" => %s \n",
662 :AUTO_INDENT=>true
663 }
664 @CONF[:PROMPT_MODE] = :OPENSTUDIO
665 workspace = WorkSpace.new(binding)
666 input_method = IRB::RelineInputMethod.new
667 irb = Irb.new(workspace, input_method)
668
669
670 @CONF[:MAIN_CONTEXT] = irb.context
671
672 catch(:IRB_EXIT) do
673 irb.eval_input
674 end
675 end
676 end
677 end)ruby";
678
679
1/1
✓ Branch 1 taken 223 times.
223 openstudio::evalString(irbPatch);
680 #endif
681 223 }
682
683 223 void setupEmbeddedGemsClearEnvVars() {
684
685 // in hash.c there's lot of env-related functions, but all private, aside from rb_env_clear();
686
1/1
✓ Branch 1 taken 223 times.
223 const std::string clearEnvs = R"ruby(
687 ENV.reject! { |k, _| ['GEM', 'BUNDLE'].any? { |x| k.start_with?(x) } }
688 # No env variable isn't the same as empty. Gem::PathSupport does stuff like @home = env["GEM_HOME"] || Gem.default_dir
689 ENV['GEM_HOME'] = ''
690 ENV['GEM_PATH'] = ''
691 ENV['GEM_SPEC_CACHE'] = ''
692 )ruby";
693
694
1/1
✓ Branch 1 taken 223 times.
223 openstudio::evalString(clearEnvs);
695 223 };
696
697 223 void addIncludeDirsToLoadPaths(const std::vector<openstudio::path>& includeDirs) {
698
1/2
✓ Branch 1 taken 223 times.
✗ Branch 2 not taken.
223 if (includeDirs.empty()) {
699 223 return;
700 }
701
702 ✗ VALUE load_path = rb_gv_get("$LOAD_PATH");
703
704 // Apparently this isn't exposed: `ary_resize_capa(rb_array_len(load_path) + includeDirs.size())``
705
706 // Can't use ranges! starts at clang 15.0, damn.
707 // for (const auto& dirPath : includeDirs | std::views::reverse) {
708 ✗ for (const auto& dirPath : includeDirs | boost::adaptors::reversed) {
709 // Insert at 0, in reverse order. Same a includeDirs.concat($LOAD_PATH) in ruby
710 ✗ rb_ary_unshift(load_path, rb_str_new2(dirPath.generic_string().c_str()));
711 }
712 }
713
714 5 void setRubyEnvVarStr(std::string_view name, std::string_view val) {
715
716
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
5 if (val.empty()) {
717 ✗ return;
718 }
719
720 // const auto envVarCmd = fmt::format(
721 // R"ruby(
722 // $logger.info "Setting {0} to {1}"
723 // ENV['{0}'] = '{1}'
724 // )ruby",
725 // name, val);
726
727
1/1
✓ Branch 1 taken 5 times.
5 std::stringstream ss;
728 ss << "$logger.info \"Setting " << name << " to " << val << "\"\n"
729
10/10
✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
✓ Branch 7 taken 5 times.
✓ Branch 10 taken 5 times.
✓ Branch 13 taken 5 times.
✓ Branch 16 taken 5 times.
✓ Branch 19 taken 5 times.
✓ Branch 22 taken 5 times.
✓ Branch 25 taken 5 times.
✓ Branch 28 taken 5 times.
5 << "ENV['" << name << "'] = '" << val << "'\n";
730
2/2
✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
5 openstudio::evalString(ss.str());
731 5 }
732
733 228 void setRubyEnvVarPath(std::string_view name, const openstudio::path& p) {
734
735
2/2
✓ Branch 1 taken 223 times.
✓ Branch 2 taken 5 times.
228 if (p.empty()) {
736 223 return;
737 }
738
739
2/2
✓ Branch 1 taken 5 times.
✓ Branch 5 taken 5 times.
5 setRubyEnvVarStr(name, p.generic_string());
740 }
741
742 223 void setGemPathDir(const std::vector<openstudio::path>& gemPathDirs) {
743
1/2
✓ Branch 1 taken 223 times.
✗ Branch 2 not taken.
223 if (gemPathDirs.empty()) {
744 223 return;
745 }
746
747 ✗ std::string result;
748 ✗ for (const auto& gemDirPath : gemPathDirs) {
749 ✗ const auto& s = gemDirPath.generic_string();
750 ✗ result += result.empty() ? s : std::string{openstudio::path::preferred_separator} + s;
751 ✗ }
752 ✗ setRubyEnvVarStr("GEM_PATH", result);
753 ✗ }
754
755 223 void locateEmbeddedGems(bool use_bundler) {
756 // It is important to find, eval, and call add_spec for all of the gemspecs
757 // that are in the embedded file system. The rubgems find/load mechanims will
758 // attempt to use a number of Ruby file, directory, and path operations that
759 // are not compaitable with the embdded file system. By doing the find, eval,
760 // add_spec routine up front, rubygems will avoid trying to load things off of
761 // the embedded file system itself.
762 //
763 // A previous version of this routine added embedded file paths to the gem
764 // path, such as Gem.paths.path << ':/ruby/3.2.0/gems/'. This is not
765 // sustainable because it requires shims for many of the Ruby file operations
766 // so that they work with embedded files. The solution here avoids File
767 // operations on embdded file system with the exception of our own
768 // EmbeddedScripting::getFileAsString
769
770
1/1
✓ Branch 1 taken 223 times.
223 std::string initCmd = R"ruby(
771
772 # This prevents default system paths from creeping in
773 class Gem::PathSupport
774 def default_path
775 return []
776 end
777 end
778
779 # module Gem
780 # def self.default_dir
781 # @default_dir = ""
782 # end
783 #
784 # def self.find_home
785 # ""
786 # end
787 #
788 # def self.default_specifications_dir
789 # @default_specifications_dir = ""
790 # end
791 # end
792 #
793 Gem.clear_paths
794 Gem::Deprecate.skip = true
795
796 # find all the embedded gems
797 original_embedded_gems = {}
798 begin
799 EmbeddedScripting::fileNames.each do |f|
800 if md = /specifications\/.*\.gemspec$/.match(f) ||
801 md = /bundler\/.*\.gemspec$/.match(f)
802 begin
803 spec = EmbeddedScripting::getFileAsString(f)
804 s = eval(spec)
805
806 # These require io-console, which we don't have on Windows
807 if Gem.win_platform?
808 next if s.name == 'reline'
809 next if s.name == 'debug'
810 next if s.name == 'irb'
811 next if s.name == 'readline'
812 end
813
814 s.loaded_from = f
815 # This is shenanigans because otherwise rubygems will think extensions are missing
816 # But we are initing them manually so they are not missing
817 # Here the missing_extensions? method is redefined for only this instance "s"
818 class << s
819 define_method(:missing_extensions?) { false }
820 end
821 original_embedded_gems[s.name] = s
822
823 init_count = 0
824 Gem::Specification.each {|x| init_count += 1}
825
826 # if already have an equivalent spec this will be a no-op
827 Gem::Specification.add_spec(s)
828
829 post_count = 0
830 Gem::Specification.each {|x| post_count += 1}
831
832 if post_count == init_count
833 $logger.debug "Found system gem #{s.name} #{s.version}, overrides embedded gem"
834 end
835
836 rescue LoadError => e
837 safe_puts e.message
838 rescue => e
839 safe_puts e.message
840 end
841 end
842 end
843 rescue NameError => e
844 # EmbeddedScripting not available
845 end
846
847 original_load_path = $LOAD_PATH.clone
848 embedded_gems_to_activate = []
849
850 # Identify the embedded gems (don't activate them yet)
851 current_dir = Dir.pwd
852 begin
853 # get a list of all the embedded gems
854 dependencies = []
855 original_embedded_gems.each_value do |spec|
856 $logger.debug "Adding dependency on #{spec.name} '~> #{spec.version}'"
857 dependencies << Gem::Dependency.new(spec.name, "~> #{spec.version}")
858 end
859
860 # resolve dependencies
861 activation_errors = false
862 resolver = Gem::Resolver.for_current_gems(dependencies)
863 activation_requests = resolver.resolve
864 $logger.debug "Processing #{activation_requests.size} activation requests"
865 activation_requests.each do |request|
866 do_activate = true
867 spec = request.spec
868
869 # check if this is one of our embedded gems
870 if original_embedded_gems[spec.name]
871 # check if gem can be loaded from RUBYLIB, this supports developer use case
872 original_load_path.each do |lp|
873 if File.exist?(File.join(lp, spec.name)) || File.exist?(File.join(lp, spec.name + '.rb')) || File.exist?(File.join(lp, spec.name + '.so'))
874 $logger.debug "Found #{spec.name} in '#{lp}', overrides gem #{spec.spec_file}"
875 do_activate = false
876 break
877 end
878 end
879 end
880 if do_activate
881 embedded_gems_to_activate << spec
882 end
883
884 end
885
886 if activation_errors
887 return false
888 end
889 ensure
890 Dir.chdir(current_dir)
891 end
892 )ruby";
893
894
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 221 times.
223 if (use_bundler) {
895
1/1
✓ Branch 1 taken 2 times.
2 initCmd += R"ruby(
896 $logger.trace "embedded_gems_to_activate=#{embedded_gems_to_activate.map{ |s| s.name} }"
897
898 # Load the bundle before activating any embedded gems
899 embedded_gems_to_activate.each do |spec|
900 if spec.name == "bundler"
901 $logger.debug "Activating Bundler gem #{spec.spec_file}"
902 # if $logger.trace?
903 # pp spec
904 # end
905
906 begin
907 # Activate will manipulate the $LOAD_PATH to include the gem
908 spec.activate
909 rescue Gem::LoadError => e
910 # There may be conflicts between the bundle and the embedded gems,
911 # those will be logged here
912 exception_msg = "Error activating gem #{spec.spec_file}: #{e.class}: #{e.message}\nTraceback:\n"
913 exception_msg += e.backtrace.join("\n")
914 STDERR.puts exception_msg
915 $logger.error "Error activating gem #{spec.spec_file}"
916 activation_errors = true
917 end
918 end
919 end
920
921 current_dir = Dir.pwd
922
923 original_arch = nil
924 if RbConfig::CONFIG['arch'] =~ /x64-mswin64/
925 # assume that system ruby of 'x64-mingw32' architecture was used to create bundle
926 original_arch = RbConfig::CONFIG['arch']
927 $logger.info "Temporarily replacing arch '#{original_arch}' with 'x64-mingw32' for Bundle"
928 RbConfig::CONFIG['arch'] = 'x64-mingw32'
929 end
930
931 $logger.trace "Requiring bundler"
932 # require bundler
933 # have to do some forward declaration and pre-require to get around autoload cycles
934 require 'bundler/errors'
935 #require 'bundler/environment_preserver'
936 require 'bundler/plugin'
937 #require 'bundler/rubygems_ext'
938 require 'bundler/rubygems_integration'
939
940 # Global list, to be populated below
941 $ignore_native_gem_names = []
942
943 module Bundler
944 class RubygemsIntegration
945
946 alias :ori_spec_missing_extensions? :spec_missing_extensions?
947
948 def spec_missing_extensions?(spec, default = true)
949
950 # This avoids getting an annoying message for no reason
951 if $ignore_native_gem_names.any? {|name| name == spec.name }
952 return false
953 end
954
955 return ori_spec_missing_extensions?(spec, default)
956 end
957 end
958 end
959
960 require 'bundler/version'
961 require 'bundler/ruby_version'
962 #require 'bundler/constants'
963 #require 'bundler/current_ruby'
964 require 'bundler/gem_helpers'
965 #require 'bundler/plugin'
966 require 'bundler/source'
967 require 'bundler/definition'
968 require 'bundler/dsl'
969 require 'bundler/uri_credentials_filter'
970 require 'bundler/uri_normalizer'
971 require 'bundler/index'
972 require 'bundler/digest'
973 require 'bundler/source/git'
974 require 'bundler/source/git/git_proxy'
975 require 'bundler/match_remote_metadata'
976 require 'bundler/remote_specification'
977 require 'bundler/stub_specification'
978 require 'bundler'
979
980 begin
981 # activate bundled gems
982 # bundler will look in:
983 # 1) ENV["BUNDLE_GEMFILE"]
984 # 2) find_file("Gemfile", "gems.rb")
985 #require 'bundler/setup'
986
987 if $logger.trace?
988 Bundler.ui.level = "debug"
989 end
990
991 groups = Bundler.definition.groups
992 keep_groups = []
993 without_groups = ENV['BUNDLE_WITHOUT']
994 $logger.info "without_groups = #{without_groups}"
995 groups.each do |g|
996 $logger.info "g = #{g}"
997 if without_groups.include?(g.to_s)
998 $logger.info "Bundling without group '#{g}'"
999 else
1000 keep_groups << g
1001 end
1002 end
1003
1004 $logger.info "Bundling with groups [#{keep_groups.join(',')}]"
1005
1006 # Filter out dependencies with native extensions that we already have embedded in the CLI and that satisfy the requirements
1007 locked_specs = Bundler.definition.instance_variable_get(:@locked_specs)
1008
1009 extra_load_paths = []
1010
1011 embedded_gems_to_activate.each do |spec|
1012 if spec.extensions.empty?
1013 # $logger.trace "Spec #{spec.name} has no extensions"
1014 next
1015 end
1016
1017 unless locked_specs.any? {|locked_spec| locked_spec.name == spec.name}
1018 # $logger.trace "#{spec.name} not in locked_specs"
1019 next
1020 end
1021
1022 $logger.trace "embedded_gems_to_activate.each: spec: #{spec.name}"
1023
1024 dep_to_requirements = {}
1025 locked_specs.each do |locked_spec|
1026 deleted_deps = locked_spec.dependencies.select { |dep| dep.name == spec.name && dep.requirement.satisfied_by?(spec.version) }
1027 locked_spec.dependencies.reject! { |dep| dep.name == spec.name && dep.requirement.satisfied_by?(spec.version) }
1028 deleted_deps&.each do |dep|
1029 dep_to_requirements[locked_spec.name] = dep.requirement.to_s
1030 end
1031 end
1032
1033 if locked_specs.none? { |locked_spec| locked_spec.dependencies.any? {|x| x.name == spec.name} }
1034 locked_spec = locked_specs.select{|locked_spec| locked_spec.name == spec.name}.first
1035 $logger.debug("Removing native gem #{spec.name} from Bundler locked_specs (version #{locked_spec.version}), using the embedded dependency (CLI one has version #{spec.version})")
1036 $logger.trace("Gems having #{spec.name} as a dependency: #{dep_to_requirements}")
1037 locked_spec.dependencies.each do |dep|
1038 $logger.trace("Activating dependency #{dep.name} first")
1039 found_spec = embedded_gems_to_activate.find { |x| x.name == dep.name }
1040 raise "Can't find #{dep.name}" unless found_spec
1041
1042 next if found_spec.activated
1043
1044 # found_spec.load_paths.reject {|path| $LOAD_PATH.include?(path) }
1045 Gem::Specification.add_spec(found_spec) # Will add it to Gem::Specification.stubs too
1046 raise "Failed to activate #{spec.name}" unless found_spec.activate
1047 Bundler.rubygems.mark_loaded(found_spec)
1048 stub = Gem::Specification.stubs_for(found_spec.name).first
1049 bundler_stub = Bundler::StubSpecification.from_stub(stub)
1050 if !locked_specs[dep].nil?
1051 x = locked_specs[dep].first
1052 x.instance_variable_set(:@version, found_spec.version)
1053 class <<x
1054 private
1055 def use_exact_resolved_specifications?
1056 @use_exact_resolved_specifications = false
1057 end
1058 end
1059 # locked_specs.delete_by_name(dep.name)
1060 # locked_specs[dep] = bundler_stub
1061 extra_load_paths += found_spec.full_require_paths
1062 end
1063 end
1064
1065 Gem::Specification.add_spec(spec)
1066 raise "Failed to activate #{spec.name}" unless spec.activate
1067 Bundler.rubygems.mark_loaded(spec)
1068 stub = Gem::Specification.stubs_for(spec.name).first
1069 bundler_stub = Bundler::StubSpecification.from_stub(stub)
1070 raise "That shouldn't happen" if locked_specs[spec.name].nil?
1071 # locked_specs.delete_by_name(spec.name)
1072 # locked_specs[spec.name] = bundler_stub
1073
1074 extra_load_paths += spec.full_require_paths
1075
1076 $ignore_native_gem_names << spec.name
1077 end
1078 end
1079
1080
1081 # I am pretty sure sure we don't need this. Because internally Bundler.definition.filter_specs will ignore it from the dependencies if it's there as well
1082 # Bundler.definition.instance_variable_set(:@dependencies, Bundler.definition.dependencies.select { |dep| !$ignore_native_gem_names.include?(dep.name) } )
1083
1084 remaining_specs = []
1085 Bundler.definition.specs_for(keep_groups).each {|s| remaining_specs << s.name}
1086
1087 $logger.info "Specs to be included [#{remaining_specs.join(',')}]"
1088
1089 # This is going to remove some stuff from the LOAD_PATH that we already loaded, when we really don't want to, so we readd them...
1090 Bundler.setup(*keep_groups)
1091
1092 Bundler.rubygems.add_to_load_path(extra_load_paths - $LOAD_PATH)
1093
1094 ensure
1095
1096 if original_arch
1097 $logger.info "Restoring arch '#{original_arch}'"
1098 RbConfig::CONFIG['arch'] = original_arch
1099 end
1100
1101 Dir.chdir(current_dir)
1102
1103 # clean up the global
1104 $ignore_native_gem_names = nil
1105 end
1106 )ruby";
1107 }
1108
1109
1/1
✓ Branch 1 taken 223 times.
223 initCmd += R"ruby(
1110
1111 original_load_path = $LOAD_PATH.clone
1112
1113 embedded_gems_to_activate.each do |spec|
1114 $logger.debug "Activating gem #{spec.spec_file}"
1115 begin
1116 # Activate will manipulate the $LOAD_PATH to include the gem
1117 spec.activate
1118 rescue Gem::LoadError
1119 # There may be conflicts between the bundle and the embedded gems,
1120 # those will be logged here
1121 $logger.error "Error activating gem #{spec.spec_file}"
1122 activation_errors = true
1123 end
1124 end
1125
1126 # Get all of the embedded gem paths which were added by activating the embedded gems
1127 # This is used by embedded_help::require
1128 $EMBEDDED_GEM_PATH = $LOAD_PATH - original_load_path
1129 # Make sure no non embedded paths snuck in
1130 $EMBEDDED_GEM_PATH = $EMBEDDED_GEM_PATH.select {|p| p.to_s.chars.first == ':'}
1131 # Restore LOAD_PATH
1132 $LOAD_PATH.reject! {|p| not original_load_path.any? p}
1133 )ruby";
1134
1135
1/1
✓ Branch 1 taken 222 times.
223 openstudio::evalString(initCmd);
1136 223 }
1137
1138 223 void setupEmbeddedGems(const std::vector<openstudio::path>& includeDirs, const std::vector<openstudio::path>& gemPathDirs,
1139 const openstudio::path& gemHomeDir, const openstudio::path& bundleGemFilePath, const openstudio::path& bundleGemDirPath,
1140 const std::string& bundleWithoutGroups) {
1141
1142 223 setupEmbeddedGemsClearEnvVars();
1143 223 initEmbeddedGems();
1144
1145 223 addIncludeDirsToLoadPaths(includeDirs);
1146 223 setGemPathDir(gemPathDirs);
1147
1/1
✓ Branch 2 taken 223 times.
223 setRubyEnvVarPath("GEM_HOME", gemHomeDir);
1148 223 bool use_bundler = false;
1149
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 221 times.
223 if (!bundleGemFilePath.empty()) {
1150
1/1
✓ Branch 2 taken 2 times.
2 setRubyEnvVarPath("BUNDLE_GEMFILE", bundleGemFilePath);
1151 2 use_bundler = true;
1152 }
1153
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 222 times.
223 if (!bundleGemDirPath.empty()) {
1154
1/1
✓ Branch 2 taken 1 time.
1 setRubyEnvVarPath("BUNDLE_PATH", bundleGemDirPath);
1155
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 221 times.
222 } else if (use_bundler) {
1156 // bundle was requested but bundle_path was not provided
1157 1 std::cout << "Warn: Bundle activated but ENV['BUNDLE_PATH'] is not set" << '\n' << "Info: Setting BUNDLE_PATH to ':/ruby/3.2.0/'" << std::endl;
1158 }
1159
1160
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 223 times.
223 if (!bundleWithoutGroups.empty()) {
1161 ✗ setRubyEnvVarPath("BUNDLE_WITHOUT", bundleWithoutGroups);
1162
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 221 times.
223 } else if (use_bundler) {
1163 // bundle was requested but bundle_path was not provided
1164 2 std::cout << "Warn: Bundle activated but ENV['BUNDLE_WITHOUT'] is not set" << '\n' << "Info: Setting BUNDLE_WITHOUT to 'test'" << std::endl;
1165
2/2
✓ Branch 1 taken 2 times.
✓ Branch 5 taken 2 times.
2 setRubyEnvVarPath("BUNDLE_WITHOUT", "test");
1166 }
1167
1168 223 locateEmbeddedGems(use_bundler);
1169 222 }
1170
1171 117 void initRubyBindings() {
1172 117 initBasicRubyBindings();
1173 117 initExtendedRubyBindings();
1174 117 }
1175
1176 } // namespace ruby
1177 } // namespace openstudio
1178