From 9cf2710dc95f1c81e8c5111e22681836225e32e2 Mon Sep 17 00:00:00 2001 From: Martin Afanasjew Date: Fri, 27 May 2016 23:13:51 +0200 Subject: [PATCH] os/mac/*_mach: move shared code into 'SharedMachO' (#282) Both the `CctoolsMachO` and `RubyMachO` module implement a common set of methods that simplify querying `mach_data`. Move these into a shared module, that gets included after either of these implementations is loaded and included in `Pathname`. --- Library/Homebrew/os/mac/cctools_mach.rb | 49 ------------------------ Library/Homebrew/os/mac/pathname.rb | 4 ++ Library/Homebrew/os/mac/ruby_mach.rb | 48 ------------------------ Library/Homebrew/os/mac/shared_mach.rb | 50 +++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 97 deletions(-) create mode 100644 Library/Homebrew/os/mac/shared_mach.rb diff --git a/Library/Homebrew/os/mac/cctools_mach.rb b/Library/Homebrew/os/mac/cctools_mach.rb index 60f12a7ee8..36f508d28c 100644 --- a/Library/Homebrew/os/mac/cctools_mach.rb +++ b/Library/Homebrew/os/mac/cctools_mach.rb @@ -1,5 +1,3 @@ -require "os/mac/architecture_list" - module CctoolsMachO # @private OTOOL_RX = /\t(.*) \(compatibility version (?:\d+\.)*\d+, current version (?:\d+\.)*\d+\)/ @@ -54,53 +52,6 @@ module CctoolsMachO end end - def archs - mach_data.map { |m| m.fetch :arch }.extend(ArchitectureListExtension) - end - - def arch - case archs.length - when 0 then :dunno - when 1 then archs.first - else :universal - end - end - - def universal? - arch == :universal - end - - def i386? - arch == :i386 - end - - def x86_64? - arch == :x86_64 - end - - def ppc7400? - arch == :ppc7400 - end - - def ppc64? - arch == :ppc64 - end - - # @private - def dylib? - mach_data.any? { |m| m.fetch(:type) == :dylib } - end - - # @private - def mach_o_executable? - mach_data.any? { |m| m.fetch(:type) == :executable } - end - - # @private - def mach_o_bundle? - mach_data.any? { |m| m.fetch(:type) == :bundle } - end - # @private class Metadata attr_reader :path, :dylib_id, :dylibs diff --git a/Library/Homebrew/os/mac/pathname.rb b/Library/Homebrew/os/mac/pathname.rb index fbe4ad5ec7..87fc729365 100644 --- a/Library/Homebrew/os/mac/pathname.rb +++ b/Library/Homebrew/os/mac/pathname.rb @@ -1,3 +1,5 @@ +require "os/mac/shared_mach" + class Pathname if ENV["HOMEBREW_RUBY_MACHO"] require "os/mac/ruby_mach" @@ -6,4 +8,6 @@ class Pathname require "os/mac/cctools_mach" include CctoolsMachO end + + include SharedMachO end diff --git a/Library/Homebrew/os/mac/ruby_mach.rb b/Library/Homebrew/os/mac/ruby_mach.rb index adef7b6613..07761e7fc1 100644 --- a/Library/Homebrew/os/mac/ruby_mach.rb +++ b/Library/Homebrew/os/mac/ruby_mach.rb @@ -1,5 +1,4 @@ require "vendor/macho/macho" -require "os/mac/architecture_list" module RubyMachO # @private @@ -54,53 +53,6 @@ module RubyMachO end end - def archs - mach_data.map { |m| m.fetch :arch }.extend(ArchitectureListExtension) - end - - def arch - case archs.length - when 0 then :dunno - when 1 then archs.first - else :universal - end - end - - def universal? - arch == :universal - end - - def i386? - arch == :i386 - end - - def x86_64? - arch == :x86_64 - end - - def ppc7400? - arch == :ppc7400 - end - - def ppc64? - arch == :ppc64 - end - - # @private - def dylib? - mach_data.any? { |m| m.fetch(:type) == :dylib } - end - - # @private - def mach_o_executable? - mach_data.any? { |m| m.fetch(:type) == :executable } - end - - # @private - def mach_o_bundle? - mach_data.any? { |m| m.fetch(:type) == :bundle } - end - def dynamically_linked_libraries macho.linked_dylibs end diff --git a/Library/Homebrew/os/mac/shared_mach.rb b/Library/Homebrew/os/mac/shared_mach.rb new file mode 100644 index 0000000000..aa8baa92ef --- /dev/null +++ b/Library/Homebrew/os/mac/shared_mach.rb @@ -0,0 +1,50 @@ +require "os/mac/architecture_list" + +module SharedMachO + def archs + mach_data.map { |m| m.fetch :arch }.extend(ArchitectureListExtension) + end + + def arch + case archs.length + when 0 then :dunno + when 1 then archs.first + else :universal + end + end + + def universal? + arch == :universal + end + + def i386? + arch == :i386 + end + + def x86_64? + arch == :x86_64 + end + + def ppc7400? + arch == :ppc7400 + end + + def ppc64? + arch == :ppc64 + end + + # @private + def dylib? + mach_data.any? { |m| m.fetch(:type) == :dylib } + end + + # @private + def mach_o_executable? + mach_data.any? { |m| m.fetch(:type) == :executable } + end + + # @private + def mach_o_bundle? + mach_data.any? { |m| m.fetch(:type) == :bundle } + end +end