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, #ask, #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.



124
125
126
127
128
# File 'padrino-gen/lib/padrino-gen/generators/project.rb', line 124

def bundle_dependencies
  if options[:bundle]
    run_bundler
  end
end

#finish_messageObject

Finish message.



133
134
135
136
137
138
139
140
141
142
# File 'padrino-gen/lib/padrino-gen/generators/project.rb', line 133

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

#git_author_emailObject

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



155
156
157
158
# File 'padrino-gen/lib/padrino-gen/generators/project.rb', line 155

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.



147
148
149
150
# File 'padrino-gen/lib/padrino-gen/generators/project.rb', line 147

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.



83
84
85
86
87
88
89
90
91
92
93
# File 'padrino-gen/lib/padrino-gen/generators/project.rb', line 83

def setup_components
  return if options[:template]
  @_components = options.dup.slice(*self.class.component_types)
  self.class.component_types.each do |comp|
    choice = @_components[comp] = resolve_valid_choice(comp)
    execute_component_setup(comp, choice)
  end
  store_component_config('.components')
  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
# 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
  @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("bin/#{name}")
    File.chmod(0755, destination_root("bin/#{name}"))
    if options.gem?
      template 'templates/gem/gemspec.tt', destination_root(name + '.gemspec')
      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.



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

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