lib/package: Get rid of eval strings (#10117)

* lib/package: Cleanup functions, get rid of `eval` strings

Signed-off-by: supechicken <me@supechicken666.dev>

* Fix error

Signed-off-by: supechicken <me@supechicken666.dev>

* Fix error

Signed-off-by: supechicken <me@supechicken666.dev>

* Add comments

Signed-off-by: supechicken <me@supechicken666.dev>

* Make rubocop happy

Signed-off-by: supechicken <me@supechicken666.dev>

* Fix error

Signed-off-by: supechicken <me@supechicken666.dev>

* Simplify

Signed-off-by: supechicken <me@supechicken666.dev>

* Bump crew version

Signed-off-by: supechicken <me@supechicken666.dev>

---------

Signed-off-by: supechicken <me@supechicken666.dev>
This commit is contained in:
SupeChicken666
2024-07-11 03:49:10 +08:00
committed by GitHub
parent 71b6a69569
commit 91f0f2ac98
8 changed files with 40 additions and 41 deletions

View File

@@ -79,7 +79,7 @@ class Command
puts <<~EOT puts <<~EOT
Explain the purpose of various package boolean properties. Explain the purpose of various package boolean properties.
Usage: crew prop <property> Usage: crew prop <property>
Available properties: #{Package.print_boolean_properties} Available properties: #{Package.available_boolean_properties.join(', ')}
EOT EOT
when 'reinstall' when 'reinstall'
puts <<~EOT puts <<~EOT

View File

@@ -59,7 +59,7 @@ class Command
puts <<~EOT puts <<~EOT
Explain the purpose of various package boolean properties. Explain the purpose of various package boolean properties.
Usage: crew prop <property> Usage: crew prop <property>
Available properties: #{Package.print_boolean_properties} Available properties: #{Package.available_boolean_properties.join(', ')}
EOT EOT
end end
end end

View File

@@ -2,7 +2,7 @@
# Defines common constants used in different parts of crew # Defines common constants used in different parts of crew
require 'etc' require 'etc'
CREW_VERSION = '1.49.4' CREW_VERSION = '1.49.5'
# Kernel architecture. # Kernel architecture.
KERN_ARCH = Etc.uname[:machine] KERN_ARCH = Etc.uname[:machine]

View File

@@ -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_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 :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, property :description, :homepage, :version, :license, :compatibility,
:binary_compression, :binary_url, :binary_sha256, :source_url, :source_sha256, :binary_compression, :binary_url, :binary_sha256, :source_url, :source_sha256,
:git_branch, :git_hashtag, :min_glibc :git_branch, :git_hashtag, :min_glibc
@@ -27,10 +25,6 @@ class Package
:preremove, # Function to perform prior to package removal. :preremove, # Function to perform prior to package removal.
:remove # Function to perform after package removal. :remove # Function to perform after package removal.
def self.print_boolean_properties
return @boolean_properties
end
class << self class << self
attr_accessor :name, :cached_build, :in_build, :build_from_source, :in_upgrade attr_accessor :name, :cached_build, :in_build, :build_from_source, :in_upgrade
end end

View File

@@ -2,8 +2,8 @@ class InstallError < RuntimeError; end
def create_placeholder(*functions) def create_placeholder(*functions)
# create_placeholder: create a placeholder for functions that will be used by crew later # create_placeholder: create a placeholder for functions that will be used by crew later
functions.each do |func| functions.each do |func_name|
class_eval("def self.#{func}; end", __FILE__, __LINE__) define_singleton_method(func_name) { true }
end end
end end
@@ -12,18 +12,20 @@ def property(*properties)
# Examples: # Examples:
# {prop_name}('example') # set {prop_name} to 'example' # {prop_name}('example') # set {prop_name} to 'example'
# {prop_name} # return the value of {prop_name} # {prop_name} # return the value of {prop_name}
properties.each do |prop| properties.each do |prop_name|
class_eval <<~EOT, __FILE__, __LINE__ + 1 define_singleton_method(prop_name) do |prop_value = nil, &block|
def self.#{prop} (prop = nil, &block) prop_var_name = "@#{prop_name}"
if block
@#{prop} = block if block
elsif prop # store the block if a block is provided
@#{prop} = prop instance_variable_set(prop_var_name, block)
else elsif prop_value
return @#{prop} instance_variable_set(prop_var_name, prop_value)
end else
# return current value if nothing is provided
return instance_variable_get(prop_var_name)
end end
EOT end
end end
end end
@@ -31,22 +33,25 @@ def boolean_property(*boolean_properties)
# boolean_property: if this exists in a package, it will return true # boolean_property: if this exists in a package, it will return true
# Examples: # Examples:
# {prop_name} # this will return #{prop_name} as true # {prop_name} # this will return #{prop_name} as true
boolean_properties.each do |prop|
class_eval <<~EOT, __FILE__, __LINE__ + 1 define_singleton_method(:available_boolean_properties) do
def self.#{prop} (#{prop} = nil) # available_boolean_properties: Return all available boolean properties for use in commands/{prop,help}.rb
@#{prop} = true if #{prop} # Usage:
!!@#{prop} # puts Package.available_boolean_properties => [:conflicts_ok, ...]
end return boolean_properties
EOT end
instance_eval <<~EOY, __FILE__, __LINE__ + 1
def self.#{prop} boolean_properties.each do |prop_name|
@#{prop} = true prop_var_name = "@#{prop_name}"
end
EOY # Adds the self.{symbol} method
# Adds the symbol? method define_singleton_method(prop_name) do
define_singleton_method("#{prop}?") do instance_variable_set(prop_var_name, true)
@prop = instance_variable_get("@#{prop}") end
!!@prop
# Adds the self.{symbol}? method
define_singleton_method("#{prop_name}?") do
return !!instance_variable_get(prop_var_name)
end end
end end
end end

View File

@@ -17,7 +17,7 @@ class HelpCommandTest < Minitest::Test
expected_output = <<~EOT expected_output = <<~EOT
Explain the purpose of various package boolean properties. Explain the purpose of various package boolean properties.
Usage: crew prop <property> Usage: crew prop <property>
Available properties: #{Package.print_boolean_properties} Available properties: #{Package.available_boolean_properties.join(', ')}
EOT EOT
assert_output expected_output, nil do assert_output expected_output, nil do
Command.help('prop') Command.help('prop')

View File

@@ -7,7 +7,7 @@ class PropCommandTest < Minitest::Test
expected_output = <<~EOT expected_output = <<~EOT
Explain the purpose of various package boolean properties. Explain the purpose of various package boolean properties.
Usage: crew prop <property> Usage: crew prop <property>
Available properties: #{Package.print_boolean_properties} Available properties: #{Package.available_boolean_properties.join(', ')}
EOT EOT
assert_output expected_output, nil do assert_output expected_output, nil do
Command.prop(nil) Command.prop(nil)

View File

@@ -10,7 +10,7 @@ help_lines = `wc -l ../commands/help.rb`.to_i
tail_lines = help_lines - property_line tail_lines = help_lines - property_line
help_commands = `head -#{property_line} ../commands/help.rb | grep " when '" | cut -d"'" -f2`.split("\n") 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") 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? if (CREW_COMMANDS.length - help_commands.length).positive?
puts 'Help is missing for the crew commands in commands/help.rb below:'.yellow puts 'Help is missing for the crew commands in commands/help.rb below:'.yellow
missing_commands = CREW_COMMANDS - help_commands missing_commands = CREW_COMMANDS - help_commands