#!/usr/bin/env ruby
require "clap"
require "erb"
require "ostruct"
class Tynn
class CLI
def self.parse(args)
new.parse(args)
end
attr_reader :template_path
def initialize
@template_path = File.expand_path("..", __dir__)
end
def parse(args)
args = Clap.run(args, opts)
usage if args.empty?
require(app_generator)
generator = AppGenerator.new(template_path, args)
generator.setup
end
def opts
return {
"-h" => method(:usage),
"--help" => method(:usage)
}
end
def app_generator
return File.join(template_path, "app_generator")
end
USAGE = <<-EOS
Usage: tynn
[options]
Options:
-h, --help output usage information
EOS
def usage
puts(USAGE); exit(0)
end
end
class Generator
attr_reader :base_dir, :app_name
def initialize(base_dir, args)
@base_dir = base_dir
@app_name = args.first
end
def setup
raise("#{ self.class }#setup not implemented")
end
def generate(path, vars = {})
output_path = File.join(app_name, path)
mkdir(File.dirname(output_path))
write(output_path, render(path, vars))
end
def mkdir(path)
unless Dir.exist?(path)
Dir.mkdir(path)
log(path)
end
end
def write(path, content)
File.write(path, content)
log(path)
end
def render(path, vars = {})
source = File.read(File.join(base_dir, "templates", path))
params = OpenStruct.new(vars).instance_eval { binding }
return ERB.new(source).result(params)
end
def log(str)
puts(" \033[36mcreate\033[0m : #{ str }")
end
end
end
Tynn::CLI.parse(ARGV)