From fe53b9934bea5d5c6d7310f60e397c53205001ef Mon Sep 17 00:00:00 2001 From: shinokaro Date: Wed, 29 May 2024 23:59:49 +0900 Subject: [PATCH] prepended Pathname in build_exe --- bin/ocran | 11 +- lib/ocran/host_config_helper.rb | 1 - lib/ocran/refine_pathname.rb | 172 ++++++++++++++++---------------- 3 files changed, 90 insertions(+), 94 deletions(-) diff --git a/bin/ocran b/bin/ocran index a0df514..f1604e3 100644 --- a/bin/ocran +++ b/bin/ocran @@ -1,12 +1,8 @@ #!/usr/bin/env ruby # -*- ruby -*- # encoding: UTF-8 -require "pathname" -require_relative "../lib/ocran/refine_pathname" -module Ocran - - using RefinePathname +module Ocran IGNORE_MODULE_NAMES = /\A(enumerator.so|rational.so|complex.so|fiber.so|thread.rb|ruby2_keywords.rb)\z/ GEM_SCRIPT_RE = /\.rbw?$/ @@ -528,6 +524,9 @@ EOF end def Ocran.build_exe + require "pathname" + require_relative "../lib/ocran/refine_pathname" + ::Pathname.prepend(RefinePathname) all_load_paths = $LOAD_PATH.map { |loadpath| Pathname.new(loadpath).expand_path } @added_load_paths = ($LOAD_PATH - @load_path_before).map { |loadpath| Pathname.new(loadpath).expand_path } working_directory = Pathname.pwd @@ -802,7 +801,7 @@ EOF end end end # module Ocran -using Ocran::RefinePathname + if File.basename(__FILE__) == File.basename($0) Ocran.init(ARGV) ARGV.replace(Ocran.arg) diff --git a/lib/ocran/host_config_helper.rb b/lib/ocran/host_config_helper.rb index 1e3ef90..489a5b8 100644 --- a/lib/ocran/host_config_helper.rb +++ b/lib/ocran/host_config_helper.rb @@ -4,7 +4,6 @@ module Ocran # Variables describing the host's build environment. module HostConfigHelper - using RefinePathname module_function def exec_prefix diff --git a/lib/ocran/refine_pathname.rb b/lib/ocran/refine_pathname.rb index a75fbb5..c30ad91 100644 --- a/lib/ocran/refine_pathname.rb +++ b/lib/ocran/refine_pathname.rb @@ -5,107 +5,105 @@ module Ocran # is case sensitive and doesn't handle paths with mixed path # separators. module RefinePathname - refine Pathname do - # Compares two paths for equality based on the case sensitivity of the - # Ruby execution environment's file system. - # If the file system is case-insensitive, it performs a case-insensitive - # comparison. Otherwise, it performs a case-sensitive comparison. - def pathequal(a, b) - if File::FNM_SYSCASE.nonzero? - a.casecmp(b) == 0 - else - a == b - end + # Compares two paths for equality based on the case sensitivity of the + # Ruby execution environment's file system. + # If the file system is case-insensitive, it performs a case-insensitive + # comparison. Otherwise, it performs a case-sensitive comparison. + def pathequal(a, b) + if File::FNM_SYSCASE.nonzero? + a.casecmp(b) == 0 + else + a == b end - private :pathequal + end + private :pathequal - def to_posix - if File::ALT_SEPARATOR - to_s.tr(File::ALT_SEPARATOR, File::SEPARATOR) - else - to_s - end + def to_posix + if File::ALT_SEPARATOR + to_s.tr(File::ALT_SEPARATOR, File::SEPARATOR) + else + to_s end + end - # Checks if two Pathname objects are equal, considering the file system's - # case sensitivity and path separators. Returns false if the other object is not - # an Pathname. - # This method enables the use of the `uniq` method on arrays of Pathname objects. - def eql?(other) - return false unless other.is_a?(Pathname) + # Checks if two Pathname objects are equal, considering the file system's + # case sensitivity and path separators. Returns false if the other object is not + # an Pathname. + # This method enables the use of the `uniq` method on arrays of Pathname objects. + def eql?(other) + return false unless other.is_a?(Pathname) - a = to_posix - b = other.to_posix - pathequal(a, b) - end + a = to_posix + b = other.to_posix + pathequal(a, b) + end - alias == eql? - alias === eql? + alias == eql? + alias === eql? - # The drive_letter? method retrieves the drive letter from the current path - # in a Windows environment. This method returns the drive letter as a string - # only if File::ALT_SEPARATOR is present and the path is absolute. - # It returns nil if the path is not absolute, the environment is not Windows, - # or there is no drive letter present. - def drive_letter? - if File::ALT_SEPARATOR && absolute? - to_s[0, 2] - else - nil - end + # The drive_letter? method retrieves the drive letter from the current path + # in a Windows environment. This method returns the drive letter as a string + # only if File::ALT_SEPARATOR is present and the path is absolute. + # It returns nil if the path is not absolute, the environment is not Windows, + # or there is no drive letter present. + def drive_letter? + if File::ALT_SEPARATOR && absolute? + to_s[0, 2] + else + nil end + end - # Compute the relative path from the 'src' path (directory) to 'tgt' - # (directory or file). Return the absolute path to 'tgt' if it can't - # be reached from 'src'. - def relative_path_from(other) - other = Pathname.new(other) unless other.is_a?(Pathname) - return other if drive_letter?&.casecmp(other.drive_letter?) != 0 + # Compute the relative path from the 'src' path (directory) to 'tgt' + # (directory or file). Return the absolute path to 'tgt' if it can't + # be reached from 'src'. + def relative_path_from(other) + other = Pathname.new(other) unless other.is_a?(Pathname) + return other if drive_letter?&.casecmp(other.drive_letter?) != 0 - if absolute? != other.absolute? - raise ArgumentError, "both paths must be either absolute or relative" - end - a = to_s.split(Pathname::SEPARATOR_PAT) - b = other.to_s.split(Pathname::SEPARATOR_PAT) - while a.first && b.first && pathequal(a.first, b.first) - a.shift - b.shift - end - b.size.times { a.unshift ".." } - Pathname.new(File.join(*a)) + if absolute? != other.absolute? + raise ArgumentError, "both paths must be either absolute or relative" end - - # Determines if 'src' is contained in 'tgt' (i.e. it is a subpath of - # 'tgt'). Both must be absolute paths and not contain '..' - def subpath?(base_directory) - base_directory = Pathname.new(base_directory) unless base_directory.is_a?(Pathname) - src_normalized = to_posix - tgt_normalized = base_directory.to_posix - src_normalized =~ /^#{Regexp.escape tgt_normalized}#{Pathname::SEPARATOR_PAT}/i + a = to_s.split(Pathname::SEPARATOR_PAT) + b = other.to_s.split(Pathname::SEPARATOR_PAT) + while a.first && b.first && pathequal(a.first, b.first) + a.shift + b.shift end + b.size.times { a.unshift ".." } + Pathname.new(File.join(*a)) + end - # Appends the given suffix to the filename, preserving the file extension. - # If the filename has an extension, the suffix is inserted before the extension. - # If the filename does not have an extension, the suffix is appended to the end. - # This method handles both directory and file paths correctly. - # - # Examples: - # pathname = Pathname("path.to/foo.tar.gz") - # pathname.append_to_filename("_bar") # => # - # - # pathname = Pathname("path.to/foo") - # pathname.append_to_filename("_bar") # => # - # - def append_to_filename(suffix) - sub(/(.*?#{Pathname::SEPARATOR_PAT})?(\.?[^.]+)?(\..*)?\z/, "\\1\\2#{suffix}\\3") - end + # Determines if 'src' is contained in 'tgt' (i.e. it is a subpath of + # 'tgt'). Both must be absolute paths and not contain '..' + def subpath?(base_directory) + base_directory = Pathname.new(base_directory) unless base_directory.is_a?(Pathname) + src_normalized = to_posix + tgt_normalized = base_directory.to_posix + src_normalized =~ /^#{Regexp.escape tgt_normalized}#{Pathname::SEPARATOR_PAT}/i + end - # Checks if the file's extension matches the expected extension. - # The comparison is case-insensitive. - # Example usage: ocran_pathname.extname?(".exe") - def extname?(expected_ext) - extname.casecmp(expected_ext) == 0 - end + # Appends the given suffix to the filename, preserving the file extension. + # If the filename has an extension, the suffix is inserted before the extension. + # If the filename does not have an extension, the suffix is appended to the end. + # This method handles both directory and file paths correctly. + # + # Examples: + # pathname = Pathname("path.to/foo.tar.gz") + # pathname.append_to_filename("_bar") # => # + # + # pathname = Pathname("path.to/foo") + # pathname.append_to_filename("_bar") # => # + # + def append_to_filename(suffix) + sub(/(.*?#{Pathname::SEPARATOR_PAT})?(\.?[^.]+)?(\..*)?\z/, "\\1\\2#{suffix}\\3") + end + + # Checks if the file's extension matches the expected extension. + # The comparison is case-insensitive. + # Example usage: ocran_pathname.extname?(".exe") + def extname?(expected_ext) + extname.casecmp(expected_ext) == 0 end end end