Skip to content

Commit

Permalink
Add find_load_path to RuntimeEnvironment and update method calls
Browse files Browse the repository at this point in the history
Added the find_load_path method to the RuntimeEnvironment class to handle path finding based on the post-execution environment. Updated all existing calls to find_load_path to use the method from the post_env instance, ensuring that the load paths are resolved in the context of the environment after script execution.
  • Loading branch information
shinokaro committed Jul 9, 2024
1 parent c80928b commit 3ec5494
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
11 changes: 8 additions & 3 deletions bin/ocran
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ module Ocran
features = features.filter_map do |feature|
if feature.absolute?
feature
elsif (load_path = find_load_path(post_env.load_path, feature))
elsif (load_path = post_env.find_load_path(feature))
feature.expand_path(load_path)
else
# This message occurs when paths for core library files (e.g., enumerator.so,
Expand Down Expand Up @@ -421,8 +421,13 @@ module Ocran
added_load_paths = (post_env.load_path - @pre_env.load_path).map { |load_path| Pathname(post_env.expand_path(load_path)) }
working_directory = Pathname(post_env.pwd)
features.each do |feature|
abs_load_path = find_load_path(all_load_paths, feature)
if abs_load_path.nil? || abs_load_path == Pathname.pwd
load_path = post_env.find_load_path(feature)
if load_path.nil?
source_files << feature
next
end
abs_load_path = Pathname(post_env.expand_path(load_path))
if abs_load_path == Pathname.pwd
source_files << feature
else
fullpath = feature.expand_path(abs_load_path)
Expand Down
16 changes: 16 additions & 0 deletions lib/ocran/runtime_environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,21 @@ def initialize
def expand_path(path)
File.expand_path(path, @pwd)
end

def find_load_path(path)
path = Pathname(path) unless path.is_a?(Pathname)

if path.absolute?
# For an absolute path feature, find the load path that contains the feature
# and determine the longest matching path (most specific path).
@load_path.select { |load_path| path.subpath?(expand_path(load_path)) }
.max_by { |load_path| expand_path(load_path).length }
else
# For a relative path feature, find the load path where the expanded feature exists
# and select the longest load path (most specific path).
@load_path.select { |load_path| path.expand_path(load_path).exist? }
.max_by { |load_path| load_path.length }
end
end
end
end

0 comments on commit 3ec5494

Please sign in to comment.