diff --git a/bin/ocran b/bin/ocran index e798cad..d345042 100644 --- a/bin/ocran +++ b/bin/ocran @@ -640,6 +640,8 @@ EOF Ocran.msg "Building #{Ocran.output_executable}" direction = proc do |builder| + require_relative "../lib/ocran/build_helper" + builder.extend(BuildHelper) # Recompute the src_prefix. Files may have been added implicitly # while scanning through features. files = source_files.map(&:expand_path) @@ -761,9 +763,9 @@ EOF builder.export("RUBYOPT", Ocran.rubyopt.gsub(%r(-r#{Regexp.escape(RbConfig::TOPDIR)}(/.*/bundler/setup)), "")) # Add the load path that are required with the correct path after # src_prefix was adjusted. - load_path = src_load_path.map { |path| TEMPDIR_ROOT / SRCDIR / path.relative_path_from(src_prefix) } - builder.export("RUBYLIB", load_path.uniq.join(";")) - builder.export("GEM_PATH", TEMPDIR_ROOT / GEMHOMEDIR) + load_path = src_load_path.map { |path| SRCDIR / path.relative_path_from(src_prefix) }.uniq + builder.set_env_path("RUBYLIB", *load_path) + builder.set_env_path("GEM_PATH", GEMHOMEDIR) # Add the opcode to launch the script installed_ruby_exe = BINDIR / Ocran.ruby_executable diff --git a/lib/ocran/build_helper.rb b/lib/ocran/build_helper.rb new file mode 100644 index 0000000..0addb8a --- /dev/null +++ b/lib/ocran/build_helper.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +module Ocran + module BuildHelper + # Sets an environment variable with a joined path value. + # This method processes an array of path strings or Pathname objects, accepts + # absolute paths as is, and appends a placeholder to relative paths to convert + # them into absolute paths. The converted paths are then joined into a single + # string using the system's path separator. + # + # @param name [String] the name of the environment variable to set. + # @param paths [Array] an array of path arguments which can + # be either absolute or relative. + # + # Example: + # set_env_path("RUBYLIB", "lib", "ext", "vendor/lib") + # # This sets RUBYLIB to a string such as "C:/ProjectRoot/lib;C:/ProjectRoot/ext;C:/ProjectRoot/vendor/lib" + # # assuming each path is correctly converted to an absolute path through a placeholder. + # + def set_env_path(name, *paths) + value = paths.map { |path| + if File.absolute_path?(path) + path + else + File.join(TEMPDIR_ROOT, path) + end + }.join(File::PATH_SEPARATOR) + + export(name, value) + end + end +end