Class: Minke::Generators::Processor
- Inherits:
-
Object
- Object
- Minke::Generators::Processor
- Defined in:
- lib/minke/generators/processor.rb
Overview
Process handles the creation of new projects from a generator template.
Class Method Summary collapse
Instance Method Summary collapse
- #build_image(docker_file) ⇒ Object
- #create_new_filename(template_location, original, output_folder, service_name) ⇒ Object
- #fetch_image(docker_image) ⇒ Object
- #get_generator(generator) ⇒ Object
-
#initialize(variables, docker_runner) ⇒ Processor
constructor
A new instance of Processor.
- #local_gems ⇒ Object
- #process(generator_name, output_folder) ⇒ Object
- #process_directory(template_location, folder, output_folder, service_name) ⇒ Object
- #process_file(template_location, original, output_folder, service_name) ⇒ Object
- #render_erb(original, new_filename) ⇒ Object
- #run_command_in_container(build_image, command) ⇒ Object
Constructor Details
#initialize(variables, docker_runner) ⇒ Processor
Returns a new instance of Processor.
16 17 18 19 |
# File 'lib/minke/generators/processor.rb', line 16 def initialize variables, docker_runner @variables = variables @docker_runner = docker_runner end |
Class Method Details
.load_generators ⇒ Object
7 8 9 10 11 12 13 14 |
# File 'lib/minke/generators/processor.rb', line 7 def self.load_generators puts '# Loading installed generators' Gem::Specification.find_all.each do |spec| if spec. != nil && spec.['entrypoint'] != nil require spec.['entrypoint'] end end end |
Instance Method Details
#build_image(docker_file) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/minke/generators/processor.rb', line 44 def build_image docker_file puts "## Building custom docker image" image_name = @variables.application_name + "-buildimage" puts @docker_runner.build_image docker_file, image_name end |
#create_new_filename(template_location, original, output_folder, service_name) ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/minke/generators/processor.rb', line 104 def create_new_filename template_location, original, output_folder, service_name new_filename = original.sub(template_location + '/', '') new_filename.sub!('.erb', '') new_filename.sub!('<%= application_name %>', service_name) output_folder + '/' + new_filename end |
#fetch_image(docker_image) ⇒ Object
51 52 53 54 |
# File 'lib/minke/generators/processor.rb', line 51 def fetch_image docker_image @docker_runner.pull_image docker_image unless @docker_runner.find_image docker_image docker_image end |
#get_generator(generator) ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'lib/minke/generators/processor.rb', line 118 def get_generator generator config = Minke::Generators.get_registrations.select { |c| c.name == generator}.first if config == nil puts "Generator not installed please select from the above list of installed generators or install the required gem" exit 1 end processor = Minke::Generators::ConfigProcessor.new @variables return processor.process config end |
#local_gems ⇒ Object
114 115 116 |
# File 'lib/minke/generators/processor.rb', line 114 def local_gems Gem::Specification.sort_by{ |g| [g.name.downcase, g.version] }.group_by{ |g| g.name } end |
#process(generator_name, output_folder) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/minke/generators/processor.rb', line 21 def process generator_name, output_folder generator = get_generator generator_name # process the files puts '# Modifiying templates' puts "#{generator.template_location}" process_directory generator.template_location, '**/*', output_folder, @variables.application_name process_directory generator.template_location, '**/.*', output_folder, @variables.application_name # run generate command if present if generator.generate_settings != nil && generator.generate_settings.command != nil image = build_image generator.generate_settings.docker_file unless generator.generate_settings.docker_file == nil image = fetch_image generator.generate_settings.docker_image unless generator.generate_settings.docker_image == nil run_command_in_container image, generator.generate_settings.command unless generator.generate_settings.command == nil end # write the shell script Minke::Generators::write_bash_script output_folder + "/_build/minke.sh" Minke::Generators::create_rvm_files output_folder + "/_build/", @variables.application_name end |
#process_directory(template_location, folder, output_folder, service_name) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/minke/generators/processor.rb', line 70 def process_directory template_location, folder, output_folder, service_name Dir.glob("#{template_location}/#{folder}").each do |file_name| puts "## Processing #{file_name}" process_file template_location, file_name, output_folder, service_name end end |
#process_file(template_location, original, output_folder, service_name) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/minke/generators/processor.rb', line 77 def process_file template_location, original, output_folder, service_name new_filename = create_new_filename template_location, original, output_folder, service_name dirname = File.dirname(new_filename) unless File.directory?(dirname) FileUtils.mkdir_p(dirname) end if !File.directory?(original) if File.extname(original) == ".erb" render_erb original, new_filename elsif FileUtils.cp(original, new_filename) end end end |
#render_erb(original, new_filename) ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/minke/generators/processor.rb', line 94 def render_erb original, new_filename b = binding b.local_variable_set(:application_name, @variables.application_name) b.local_variable_set(:namespace, @variables.namespace) b.local_variable_set(:src_root, @variables.src_root) renderer = ERB.new(File.read(original)) File.open(new_filename, 'w') {|f| f.write renderer.result(b) } end |
#run_command_in_container(build_image, command) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/minke/generators/processor.rb', line 56 def run_command_in_container build_image, command puts command begin container, success = @docker_runner.create_and_run_container build_image, ["#{File.(@variables.src_root)}:/src"], nil, '/src', command # throw exception if failed @helper.fatal_error "Unable to run command #{command}" unless success #command = Minke::Helpers.replace_vars_in_section generator.generate_command, '##SERVICE_NAME##', APPLICATION_NAME #container, ret = Minke::Docker.create_and_run_container config, command ensure @docker_runner.delete_container container end end |