Class: Padrino::Generators::Project

Inherits:
Thor::Group
  • Object
show all
Includes:
Actions, Components::Actions, Runner, Thor::Actions
Defined in:
padrino-gen/lib/padrino-gen/generators/project.rb

Overview

Responsible for generating new Padrino projects based on the specified project components.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Runner

#app, #generate, #git, #project, #rake

Methods included from Actions

#already_exists?, #app_skeleton, #apply_component_for, #apply_default_fields, #check_app_existence, #destination_root, #empty_directory_with_keep_file, #execute_component_setup, #fetch_app_name, #fetch_component_choice, #fetch_project_name, #in_app_root?, #include_component_module_for, #initializer, #inject_into_file, #insert_hook, #insert_into_gemfile, #insert_middleware, #invalid_fields, #keep_file, #middleware, #recognize_path, #require_contrib, #require_dependencies, #resolve_valid_choice, #retrieve_component_config, #run_bundler, #store_component_choice, #store_component_config, #test?, #tiny?, #valid_choice?, #valid_constant?, #validate_namespace

Class Method Details



13
# File 'padrino-gen/lib/padrino-gen/generators/project.rb', line 13

def self.banner; "padrino-gen project [name] [options]"; end

.source_rootObject



12
# File 'padrino-gen/lib/padrino-gen/generators/project.rb', line 12

def self.source_root; File.expand_path(File.dirname(__FILE__)); end

Instance Method Details

#bundle_dependenciesObject

Bundle all required components using bundler and Gemfile.



128
129
130
131
132
# File 'padrino-gen/lib/padrino-gen/generators/project.rb', line 128

def bundle_dependencies
  if options[:bundle]
    run_bundler
  end
end

#finish_messageObject

Finish message.



137
138
139
140
141
142
143
144
145
146
# File 'padrino-gen/lib/padrino-gen/generators/project.rb', line 137

def finish_message
  say
  say '=' * 65, :green
  say "#{name} is ready for development!", :green
  say '=' * 65, :green
  say "$ cd #{options[:root]}/#{name}"
  say "$ bundle --binstubs" unless options[:bundle]
  say "=" * 65, :green
  say
end

#git_author_emailObject

Returns the git author email config or a fill-in value.



159
160
161
162
# File 'padrino-gen/lib/padrino-gen/generators/project.rb', line 159

def git_author_email
  git_author_email = `git config user.email`.chomp rescue ''
  git_author_email.empty? ? "TODO: Write your email address" : git_author_email
end

#git_author_nameObject

Returns the git author name config or a fill-in value.



151
152
153
154
# File 'padrino-gen/lib/padrino-gen/generators/project.rb', line 151

def git_author_name
  git_author_name = `git config user.name`.chomp rescue ''
  git_author_name.empty? ? "TODO: Write your name" : git_author_name
end

#setup_componentsObject

For each component, retrieve a valid choice and then execute the associated generator.



87
88
89
90
91
92
93
94
95
96
97
# File 'padrino-gen/lib/padrino-gen/generators/project.rb', line 87

def setup_components
  return if options[:template]
  @_components = options.class.new options.select{ |key,_| self.class.component_types.include?(key.to_sym) }
  self.class.component_types.each do |comp|
    choice = @_components[comp] = resolve_valid_choice(comp)
    execute_component_setup(comp, choice)
  end
  store_component_config('.components', :force => true)
  store_component_choice(:namespace, @project_name)
  store_component_choice(:migration_format, options[:migration_format])
end

#setup_projectObject

Copies over the Padrino base application app.



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
# File 'padrino-gen/lib/padrino-gen/generators/project.rb', line 45

def setup_project
  valid_constant? name
  app = (options[:app] || "App")

  @project_name = name.gsub(/\W/, '_').underscore.camelize

  fail "Constant `#{@project_name}` already exists. Please, use another name" if already_exists?(@project_name)

  @app_name = app.gsub(/\W/, '_').camelize
  self.destination_root = File.join(options[:root], name)
  if options[:template]
    execute_runner(:template, options[:template])
  else
    directory('project/', destination_root)
    empty_directory destination_root('public/images')
    empty_directory destination_root('public/javascripts')
    empty_directory destination_root('public/stylesheets')
    store_component_config('.components')
    unless options[:lean]
      app_skeleton('app', options[:tiny])
      append_file destination_root('config/apps.rb'), "Padrino.mount('#{@project_name}::#{@app_name}', :app_file => Padrino.root('app/app.rb')).to('/')\n"
    end
    template 'templates/Gemfile.tt', destination_root('Gemfile')
    template 'templates/Rakefile.tt', destination_root('Rakefile')
    template 'templates/project_bin.tt', destination_root("exe/#{name}")
    File.chmod(0755, destination_root("exe/#{name}"))
    if options.gem?
      template 'templates/gem/gemspec.tt', destination_root(name + '.gemspec')
      inject_into_file destination_root('Rakefile'), "require 'bundler/gem_tasks'\n", :after => "require 'bundler/setup'\n"
      template 'templates/gem/README.md.tt', destination_root('README.md')
      template 'templates/gem/lib/libname.tt', destination_root("lib/#{name}.rb")
      template 'templates/gem/lib/libname/version.tt', destination_root("lib/#{name}/version.rb")
    else
      empty_directory_with_keep_file destination_root('tmp')
      empty_directory_with_keep_file destination_root('log')
    end
  end
end

#setup_test_filesObject

Generates test files for tiny app skeleton.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'padrino-gen/lib/padrino-gen/generators/project.rb', line 102

def setup_test_files
  if options[:tiny] && @_components[:test] != :none
    test_component = @_components[:test]
    test_component = "rspec" if test_component == "cucumber"
    uppercase_test_component = test_component.upcase
    controller_template_name = "#{uppercase_test_component}_CONTROLLER_TEST"
    helper_template_name     = "#{uppercase_test_component}_HELPER_TEST"
    return unless defined?(controller_template_name)

    controller_content = instance_eval(controller_template_name).gsub(/!PATH!/, "Controller").gsub(/!NAME!/, "").gsub(/!EXPANDED_PATH!/, "/")
    helper_content     = instance_eval(helper_template_name).gsub(/!NAME!/, "#{@project_name}::#{@app_name}::#{DEFAULT_HELPER_NAME}")

    proc{|*args| args.map{|str| str.gsub!(/!PATH!/, recognize_path)} }.call(controller_content, helper_content)

    directory_name = [:rspec].include?(test_component.to_sym) ? "spec" : "test"
    base_path      = File.join(directory_name, "app")
    create_file destination_root("#{base_path}/controllers/controllers_#{directory_name}.rb"), controller_content, :skip => true
    create_file destination_root("#{base_path}/helpers/helpers_#{directory_name}.rb"),         helper_content,     :skip => true
    helper_path = destination_root(File.join(directory_name, "#{directory_name == "spec" ? "spec_helper" : "test_config"}.rb"))
    gsub_file helper_path, %r{helpers/\*\*/\*\.rb}, "helpers.rb"
  end
end