Class: GemMine::Generator
- Inherits:
-
Object
- Object
- GemMine::Generator
- Defined in:
- lib/gem_mine/generator.rb
Overview
Internal engine for GemMine.factory
Defined Under Namespace
Classes: CtxObj
Constant Summary collapse
- DEFAULTS =
{ :count => 100, :root_dir => File.("../../spec/fixtures/gem_mine", __dir__), :library_name_prefix => "bench_gem_", :start_index => 1, :group_size => 10, :groups_env_prefix => "GEM_MINE_GROUP_", :namespace_proc => nil, :include_floss_funding => false, :dependencies => [], :authors => [], :version_strategy => proc { |ctx| "0.0.#{ctx[:index]}" }, :gemspec_extras => {}, :yaml_templates => {}, :file_contents => nil, # if nil, a minimal default lib file is generated via ERB :overwrite => true, :cleanup => false, :seed => nil, :after_generate => nil, :progress_bar => nil, }.freeze
- YAML_KEY_SUFFIX =
/(.*)_ya?ml\z/.freeze
- YML_EXT_REGEX =
/\.yml\z/.freeze
- YAML_EXT_REGEX =
/\.yaml\z/.freeze
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Generator
constructor
A new instance of Generator.
-
#run ⇒ Object
Constructor Details
Instance Method Details
#run ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/gem_mine/generator.rb', line 39 def run validate! srand(@options[:seed]) if @options[:seed] root_dir = File.(@options[:root_dir]) FileUtils.rm_rf(root_dir) if @options[:cleanup] FileUtils.mkdir_p(root_dir) = [] count = @options[:count].to_i start_index = @options[:start_index].to_i group_size = @options[:group_size].to_i # Optional progress bar = @options[:progress_bar] = nil if begin require "ruby-progressbar" opts = .dup opts[:total] ||= count = ProgressBar.create(opts) rescue LoadError = nil end end count.times do |offset| index = start_index + offset ordinal = offset group_index = (ordinal / group_size) library_name = format_name(@options[:library_name_prefix], index, count) module_name = Helpers.camelize(library_name) gem_dir = File.join(root_dir, library_name) lib_dir = File.join(gem_dir, "lib") FileUtils.mkdir_p(lib_dir) env_group_var = "#{@options[:groups_env_prefix]}#{group_index}" # Build per-gem context used by ERB templates and value callables ctx = build_context( :index => index, :ordinal => ordinal, :count => count, :group_size => group_size, :group_index => group_index, :groups_env_prefix => @options[:groups_env_prefix], :env_group_var => env_group_var, :root_dir => root_dir, :gem_dir => gem_dir, :lib_dir => lib_dir, :library_name => library_name, :module_name => module_name, :include_floss_funding => !!@options[:include_floss_funding] ) ctx[:namespace] = call_opt(@options[:namespace_proc], ctx) ctx[:dependencies] = normalize_dependencies(call_opt(@options[:dependencies], ctx)) # Write Gemfile and gemspec gemfile_path = File.join(gem_dir, "Gemfile") gemspec_path = File.join(gem_dir, "#{library_name}.gemspec") write_gemfile(gemfile_path, ctx[:dependencies], @options[:overwrite]) = call_opt(@options[:authors], ctx) || [] version = call_opt(@options[:version_strategy], ctx) extras = symbolize_keys(call_opt(@options[:gemspec_extras], ctx) || {}) write_gemspec(gemspec_path, library_name, version, , extras, ctx, @options[:overwrite]) # YAML templates yaml_templates = call_opt(@options[:yaml_templates], ctx) || {} write_yaml_templates(gem_dir, yaml_templates, ctx, @options[:overwrite]) # Files (lib and others) files_templates = call_opt(@options[:file_contents], ctx) if files_templates.nil? || files_templates.empty? # Provide default minimal lib file rendered via ERB files_templates = { File.join("lib", "#{library_name}.rb") => default_lib_template, } end write_file_templates(gem_dir, files_templates, ctx, @options[:overwrite]) per_gem = { :index => index, :library_name => library_name, :module_name => module_name, :namespace => ctx[:namespace], :group_index => group_index, :env_group_var => env_group_var, :dir => gem_dir, :lib_dir => lib_dir, :gemspec_path => gemspec_path, :gemfile_path => gemfile_path, } << per_gem if @options[:after_generate].respond_to?(:call) @options[:after_generate].call(:result => nil, :gem => per_gem) end &.increment end &.finish if && .respond_to?(:finish) { :root_dir => root_dir, :groups => { :env_prefix => @options[:groups_env_prefix], :group_size => group_size }, :gems => , } end |