diff --git a/lib/compass/actions.rb b/lib/compass/actions.rb index 9cf75914..511fe34b 100644 --- a/lib/compass/actions.rb +++ b/lib/compass/actions.rb @@ -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 \ No newline at end of file diff --git a/lib/compass/commands/base.rb b/lib/compass/commands/base.rb index 259a50a9..9e4502c5 100644 --- a/lib/compass/commands/base.rb +++ b/lib/compass/commands/base.rb @@ -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 \ No newline at end of file diff --git a/lib/compass/commands/create_project.rb b/lib/compass/commands/create_project.rb index 065abc8d..3382f6ac 100644 --- a/lib/compass/commands/create_project.rb +++ b/lib/compass/commands/create_project.rb @@ -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 \ No newline at end of file diff --git a/lib/compass/commands/list_frameworks.rb b/lib/compass/commands/list_frameworks.rb index dd8bbbe5..fe7f6088 100644 --- a/lib/compass/commands/list_frameworks.rb +++ b/lib/compass/commands/list_frameworks.rb @@ -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 diff --git a/lib/compass/commands/print_version.rb b/lib/compass/commands/print_version.rb index ef4037a0..244616ba 100644 --- a/lib/compass/commands/print_version.rb +++ b/lib/compass/commands/print_version.rb @@ -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 diff --git a/lib/compass/commands/project_base.rb b/lib/compass/commands/project_base.rb index 11f096f7..96350820 100644 --- a/lib/compass/commands/project_base.rb +++ b/lib/compass/commands/project_base.rb @@ -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')) @@ -58,37 +43,30 @@ module Compass end 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) diff --git a/lib/compass/commands/update_project.rb b/lib/compass/commands/update_project.rb index ad7dc4af..ef227d6c 100644 --- a/lib/compass/commands/update_project.rb +++ b/lib/compass/commands/update_project.rb @@ -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 - 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 + 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 - 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 \ No newline at end of file diff --git a/lib/compass/commands/watch_project.rb b/lib/compass/commands/watch_project.rb index a787a808..078666b2 100644 --- a/lib/compass/commands/watch_project.rb +++ b/lib/compass/commands/watch_project.rb @@ -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 diff --git a/lib/compass/installers/base.rb b/lib/compass/installers/base.rb index 9148790c..328d74d6 100644 --- a/lib/compass/installers/base.rb +++ b/lib/compass/installers/base.rb @@ -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 = "\n" manifest.each_stylesheet do |stylesheet| diff --git a/lib/compass/logger.rb b/lib/compass/logger.rb index eb0a184e..152ed72e 100644 --- a/lib/compass/logger.rb +++ b/lib/compass/logger.rb @@ -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