Factored out an Actions module for doing the filesystem actions like copying, making directories, etc.

This commit is contained in:
Chris Eppstein 2009-02-04 08:10:45 -08:00
parent 98a2f0538b
commit dbc262fbe1
5 changed files with 89 additions and 83 deletions

72
lib/compass/actions.rb Normal file
View File

@ -0,0 +1,72 @@
module Compass
module Actions
attr_writer :logger
def logger
@logger ||= Logger.new
end
# copy/process a template in the compass template directory to the project directory.
def copy(from, to, options = nil)
options ||= self.options if self.respond_to?(:options)
if File.exists?(to) && !options[:force]
#TODO: Detect differences & provide an overwrite prompt
msg = "#{basename(to)} already exists."
raise InstallationError.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
# create a directory and all the directories necessary to reach it.
def directory(dir, options = nil)
options ||= self.options if self.respond_to?(:options)
if File.exists?(dir) && File.directory?(dir)
logger.record :exists, basename(dir)
elsif File.exists?(dir)
msg = "#{basename(dir)} already exists and is not a directory."
raise InstallationError.new(msg)
else
logger.record :directory, basename(dir)
FileUtils.mkdir_p(dir) unless options[:dry_run]
end
end
def write_file(file_name, contents, options = nil)
options ||= self.options if self.respond_to?(:options)
if File.exists?(file_name) && !options[:force]
msg = "File #{basename(file_name)} already exists. Run with --force to force creation."
raise InstallationError.new(msg)
end
if File.exists?(file_name)
logger.record :overwrite, basename(file_name)
else
logger.record :create, basename(file_name)
end
open(file_name,'w') do |file|
file.write(contents)
end
end
def basename(file)
relativize(file) {|f| File.basename(file)}
end
def relativize(path)
if path.index(working_path+File::SEPARATOR) == 0
path[(working_path+File::SEPARATOR).length..-1]
elsif block_given?
yield path
else
path
end
end
end
end

View File

@ -3,6 +3,7 @@ require 'rubygems'
require 'haml' require 'haml'
require File.join(Compass.lib_directory, 'compass', 'logger') require File.join(Compass.lib_directory, 'compass', 'logger')
require File.join(Compass.lib_directory, 'compass', 'configuration') require File.join(Compass.lib_directory, 'compass', 'configuration')
require File.join(Compass.lib_directory, 'compass', 'actions')
module Compass module Compass
module Exec module Exec

View File

@ -2,10 +2,12 @@ module Compass
module Installers module Installers
class Base class Base
include Compass::Actions
attr_accessor :template_path, :target_path, :working_path attr_accessor :template_path, :target_path, :working_path
attr_accessor :options attr_accessor :options
attr_accessor :manifest attr_accessor :manifest
attr_accessor :logger
attr_accessor :css_dir, :sass_dir, :images_dir, :javascripts_dir attr_accessor :css_dir, :sass_dir, :images_dir, :javascripts_dir
def initialize(template_path, target_path, options = {}) def initialize(template_path, target_path, options = {})
@ -14,7 +16,7 @@ module Compass
@working_path = Dir.getwd @working_path = Dir.getwd
@options = options @options = options
@manifest = Manifest.new(manifest_file) @manifest = Manifest.new(manifest_file)
configure_option_with_default :logger self.logger = options[:logger]
configure configure
end end
@ -80,23 +82,19 @@ module Compass
end end
def install_stylesheet(from, to, options) def install_stylesheet(from, to, options)
copy from, "#{sass_dir}/#{to}" copy templatize(from), targetize("#{sass_dir}/#{to}")
end end
def install_image(from, to, options) def install_image(from, to, options)
copy from, "#{images_dir}/#{to}" copy templatize(from), targetize("#{images_dir}/#{to}")
end end
def install_script(from, to, options) def install_script(from, to, options)
copy from, "#{javascripts_dir}/#{to}" copy templatize(from), targetize("#{javascripts_dir}/#{to}")
end end
def install_file(from, to, options) def install_file(from, to, options)
copy from, to copy templatize(from), targetize(to)
end
def default_logger
Compass::Logger.new
end end
# returns an absolute path given a path relative to the current installation target. # returns an absolute path given a path relative to the current installation target.
@ -116,71 +114,6 @@ module Compass
path.gsub(%r{/}, File::SEPARATOR) path.gsub(%r{/}, File::SEPARATOR)
end end
# copy/process a template in the compass template directory to the project directory.
def copy(from, to, options = nil)
options ||= self.options
from = templatize(from)
to = targetize(to)
if File.exists?(to) && !options[:force]
#TODO: Detect differences & provide an overwrite prompt
msg = "#{basename(to)} already exists."
raise InstallationError.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
# create a directory and all the directories necessary to reach it.
def directory(dir, options = nil)
options ||= self.options
dir = targetize(dir)
if File.exists?(dir) && File.directory?(dir)
logger.record :exists, basename(dir)
elsif File.exists?(dir)
msg = "#{basename(dir)} already exists and is not a directory."
raise InstallationError.new(msg)
else
logger.record :directory, basename(dir)
FileUtils.mkdir_p(dir) unless options[:dry_run]
end
end
def write_file(file_name, contents, options = nil)
options ||= self.options
file_name = targetize(file_name)
if File.exists?(file_name) && !options[:force]
msg = "File #{basename(file_name)} already exists. Run with --force to force creation."
raise InstallationError.new(msg)
end
if File.exists?(file_name)
logger.record :overwrite, basename(file_name)
else
logger.record :create, basename(file_name)
end
open(file_name,'w') do |file|
file.write(contents)
end
end
def basename(file)
relativize(file) {|f| File.basename(file)}
end
def relativize(path)
if path.index(working_path+File::SEPARATOR) == 0
path[(working_path+File::SEPARATOR).length..-1]
elsif block_given?
yield path
else
path
end
end
def stylesheet_links def stylesheet_links
html = "<head>\n" html = "<head>\n"
manifest.each_stylesheet do |stylesheet| manifest.each_stylesheet do |stylesheet|

View File

@ -15,9 +15,9 @@ module Compass
def init def init
set_sass_dir unless sass_dir set_sass_dir unless sass_dir
set_css_dir unless css_dir set_css_dir unless css_dir
directory css_dir directory targetize(css_dir)
directory sass_dir directory targetize(sass_dir)
write_file 'config/initializers/compass.rb', initializer_contents write_file targetize('config/initializers/compass.rb'), initializer_contents
end end
def prepare def prepare

View File

@ -13,14 +13,14 @@ module Compass
end end
def init def init
directory "" directory targetize("")
directory css_dir directory targetize(css_dir)
directory sass_dir directory targetize(sass_dir)
end end
def prepare def prepare
directory images_dir if manifest.has_image? directory targetize(images_dir) if manifest.has_image?
directory javascripts_dir if manifest.has_javascript? directory targetize(javascripts_dir) if manifest.has_javascript?
end end
def default_css_dir def default_css_dir