Refactor the Compass commands to centralize and cleanup the project commands.
This commit is contained in:
parent
911041f355
commit
1e5502159b
@ -13,18 +13,22 @@ module Compass
|
|||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def projectize(path)
|
def relativize(path)
|
||||||
File.join(project_directory, separate(path))
|
if path.index(working_directory+File::SEPARATOR) == 0
|
||||||
|
path[(working_directory+File::SEPARATOR).length..-1]
|
||||||
|
else
|
||||||
|
path
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# create a directory and all the directories necessary to reach it.
|
# create a directory and all the directories necessary to reach it.
|
||||||
def directory(subdir, options = nil)
|
def directory(dir, options = nil)
|
||||||
options ||= self.options
|
options ||= self.options
|
||||||
dir = subdir ? projectize(subdir) : project_directory
|
|
||||||
if File.exists?(dir) && File.directory?(dir) && options[:force]
|
if File.exists?(dir) && File.directory?(dir) && options[:force]
|
||||||
print_action :exists, basename(dir) + File::SEPARATOR
|
print_action :exists, basename(dir) + File::SEPARATOR
|
||||||
elsif File.exists?(dir) && File.directory?(dir)
|
elsif File.exists?(dir) && File.directory?(dir)
|
||||||
msg = "Directory #{basename(dir)} already exists. Run with --force to force project creation."
|
msg = "Directory #{basename(dir)} already exists. Run with --force to force creation."
|
||||||
raise ::Compass::Exec::ExecError.new(msg)
|
raise ::Compass::Exec::DirectoryExistsError.new(msg)
|
||||||
elsif File.exists?(dir)
|
elsif File.exists?(dir)
|
||||||
msg = "#{basename(dir)} already exists and is not a directory."
|
msg = "#{basename(dir)} already exists and is not a directory."
|
||||||
raise ::Compass::Exec::ExecError.new(msg)
|
raise ::Compass::Exec::ExecError.new(msg)
|
||||||
@ -34,9 +38,13 @@ module Compass
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def absolute_path?(path)
|
||||||
|
# This is only going to work on unix, gonna need a better implementation.
|
||||||
|
path.index(File::SEPARATOR) == 0
|
||||||
|
end
|
||||||
|
|
||||||
# copy/process a template in the compass template directory to the project directory.
|
# copy/process a template in the compass template directory to the project directory.
|
||||||
def template(from, to, options)
|
def template(from, to, options)
|
||||||
to = projectize(to)
|
|
||||||
from = File.join(templates_directory, separate(from))
|
from = File.join(templates_directory, separate(from))
|
||||||
if File.exists?(to) && !options[:force]
|
if File.exists?(to) && !options[:force]
|
||||||
#TODO: Detect differences & provide an overwrite prompt
|
#TODO: Detect differences & provide an overwrite prompt
|
||||||
@ -75,9 +83,9 @@ module Compass
|
|||||||
path.gsub(%r{/}, File::SEPARATOR)
|
path.gsub(%r{/}, File::SEPARATOR)
|
||||||
end
|
end
|
||||||
|
|
||||||
def basename(file, extra = 0)
|
def basename(file)
|
||||||
if file.length > (working_directory.length + extra)
|
if file.length > working_directory.length
|
||||||
file[(working_directory.length + extra + 1)..-1]
|
relativize(file)
|
||||||
else
|
else
|
||||||
File.basename(file)
|
File.basename(file)
|
||||||
end
|
end
|
||||||
|
@ -4,33 +4,41 @@ require File.join(File.dirname(__FILE__), 'update_project')
|
|||||||
|
|
||||||
module Compass
|
module Compass
|
||||||
module Commands
|
module Commands
|
||||||
class CreateProject < Base
|
class CreateProject < ProjectBase
|
||||||
|
|
||||||
attr_accessor :project_directory, :project_name
|
|
||||||
|
|
||||||
def initialize(working_directory, options)
|
def initialize(working_directory, options)
|
||||||
super(working_directory, options)
|
super(working_directory, options)
|
||||||
self.project_name = options[:project_name]
|
|
||||||
self.project_directory = File.expand_path File.join(working_directory, project_name)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# all commands must implement perform
|
# all commands must implement perform
|
||||||
def perform
|
def perform
|
||||||
|
begin
|
||||||
directory nil, options
|
directory nil, options
|
||||||
|
rescue Compass::Exec::DirectoryExistsError
|
||||||
|
msg = "Project directory already exists. Run with -u to update or --force to force creation."
|
||||||
|
raise ::Compass::Exec::DirectoryExistsError.new(msg)
|
||||||
|
end
|
||||||
src_dir = options[:src_dir] || "src"
|
src_dir = options[:src_dir] || "src"
|
||||||
css_dir = options[:css_dir] || "stylesheets"
|
css_dir = options[:css_dir] || "stylesheets"
|
||||||
directory src_dir, options.merge(:force => true)
|
directory src_dir, options.merge(:force => true)
|
||||||
directory css_dir, options.merge(:force => true)
|
directory css_dir, options.merge(:force => true)
|
||||||
project_dir = File.join(templates_directory, "project")
|
framework_templates.each do |t|
|
||||||
templates = Dir.chdir(project_dir) do
|
|
||||||
Dir.glob("*")
|
|
||||||
end
|
|
||||||
templates.each do |t|
|
|
||||||
template "project/#{t}", "#{src_dir}/#{t}", options
|
template "project/#{t}", "#{src_dir}/#{t}", options
|
||||||
end
|
end
|
||||||
UpdateProject.new(working_directory, options).perform
|
UpdateProject.new(working_directory, options).perform
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def framework_templates
|
||||||
|
framework_project_dir = File.join(templates_directory, "project")
|
||||||
|
Dir.chdir(framework_project_dir) do
|
||||||
|
Dir.glob("*")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def skip_project_directory_assertion?
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
@ -2,42 +2,26 @@ require File.join(File.dirname(__FILE__), 'base')
|
|||||||
|
|
||||||
module Compass
|
module Compass
|
||||||
module Commands
|
module Commands
|
||||||
class InstallRails < Base
|
class InstallRails < CreateProject
|
||||||
def initialize(working_directory, options)
|
def initialize(*args)
|
||||||
wd = options[:project_name] ? File.join(working_directory, options.delete(:project_name)) : working_directory
|
super
|
||||||
super(wd, options)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def perform
|
def perform
|
||||||
set_install_location
|
set_install_location
|
||||||
set_output_location
|
set_output_location
|
||||||
directory options[:stylesheets_location]
|
directory options[:stylesheets_location]
|
||||||
template 'project/screen.sass', "#{options[:stylesheets_location]}/screen.sass", options
|
framework_templates.each do |t|
|
||||||
template 'project/print.sass', "#{options[:stylesheets_location]}/print.sass", options
|
template "project/#{t}", "#{options[:stylesheets_location]}/#{t}", options
|
||||||
template 'project/ie.sass', "#{options[:stylesheets_location]}/ie.sass", options
|
end
|
||||||
write_file projectize('config/initializers/compass.rb'), initializer_contents
|
write_file 'config/initializers/compass.rb', initializer_contents
|
||||||
if has_application_layout?
|
if has_application_layout?
|
||||||
next_steps
|
next_steps
|
||||||
else
|
else
|
||||||
write_file projectize('app/views/layouts/application.html.haml'), application_layout_contents
|
write_file 'app/views/layouts/application.html.haml', application_layout_contents
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def initializer
|
|
||||||
init_file =
|
|
||||||
if File.exists?(init_file) && !options[:force]
|
|
||||||
msg = "File #{basename(init_file)} already exists. Run with --force to force project creation."
|
|
||||||
raise ::Compass::Exec::ExecError.new(msg)
|
|
||||||
end
|
|
||||||
if File.exists?(init_file)
|
|
||||||
print_action :overwrite, basename(init_file)
|
|
||||||
else
|
|
||||||
print_action :create, basename(init_file)
|
|
||||||
end
|
|
||||||
output = open(init_file,'w')
|
|
||||||
output.write(initializer_contents)
|
|
||||||
output.close
|
|
||||||
end
|
|
||||||
|
|
||||||
def initializer_contents
|
def initializer_contents
|
||||||
%Q{require 'compass'
|
%Q{require 'compass'
|
||||||
# If you have any compass plugins, require them here.
|
# If you have any compass plugins, require them here.
|
||||||
@ -97,10 +81,6 @@ NEXTSTEPS
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def project_directory
|
|
||||||
working_directory
|
|
||||||
end
|
|
||||||
|
|
||||||
def set_install_location
|
def set_install_location
|
||||||
print "Compass recommends that you keep your stylesheets in app/stylesheets/ instead of the Sass default location of public/stylesheets/sass/.\nIs this OK? (Y/n) "
|
print "Compass recommends that you keep your stylesheets in app/stylesheets/ instead of the Sass default location of public/stylesheets/sass/.\nIs this OK? (Y/n) "
|
||||||
answer = gets
|
answer = gets
|
||||||
|
89
lib/compass/commands/project_base.rb
Normal file
89
lib/compass/commands/project_base.rb
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
require 'rubygems'
|
||||||
|
require 'sass'
|
||||||
|
require 'fileutils'
|
||||||
|
require 'pathname'
|
||||||
|
require File.join(File.dirname(__FILE__), 'base')
|
||||||
|
|
||||||
|
module Compass
|
||||||
|
module Commands
|
||||||
|
class ProjectBase < Base
|
||||||
|
attr_accessor :project_directory, :project_name, :options, :project_src_subdirectory, :project_css_subdirectory
|
||||||
|
|
||||||
|
def initialize(working_directory, options = {})
|
||||||
|
super(working_directory, options)
|
||||||
|
self.project_name = determine_project_name(working_directory, options)
|
||||||
|
self.project_directory = determine_project_directory(working_directory, options)
|
||||||
|
assert_project_directory_exists!
|
||||||
|
end
|
||||||
|
|
||||||
|
protected
|
||||||
|
|
||||||
|
def directory(subdir, options = nil)
|
||||||
|
subdir ||= project_directory
|
||||||
|
subdir = projectize(subdir) unless absolute_path?(subdir)
|
||||||
|
super(subdir, options)
|
||||||
|
end
|
||||||
|
|
||||||
|
def template(from, to, options)
|
||||||
|
to = projectize(to) unless absolute_path?(to)
|
||||||
|
super(from, to, options)
|
||||||
|
end
|
||||||
|
|
||||||
|
def write_file(path, contents)
|
||||||
|
path = projectize(path) unless absolute_path?(path)
|
||||||
|
super(path, contents)
|
||||||
|
end
|
||||||
|
|
||||||
|
def projectize(path)
|
||||||
|
File.join(project_directory, separate(path))
|
||||||
|
end
|
||||||
|
|
||||||
|
# Read the configuration file for this project
|
||||||
|
def read_project_configuration
|
||||||
|
config_file = projectize('src/config.rb')
|
||||||
|
if File.exists?(config_file)
|
||||||
|
contents = open(config_file) {|f| f.read}
|
||||||
|
eval(contents, nil, config_file)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def determine_project_name(working_directory, options)
|
||||||
|
if options[:project_name]
|
||||||
|
File.basename(strip_trailing_separator(options[:project_name]))
|
||||||
|
else
|
||||||
|
File.basename(working_directory)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def determine_project_directory(working_directory, options)
|
||||||
|
if options[:project_name]
|
||||||
|
if absolute_path?(options[:project_name])
|
||||||
|
options[:project_name]
|
||||||
|
else
|
||||||
|
File.join(working_directory, options[:project_name])
|
||||||
|
end
|
||||||
|
else
|
||||||
|
working_directory
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def assert_project_directory_exists!
|
||||||
|
if File.exists?(project_directory) && !File.directory?(project_directory)
|
||||||
|
raise Compass::Exec::ExecError.new("#{project_directory} is not a directory.")
|
||||||
|
elsif !File.directory?(project_directory) && !skip_project_directory_assertion?
|
||||||
|
raise ::Compass::Exec::ExecError.new("#{project_directory} does not exist.")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def skip_project_directory_assertion?
|
||||||
|
options[:force] || options[:dry_run]
|
||||||
|
end
|
||||||
|
|
||||||
|
def strip_trailing_separator(path)
|
||||||
|
(path[-1..-1] == File::SEPARATOR) ? path[0..-2] : path
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -1,40 +1,12 @@
|
|||||||
require 'rubygems'
|
require File.join(File.dirname(__FILE__), 'project_base')
|
||||||
require 'sass'
|
|
||||||
require 'fileutils'
|
|
||||||
require 'pathname'
|
|
||||||
require File.join(File.dirname(__FILE__), 'base')
|
|
||||||
|
|
||||||
module Compass
|
module Compass
|
||||||
module Commands
|
module Commands
|
||||||
class UpdateProject < Base
|
class UpdateProject < ProjectBase
|
||||||
|
|
||||||
Base::ACTIONS << :compile
|
Base::ACTIONS << :compile
|
||||||
Base::ACTIONS << :overwrite
|
Base::ACTIONS << :overwrite
|
||||||
|
|
||||||
attr_accessor :project_directory, :project_name, :options, :project_src_subdirectory, :project_css_subdirectory
|
|
||||||
|
|
||||||
def initialize(working_directory, options = {})
|
|
||||||
super(working_directory, options)
|
|
||||||
if options[:project_name]
|
|
||||||
options[:project_name] = options[:project_name][0..-2] if options[:project_name][-1..-1] == File::SEPARATOR
|
|
||||||
self.project_name = File.basename(options[:project_name])
|
|
||||||
if options[:project_name][0] == File::SEPARATOR
|
|
||||||
self.project_directory = options[:project_name]
|
|
||||||
elsif File.directory?(File.join(working_directory, options[:project_name]))
|
|
||||||
self.project_directory = File.expand_path(File.join(working_directory, options[:project_name]))
|
|
||||||
else
|
|
||||||
if File.exists?(options[:project_name]) or File.exists?(File.join(working_directory, options[:project_name]))
|
|
||||||
raise ::Compass::Exec::ExecError.new("#{options[:project_name]} is not a directory.")
|
|
||||||
elsif !(options[:force] || options[:dry_run])
|
|
||||||
raise ::Compass::Exec::ExecError.new("#{options[:project_name]} does not exist.")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
else
|
|
||||||
self.project_name = File.basename(working_directory)
|
|
||||||
self.project_directory = working_directory
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def perform
|
def perform
|
||||||
read_project_configuration
|
read_project_configuration
|
||||||
Dir.glob(separate("#{project_src_directory}/**/[^_]*.sass")).each do |sass_file|
|
Dir.glob(separate("#{project_src_directory}/**/[^_]*.sass")).each do |sass_file|
|
||||||
@ -77,15 +49,6 @@ module Compass
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Read the configuration file for this project
|
|
||||||
def read_project_configuration
|
|
||||||
config_file = projectize('src/config.rb')
|
|
||||||
if File.exists?(config_file)
|
|
||||||
contents = open(config_file) {|f| f.read}
|
|
||||||
eval(contents, nil, config_file)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# where to load sass files from
|
# where to load sass files from
|
||||||
def sass_load_paths
|
def sass_load_paths
|
||||||
@sass_load_paths ||= [project_src_directory] + Compass::Frameworks::ALL.map{|f| f.stylesheets_directory}
|
@sass_load_paths ||= [project_src_directory] + Compass::Frameworks::ALL.map{|f| f.stylesheets_directory}
|
||||||
|
@ -7,6 +7,9 @@ module Compass
|
|||||||
class ExecError < StandardError
|
class ExecError < StandardError
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class DirectoryExistsError < ExecError
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
def report_error(e, options)
|
def report_error(e, options)
|
||||||
$stderr.puts "#{e.class} on line #{get_line e} of #{get_file e}: #{e.message}"
|
$stderr.puts "#{e.class} on line #{get_line e} of #{get_file e}: #{e.message}"
|
||||||
|
@ -55,6 +55,7 @@ class CommandLineTest < Test::Unit::TestCase
|
|||||||
assert_action_performed :create, "app/stylesheets/screen.sass"
|
assert_action_performed :create, "app/stylesheets/screen.sass"
|
||||||
assert_action_performed :create, "config/initializers/compass.rb"
|
assert_action_performed :create, "config/initializers/compass.rb"
|
||||||
assert_action_performed :create, "app/views/layouts/application.html.haml"
|
assert_action_performed :create, "app/views/layouts/application.html.haml"
|
||||||
|
assert_action_performed :create, "config/initializers/compass.rb"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
rescue LoadError
|
rescue LoadError
|
||||||
|
Loading…
Reference in New Issue
Block a user