Class: Mimi::CLI::AppGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/mimi/cli/app_generator.rb

Constant Summary collapse

FILE_MAPPINGS =
{
  'app/models/README.md' => 'app/models/README.md',
  'app/messaging/README.md' => 'app/messaging/README.md',
  'bin/start' => 'bin/start',
  'config/manifest.yml' => 'config/manifest.yml',
  'lib/file_name.rb' => 'lib/#{file_dirname}/#{file_basename}.rb',
  'lib/file_name/application.rb' => 'lib/#{file_name}/application.rb',
  'lib/file_name/version.rb' => 'lib/#{file_name}/version.rb',
  'lib/boot.rb' => 'lib/boot.rb',
  'Gemfile' => 'Gemfile',
  'Rakefile' => 'Rakefile',
  'app.env' => '.env',
  'app.gitignore' => '.gitignore'
}
FILE_MAPPINGS_EXEC =
['bin/start']
APP_TEMPLATE_PATH =
Pathname.new(__FILE__).dirname.join('template')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_name, target_path = nil) ⇒ AppGenerator

Returns a new instance of AppGenerator.



26
27
28
29
# File 'lib/mimi/cli/app_generator.rb', line 26

def initialize(app_name, target_path = nil)
  @app_name = app_name
  @target_path = Pathname.new(target_path).expand_path
end

Instance Attribute Details

#app_nameObject (readonly)

Returns the value of attribute app_name.



7
8
9
# File 'lib/mimi/cli/app_generator.rb', line 7

def app_name
  @app_name
end

#target_pathObject (readonly)

Returns the value of attribute target_path.



7
8
9
# File 'lib/mimi/cli/app_generator.rb', line 7

def target_path
  @target_path
end

Instance Method Details

#app_root_pathObject



76
77
78
# File 'lib/mimi/cli/app_generator.rb', line 76

def app_root_path
  target_path.join(app_name).expand_path
end

#file_basenameObject

Returns base part of the application name as file name.

Examples:

"some-my_app" -> "my_app"


63
64
65
# File 'lib/mimi/cli/app_generator.rb', line 63

def file_basename
  app_name.split('-').last
end

#file_dirnameObject

Returns directory part of the application name as file name.

Examples:

"some-my_app" -> "some"


72
73
74
# File 'lib/mimi/cli/app_generator.rb', line 72

def file_dirname
  app_name.split('-')[0..-2].join('/')
end

#file_nameObject

Returns application name as file name.

Examples:

"some-my_app" -> "some/my_app"


54
55
56
# File 'lib/mimi/cli/app_generator.rb', line 54

def file_name
  app_name.split('-').join('/')
end

#generate(opts = {}) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/mimi/cli/app_generator.rb', line 80

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) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/mimi/cli/app_generator.rb', line 103

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) ⇒ Object



99
100
101
# File 'lib/mimi/cli/app_generator.rb', line 99

def install_path(path)
  FileUtils.mkdir_p(path)
end

#module_nameObject

Returns application name as class name.

Examples:

"some-my_app" -> "Some::MyApp"


36
37
38
39
40
41
# File 'lib/mimi/cli/app_generator.rb', line 36

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_partsObject

Returns module name split into parts



45
46
47
# File 'lib/mimi/cli/app_generator.rb', line 45

def module_name_parts
  module_name.split('::')
end