From 98b2b293c19c06e009e6fed827cc399e16441e28 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Wed, 21 Dec 2011 12:13:28 -0800 Subject: [PATCH] Fix custom commands for Ruby 1.9.x --- bin/middleman | 2 +- features/builder.feature | 5 +++++ features/cli.feature | 12 ++++++++++ fixtures/3rd-party-command/config.rb | 2 +- lib/middleman.rb | 4 ++-- lib/middleman/cli.rb | 17 ++++++++++++-- lib/middleman/cli/build.rb | 15 +++++++------ lib/middleman/cli/{templates.rb => init.rb} | 25 +++++++++++---------- lib/middleman/cli/server.rb | 19 ++++++++-------- 9 files changed, 67 insertions(+), 34 deletions(-) rename lib/middleman/cli/{templates.rb => init.rb} (74%) diff --git a/bin/middleman b/bin/middleman index 46e999f7..b743f2d3 100755 --- a/bin/middleman +++ b/bin/middleman @@ -50,7 +50,7 @@ module Middleman def start_cli! require 'middleman' - Middleman::CLI::Base.start + Middleman::Cli::Base.start end end end diff --git a/features/builder.feature b/features/builder.feature index 8445895c..c5b23f20 100644 --- a/features/builder.feature +++ b/features/builder.feature @@ -45,3 +45,8 @@ Feature: Builder Scenario: Build with errors Given a built app at "build-with-errors-app" Then the exit status should be 1 + + Scenario: Build alias (b) + Given a fixture app "test-app" + When I run `middleman b` + Then was successfully built \ No newline at end of file diff --git a/features/cli.feature b/features/cli.feature index 938849f4..dcb6e6a4 100644 --- a/features/cli.feature +++ b/features/cli.feature @@ -15,6 +15,18 @@ Feature: Middleman CLI | index.html.erb | | layout.erb | | stylesheets/site.css.scss | + + Scenario: Create a new project (alias i) + When I run `middleman i MY_PROJECT` + Then a directory named "MY_PROJECT" should exist + + Scenario: Create a new project (alias i) + When I run `middleman new MY_PROJECT` + Then a directory named "MY_PROJECT" should exist + + Scenario: Create a new project (alias i) + When I run `middleman n MY_PROJECT` + Then a directory named "MY_PROJECT" should exist Scenario: Create a new project with Rack When I run `middleman init MY_PROJECT --rack` diff --git a/fixtures/3rd-party-command/config.rb b/fixtures/3rd-party-command/config.rb index edc0d327..f7bded87 100644 --- a/fixtures/3rd-party-command/config.rb +++ b/fixtures/3rd-party-command/config.rb @@ -7,4 +7,4 @@ class HelloWorld < Thor end end -Middleman::CLI::Base.register(HelloWorld, :hello, "hello", "Say hello") \ No newline at end of file +Middleman::Cli::Base.register(HelloWorld, :hello, "hello", "Say hello") \ No newline at end of file diff --git a/lib/middleman.rb b/lib/middleman.rb index 33de9f81..1cf7785b 100755 --- a/lib/middleman.rb +++ b/lib/middleman.rb @@ -15,10 +15,10 @@ module Middleman autoload :Templates, "middleman/templates" autoload :Guard, "middleman/guard" - module CLI + module Cli autoload :Base, "middleman/cli" autoload :Build, "middleman/cli/build" - autoload :Templates, "middleman/cli/templates" + autoload :Init, "middleman/cli/init" autoload :Server, "middleman/cli/server" end diff --git a/lib/middleman/cli.rb b/lib/middleman/cli.rb index bc2ac6be..db83b002 100644 --- a/lib/middleman/cli.rb +++ b/lib/middleman/cli.rb @@ -2,7 +2,7 @@ require 'thor' require "thor/group" # CLI Module -module Middleman::CLI +module Middleman::Cli class Base < Thor include Thor::Actions @@ -22,6 +22,19 @@ module Middleman::CLI say "Middleman #{Middleman::VERSION}" end + def method_missing(meth, *args) + meth = meth.to_s + + if self.class.map.has_key?(meth) + meth = self.class.map[meth] + end + + # initialize_thorfiles(meth) + klass, task = Thor::Util.find_class_and_task_by_namespace("#{meth}:#{meth}") + args.unshift(task) if task + klass.start(args, :shell => self.shell) + end + private def help_check @@ -31,6 +44,6 @@ module Middleman::CLI end end -require "middleman/cli/templates" +require "middleman/cli/init" require "middleman/cli/server" require "middleman/cli/build" \ No newline at end of file diff --git a/lib/middleman/cli/build.rb b/lib/middleman/cli/build.rb index 37d854a3..6a09fa7b 100644 --- a/lib/middleman/cli/build.rb +++ b/lib/middleman/cli/build.rb @@ -1,23 +1,25 @@ require "rack" require "rack/test" -module Middleman::CLI - class Build < Thor::Group +module Middleman::Cli + class Build < Thor include Thor::Actions check_unknown_options! - desc "build [options]" - class_option :relative, + namespace :build + + desc "build [options]", "Builds the static site for deployment" + method_option :relative, :type => :boolean, :aliases => "-r", :default => false, :desc => 'Force relative urls' - class_option :clean, + method_option :clean, :type => :boolean, :aliases => "-c", :default => false, :desc => 'Removes orpahand files or directories from build' - class_option :glob, + method_option :glob, :type => :string, :aliases => "-g", :default => nil, @@ -185,6 +187,5 @@ module Middleman::CLI end end - Base.register(Build, :build, "build [options]", "Builds the static site for deployment") Base.map({ "b" => "build" }) end \ No newline at end of file diff --git a/lib/middleman/cli/templates.rb b/lib/middleman/cli/init.rb similarity index 74% rename from lib/middleman/cli/templates.rb rename to lib/middleman/cli/init.rb index 5db200ae..1ba260d9 100644 --- a/lib/middleman/cli/templates.rb +++ b/lib/middleman/cli/init.rb @@ -1,32 +1,34 @@ -module Middleman::CLI - class Templates < Thor::Group +module Middleman::Cli + class Init < Thor check_unknown_options! - desc "init NAME [options]" + namespace :init + + desc "init NAME [options]", "Create new project NAME" available_templates = ::Middleman::Templates.registered.keys.join(", ") - argument :name - class_option "template", + # argument :name + method_option "template", :aliases => "-T", :default => "default", :desc => "Use a project template: #{available_templates}" - class_option "css_dir", + method_option "css_dir", :default => "stylesheets", :desc => 'The path to the css files' - class_option "js_dir", + method_option "js_dir", :default => "javascripts", :desc => 'The path to the javascript files' - class_option "images_dir", + method_option "images_dir", :default => "images", :desc => 'The path to the image files' - class_option "rack", + method_option "rack", :type => :boolean, :default => false, :desc => 'Include a config.ru file' - class_option "bundler", + method_option "bundler", :type => :boolean, :default => false, :desc => 'Create a Gemfile and use Bundler to manage gems' - def init + def init(name) key = options[:template].to_sym unless ::Middleman::Templates.registered.has_key?(key) raise Thor::Error.new "Unknown project template '#{key}'" @@ -37,7 +39,6 @@ module Middleman::CLI end end - Base.register(Templates, :init, "init NAME [options]", "Create new project NAME") Base.map({ "i" => "init", "new" => "init", diff --git a/lib/middleman/cli/server.rb b/lib/middleman/cli/server.rb index 48f9ad12..1cfde75c 100644 --- a/lib/middleman/cli/server.rb +++ b/lib/middleman/cli/server.rb @@ -1,23 +1,25 @@ -module Middleman::CLI - class Server < Thor::Group +module Middleman::Cli + class Server < Thor check_unknown_options! - - desc "server [options]" - class_option "environment", + + namespace :server + + desc "server [options]", "Start the preview server" + method_option "environment", :aliases => "-e", :default => ENV['MM_ENV'] || ENV['RACK_ENV'] || 'development', :desc => "The environment Middleman will run under" - class_option :host, + method_option :host, :type => :string, :aliases => "-h", # :required => true, :default => "0.0.0.0", :desc => "Bind to HOST address" - class_option "port", + method_option "port", :aliases => "-p", :default => "4567", :desc => "The port Middleman will listen on" - class_option "debug", + method_option "debug", :type => :boolean, :default => false, :desc => 'Print debug messages' @@ -34,7 +36,6 @@ module Middleman::CLI end end - Base.register(Server, :server, "server [options]", "Start the preview server") Base.map({ "s" => "server" }) Base.default_task :server end \ No newline at end of file