From 3ec5494e0fd00f2e33ef90aa493c534ec1620b86 Mon Sep 17 00:00:00 2001 From: shinokaro Date: Tue, 9 Jul 2024 22:09:57 +0900 Subject: [PATCH] Add find_load_path to RuntimeEnvironment and update method calls 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. --- bin/ocran | 11 ++++++++--- lib/ocran/runtime_environment.rb | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/bin/ocran b/bin/ocran index d180123..d815a9f 100644 --- a/bin/ocran +++ b/bin/ocran @@ -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, @@ -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) diff --git a/lib/ocran/runtime_environment.rb b/lib/ocran/runtime_environment.rb index 3623a7b..b0714b9 100644 --- a/lib/ocran/runtime_environment.rb +++ b/lib/ocran/runtime_environment.rb @@ -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