Permalink
Browse files

Refactored template to a Rails AppGenerator

  • Loading branch information...
1 parent 9900d15 commit 517f0c33597789f790fc5b55790f0650aaa6d6fe Harold Gim��nez committed Oct 21, 2011
View
@@ -1,23 +1,16 @@
#!/usr/bin/env ruby
-require File.expand_path(File.dirname(__FILE__) + "/../lib/create")
-require File.expand_path(File.dirname(__FILE__) + "/../lib/errors")
+require File.expand_path(File.join('..', 'lib', 'suspenders', 'generators', 'app_generator'), File.dirname(__FILE__))
+require File.expand_path(File.join('..', 'lib', 'suspenders', 'actions'), File.dirname(__FILE__))
+require File.expand_path(File.join('..', 'lib', 'suspenders', 'app_builder'), File.dirname(__FILE__))
-include Suspenders::Errors
-
-def usage
- "Usage: #{File.basename(__FILE__)} create new_project_name"
+if ['create', '--create'].include? ARGV[0]
+ ARGV.shift
+ puts "[WARNING] the suspenders create argument is deprecated. Just use `suspenders #{ARGV.join}` instead"
end
-case ARGV[0]
-when 'create', '--create'
- begin
- Suspenders::Create.run!(ARGV[1], ARGV[2])
- rescue Suspenders::InvalidInput => e
- error_with_message(e.message)
- end
-when 'help', '--help'
- puts usage
-else
- error_with_message "Unknown subcommand: #{ARGV[0]}\n#{usage}"
-end
+templates_root = File.expand_path(File.join("..", "template", "files"), File.dirname(__FILE__))
+Suspenders::Generator.source_root templates_root
+Suspenders::Generator.source_paths << Rails::Generators::AppGenerator.source_root << templates_root
+
+Suspenders::Generator.start
View
@@ -2,67 +2,34 @@
require 'rubygems'
require File.expand_path(File.dirname(__FILE__) + "/errors")
+require File.expand_path("../template/suspenders", File.dirname(__FILE__))
module Suspenders
class Create
attr_accessor :project_path, :repo
- def self.run!(project_path, repo)
- creator = self.new(project_path, repo)
+ def self.run!
+ creator = self.new
creator.create_project!
end
- def initialize(project_path, repo)
- self.project_path = File.expand_path(project_path)
- validate_project_path
- validate_project_name
- self.repo = repo if present?(repo)
- end
-
def create_project!
- command = <<-COMMAND
- rails #{rails_version} new #{project_path} \
- --template=#{template} \
- --skip-test-unit \
- --skip-prototype \
- --database=postgresql
- COMMAND
- command_with_repo = if repo
- "REPO='#{repo}' #{command}"
- else
- command
- end
- exec(command_with_repo)
- end
-
- private
-
- def validate_project_name
- project_name = File.basename(project_path)
- unless project_name =~ /^[a-z0-9_]+$/
- raise InvalidInput.new("Project name must only contain [a-z0-9_]")
- end
- end
-
- def validate_project_path
- base_directory = Dir.pwd
- full_path = File.join(base_directory, project_path)
+ Suspenders::Generator.source_root templates_root
+ Suspenders::Generator.source_paths << Rails::Generators::AppGenerator.source_root << templates_root
- # This is for the common case for the user's convenience; the race
- # condition can still occur.
- if File.exists?(full_path)
- raise InvalidInput.new("Project directory (#{project_path}) already exists")
- end
+ Suspenders::Generator.start
end
- def template
- File.expand_path(File.join("..", "template", "suspenders.rb"), File.dirname(__FILE__))
- end
+ private
def gemfile
File.expand_path(File.join("..", "template", "trout", "Gemfile"), File.dirname(__FILE__))
end
+ def templates_root
+ File.expand_path(File.join("..", "template", "files"), File.dirname(__FILE__))
+ end
+
def rails_version
gemfile_contents = File.read(gemfile)
gemfile_contents =~ /gem "rails", "(.*)"/
@@ -0,0 +1,35 @@
+module Suspenders
+ module Actions
+ def concat_file(source, destination)
+ contents = IO.read(find_in_source_paths(source))
+ append_file destination, contents
+ end
+
+ def replace_in_file(relative_path, find, replace)
+ path = File.join(destination_root, relative_path)
+ contents = IO.read(path)
+ unless contents.gsub!(find, replace)
+ raise "#{find.inspect} not found in #{relative_path}"
+ end
+ File.open(path, "w") { |file| file.write(contents) }
+ end
+
+ def action_mailer_host(rails_env, host)
+ inject_into_file(
+ "config/environments/#{rails_env}.rb",
+ "\n\n config.action_mailer.default_url_options = { :host => '#{host}' }",
+ :before => "\nend"
+ )
+ end
+
+ def download_file(uri_string, destination)
+ uri = URI.parse(uri_string)
+ http = Net::HTTP.new(uri.host, uri.port)
+ http.use_ssl = true if uri_string =~ /^https/
+ request = Net::HTTP::Get.new(uri.path)
+ contents = http.request(request).body
+ path = File.join(destination_root, destination)
+ File.open(path, "w") { |file| file.write(contents) }
+ end
+ end
+end
@@ -0,0 +1,176 @@
+module Suspenders
+ class AppBuilder < Rails::AppBuilder
+ include Suspenders::Actions
+
+ def readme
+ copy_file "README_FOR_SUSPENDERS"
+ end
+
+ def remove_public_index
+ remove_file 'public/index.html'
+ end
+
+ def remove_public_images_rails
+ remove_file 'public/images/rails.png'
+ end
+
+ def setup_staging
+ run "cp config/environments/production.rb config/environments/staging.rb"
+ end
+
+ def create_views_shared
+ empty_directory "app/views/shared"
+ end
+
+ def create_shared_flashes
+ copy_file "_flashes.html.erb", "app/views/shared/_flashes.html.erb"
+ end
+
+ def create_shared_javascripts
+ copy_file "_javascript.html.erb", "app/views/shared/_javascript.html.erb"
+ end
+
+ def create_application_layout
+ template "suspenders_layout.html.erb.erb",
+ "app/views/layouts/application.html.erb",
+ :force => true
+ end
+
+ def create_common_javascripts
+ directory "javascripts", "app/assets/javascripts"
+ end
+
+ def add_jquery_ui
+ inject_into_file "app/assets/javascripts/application.js", "//= require jquery-ui\n", :before => "//= require_tree ."
+ end
+
+ def use_postgres_config_template
+ template "postgresql_database.yml.erb", "config/database.yml", :force => true
+ end
+
+ def create_database
+ rake "db:create"
+ end
+
+ def include_custom_gems
+ insert_into_file("Gemfile", <<-GEMS, :after => /gem 'jquery-rails'/)
+\ngem 'thin'
+gem 'sass'
+gem 'clearance', '~> 0.13.0'
+gem 'high_voltage'
+gem 'paperclip'
+gem 'formtastic'
+gem 'flutie'
+gem 'bourbon'
+gem 'copycopter_client'
+gem 'airbrake'
+
+group :development do
+ gem "heroku"
+end
+
+group :development, :test do
+ gem "rspec-rails", "~> 2.6.1"
+ gem "ruby-debug19"
+ gem "sham_rack"
+end
+
+group :test do
+ gem "cucumber-rails", "1.1.0"
+ gem "capybara-webkit", "~> 0.7.1"
+ gem "factory_girl_rails"
+ gem "bourne"
+ gem "database_cleaner"
+ gem "timecop"
+ gem "shoulda-matchers"
+ gem "launchy"
+ gem "email_spec"
+end
+
+group :staging, :production do
+ gem 'newrelic_rpm'
+ gem 'sprockets-redirect'
+end
+ GEMS
+ end
+
+ def configure_rspec
+ generators_config = <<-RUBY
+ config.generators do |generate|
+ generate.test_framework :rspec
+ end
+ RUBY
+ inject_into_class "config/application.rb", "Application", generators_config
+ end
+
+ def configure_action_mailer
+ action_mailer_host "development", "#{app_name}.local"
+ action_mailer_host "test", "example.com"
+ action_mailer_host "staging", "staging.#{app_name}.com"
+ action_mailer_host "production", "#{app_name}.com"
+ end
+
+ def generate_rspec
+ generate "rspec:install"
+ replace_in_file "spec/spec_helper.rb", "mock_with :rspec", "mock_with :mocha"
+ end
+
+ def generate_cucumber
+ generate "cucumber:install", "--rspec", "--capybara"
+ inject_into_file "features/support/env.rb",
+ %{Capybara.save_and_open_page_path = 'tmp'\n} +
+ %{Capybara.javascript_driver = :webkit\n},
+ :before => %{Capybara.default_selector = :css}
+ end
+
+ def generate_clearance
+ generate "clearance:install"
+ generate "clearance:features"
+ end
+
+ def install_factory_girl_steps
+ copy_file "factory_girl_steps.rb", "features/step_definitions/factory_girl_steps.rb"
+ end
+
+ def setup_stylesheets
+ copy_file "app/assets/stylesheets/application.css", "app/assets/stylesheets/application.css.scss"
+ remove_file "app/assets/stylesheets/application.css"
+ concat_file "import_scss_styles", "app/assets/stylesheets/application.css.scss"
+ create_file "app/assets/stylesheets/_screen.scss"
+ end
+
+ def gitignore_files
+ concat_file "suspenders_gitignore", ".gitignore"
+ concat_file "cucumber_assertions_hack", "features/support/env.rb"
+ ["app/models",
+ "app/views/pages",
+ "db/migrate",
+ "log",
+ "public/images",
+ "spec/support",
+ "spec/lib",
+ "spec/models",
+ "spec/views",
+ "spec/controllers",
+ "spec/helpers",
+ "spec/support/matchers",
+ "spec/support/mixins",
+ "spec/support/shared_examples"].each do |dir|
+ empty_directory_with_gitkeep dir
+ end
+ end
+
+ def copy_miscelaneous_files
+ copy_file "errors.rb", "config/initializers/errors.rb"
+ copy_file "time_formats.rb", "config/initializers/time_formats.rb"
+ end
+
+ def setup_root_route
+ route "root :to => 'Clearance::Sessions#new'"
+ end
+
+ def migrate_database
+ rake "db:migrate"
+ end
+ end
+end
Oops, something went wrong. Retry.

0 comments on commit 517f0c3

Please sign in to comment.