Skip to content

Commit

Permalink
Implement BuildHelper with set_env_path method
Browse files Browse the repository at this point in the history
Implemented BuildHelper module and extended it to builder objects. The new set_env_path method provides specialized handling for environment variables that contain paths, utilizing the builder's export method. This functionality is specifically applied to the RUBYLIB and GEM_PATH variables, enhancing how paths are managed and set in the environment.

This update ensures that path-related environment variables are handled more efficiently and consistently, streamlining the setup process for development environments and deployments.
  • Loading branch information
shinokaro committed Jun 16, 2024
1 parent 0a98a77 commit 90fd06e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
8 changes: 5 additions & 3 deletions bin/ocran
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions lib/ocran/build_helper.rb
Original file line number Diff line number Diff line change
@@ -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<String, Pathname>] 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

0 comments on commit 90fd06e

Please sign in to comment.