Subcommand UI for project creation and initialization.

This commit is contained in:
Chris Eppstein 2009-10-04 00:33:36 -07:00
parent 4cc569586b
commit 0fc9a0e3c8
10 changed files with 286 additions and 74 deletions

68
COMMAND_LINE.markdown Normal file
View File

@ -0,0 +1,68 @@
Compass Command Line Documentation
==================================
Extensions Commands
-------------------
# install a global extension. probably requires sudo.
compass extension install extension_name
# install an extension into a project
compass extension unpack extension_name [path/to/project]
# uninstall a local or global extension. global extensions will require sudo.
compass extension uninstall extension_name [path/to/project]
# list the extensions in the project
compass extensions list
# list the extensions available for install
compass extensions available
Project Commands
----------------
# Create a new compass project
compass create path/to/project [--using blueprint] [--sass-dir=sass ...] [--project-type=rails]
# Initialize an existing project to work with compass
compass init rails path/to/project [--using blueprint]
# Install a pattern from an extension into a project
compass install blueprint/buttons [path/to/project]
# Compile the project's sass files into css
compass compile [path/to/project]
# Watch the project for changes and compile whenever it does
compass watch [path/to/project]
# Emit a configuration file at the location specified.
compass config [path/to/config] [--sass-dir=sass --css-dir=css ...]
# Validate the generated CSS.
compass validate [path/to/project]
misc commands
-------------
# Generate a background image that can be used to verify grid alignment
compass grid-background W+GxH [path/to/image.png]
# Emit the version of compass
compass version
# Get help on compass
compass help
# Get help on an extension
compass help extension_name
# Get help about an extension pattern
compass help extension_name/pattern_name
# Get help about a particular sub command
compass help command_name

View File

@ -1,10 +1,8 @@
module Compass module Compass
module Commands module Commands
class Base class Base
def self.inherited(command_class) def self.register(command_name)
if command_class.respond_to? :name Compass::Commands[command_name] = self
Compass::Commands[command_class.name] = command_class
end
end end
include Actions include Actions

View File

@ -3,10 +3,72 @@ require 'compass/commands/stamp_pattern'
module Compass module Compass
module Commands module Commands
module CreateProjectOptionsParser
def set_options(opts)
opts.banner = %q{
Usage: compass create path/to/project [options]
Description:
Create a new compass project at the path specified.
}.split("\n").map{|l| l.gsub(/^ */,'')}.join("\n")
opts.on("--using FRAMEWORK", "Framework to use when creating the project.") do |framework|
framework = framework.split('/', 2)
self.options[:framework] = framework[0]
self.options[:pattern] = framework[1]
end
super
end
end
class CreateProject < StampPattern class CreateProject < StampPattern
def initialize(working_path, options) register :create
super(working_path, options.merge(:pattern => "project", :pattern_name => nil)) register :init
class << self
def parse!(arguments)
parser = parse_options!(arguments)
parse_arguments!(parser, arguments)
set_default_arguments(parser)
parser.options
end
def parse_init!(arguments)
parser = parse_options!(arguments)
if arguments.size > 0
parser.options[:project_type] = arguments.shift.to_sym
end
parse_arguments!(parser, arguments)
set_default_arguments(parser)
parser.options
end
def parse_options!(arguments)
parser = Compass::Exec::CommandOptionParser.new(arguments)
parser.extend(Compass::Exec::GlobalOptionsParser)
parser.extend(Compass::Exec::ProjectOptionsParser)
parser.extend(CreateProjectOptionsParser)
parser.parse!
parser
end
def parse_arguments!(parser, arguments)
if arguments.size == 1
parser.options[:project_name] = arguments.shift
elsif arguments.size == 0
raise Compass::Error, "Please specify a path to the project."
else
raise Compass::Error, "Too many arguments were specified."
end
end
def set_default_arguments(parser)
parser.options[:framework] ||= :compass
parser.options[:pattern] ||= "project"
end
end end
def is_project_creation? def is_project_creation?

View File

@ -27,7 +27,7 @@ module Compass
end end
def installer_args def installer_args
[template_directory(options[:pattern]), project_directory, options] [template_directory(options[:pattern] || "project"), project_directory, options]
end end
end end
end end

View File

@ -8,8 +8,8 @@ require 'compass/commands'
module Compass::Exec module Compass::Exec
end end
require 'compass/exec/helpers' %w(helpers switch_ui sub_command_ui
require 'compass/exec/switch_ui' global_options_parser project_options_parser
require 'compass/exec/sub_command_ui' command_option_parser).each do |lib|
require "compass/exec/#{lib}"
end

View File

@ -0,0 +1,16 @@
module Compass::Exec
class CommandOptionParser
attr_accessor :options, :arguments
def initialize(arguments)
self.arguments = arguments
self.options = {}
end
def parse!
opts = OptionParser.new(&method(:set_options))
opts.parse!(arguments)
end
def set_options(opts)
end
end
end

View File

@ -0,0 +1,37 @@
module Compass::Exec::GlobalOptionsParser
def set_options(opts)
super
set_global_options(opts)
end
def set_global_options(opts)
opts.on('-r LIBRARY', '--require LIBRARY',
"Require the given ruby LIBRARY before running commands.",
" This is used to access compass plugins without having a",
" project configuration file.") do |library|
::Compass.configuration.require library
end
opts.on('-q', '--quiet', :NONE, 'Quiet mode.') do
self.options[:quiet] = true
end
opts.on('--trace', :NONE, 'Show a full stacktrace on error') do
self.options[:trace] = true
end
opts.on('--force', :NONE, 'Allows some failing commands to succeed instead.') do
self.options[:force] = true
end
opts.on('--dry-run', :NONE, 'Dry Run. Tells you what it plans to do.') do
self.options[:dry_run] = true
end
opts.on_tail("-?", "-h", "--help", "Show this message") do
puts opts
exit
end
end
end

View File

@ -0,0 +1,42 @@
module Compass::Exec::ProjectOptionsParser
def set_options(opts)
super
set_project_options(opts)
end
def set_project_options(opts)
opts.on('-c', '--config CONFIG_FILE', 'Specify the location of the configuration file explicitly.') do |configuration_file|
self.options[:configuration_file] = configuration_file
end
opts.on('--sass-dir SRC_DIR', "The source directory where you keep your sass stylesheets.") do |sass_dir|
self.options[:sass_dir] = sass_dir
end
opts.on('--css-dir CSS_DIR', "The target directory where you keep your css stylesheets.") do |css_dir|
self.options[:css_dir] = css_dir
end
opts.on('--images-dir IMAGES_DIR', "The directory where you keep your images.") do |images_dir|
self.options[:images_dir] = images_dir
end
opts.on('--javascripts-dir JS_DIR', "The directory where you keep your javascripts.") do |javascripts_dir|
self.options[:javascripts_dir] = javascripts_dir
end
opts.on('-e ENV', '--environment ENV', [:development, :production], 'Use sensible defaults for your current environment.',
' One of: development, production (default)') do |env|
self.options[:environment] = env
end
opts.on('-s STYLE', '--output-style STYLE', [:nested, :expanded, :compact, :compressed], 'Select a CSS output mode.',
' One of: nested, expanded, compact, compressed') do |style|
self.options[:output_style] = style
end
opts.on('--relative-assets', :NONE, 'Make compass asset helpers generate relative urls to assets.') do
self.options[:relative_assets] = true
end
end
end

View File

@ -0,0 +1,42 @@
require 'compass/exec/global_options_parser'
require 'compass/exec/project_options_parser'
module Compass::Exec
class SubCommandUI
attr_accessor :args
def initialize(args)
self.args = args
end
def run!
begin
perform!
rescue Exception => e
raise e if e.is_a? SystemExit
if e.is_a?(::Compass::Error) || e.is_a?(OptionParser::ParseError)
$stderr.puts e.message
else
::Compass::Exec::Helpers.report_error(e, @options)
end
return 1
end
return 0
end
protected
def perform!
command = args.shift
command_class = Compass::Commands[command]
options = if command_class.respond_to?("parse_#{command}!")
command_class.send("parse_#{command}!", args)
else
command_class.parse!(args)
end
command_class.new(Dir.getwd, options).execute
end
end
end

View File

@ -1,6 +1,10 @@
require 'compass/exec/global_options_parser'
require 'compass/exec/project_options_parser'
module Compass::Exec module Compass::Exec
class SwitchUI class SwitchUI
include GlobalOptionsParser
include ProjectOptionsParser
attr_accessor :args, :options, :opts attr_accessor :args, :options, :opts
def initialize(args) def initialize(args)
@ -127,64 +131,12 @@ END
opts.separator '' opts.separator ''
opts.separator 'Configuration Options:' opts.separator 'Configuration Options:'
opts.on('-c', '--config CONFIG_FILE', 'Specify the location of the configuration file explicitly.') do |configuration_file| set_project_options(opts)
self.options[:configuration_file] = configuration_file
end
opts.on('--sass-dir SRC_DIR', "The source directory where you keep your sass stylesheets.") do |sass_dir|
self.options[:sass_dir] = sass_dir
end
opts.on('--css-dir CSS_DIR', "The target directory where you keep your css stylesheets.") do |css_dir|
self.options[:css_dir] = css_dir
end
opts.on('--images-dir IMAGES_DIR', "The directory where you keep your images.") do |images_dir|
self.options[:images_dir] = images_dir
end
opts.on('--javascripts-dir JS_DIR', "The directory where you keep your javascripts.") do |javascripts_dir|
self.options[:javascripts_dir] = javascripts_dir
end
opts.on('-e ENV', '--environment ENV', [:development, :production], 'Use sensible defaults for your current environment.',
' One of: development, production (default)') do |env|
self.options[:environment] = env
end
opts.on('-s STYLE', '--output-style STYLE', [:nested, :expanded, :compact, :compressed], 'Select a CSS output mode.',
' One of: nested, expanded, compact, compressed') do |style|
self.options[:output_style] = style
end
opts.on('--relative-assets', :NONE, 'Make compass asset helpers generate relative urls to assets.') do
self.options[:relative_assets] = true
end
opts.separator '' opts.separator ''
opts.separator 'General Options:' opts.separator 'General Options:'
opts.on('-r LIBRARY', '--require LIBRARY', "Require the given ruby LIBRARY before running commands.", set_global_options(opts)
" This is used to access compass plugins without having a",
" project configuration file.") do |library|
::Compass.configuration.require library
end
opts.on('-q', '--quiet', :NONE, 'Quiet mode.') do
self.options[:quiet] = true
end
opts.on('--dry-run', :NONE, 'Dry Run. Tells you what it plans to do.') do
self.options[:dry_run] = true
end
opts.on('--trace', :NONE, 'Show a full stacktrace on error') do
self.options[:trace] = true
end
opts.on('--force', :NONE, 'Force. Allows some failing commands to succeed instead.') do
self.options[:force] = true
end
opts.on('--imports', :NONE, 'Emit an imports suitable for passing to the sass command-line.', opts.on('--imports', :NONE, 'Emit an imports suitable for passing to the sass command-line.',
' Example: sass `compass --imports`', ' Example: sass `compass --imports`',
@ -198,11 +150,6 @@ END
exit exit
end end
opts.on_tail("-?", "-h", "--help", "Show this message") do
puts opts
exit
end
opts.on_tail("-v", "--version", "Print version") do opts.on_tail("-v", "--version", "Print version") do
self.options[:command] = :print_version self.options[:command] = :print_version
end end