diff --git a/README.rdoc b/README.rdoc index 8fd8b9c..990f7a4 100644 --- a/README.rdoc +++ b/README.rdoc @@ -9,7 +9,8 @@ Rails apps via RubyGems. == Usage -[Coming soon] +Your Rails app will need to be modified a bit to become a gem and work +with Harker. See test/sample for an example. == Requirements diff --git a/Rakefile b/Rakefile index b3def23..583029a 100644 --- a/Rakefile +++ b/Rakefile @@ -8,6 +8,8 @@ Hoe.new('harker', Harker::VERSION) do |p| p.readme_file = 'README.rdoc' p.summary = "Concourse is a tool to make planning gatherings easy." p.extra_deps << ["rails", "~> 2.3.2"] + p.extra_dev_deps << ["minitest", "~> 1.3.1"] + p.extra_dev_deps << ["sqlite3-ruby", "~> 1.2.4"] p.developer('Phil Hagelberg', 'technomancy@gmail.com') end diff --git a/lib/harker.rb b/lib/harker.rb index 0520da0..1953afa 100644 --- a/lib/harker.rb +++ b/lib/harker.rb @@ -1,40 +1,47 @@ require 'fileutils' -# Harker lets you deploy Rails apps via RubyGems +# Harker lets you deploy Rails apps via RubyGems. module Harker - VERSION = '0.0.1' - ACTIONS = %w(start stop restart init migrate console) + VERSION = '0.0.2' + ACTIONS = %w(start stop restart init migrate console foreground) module_function # Dispatch based on user's command def launch(name, args) - @root = args.shift || Dir.pwd + @root = File.expand_path(args.shift || Dir.pwd) @name = name action = args.shift - + unless ACTIONS.include?(action) abort("Usage: #{@name} INSTANCE_DIR (#{ACTIONS.join('|')})") end - load_app + load_app unless action == 'init' self.send(action) end - # Start the server - def start - # TODO: this needs to be way more configurable - Rack::Handler::Mongrel.run(ActionController::Dispatcher.new, - :Port => 9292, :Host => "0.0.0.0", :AccessLog => []) + # Start the application server in the foreground. + def foreground + start(false) + end + + # Start and daemonize the application server + def start(daemonize = true) + # can has internal consistency plz, Rails? + Rails::Rack::LogTailer::EnvironmentLog.replace(Rails.configuration.log_path) + + ARGV.replace ["--config=#{@root}/config.ru"] + ARGV.push('--daemon') if daemonize + require 'commands/server' end - # Stop the server def stop - raise "Not implemented yet; sorry!" # TODO + # TODO: this depends on sane (non-shared) tmpdir behaviour + raise "Can't stop the funk! (Not implemented yet; sorry!)" end - # Stop and start the application server def restart stop start @@ -45,34 +52,48 @@ def init FileUtils.mkdir_p(@root) FileUtils.mkdir_p(@root + '/log') FileUtils.mkdir_p(@root + '/tmp') - FileUtils.cp("#{RAILS_ROOT}/config/database.yml", "#{@root}/database.yml") + + # TODO: use the in-gem copy of database.yml as a base for this + File.open("#{@root}/database.yml", 'w') do |fp| + # TODO: be smart about environments; bleh + fp.puts({ 'production' => { 'adapter' => 'sqlite3', + 'database' => @root + '/db.sqlite3', + 'pool' => 5, 'timeout' => 5000 }}.to_yaml) + end + + # TODO: write a default config.ru? + puts "Initialized #{@name} instance in #{@root}..." - puts "Migrate with: #{@name} migrate" + puts + puts "Configure your database by editing #{@root}/database.yml." + puts "Optionally configure your web server via rack in #{@root}/config.ru." + puts + puts "Migrate your DB with: #{@name} migrate" puts "Then launch with: #{@name} start" end - # Perform all pending migrations for this instance def migrate - ActiveRecord::Migrator.migrate(RAILS_ROOT + '/db', + ActiveRecord::Migrator.migrate(RAILS_ROOT + '/db/migrate', ENV["VERSION"] && ENV["VERSION"].to_i) end - # Run script/console def console - require 'commands/console' + require 'irb' + IRB.start end def configure(config) - # config.instance_eval { @root_path = root } config.database_configuration_file = File.join(@root, 'database.yml') - config.log_path = File.join(@root, 'log', "#{environment}.log") - config.log_path = [:file_store, - File.join(@root, 'tmp', 'cache', "#{environment}.log")] - # TODO: pids? sessions? sockets? + config.log_path = File.join(@root, 'log', "#{RAILS_ENV}.log") + config.cache_store = [:file_store, File.join(@root, 'tmp', 'cache')] + # TODO: tmp dir? multiple instances will break if they share a tmp + # This may require fixing Rails. =\ + + # Currently you need to mkdir RAILS_ROOT/tmp and chmod/chown it + # after installing your app. This sucks! end - # :nodoc: def load_app - require name + require @name end end diff --git a/test/sample/Manifest.txt b/test/sample/Manifest.txt index 2f98518..e10506b 100644 --- a/test/sample/Manifest.txt +++ b/test/sample/Manifest.txt @@ -2,10 +2,10 @@ Manifest.txt README.rdoc Rakefile app/controllers/application_controller.rb -app/controllers/harkers_controller.rb +app/controllers/harks_controller.rb app/helpers/application_helper.rb -app/helpers/harkers_helper.rb -app/models/harker.rb +app/helpers/harks_helper.rb +app/models/hark.rb bin/sample_rails config/boot.rb config/database.yml @@ -20,7 +20,7 @@ config/initializers/new_rails_defaults.rb config/initializers/session_store.rb config/locales/en.yml config/routes.rb -db/migrate/20090325225423_create_harkers.rb +db/migrate/20090326050232_create_harks.rb lib/sample_rails.rb public/404.html public/422.html @@ -44,9 +44,10 @@ script/performance/profiler script/plugin script/runner script/server -test/fixtures/harkers.yml -test/functional/harkers_controller_test.rb +test/fixtures/harks.yml +test/functional/harks_controller_test.rb test/performance/browsing_test.rb test/test_helper.rb -test/unit/harker_test.rb -test/unit/helpers/harkers_helper_test.rb +test/unit/hark_test.rb +test/unit/helpers/harks_helper_test.rb +tmp diff --git a/test/sample/app/controllers/harkers_controller.rb b/test/sample/app/controllers/harkers_controller.rb deleted file mode 100644 index 15d1d78..0000000 --- a/test/sample/app/controllers/harkers_controller.rb +++ /dev/null @@ -1,2 +0,0 @@ -class HarkersController < ApplicationController -end diff --git a/test/sample/app/controllers/harks_controller.rb b/test/sample/app/controllers/harks_controller.rb new file mode 100644 index 0000000..c38878b --- /dev/null +++ b/test/sample/app/controllers/harks_controller.rb @@ -0,0 +1,2 @@ +class HarksController < ApplicationController +end diff --git a/test/sample/app/helpers/harkers_helper.rb b/test/sample/app/helpers/harkers_helper.rb deleted file mode 100644 index 3e71cb1..0000000 --- a/test/sample/app/helpers/harkers_helper.rb +++ /dev/null @@ -1,2 +0,0 @@ -module HarkersHelper -end diff --git a/test/sample/app/helpers/harks_helper.rb b/test/sample/app/helpers/harks_helper.rb new file mode 100644 index 0000000..f4ebfbb --- /dev/null +++ b/test/sample/app/helpers/harks_helper.rb @@ -0,0 +1,2 @@ +module HarksHelper +end diff --git a/test/sample/app/models/hark.rb b/test/sample/app/models/hark.rb new file mode 100644 index 0000000..c9224d1 --- /dev/null +++ b/test/sample/app/models/hark.rb @@ -0,0 +1,2 @@ +class Hark < ActiveRecord::Base +end diff --git a/test/sample/app/models/harker.rb b/test/sample/app/models/harker.rb deleted file mode 100644 index 6f3522a..0000000 --- a/test/sample/app/models/harker.rb +++ /dev/null @@ -1,2 +0,0 @@ -class Harker < ActiveRecord::Base -end diff --git a/test/sample/config/routes.rb b/test/sample/config/routes.rb index 69071e9..a428779 100644 --- a/test/sample/config/routes.rb +++ b/test/sample/config/routes.rb @@ -1,5 +1,5 @@ ActionController::Routing::Routes.draw do |map| - map.resources :harkers + map.resources :harks # The priority is based upon order of creation: first created -> highest priority. diff --git a/test/sample/db/migrate/20090325225423_create_harkers.rb b/test/sample/db/migrate/20090325225423_create_harkers.rb deleted file mode 100644 index 793d61e..0000000 --- a/test/sample/db/migrate/20090325225423_create_harkers.rb +++ /dev/null @@ -1,13 +0,0 @@ -class CreateHarkers < ActiveRecord::Migration - def self.up - create_table :harkers do |t| - t.string :name - t.date :birthday - t.timestamps - end - end - - def self.down - drop_table :harkers - end -end diff --git a/test/sample/db/migrate/20090326050232_create_harks.rb b/test/sample/db/migrate/20090326050232_create_harks.rb new file mode 100644 index 0000000..33b67e9 --- /dev/null +++ b/test/sample/db/migrate/20090326050232_create_harks.rb @@ -0,0 +1,12 @@ +class CreateHarks < ActiveRecord::Migration + def self.up + create_table :harks do |t| + t.string :tidings + t.timestamps + end + end + + def self.down + drop_table :harks + end +end diff --git a/test/sample/lib/sample_rails.rb b/test/sample/lib/sample_rails.rb index d81e525..7c8d156 100644 --- a/test/sample/lib/sample_rails.rb +++ b/test/sample/lib/sample_rails.rb @@ -1,2 +1,2 @@ # Allow rubygems to load this app -require File.dirname(__FILE__) + '/../config/boot' +require File.dirname(__FILE__) + '/../config/environment' diff --git a/test/sample/test/fixtures/harkers.yml b/test/sample/test/fixtures/harks.yml similarity index 100% rename from test/sample/test/fixtures/harkers.yml rename to test/sample/test/fixtures/harks.yml diff --git a/test/sample/test/functional/harkers_controller_test.rb b/test/sample/test/functional/harks_controller_test.rb similarity index 65% rename from test/sample/test/functional/harkers_controller_test.rb rename to test/sample/test/functional/harks_controller_test.rb index 7289d73..1c51a7c 100644 --- a/test/sample/test/functional/harkers_controller_test.rb +++ b/test/sample/test/functional/harks_controller_test.rb @@ -1,6 +1,6 @@ require 'test_helper' -class HarkersControllerTest < ActionController::TestCase +class HarksControllerTest < ActionController::TestCase # Replace this with your real tests. test "the truth" do assert true diff --git a/test/sample/test/unit/harker_test.rb b/test/sample/test/unit/hark_test.rb similarity index 71% rename from test/sample/test/unit/harker_test.rb rename to test/sample/test/unit/hark_test.rb index 28173e4..a2ca6a9 100644 --- a/test/sample/test/unit/harker_test.rb +++ b/test/sample/test/unit/hark_test.rb @@ -1,6 +1,6 @@ require 'test_helper' -class HarkerTest < ActiveSupport::TestCase +class HarkTest < ActiveSupport::TestCase # Replace this with your real tests. test "the truth" do assert true diff --git a/test/sample/test/unit/helpers/harkers_helper_test.rb b/test/sample/test/unit/helpers/harkers_helper_test.rb deleted file mode 100644 index d2435ab..0000000 --- a/test/sample/test/unit/helpers/harkers_helper_test.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'test_helper' - -class HarkersHelperTest < ActionView::TestCase -end diff --git a/test/sample/test/unit/helpers/harks_helper_test.rb b/test/sample/test/unit/helpers/harks_helper_test.rb new file mode 100644 index 0000000..0dc4c7c --- /dev/null +++ b/test/sample/test/unit/helpers/harks_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class HarksHelperTest < ActionView::TestCase +end diff --git a/test/sample/tmp b/test/sample/tmp new file mode 120000 index 0000000..cad2309 --- /dev/null +++ b/test/sample/tmp @@ -0,0 +1 @@ +/tmp \ No newline at end of file diff --git a/test/test_harker.rb b/test/test_harker.rb index 1729cc6..843162e 100644 --- a/test/test_harker.rb +++ b/test/test_harker.rb @@ -1,8 +1,54 @@ -require "test/unit" -require "harker" +require "rubygems" +require "minitest/unit" +require 'fileutils' -class TestHarker < Test::Unit::TestCase - def test_sanity - flunk "write tests or I will kneecap you" +require File.dirname(__FILE__) + "/../lib/harker" + +begin + ENV['RAILS_ENV'] = 'production' + gem 'sample_rails' +rescue LoadError + abort "You need the sample_rails gem installed: + $ cd #{File.dirname(__FILE__) + '/sample'} && rake install_gem" +end + +class TestHarker < MiniTest::Unit::TestCase + ROOT = "/tmp/harker-test-#{Process.pid}" + + def setup + suppress_out { Harker.launch('sample_rails', [ROOT, 'init']) } + end + + def teardown + FileUtils.rm_rf ROOT + end + + def test_init + assert File.exist?(ROOT) + assert File.exist?(ROOT + '/database.yml') + end + + # def test_start + # flunk "not yet" + # end + + # def test_stop + # flunk "can't stop the funk" + # end + + def test_migrate + suppress_out { Harker.launch('sample_rails', [ROOT, 'migrate']) } + assert_equal(['id', 'tidings', 'updated_at', 'created_at'].sort, + Hark.columns.map{ |c| c.name }.sort) + end + + private + def suppress_out + $stdout = StringIO.new + yield + ensure + $stdout = STDOUT end end + +MiniTest::Unit.autorun