Skip to content

Commit

Permalink
Refactor option accessors and synchronize names with internal represe…
Browse files Browse the repository at this point in the history
…ntations

Explicitly defined all accessors for @options, removing macro-style metaprogramming for method definitions. Renamed accessors to more meaningful terms, ensuring names are consistent with their internal representations. Additionally, renamed boolean-returning accessors to query method format to better indicate their purpose.
  • Loading branch information
shinokaro committed Jul 5, 2024
1 parent f4206de commit 1e49bd3
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 68 deletions.
176 changes: 109 additions & 67 deletions bin/ocran
Original file line number Diff line number Diff line change
Expand Up @@ -25,47 +25,89 @@ module Ocran
@ignore_modules = []

@options = {
:lzma_mode => true,
:enable_compression? => true,
:extra_dlls => [],
:files => [],
:run_script => true,
:add_all_core => false,
:source_files => [],
:run_script? => true,
:add_all_core? => false,
:output_override => nil,
:load_autoload => true,
:chdir_first => false,
:force_windows => false,
:force_console => false,
:load_autoload? => true,
:chdir_before? => false,
:force_windows? => false,
:force_console? => false,
:icon_filename => nil,
:rubyopt => nil,
:gemfile => nil,
:inno_script => nil,
:quiet => false,
:verbose => false,
:autodll => true,
:show_warnings => true,
:debug => false,
:debug_extract => false,
:arg => [],
:enc => true,
:gem => [],
:inno_setup_script => nil,
:quiet? => false,
:verbose? => false,
:auto_detect_dlls? => true,
:warn? => true,
:enable_debug_mode? => false,
:enable_debug_extract? => false,
:argv => [],
:add_all_encoding? => true,
:gem_options => [],
}

@options.each_key { |opt| eval("def self.#{opt}; @options[:#{opt}]; end") }
def self.add_all_core? = @options[__method__]

def self.quiet = @options[:quiet]
def self.add_all_encoding? = @options[__method__]

def self.verbose = !Ocran.quiet && @options[:verbose]
def self.argv = @options[__method__]

def self.auto_detect_dlls? = @options[__method__]

def self.chdir_before? = @options[__method__]

def self.enable_compression? = @options[__method__]

def self.enable_debug_extract? = @options[__method__]

def self.enable_debug_mode? = @options[__method__]

def self.extra_dlls = @options[__method__]

def self.force_autoload? = run_script? && load_autoload?

def self.force_console? = @options[__method__]

def self.force_windows? = @options[__method__]

def self.gem_options = @options[__method__]

def self.gemfile = @options[__method__]

def self.icon_filename = @options[__method__]

def self.inno_setup_script = @options[__method__]

def self.load_autoload? = @options[__method__]

def self.output_override = @options[__method__]

def self.quiet? = @options[__method__]

def self.run_script? = @options[__method__]

def self.source_files = @options[__method__]

def self.use_inno_setup? = !!inno_setup_script

def self.verbose? = !quiet? && @options[__method__]

def self.warn? = @options[__method__]

def Ocran.msg(s)
puts "=== #{s}" unless Ocran.quiet
puts "=== #{s}" unless Ocran.quiet?
end

def Ocran.verbose_msg(s)
puts s if Ocran.verbose
puts s if Ocran.verbose?
end

def Ocran.warn(s)
msg "WARNING: #{s}" if Ocran.show_warnings
msg "WARNING: #{s}" if Ocran.warn?
end

def Ocran.fatal_error(s)
Expand Down Expand Up @@ -151,29 +193,29 @@ EOF
while (arg = argv.shift)
case arg
when /\A--(no-)?lzma\z/
@options[:lzma_mode] = !$1
@options[:enable_compression?] = !$1
when "--no-dep-run"
@options[:run_script] = false
@options[:run_script?] = false
when "--add-all-core"
@options[:add_all_core] = true
@options[:add_all_core?] = true
when "--output"
path = argv.shift
@options[:output_override] = Pathname.new(path) if path
when "--dll"
path = argv.shift
@options[:extra_dlls] << path if path
when "--quiet"
@options[:quiet] = true
@options[:quiet?] = true
when "--verbose"
@options[:verbose] = true
@options[:verbose?] = true
when "--windows"
@options[:force_windows] = true
@options[:force_windows?] = true
when "--console"
@options[:force_console] = true
@options[:force_console?] = true
when "--no-autoload"
@options[:load_autoload] = false
@options[:load_autoload?] = false
when "--chdir-first"
@options[:chdir_first] = true
@options[:chdir_before?] = true
when "--icon"
path = argv.shift
Ocran.fatal_error "Icon file #{path} not found.\n" unless path && File.exist?(path)
Expand All @@ -187,27 +229,27 @@ EOF
when "--innosetup"
path = argv.shift
Ocran.fatal_error "Inno Script #{path} not found.\n" unless path && File.exist?(path)
@options[:inno_script] = Pathname.new(path)
@options[:inno_setup_script] = Pathname.new(path)
when "--no-autodll"
@options[:autodll] = false
@options[:auto_detect_dlls?] = false
when "--version"
require_relative "../lib/ocran/version"
puts "Ocran #{VERSION}"
exit 0
when "--no-warnings"
@options[:show_warnings] = false
@options[:warn?] = false
when "--debug"
@options[:debug] = true
@options[:enable_debug_mode?] = true
when "--debug-extract"
@options[:debug_extract] = true
@options[:enable_debug_extract?] = true
when "--"
@options[:arg] = ARGV.dup
@options[:argv] = ARGV.dup
ARGV.clear
when /\A--(no-)?enc\z/
@options[:enc] = !$1
@options[:add_all_encoding?] = !$1
when /\A--(no-)?gem-(\w+)(?:=(.*))?$/
negate, group, list = $1, $2, $3
@options[:gem] << [negate, group.to_sym, list&.split(",")] if group
@options[:gem_options] << [negate, group.to_sym, list&.split(",")] if group
when "--help", /\A--./
puts usage
exit 0
Expand All @@ -219,33 +261,33 @@ EOF
Ocran.fatal_error "#{arg} is empty!"
end
# If a directory is passed, we want all files under that directory
@options[:files] += Pathname.glob("#{arg}/**/*").map(&:expand_path)
@options[:source_files] += Pathname.glob("#{arg}/**/*").map(&:expand_path)
else
@options[:files] << Pathname.new(arg).expand_path
@options[:source_files] << Pathname.new(arg).expand_path
end
end
end

if Ocran.debug_extract && Ocran.inno_script
if Ocran.enable_debug_extract? && Ocran.use_inno_setup?
Ocran.fatal_error "The --debug-extract option conflicts with use of Inno Setup"
end

if Ocran.lzma_mode && Ocran.inno_script
if Ocran.enable_compression? && Ocran.use_inno_setup?
Ocran.fatal_error "LZMA compression must be disabled (--no-lzma) when using Inno Setup"
end

if !Ocran.chdir_first && Ocran.inno_script
if !Ocran.chdir_before? && Ocran.use_inno_setup?
Ocran.fatal_error "Chdir-first mode must be enabled (--chdir-first) when using Inno Setup"
end

if files.empty?
if Ocran.source_files.empty?
puts usage
exit 1
end
end

def self.main_script
Ocran.files.first
Ocran.source_files.first
end

def self.output_executable
Expand All @@ -254,13 +296,13 @@ EOF
else
executable = Ocran.main_script
# If debug mode is enabled, append "-debug" to the filename
executable = executable.append_to_filename("-debug") if Ocran.debug
executable = executable.append_to_filename("-debug") if Ocran.enable_debug_mode?
executable.basename.sub_ext(".exe")
end
end

def self.windowed?
(Ocran.main_script.extname?(".rbw") || Ocran.force_windows) && !Ocran.force_console
(Ocran.main_script.extname?(".rbw") || Ocran.force_windows?) && !Ocran.force_console?
end

def self.ruby_executable
Expand All @@ -274,7 +316,7 @@ EOF
def Ocran.init
save_environment
parseargs(ARGV)
ARGV.replace(Ocran.arg)
ARGV.replace(Ocran.argv)
@ignore_modules += ObjectSpace.each_object(Module).to_a
end

Expand Down Expand Up @@ -340,7 +382,7 @@ EOF

def self.gem_inclusion_set(spec_name)
include = [:loaded, :files]
Ocran.gem.each do |negate, option, list|
Ocran.gem_options.each do |negate, option, list|
next unless list.nil? || list.include?(spec_name)

case option
Expand Down Expand Up @@ -419,7 +461,7 @@ EOF
restore_environment

# If the script was run and autoload is enabled, attempt to autoload libraries.
if Ocran.run_script && Ocran.load_autoload
if Ocran.force_autoload?
attempt_load_autoload(@ignore_modules)
end

Expand Down Expand Up @@ -484,7 +526,7 @@ EOF
direction = proc do |builder|
Ocran.msg "Building #{Ocran.output_executable}"
require_relative "../lib/ocran/builder_ops_logger"
builder.extend(BuilderOpsLogger) if Ocran.verbose
builder.extend(BuilderOpsLogger) if Ocran.verbose?
require_relative "../lib/ocran/build_helper"
builder.extend(BuildHelper)

Expand All @@ -496,7 +538,7 @@ EOF
end

# Add detected DLLs
if Ocran.autodll
if Ocran.auto_detect_dlls?
Ocran.detect_dlls.each do |dll|
Ocran.msg "Adding detected DLL #{dll}"
if dll.subpath?(exec_prefix)
Expand Down Expand Up @@ -571,7 +613,7 @@ EOF
features -= gem_files

# If requested, add all ruby standard libraries
if Ocran.add_all_core
if Ocran.add_all_core?
Ocran.msg "Will include all ruby core libraries"
@load_path_before.each do |lp|
path = Pathname.new(lp)
Expand All @@ -587,7 +629,7 @@ EOF
end

# Include encoding support files
if Ocran.enc
if Ocran.add_all_encoding?
exec_prefix_load_path = all_load_paths.select { |path| path.subpath?(exec_prefix) }
exec_prefix_load_path.each do |load_path|
enc_dir = load_path / "enc"
Expand All @@ -608,7 +650,7 @@ EOF
builder.touch('msys64/usr/bin/msys-2.0.dll')

# Find the source root and adjust paths
source_files = Ocran.files.dup
source_files = Ocran.source_files.dup
src_prefix = find_src_prefix(source_files)

# Find features and decide where to put them in the temporary
Expand Down Expand Up @@ -700,25 +742,25 @@ EOF

# Add the opcode to launch the script
installed_ruby_exe = BINDIR / Ocran.ruby_executable
builder.exec(installed_ruby_exe, target_script, *Ocran.arg)
builder.exec(installed_ruby_exe, target_script, *Ocran.argv)
end

unless Ocran.inno_script
unless Ocran.use_inno_setup?
require_relative "../lib/ocran/stub_builder"
StubBuilder.new(Ocran.output_executable,
chdir_before: Ocran.chdir_first,
debug_extract: Ocran.debug_extract,
debug_mode: Ocran.debug,
enable_compression: Ocran.lzma_mode,
chdir_before: Ocran.chdir_before?,
debug_extract: Ocran.enable_debug_extract?,
debug_mode: Ocran.enable_debug_mode?,
enable_compression: Ocran.enable_compression?,
gui_mode: Ocran.windowed?,
icon_path: Ocran.icon_filename,
&direction)
Ocran.msg "Finished building #{Ocran.output_executable} (#{Ocran.output_executable.size} bytes)"
else
require_relative "../lib/ocran/inno_setup_builder"
InnoSetupBuilder.new(Ocran.output_executable,
Ocran.inno_script,
chdir_before: Ocran.chdir_first,
Ocran.inno_setup_script,
chdir_before: Ocran.chdir_before?,
icon_path: Ocran.icon_filename,
title: Ocran.output_executable.basename.sub_ext(""),
&direction)
Expand Down Expand Up @@ -749,7 +791,7 @@ if File.basename(__FILE__) == File.basename($PROGRAM_NAME)
end
end

if Ocran.run_script
if Ocran.run_script?
Ocran.msg "Loading script to check dependencies"
$PROGRAM_NAME = Ocran.main_script.to_s
load Ocran.main_script
Expand Down
2 changes: 1 addition & 1 deletion lib/ocran/inno_setup_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def initialize(path, inno_setup_script, chdir_before: nil, icon_path: nil, title

def compile
iscc_cmd = ["ISCC"]
iscc_cmd << "/Q" unless Ocran.verbose
iscc_cmd << "/Q" unless Ocran.verbose?
iscc_cmd << @iss.to_path
unless system(*iscc_cmd)
case $?.exitstatus
Expand Down

0 comments on commit 1e49bd3

Please sign in to comment.