class Mimi::CLI::AppGenerator
Constants
- APP_TEMPLATE_PATH
- FILE_MAPPINGS
- FILE_MAPPINGS_EXEC
Attributes
app_name[R]
target_path[R]
Public Class Methods
new(app_name, target_path = nil)
click to toggle source
# File lib/mimi/cli/app_generator.rb, line 34 def initialize(app_name, target_path = nil) @app_name = app_name @target_path = Pathname.new(target_path).expand_path end
Public Instance Methods
app_root_path()
click to toggle source
# File lib/mimi/cli/app_generator.rb, line 84 def app_root_path target_path.join(app_name).expand_path end
file_basename()
click to toggle source
Returns base part of the application name as file name.
@example
"some-my_app" -> "my_app"
# File lib/mimi/cli/app_generator.rb, line 71 def file_basename app_name.split('-').last end
file_dirname()
click to toggle source
Returns directory part of the application name as file name.
@example
"some-my_app" -> "some"
# File lib/mimi/cli/app_generator.rb, line 80 def file_dirname app_name.split('-')[0..-2].join('/') end
file_name()
click to toggle source
Returns application name as file name.
@example
"some-my_app" -> "some/my_app"
# File lib/mimi/cli/app_generator.rb, line 62 def file_name app_name.split('-').join('/') end
generate(opts = {})
click to toggle source
# File lib/mimi/cli/app_generator.rb, line 88 def generate(opts = {}) raise "Application already exists: #{app_root_path}" if app_root_path.exist? install_path(app_root_path) unless opts[:dry_run] FILE_MAPPINGS.each do |from_file, to_file| to_file = eval "\"#{to_file}\"" from_path = APP_TEMPLATE_PATH.join(from_file) to_path = app_root_path.join(to_file) puts " #{from_path} -> #{to_path}" next if opts[:dry_run] if from_path.directory? install_path(to_path) else file_contents = File.read(from_path) file_processed = ERB.new(file_contents).result(binding) install_file(to_path, file_processed, FILE_MAPPINGS_EXEC.include?(from_file)) end end end
install_file(path, contents, executable = false)
click to toggle source
# File lib/mimi/cli/app_generator.rb, line 111 def install_file(path, contents, executable = false) FileUtils.mkdir_p(path.dirname) File.open(path, 'w') do |f| f.puts contents end FileUtils.chmod(0755, path, verbose: true) if executable end
install_path(path)
click to toggle source
# File lib/mimi/cli/app_generator.rb, line 107 def install_path(path) FileUtils.mkdir_p(path) end
module_name()
click to toggle source
Returns application name as class name.
@example
"some-my_app" -> "Some::MyApp"
# File lib/mimi/cli/app_generator.rb, line 44 def module_name name_parts = app_name.split('-').map do |word| word.gsub(/(^|_)(\w)/) { |m| m.chars.last.upcase } end name_parts.join('::') end
module_name_parts()
click to toggle source
Returns module name split into parts
# File lib/mimi/cli/app_generator.rb, line 53 def module_name_parts module_name.split('::') end