class Kolo::AppGenerator
AppGenerator
class is responsible for generating files based on the provided configuration file and templates
Constants
- APP_EXISTS_ERROR_MESSAGE
- FILE_PARSE_ERROR_MESSAGE
- INVALID_CONFIGURATION_ERROR_MESSAGE
- TEMPLATE_ERROR_MESSAGE
Public Class Methods
Initializes AppGenerator
class
@param [String] app_name Name of the new application.
@param [String] config_file Path to the configuration file. Configuration file must be follow the format defined in ./lib/kolo/configurations/default_configuration.json.
@param [String] template_dir Directory that contains templates referenced from configuration file. A template should have .tt extension, and can use features of ERB templating system.
@param [Tempate] template_class Class which is responsible for rendering ERB templates, must implement initialize
and call
methods.
# File lib/kolo/app_generator.rb, line 29 def initialize(app_name:, config_file:, template_dir:, template_class: Template) @app_name = app_name @config = validate_config(config_file) @template_dir = template_dir @template_class = template_class end
Public Instance Methods
call
method contains the logic for generating the application. See ./spec/lib/kolo/app_generator_spec.rb for documentation of the expected behaviour.
# File lib/kolo/app_generator.rb, line 39 def call raise AppExistsError, APP_EXISTS_ERROR_MESSAGE % File.join(File.dirname(__FILE__), @app_name) if app_exists? create_app_structure generate_files generate_template_files generate_keep_files initialize_git end
Private Instance Methods
# File lib/kolo/app_generator.rb, line 95 def app_exists? File.exist?(@app_name) end
# File lib/kolo/app_generator.rb, line 90 def copy_file(relative_src, relative_dest) validate_template_presence(relative_src) FileUtils.cp(relative_src, relative_dest) end
# File lib/kolo/app_generator.rb, line 50 def create_app_structure @config[:dirs].each do |d| FileUtils.mkdir_p(File.join(@app_name, d)) end end
# File lib/kolo/app_generator.rb, line 64 def generate_files @config[:files].each do |f| relative_src = File.join(@template_dir, f[:src]) relative_dest = File.join(@app_name, f[:dest]) copy_file(relative_src, relative_dest) end end
# File lib/kolo/app_generator.rb, line 72 def generate_keep_files @config[:dirs].each do |d| dir = File.join(@app_name, d) FileUtils.touch(File.join(dir, ".keep")) if Dir.empty?(dir) end end
# File lib/kolo/app_generator.rb, line 56 def generate_template_files @config[:template_files].each do |f| relative_src = File.join(@template_dir, f[:src]) relative_dest = File.join(@app_name, f[:dest]) template(relative_src, relative_dest, params: f[:params]) end end
# File lib/kolo/app_generator.rb, line 79 def initialize_git FileUtils.cd(@app_name) do system("git init") end end
# File lib/kolo/app_generator.rb, line 85 def template(relative_src, relative_dest, params: {}) validate_template_presence(relative_src) @template_class.new(params).call(relative_src, relative_dest) end
# File lib/kolo/app_generator.rb, line 99 def validate_config(config_file) begin config = JSON.parse(File.read(config_file), symbolize_names: true) rescue StandardError raise InvalidConfigurationError, FILE_PARSE_ERROR_MESSAGE end raise InvalidConfigurationError, INVALID_CONFIGURATION_ERROR_MESSAGE % "template files" unless config[:template_files] && config[:template_files].all? { |h| h[:src] && h[:dest] && h[:params] } raise InvalidConfigurationError, INVALID_CONFIGURATION_ERROR_MESSAGE % "files" unless config[:files] && config[:files].all? { |h| h[:src] && h[:dest] } raise InvalidConfigurationError, INVALID_CONFIGURATION_ERROR_MESSAGE % "dirs" unless config[:dirs] config end
# File lib/kolo/app_generator.rb, line 117 def validate_template_presence(path) raise TemplateError, TEMPLATE_ERROR_MESSAGE % "template is missing at #{path}" unless File.exist?(path) end