diff --git a/commands/help.rb b/commands/help.rb index f808c675e..78f0c0f86 100644 --- a/commands/help.rb +++ b/commands/help.rb @@ -79,7 +79,7 @@ class Command puts <<~EOT Explain the purpose of various package boolean properties. Usage: crew prop - Available properties: #{Package.print_boolean_properties} + Available properties: #{Package.available_boolean_properties.join(', ')} EOT when 'reinstall' puts <<~EOT diff --git a/commands/prop.rb b/commands/prop.rb index 5acb563dc..410bec2b7 100644 --- a/commands/prop.rb +++ b/commands/prop.rb @@ -59,7 +59,7 @@ class Command puts <<~EOT Explain the purpose of various package boolean properties. Usage: crew prop - Available properties: #{Package.print_boolean_properties} + Available properties: #{Package.available_boolean_properties.join(', ')} EOT end end diff --git a/lib/const.rb b/lib/const.rb index 58ad4d06f..b581a9ba7 100644 --- a/lib/const.rb +++ b/lib/const.rb @@ -2,7 +2,7 @@ # Defines common constants used in different parts of crew require 'etc' -CREW_VERSION = '1.49.4' +CREW_VERSION = '1.49.5' # Kernel architecture. KERN_ARCH = Etc.uname[:machine] diff --git a/lib/package.rb b/lib/package.rb index d1d3ffff2..7e3e6d329 100644 --- a/lib/package.rb +++ b/lib/package.rb @@ -9,8 +9,6 @@ class Package :no_compile_needed, :no_compress, :no_env_options, :no_fhs, :no_git_submodules, :no_links, :no_lto, :no_patchelf, :no_shrink, :no_source_build, :no_strip, :no_upstream_update, :no_zstd, :patchelf, :print_source_bashrc, :run_tests - @boolean_properties = methods(false).join(',').gsub('?', '').split(',').sort.uniq.join(', ') - property :description, :homepage, :version, :license, :compatibility, :binary_compression, :binary_url, :binary_sha256, :source_url, :source_sha256, :git_branch, :git_hashtag, :min_glibc @@ -27,10 +25,6 @@ class Package :preremove, # Function to perform prior to package removal. :remove # Function to perform after package removal. - def self.print_boolean_properties - return @boolean_properties - end - class << self attr_accessor :name, :cached_build, :in_build, :build_from_source, :in_upgrade end diff --git a/lib/package_helpers.rb b/lib/package_helpers.rb index 5c73843da..a266f47be 100644 --- a/lib/package_helpers.rb +++ b/lib/package_helpers.rb @@ -2,8 +2,8 @@ class InstallError < RuntimeError; end def create_placeholder(*functions) # create_placeholder: create a placeholder for functions that will be used by crew later - functions.each do |func| - class_eval("def self.#{func}; end", __FILE__, __LINE__) + functions.each do |func_name| + define_singleton_method(func_name) { true } end end @@ -12,18 +12,20 @@ def property(*properties) # Examples: # {prop_name}('example') # set {prop_name} to 'example' # {prop_name} # return the value of {prop_name} - properties.each do |prop| - class_eval <<~EOT, __FILE__, __LINE__ + 1 - def self.#{prop} (prop = nil, &block) - if block - @#{prop} = block - elsif prop - @#{prop} = prop - else - return @#{prop} - end + properties.each do |prop_name| + define_singleton_method(prop_name) do |prop_value = nil, &block| + prop_var_name = "@#{prop_name}" + + if block + # store the block if a block is provided + instance_variable_set(prop_var_name, block) + elsif prop_value + instance_variable_set(prop_var_name, prop_value) + else + # return current value if nothing is provided + return instance_variable_get(prop_var_name) end - EOT + end end end @@ -31,22 +33,25 @@ def boolean_property(*boolean_properties) # boolean_property: if this exists in a package, it will return true # Examples: # {prop_name} # this will return #{prop_name} as true - boolean_properties.each do |prop| - class_eval <<~EOT, __FILE__, __LINE__ + 1 - def self.#{prop} (#{prop} = nil) - @#{prop} = true if #{prop} - !!@#{prop} - end - EOT - instance_eval <<~EOY, __FILE__, __LINE__ + 1 - def self.#{prop} - @#{prop} = true - end - EOY - # Adds the symbol? method - define_singleton_method("#{prop}?") do - @prop = instance_variable_get("@#{prop}") - !!@prop + + define_singleton_method(:available_boolean_properties) do + # available_boolean_properties: Return all available boolean properties for use in commands/{prop,help}.rb + # Usage: + # puts Package.available_boolean_properties => [:conflicts_ok, ...] + return boolean_properties + end + + boolean_properties.each do |prop_name| + prop_var_name = "@#{prop_name}" + + # Adds the self.{symbol} method + define_singleton_method(prop_name) do + instance_variable_set(prop_var_name, true) + end + + # Adds the self.{symbol}? method + define_singleton_method("#{prop_name}?") do + return !!instance_variable_get(prop_var_name) end end end diff --git a/tests/commands/help.rb b/tests/commands/help.rb index b70e33987..1f2b5dcfa 100644 --- a/tests/commands/help.rb +++ b/tests/commands/help.rb @@ -17,7 +17,7 @@ class HelpCommandTest < Minitest::Test expected_output = <<~EOT Explain the purpose of various package boolean properties. Usage: crew prop - Available properties: #{Package.print_boolean_properties} + Available properties: #{Package.available_boolean_properties.join(', ')} EOT assert_output expected_output, nil do Command.help('prop') diff --git a/tests/commands/prop.rb b/tests/commands/prop.rb index ee70df684..9cddb801b 100644 --- a/tests/commands/prop.rb +++ b/tests/commands/prop.rb @@ -7,7 +7,7 @@ class PropCommandTest < Minitest::Test expected_output = <<~EOT Explain the purpose of various package boolean properties. Usage: crew prop - Available properties: #{Package.print_boolean_properties} + Available properties: #{Package.available_boolean_properties.join(', ')} EOT assert_output expected_output, nil do Command.prop(nil) diff --git a/tests/help_test b/tests/help_test index 7da45a038..6508f23c2 100755 --- a/tests/help_test +++ b/tests/help_test @@ -10,7 +10,7 @@ help_lines = `wc -l ../commands/help.rb`.to_i tail_lines = help_lines - property_line help_commands = `head -#{property_line} ../commands/help.rb | grep " when '" | cut -d"'" -f2`.split("\n") help_properties = `tail -#{tail_lines} ../commands/help.rb | grep " when '" | cut -d"'" -f2`.split("\n") -package_properties = Package.print_boolean_properties.split(', ') +package_properties = Package.available_boolean_properties if (CREW_COMMANDS.length - help_commands.length).positive? puts 'Help is missing for the crew commands in commands/help.rb below:'.yellow missing_commands = CREW_COMMANDS - help_commands