Skip to content

more scaffold for the generated cli app #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions bintest/mruby-cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@
assert_true status.success?, "`#{app_name}` did not exit cleanly"
assert_include output, "Hello World"

output, status = Open3.capture2(APP_PATH, "--version")
assert_true status.success?, "`#{app_name}` did not exit cleanly when requesting version"
assert_include output, "#{app_name} version 0.0.1"

output, status = Open3.capture2(APP_PATH, "--help")
assert_true status.success?, "`#{app_name}` did not exit cleanly when requesting help"
assert_include output, "#{app_name} [switches] [arguments]"

%w(x86_64-pc-linux-gnu i686-pc-linux-gnu).each do |host|
output, status = Open3.capture2("file mruby/build/x86_64-pc-linux-gnu/bin/#{app_name}")
assert_include output, ", stripped"
Expand Down
187 changes: 187 additions & 0 deletions mrblib/mruby-cli/generate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
module MRubyCLI
class Generate
OBJECT_GENERATOR = [
:cli,
:help,
:options,
:version
]

def initialize(name, output)
@name = name
@output = output
end

def run(object)
raise RuntimeError unless OBJECT_GENERATOR.include? object
send("generate_#{object}")
end

private
def generate_help
Util::write_file("mrblib/#{@name}/help.rb", content_of_help_rb)
end

def content_of_help_rb
<<HELP_RB
module #{Util::camelize(@name)}
class Help
def initialize(output_io)
@output_io = output_io
end

def run
@output_io.puts "#{@name} [switches] [arguments]"
@output_io.puts "#{@name} -h, --help : show this message"
@output_io.puts "#{@name} -v, --version : print #{@name} version"
end
end
end
HELP_RB
end

def generate_cli
Util::write_file("mrblib/#{@name}/cli.rb", content_of_cli_rb)
end

def content_of_cli_rb
<<CLI_RB
module #{Util::camelize(@name)}
class CLI
def initialize(argv, output_io = $stdout, error_io = $stderr)
@options = setup_options
@opts = @options.parse(argv)
@output_io = output_io
@error_io = error_io
end

def run
if @options.option(:version)
Version.new(@output_io).run
elsif @options.option(:help)
Help.new(@output_io).run
else
@output_io.puts "Hello World"
end
end

private
def setup_options
options = Options.new
options.add(Option.new("version", "v"))
options.add(Option.new("help", "h"))

options
end
end
end
CLI_RB
end

def generate_version
Util::write_file("mrblib/#{@name}/version.rb", content_of_version_rb)
end

def content_of_version_rb
<<VERSION_RB
module #{Util::camelize(@name)}
class Version
VERSION = "0.0.1"

def initialize(output_io)
@output_io = output_io
end

def run
@output_io.puts "#{@name} version \#{VERSION}"
end
end
end
VERSION_RB
end

def generate_options
Util::write_file("mrblib/#{@name}/option.rb", content_of_option_rb)
Util::write_file("mrblib/#{@name}/options.rb", content_of_options_rb)
end

def content_of_option_rb
<<OPTION_RB
module #{Util::camelize(@name)}
class Option
attr_reader :short, :long, :value

def initialize(long, short, value = false)
@short = short
@long = long
@value = value
end

def to_long_opt
to_getopt(@long, @value)
end

def to_short_opt
to_getopt(@short, @value)
end

private
def to_getopt(name, value)
value ? "\#{name}:" : name
end
end
end
OPTION_RB
end

def content_of_options_rb
<<OPTIONS_RB
module #{Util::camelize(@name)}
class Options
attr_reader :short_opts, :long_opts
attr_writer :parsed_opts

def initialize
@options = {}
@short_opts_array = []
@short_opts = ""
@long_opts = []
@parsed_opts = {}
end

def add(option)
@options[option.long.to_sym] = option
@long_opts << option.to_long_opt
@long_opts.sort!
@short_opts_array << option.to_short_opt
@short_opts = @short_opts_array.sort!.join("")

option
end

def parse(args)
class << args; include Getopts; end
@parsed_opts = args.getopts(@short_opts, *@long_opts)
end

def option(long_opt)
option = @options[long_opt]

return nil unless option
if retn = @parsed_opts[option.long]
if option.value
return retn unless retn.empty?
else
return retn
end
end
return @parsed_opts[option.short] if @parsed_opts[option.short]
return false
end
end
end
OPTIONS_RB
end

end
end
95 changes: 30 additions & 65 deletions mrblib/mruby-cli/setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,59 +8,35 @@ def initialize(name, output)
def run
Dir.mkdir(@name) unless Dir.exist?(@name)
Dir.chdir(@name) do
write_file(".gitignore", gitignore)
write_file("mrbgem.rake", mrbgem_rake)
write_file("build_config.rb", build_config_rb)
write_file("Rakefile", rakefile)
write_file("Dockerfile", dockerfile)
write_file("docker-compose.yml", docker_compose_yml)

create_dir_p("tools/#{@name}")
write_file("tools/#{@name}/#{@name}.c", tools)

create_dir("mrblib")
write_file("mrblib/#{@name}.rb", mrblib)

create_dir("mrblib/#{@name}")
write_file("mrblib/#{@name}/version.rb", version)

create_dir("bintest")
write_file("bintest/#{@name}.rb", bintest)

create_dir("test")
write_file("test/test_#{@name}.rb", test)
Util::write_file(".gitignore", gitignore)
Util::write_file("mrbgem.rake", mrbgem_rake)
Util::write_file("build_config.rb", build_config_rb)
Util::write_file("Rakefile", rakefile)
Util::write_file("Dockerfile", dockerfile)
Util::write_file("docker-compose.yml", docker_compose_yml)

Util::create_dir_p("tools/#{@name}")
Util::write_file("tools/#{@name}/#{@name}.c", tools)

Util::create_dir("mrblib")
Util::write_file("mrblib/#{@name}.rb", mrblib)

Util::create_dir("bintest")
Util::write_file("bintest/#{@name}.rb", bintest)

Util::create_dir("test")
Util::write_file("test/test_#{@name}.rb", test)

Util::create_dir("mrblib/#{@name}")
generate = Generate.new(@name, @output)
generate.run(:cli)
generate.run(:help)
generate.run(:version)
generate.run(:options)
end
end

private
def create_dir_p(dir)
dir.split("/").inject("") do |parent, base|
new_dir =
if parent == ""
base
else
"#{parent}/#{base}"
end

create_dir(new_dir)

new_dir
end
end

def create_dir(dir)
if Dir.exist?(dir)
@output.puts " skip #{dir}"
else
@output.puts " create #{dir}/"
Dir.mkdir(dir)
end
end

def write_file(file, contents)
@output.puts " create #{file}"
File.open(file, 'w') {|file| file.puts contents }
end

def test
<<TEST
Expand Down Expand Up @@ -88,10 +64,10 @@ def bintest
end

assert('version') do
output, status = Open3.capture2(BIN_PATH, "version")
output, status = Open3.capture2(BIN_PATH, "--version")

assert_true status.success?, "Process did not exit cleanly"
assert_include output, "v0.0.1"
assert_include output, "#{@name} version 0.0.1"
end
BINTEST
end
Expand All @@ -105,10 +81,11 @@ def mrbgem_rake
spec.author = 'MRuby Developer'
spec.summary = '#{@name}'
spec.bins = ['#{@name}']
spec.version = #{Util.camelize(@name)}::VERSION
spec.version = #{Util.camelize(@name)}::Version::VERSION

spec.add_dependency 'mruby-print', :core => 'mruby-print'
spec.add_dependency 'mruby-mtest', :mgem => 'mruby-mtest'
spec.add_dependency 'mruby-getopts', :mgem => 'mruby-getopts'
end
MRBGEM_RAKE
end
Expand Down Expand Up @@ -259,23 +236,11 @@ def tools
def mrblib
<<TOOLS
def __main__(argv)
if argv[1] == "version"
puts "v\#{#{Util.camelize(@name)}::VERSION}"
else
puts "Hello World"
end
#{Util::camelize(@name)}::CLI.new(argv).run
end
TOOLS
end

def version
<<VERSION
module #{Util.camelize(@name)}
VERSION = "0.0.1"
end
VERSION
end

def dockerfile
<<DOCKERFILE
FROM hone/mruby-cli
Expand Down
29 changes: 29 additions & 0 deletions mrblib/mruby-cli/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,35 @@ def camelize(string)
w.split("_").map {|w2| w2.capitalize }.join('')
}.join('')
end

def create_dir_p(dir)
dir.split("/").inject("") do |parent, base|
new_dir =
if parent == ""
base
else
"#{parent}/#{base}"
end

create_dir(new_dir)

new_dir
end
end

def create_dir(dir)
if Dir.exist?(dir)
@output.puts " skip #{dir}"
else
@output.puts " create #{dir}/"
Dir.mkdir(dir)
end
end

def write_file(file, contents)
@output.puts " create #{file}"
File.open(file, 'w') {|file| file.puts contents }
end
end
end
end