| 1 |
#! C:/home/ruby/bin/ruby.exe |
| 2 |
|
| 3 |
#==============================================================================# |
| 4 |
# $Id: exerb,v 1.36 2008/06/13 23:54:45 arton Exp $ |
| 5 |
#==============================================================================# |
| 6 |
|
| 7 |
require 'getoptlong' |
| 8 |
require 'exerb/version' |
| 9 |
require 'exerb/config' |
| 10 |
require 'exerb/recipe' |
| 11 |
require 'exerb/executable' |
| 12 |
|
| 13 |
#==============================================================================# |
| 14 |
|
| 15 |
module ExerbCommand |
| 16 |
|
| 17 |
OPTIONS = [ |
| 18 |
['--corename', '-c', GetoptLong::REQUIRED_ARGUMENT, 'specifies exerb-core by defined name.'], |
| 19 |
['--corefile', '-C', GetoptLong::REQUIRED_ARGUMENT, 'specifies exerb-core by path.'], |
| 20 |
['--outfile', '-o', GetoptLong::REQUIRED_ARGUMENT, 'specifies output file.'], |
| 21 |
['--kcode', '-k', GetoptLong::REQUIRED_ARGUMENT, 'specifies character code-set. (none/euc/sjis/utf8)'], |
| 22 |
['--archive', '-a', GetoptLong::NO_ARGUMENT, 'create an archive only.'], |
| 23 |
['--verbose', '-v', GetoptLong::NO_ARGUMENT, 'enable verbose mode.'], |
| 24 |
# ['--debug', '-g', GetoptLong::NO_ARGUMENT, 'enable debug mode.'], |
| 25 |
['--execute', '-e', GetoptLong::NO_ARGUMENT, 'execute an executable file after creation.'], |
| 26 |
['--config', '-i', GetoptLong::NO_ARGUMENT, 'display config information.'], |
| 27 |
['--version', '-V', GetoptLong::NO_ARGUMENT, 'display version number.'], |
| 28 |
['--help', '-h', GetoptLong::NO_ARGUMENT, 'display this information.'], |
| 29 |
] |
| 30 |
|
| 31 |
def self.main(argv) |
| 32 |
options = self.parse_options(argv) |
| 33 |
|
| 34 |
self.print_config_and_exit if options[:config] |
| 35 |
self.print_version_and_exit if options[:version] |
| 36 |
self.print_usage_and_exit if options[:help] || argv.size < 1 |
| 37 |
|
| 38 |
arg_fname = argv.shift |
| 39 |
arg_kcode = options[:kcode] |
| 40 |
arg_core = options[:corefile] |
| 41 |
arg_core ||= Exerb::Utility.find_core_by_name(options[:corename], true) if options[:corename] |
| 42 |
|
| 43 |
recipe = |
| 44 |
if /\.rb$/i =~ arg_fname |
| 45 |
Exerb::Recipe.new({ |
| 46 |
'general' => { |
| 47 |
'startup' => File.basename(arg_fname), |
| 48 |
'output' => File.expand_path(arg_fname).sub(/(\.rb)?$/i, '.exe'), |
| 49 |
'core' => arg_core, # use arg value, or default value |
| 50 |
'kcode' => arg_kcode, # use arg value, or default value |
| 51 |
}, |
| 52 |
'file' => { |
| 53 |
File.basename(arg_fname) => { |
| 54 |
'file' => File.expand_path(arg_fname), |
| 55 |
'type' => 'script', |
| 56 |
}, |
| 57 |
}, |
| 58 |
}) |
| 59 |
else |
| 60 |
Exerb::Recipe.load(arg_fname) |
| 61 |
end |
| 62 |
|
| 63 |
# FIXME: arg priority |
| 64 |
archive = recipe.create_archive(arg_kcode) |
| 65 |
core_file = recipe.core_filepath(arg_core) |
| 66 |
output_file = recipe.output_filepath(options[:outfile]) |
| 67 |
archive_file = recipe.archive_filepath(options[:outfile]) |
| 68 |
|
| 69 |
raise(Exerb::ExerbError, "a core file isn't specified") if core_file.nil? |
| 70 |
raise(Exerb::ExerbError, "no such file -- #{core_file}") unless File.exist?(core_file) |
| 71 |
raise(Exerb::ExerbError, "no such directory -- #{File.dirname(output_file)}") unless File.directory?(File.dirname(output_file)) |
| 72 |
|
| 73 |
if options[:verbose] |
| 74 |
puts("Recipe File : #{arg_fname}") |
| 75 |
puts("Core File : #{core_file}") |
| 76 |
puts("Archive File : #{archive_file}") if options[:archive] |
| 77 |
puts("Output File : #{output_file}") |
| 78 |
end |
| 79 |
|
| 80 |
if options[:archive] |
| 81 |
archive.write_to_file(archive_file) |
| 82 |
else |
| 83 |
executable = Exerb::Executable.read(core_file) |
| 84 |
executable.rsrc.add_archive(archive) |
| 85 |
recipe.update_resource(executable.rsrc) |
| 86 |
executable.write(output_file) |
| 87 |
|
| 88 |
exec(File.expand_path(output_file)) if options[:execute] |
| 89 |
end |
| 90 |
|
| 91 |
exit(0) |
| 92 |
rescue Exerb::ExerbError => e |
| 93 |
STDERR.puts("exerb: #{e.message}") |
| 94 |
exit(1) |
| 95 |
end |
| 96 |
|
| 97 |
def self.parse_options(argv) |
| 98 |
options = {} |
| 99 |
config = OPTIONS.collect { |ary| ary[0, 3] } |
| 100 |
|
| 101 |
parser = GetoptLong.new |
| 102 |
parser.set_options(*config) |
| 103 |
parser.each_option { |name, argument| |
| 104 |
options[name.sub(/^--/, '').intern] = argument |
| 105 |
} |
| 106 |
|
| 107 |
options[:archive] = !!options[:archive] |
| 108 |
options[:verbose] = !!options[:verbose] |
| 109 |
# options[:debug] = !!options[:debug] |
| 110 |
options[:execute] = !!options[:execute] |
| 111 |
options[:config] = !!options[:config] |
| 112 |
options[:version] = !!options[:version] |
| 113 |
options[:help] = !!options[:help] |
| 114 |
|
| 115 |
return options |
| 116 |
rescue GetoptLong::InvalidOption |
| 117 |
exit(1) |
| 118 |
end |
| 119 |
|
| 120 |
def self.print_config_and_exit |
| 121 |
puts "Exerb #{Exerb::VERSION}" |
| 122 |
|
| 123 |
puts |
| 124 |
puts "Search directories of Ruby library:" |
| 125 |
$LOAD_PATH.each { |path| printf(" %s\n", File.expand_path(path)) } |
| 126 |
|
| 127 |
puts |
| 128 |
puts "Search directories of Exerb core:" |
| 129 |
Exerb::CORE_PATH.each { |path| printf(" %s\n", File.expand_path(path)) } |
| 130 |
|
| 131 |
name_max = Exerb::CORE_NAME.keys.collect { |name| name.size }.max |
| 132 |
|
| 133 |
puts |
| 134 |
puts "Defined names of Exerb core:" |
| 135 |
Exerb::CORE_NAME.keys.sort.each { |name| |
| 136 |
filename = Exerb::Utility.find_core_by_name(name) |
| 137 |
message = (filename.nil? ? " (not found in search directories)" : "") |
| 138 |
printf(" %s => %s%s\n", name.ljust(name_max), Exerb::CORE_NAME[name], message) |
| 139 |
} |
| 140 |
|
| 141 |
exit(1) |
| 142 |
end |
| 143 |
|
| 144 |
def self.print_version_and_exit |
| 145 |
puts "Exerb #{Exerb::VERSION}" |
| 146 |
exit(1) |
| 147 |
end |
| 148 |
|
| 149 |
def self.print_usage_and_exit |
| 150 |
puts("Exerb #{Exerb::VERSION}") |
| 151 |
|
| 152 |
puts |
| 153 |
puts("Usage: exerb [options] {script-file|recipe-file}") |
| 154 |
|
| 155 |
puts |
| 156 |
puts("Options:") |
| 157 |
OPTIONS.each { |long, short, arg, comment| |
| 158 |
printf(" %s %-10s %s\n", short, long, comment) |
| 159 |
} |
| 160 |
|
| 161 |
exit(1) |
| 162 |
end |
| 163 |
|
| 164 |
=begin |
| 165 |
def self.make_filename(filename, extension) |
| 166 |
return filename.sub(/(\.exr$|$)/i, '.' + extension) |
| 167 |
end |
| 168 |
|
| 169 |
def self.select_core_file(options, recipe) |
| 170 |
core_file = options[:corefile] |
| 171 |
core_file ||= Exerb::Utility.find_core_by_name(options[:corename], true) if options[:corename] |
| 172 |
core_file ||= recipe.corefile |
| 173 |
core_file ||= Exerb::Utility.find_core_by_name('cui') |
| 174 |
core_file ||= Exerb::Utility.find_core_by_name('gui') |
| 175 |
core_file ||= raise(Exerb::ExerbError, "a core file isn't specified in the recipe file and command line options.") |
| 176 |
|
| 177 |
return core_file |
| 178 |
end |
| 179 |
|
| 180 |
def self.select_out_file(options, recipe, recipe_file) |
| 181 |
out_file = options[:outfile] |
| 182 |
out_file ||= recipe.outfile |
| 183 |
out_file ||= self.make_filename(recipe_file, 'exe') |
| 184 |
|
| 185 |
return out_file |
| 186 |
end |
| 187 |
=end |
| 188 |
|
| 189 |
end |
| 190 |
|
| 191 |
#==============================================================================# |
| 192 |
|
| 193 |
ExerbCommand.main(ARGV) |
| 194 |
|
| 195 |
#==============================================================================# |
| 196 |
#==============================================================================# |