From ab71230b9459c809b94e73cc646ff3cfeb516a69 Mon Sep 17 00:00:00 2001 From: Chris Eppstein Date: Wed, 8 Dec 2010 12:14:45 -0800 Subject: [PATCH] Add a --time option to the compile and watch commands. --- doc-src/content/CHANGELOG.markdown | 7 +++++ lib/compass/actions.rb | 7 +++-- lib/compass/commands/update_project.rb | 4 +++ lib/compass/compiler.rb | 42 +++++++++++++++++++------- 4 files changed, 46 insertions(+), 14 deletions(-) diff --git a/doc-src/content/CHANGELOG.markdown b/doc-src/content/CHANGELOG.markdown index f5efa8b7..8ba1ea10 100644 --- a/doc-src/content/CHANGELOG.markdown +++ b/doc-src/content/CHANGELOG.markdown @@ -14,6 +14,13 @@ The Documentation for the [latest stable release](http://compass-style.org/docs/ The Documentation for the [latest preview release](http://beta.compass-style.org/) +0.11.alpha.4 (12/08/2010) +------------------------- + +* Add a `--time` option to the compile and watch commands. This will print out + the time spent compiling each sass file and a total at the end. + + 0.11.alpha.3 (12/05/2010) ------------------------- diff --git a/lib/compass/actions.rb b/lib/compass/actions.rb index 93d7a8d5..fe4df6e1 100644 --- a/lib/compass/actions.rb +++ b/lib/compass/actions.rb @@ -33,19 +33,20 @@ module Compass options ||= self.options if self.respond_to?(:options) skip_write = options[:dry_run] contents = process_erb(contents, options[:erb]) if options[:erb] + extra = options[:extra] || "" if File.exists?(file_name) existing_contents = IO.read(file_name) if existing_contents == contents - logger.record :identical, basename(file_name) + logger.record :identical, basename(file_name), extra skip_write = true elsif options[:force] - logger.record :overwrite, basename(file_name) + logger.record :overwrite, basename(file_name), extra else msg = "File #{basename(file_name)} already exists. Run with --force to force overwrite." raise Compass::FilesystemConflict.new(msg) end else - logger.record :create, basename(file_name) + logger.record :create, basename(file_name), extra end if skip_write FileUtils.touch file_name unless options[:dry_run] diff --git a/lib/compass/commands/update_project.rb b/lib/compass/commands/update_project.rb index 2e961c61..7ce101f2 100644 --- a/lib/compass/commands/update_project.rb +++ b/lib/compass/commands/update_project.rb @@ -14,6 +14,9 @@ module Compass Options: }.split("\n").map{|l| l.gsub(/^ */,'')}.join("\n") + opts.on("--time", "Display compilation times.") do + self.options[:time] = true + end super end end @@ -51,6 +54,7 @@ module Compass :sass_files => explicit_sass_files, :dry_run => options[:dry_run]) compiler_opts[:quiet] = options[:quiet] if options[:quiet] + compiler_opts[:time] = options[:time] if options[:time] compiler_opts.merge!(additional_options) Compass::Compiler.new(working_path, Compass.configuration.sass_path, diff --git a/lib/compass/compiler.rb b/lib/compass/compiler.rb index f4897735..b259dfce 100644 --- a/lib/compass/compiler.rb +++ b/lib/compass/compiler.rb @@ -76,29 +76,49 @@ module Compass target_directories.each {|dir| directory dir} # Compile each sass file. - sass_files.zip(css_files).each do |sass_filename, css_filename| - begin - compile_if_required sass_filename, css_filename - rescue Sass::SyntaxError => e - handle_exception(sass_filename, css_filename, e) + result = timed do + sass_files.zip(css_files).each do |sass_filename, css_filename| + begin + compile_if_required sass_filename, css_filename + rescue Sass::SyntaxError => e + handle_exception(sass_filename, css_filename, e) + end end end + if options[:time] + puts "Compilation took #{(result.__duration * 1000).round / 1000.0}s" + end end def compile_if_required(sass_filename, css_filename) if should_compile?(sass_filename, css_filename) - compile sass_filename, css_filename + compile sass_filename, css_filename, :time => options[:time] else logger.record :unchanged, basename(sass_filename) unless options[:quiet] end end - # Compile one Sass file - def compile(sass_filename, css_filename) - css_content = logger.red do - engine(sass_filename, css_filename).render + def timed + start_time = Time.now + res = yield + end_time = Time.now + res.instance_variable_set("@__duration", end_time - start_time) + def res.__duration + @__duration end - write_file(css_filename, css_content, options.merge(:force => true)) + res + end + + # Compile one Sass file + def compile(sass_filename, css_filename, additional_options = {}) + start_time = end_time = nil + css_content = logger.red do + timed do + engine(sass_filename, css_filename).render + end + end + duration = additional_options[:time] ? "(#{(css_content.__duration * 1000).round / 1000.0}s)" : "" + write_file(css_filename, css_content, options.merge(:force => true, :extra => duration)) end def should_compile?(sass_filename, css_filename)