Detect identical files when writing files during install, compile, etc. Also centralize the actions to use the write_file action as much as possible.
This commit is contained in:
parent
0dfd60671f
commit
10e6808648
@ -10,18 +10,8 @@ module Compass
|
|||||||
# 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 copy(from, to, options = nil)
|
def copy(from, to, options = nil)
|
||||||
options ||= self.options if self.respond_to?(:options)
|
options ||= self.options if self.respond_to?(:options)
|
||||||
if File.exists?(to) && !options[:force]
|
contents = File.new(from).read
|
||||||
#TODO: Detect differences & provide an overwrite prompt
|
write_file to, contents, options
|
||||||
msg = "#{basename(to)} already exists."
|
|
||||||
raise Compass::FilesystemConflict.new(msg)
|
|
||||||
elsif File.exists?(to)
|
|
||||||
logger.record :overwrite, basename(to)
|
|
||||||
FileUtils.rm to unless options[:dry_run]
|
|
||||||
FileUtils.cp from, to unless options[:dry_run]
|
|
||||||
else
|
|
||||||
logger.record :create, basename(to)
|
|
||||||
FileUtils.cp from, to unless options[:dry_run]
|
|
||||||
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.
|
||||||
@ -41,28 +31,29 @@ module Compass
|
|||||||
# Write a file given the file contents as a string
|
# Write a file given the file contents as a string
|
||||||
def write_file(file_name, contents, options = nil)
|
def write_file(file_name, contents, options = nil)
|
||||||
options ||= self.options if self.respond_to?(:options)
|
options ||= self.options if self.respond_to?(:options)
|
||||||
if File.exists?(file_name) && !options[:force]
|
skip_write = options[:dry_run]
|
||||||
msg = "File #{basename(file_name)} already exists. Run with --force to force creation."
|
|
||||||
raise Compass::FilesystemConflict.new(msg)
|
|
||||||
end
|
|
||||||
if File.exists?(file_name)
|
if File.exists?(file_name)
|
||||||
logger.record :overwrite, basename(file_name)
|
existing_contents = File.new(file_name).read
|
||||||
|
if existing_contents == contents
|
||||||
|
logger.record :identical, basename(file_name)
|
||||||
|
skip_write = true
|
||||||
|
elsif options[:force]
|
||||||
|
logger.record :overwrite, basename(file_name)
|
||||||
|
else
|
||||||
|
msg = "File #{basename(file_name)} already exists. Run with --force to force overwrite."
|
||||||
|
raise Compass::FilesystemConflict.new(msg)
|
||||||
|
end
|
||||||
else
|
else
|
||||||
logger.record :create, basename(file_name)
|
logger.record :create, basename(file_name)
|
||||||
end
|
end
|
||||||
open(file_name,'w') do |file|
|
open(file_name,'w') do |file|
|
||||||
file.write(contents)
|
file.write(contents)
|
||||||
end
|
end unless skip_write
|
||||||
end
|
end
|
||||||
|
|
||||||
# Compile one Sass file
|
# Compile one Sass file
|
||||||
def compile(sass_filename, css_filename, options)
|
def compile(sass_filename, css_filename, options)
|
||||||
logger.record :compile, basename(sass_filename)
|
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,
|
engine = ::Sass::Engine.new(open(sass_filename).read,
|
||||||
:filename => sass_filename,
|
:filename => sass_filename,
|
||||||
:line_comments => options[:environment] == :development,
|
:line_comments => options[:environment] == :development,
|
||||||
@ -70,7 +61,7 @@ module Compass
|
|||||||
:css_filename => css_filename,
|
:css_filename => css_filename,
|
||||||
:load_paths => options[:load_paths])
|
:load_paths => options[:load_paths])
|
||||||
css_content = engine.render
|
css_content = engine.render
|
||||||
open(css_filename,'w') {|output| output.write(css_content)} unless options[:dry_run]
|
write_file(css_filename, css_content, options.merge(:force => true))
|
||||||
end
|
end
|
||||||
|
|
||||||
def basename(file)
|
def basename(file)
|
||||||
|
@ -43,7 +43,7 @@ class CommandLineTest < Test::Unit::TestCase
|
|||||||
Dir.chdir "basic" do
|
Dir.chdir "basic" do
|
||||||
compass
|
compass
|
||||||
assert_action_performed :compile, "src/screen.sass"
|
assert_action_performed :compile, "src/screen.sass"
|
||||||
assert_action_performed :overwrite, "stylesheets/screen.css"
|
assert_action_performed :identical, "stylesheets/screen.css"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user