Modify the update command to use the Actions module and Logger. Moved the compile action to the Actions module.

This commit is contained in:
Chris Eppstein 2009-02-04 09:10:07 -08:00
parent dbc262fbe1
commit a0552a67c4
10 changed files with 80 additions and 167 deletions

View File

@ -38,6 +38,7 @@ module Compass
end
end
# Write a file given the file contents as a string
def write_file(file_name, contents, options = nil)
options ||= self.options if self.respond_to?(:options)
if File.exists?(file_name) && !options[:force]
@ -54,6 +55,26 @@ module Compass
end
end
# Compile one Sass file
def compile(sass_filename, css_filename, options)
target_directory = File.dirname(css_filename)
directory target_directory
logger.record :compile, basename(sass_filename)
if File.exists?(css_filename)
logger.record :overwrite, basename(css_filename)
else
logger.record :create, basename(css_filename)
end
engine = ::Sass::Engine.new(open(sass_filename).read,
:filename => sass_filename,
:line_comments => options[:environment] == :development,
:style => options[:style],
:css_filename => css_filename,
:load_paths => options[:load_paths])
css_content = engine.render
open(css_filename,'w') {|output| output.write(css_content)} unless options[:dry_run]
end
def basename(file)
relativize(file) {|f| File.basename(file)}
end
@ -68,5 +89,10 @@ module Compass
end
end
# Write paths like we're on unix and then fix it
def separate(path)
path.gsub(%r{/}, File::SEPARATOR)
end
end
end

View File

@ -1,9 +1,13 @@
module Compass
module Commands
class Base
attr_accessor :working_directory, :options
def initialize(working_directory, options)
self.working_directory = working_directory
include Actions
attr_accessor :working_path, :options
def initialize(working_path, options)
self.working_path = working_path
self.options = options
end
@ -13,66 +17,6 @@ module Compass
protected
def relativize(path)
if path.index(working_directory+File::SEPARATOR) == 0
path[(working_directory+File::SEPARATOR).length..-1]
else
path
end
end
# create a directory and all the directories necessary to reach it.
def directory(dir, options = nil)
options ||= self.options
if File.exists?(dir) && File.directory?(dir) && options[:force]
print_action :exists, basename(dir) + File::SEPARATOR
elsif File.exists?(dir) && File.directory?(dir)
msg = "Directory #{basename(dir)} already exists. Run with --force to force creation."
raise ::Compass::Exec::DirectoryExistsError.new(msg)
elsif File.exists?(dir)
msg = "#{basename(dir)} already exists and is not a directory."
raise ::Compass::Exec::ExecError.new(msg)
else
print_action :directory, basename(dir) + File::SEPARATOR
FileUtils.mkdir_p(dir) unless options[:dry_run]
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.
def template(from, to, options)
from = File.join(templates_directory, separate(from))
if File.exists?(to) && !options[:force]
#TODO: Detect differences & provide an overwrite prompt
msg = "#{basename(to)} already exists."
raise ::Compass::Exec::ExecError.new(msg)
elsif File.exists?(to)
print_action :remove, basename(to)
FileUtils.rm to unless options[:dry_run]
end
print_action :create, basename(to)
FileUtils.cp from, to unless options[:dry_run]
end
def write_file(file_name, contents)
if File.exists?(file_name) && !options[:force]
msg = "File #{basename(file_name)} already exists. Run with --force to force creation."
raise ::Compass::Exec::ExecError.new(msg)
end
if File.exists?(file_name)
print_action :overwrite, basename(file_name)
else
print_action :create, basename(file_name)
end
output = open(file_name,'w')
output.write(contents)
output.close
end
# returns the path to the templates directory and caches it
def templates_directory
@templates_directory ||= framework.templates_directory
@ -82,25 +26,6 @@ module Compass
Compass::Frameworks[options[:framework]]
end
# Write paths like we're on unix and then fix it
def separate(path)
path.gsub(%r{/}, File::SEPARATOR)
end
def basename(file)
if file.length > working_directory.length
relativize(file)
else
File.basename(file)
end
end
ACTIONS = [:directory, :exists, :remove, :create, :overwrite]
MAX_ACTION_LENGTH = ACTIONS.inject(0){|memo, a| [memo, a.to_s.length].max}
def print_action(action, extra)
puts "#{' ' * (MAX_ACTION_LENGTH - action.to_s.length)}#{action} #{extra}" if !options[:quiet] || options[:dry_run]
end
end
end
end

View File

@ -11,8 +11,8 @@ module Compass
attr_accessor :installer
def initialize(working_directory, options)
super(working_directory, options)
def initialize(working_path, options)
super(working_path, options)
installer_args = [project_template_directory, project_directory, self.options]
@installer = case options[:project_type]
when :stand_alone
@ -28,7 +28,7 @@ module Compass
def perform
installer.init
installer.run(:skip_finalization => true)
UpdateProject.new(working_directory, options).perform if installer.compilation_required?
UpdateProject.new(working_path, options).perform if installer.compilation_required?
installer.finalize(:create => true)
end
@ -36,10 +36,6 @@ module Compass
File.join(framework.templates_directory, "project")
end
def skip_project_directory_assertion?
true
end
end
end
end

View File

@ -2,7 +2,7 @@ module Compass
module Commands
class ListFrameworks
attr_accessor :options
def initialize(working_directory, options)
def initialize(working_path, options)
self.options = options
end

View File

@ -2,7 +2,7 @@ module Compass
module Commands
class PrintVersion
attr_accessor :options
def initialize(working_directory, options)
def initialize(working_path, options)
self.options = options
end

View File

@ -9,31 +9,14 @@ module Compass
class ProjectBase < Base
attr_accessor :project_directory, :project_name, :options
def initialize(working_directory, options = {})
super(working_directory, options)
self.project_name = determine_project_name(working_directory, options)
Compass.configuration.project_path = determine_project_directory(working_directory, options)
assert_project_directory_exists!
def initialize(working_path, options = {})
super(working_path, options)
self.project_name = determine_project_name(working_path, options)
Compass.configuration.project_path = determine_project_directory(working_path, options)
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
@ -45,9 +28,11 @@ module Compass
def project_css_subdirectory
Compass.configuration.css_dir
end
def project_src_subdirectory
Compass.configuration.sass_dir
end
# Read the configuration file for this project
def read_project_configuration
if File.exists?(projectize('config.rb'))
@ -59,36 +44,29 @@ module Compass
private
def determine_project_name(working_directory, options)
def determine_project_name(working_path, options)
if options[:project_name]
File.basename(strip_trailing_separator(options[:project_name]))
else
File.basename(working_directory)
File.basename(working_path)
end
end
def determine_project_directory(working_directory, options)
def determine_project_directory(working_path, options)
if options[:project_name]
if absolute_path?(options[:project_name])
options[:project_name]
else
File.join(working_directory, options[:project_name])
File.join(working_path, options[:project_name])
end
else
working_directory
working_path
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]
def absolute_path?(path)
# This is only going to work on unix, gonna need a better implementation.
path.index(File::SEPARATOR) == 0
end
def strip_trailing_separator(path)

View File

@ -4,45 +4,26 @@ module Compass
module Commands
class UpdateProject < ProjectBase
Base::ACTIONS << :compile
Base::ACTIONS << :overwrite
def initialize(working_path, options)
super
assert_project_directory_exists!
end
def perform
read_project_configuration
default_options = { :style => default_output_style }
compilation_options = default_options.merge(options).merge(:load_paths => sass_load_paths)
Dir.glob(separate("#{project_src_directory}/**/[^_]*.sass")).each do |sass_file|
stylesheet_name = sass_file[("#{project_src_directory}/".length)..-6]
compile "#{project_src_subdirectory}/#{stylesheet_name}.sass", "#{project_css_subdirectory}/#{stylesheet_name}.css", options
sass_filename = projectize("#{project_src_subdirectory}/#{stylesheet_name}.sass")
css_filename = projectize("#{project_css_subdirectory}/#{stylesheet_name}.css")
compile sass_filename, css_filename, compilation_options
end
end
# Compile one Sass file
def compile(sass_filename, css_filename, options)
sass_filename = projectize(sass_filename)
css_filename = projectize(css_filename)
if !File.directory?(File.dirname(css_filename))
directory basename(File.dirname(css_filename)), options.merge(:force => true) unless options[:dry_run]
end
print_action :compile, basename(sass_filename)
if File.exists?(css_filename)
print_action :overwrite, basename(css_filename)
else
print_action :create, basename(css_filename)
end
unless options[:dry_run]
engine = ::Sass::Engine.new(open(sass_filename).read,
:filename => sass_filename,
:line_comments => options[:environment] == :development,
:style => output_style,
:css_filename => css_filename,
:load_paths => sass_load_paths)
output = open(css_filename,'w')
output.write(engine.render)
output.close
end
end
def output_style
@output_style ||= options[:style] || if options[:environment] == :development
def default_output_style
if options[:environment] == :development
:expanded
else
:compact
@ -51,7 +32,7 @@ module Compass
# where to load sass files from
def sass_load_paths
@sass_load_paths ||= [project_src_directory] + Compass::Frameworks::ALL.map{|f| f.stylesheets_directory}
[project_src_directory] + Compass::Frameworks::ALL.map{|f| f.stylesheets_directory}
end
# The subdirectory where the sass source is kept.
@ -69,6 +50,14 @@ module Compass
@project_src_directory ||= separate("#{project_directory}/#{project_src_subdirectory}")
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)
raise ::Compass::Exec::ExecError.new("#{project_directory} does not exist.")
end
end
end
end
end

View File

@ -8,7 +8,9 @@ require File.join(File.dirname(__FILE__), 'update_project')
module Compass
module Commands
class WatchProject < UpdateProject
attr_accessor :last_update_time
def perform
puts ">>> Compiling all stylesheets."
super
@ -34,9 +36,11 @@ module Compass
end
end
end
def most_recent_update_time
Dir.glob(separate("#{project_src_directory}/**/*.sass")).map {|sass_file| File.stat(sass_file).mtime}.max
end
def should_update?
t = most_recent_update_time
if t > last_update_time

View File

@ -3,7 +3,7 @@ module Compass
class Base
include Compass::Actions
include Actions
attr_accessor :template_path, :target_path, :working_path
attr_accessor :options
@ -109,11 +109,6 @@ module Compass
File.join(template_path, separate(path))
end
# Write paths like we're on unix and then fix it
def separate(path)
path.gsub(%r{/}, File::SEPARATOR)
end
def stylesheet_links
html = "<head>\n"
manifest.each_stylesheet do |stylesheet|

View File

@ -1,7 +1,7 @@
module Compass
class Logger
DEFAULT_ACTIONS = [:directory, :exists, :remove, :create, :overwrite]
DEFAULT_ACTIONS = [:directory, :exists, :remove, :create, :overwrite, :compile]
attr_accessor :actions, :options