Module: Compass::Actions
- Included in:
- Commands::Base, Commands::GenerateGridBackground, Compiler, GridBuilder, Installers::Base
- Defined in:
- lib/compass/actions.rb
Instance Attribute Summary (collapse)
Instance Method Summary (collapse)
- - (Object) basename(file)
-
- (Object) copy(from, to, options = nil, binary = false)
copy/process a template in the compass template directory to the project directory.
-
- (Object) directory(dir, options = nil)
create a directory and all the directories necessary to reach it.
- - (Object) log_action(action, file, options)
- - (Object) process_erb(contents, ctx = nil)
- - (Object) relativize(path)
- - (Object) remove(file_name)
-
- (Object) separate(path)
Write paths like we're on unix and then fix it.
-
- (Object) strip_trailing_separator(path)
Removes the trailing separator, if any, from a path.
-
- (Object) write_file(file_name, contents, options = nil, binary = false)
Write a file given the file contents as a string.
Instance Attribute Details
- (Object) logger
6 7 8 |
# File 'lib/compass/actions.rb', line 6 def logger @logger ||= ::Compass::Logger.new end |
Instance Method Details
- (Object) basename(file)
78 79 80 |
# File 'lib/compass/actions.rb', line 78 def basename(file) relativize(file) {|f| File.basename(file)} end |
- (Object) copy(from, to, options = nil, binary = false)
copy/process a template in the compass template directory to the project directory.
11 12 13 14 15 |
# File 'lib/compass/actions.rb', line 11 def copy(from, to, = nil, binary = false) ||= self. if self.respond_to?(:options) contents = File.new(from).read write_file to, contents, , binary end |
- (Object) directory(dir, options = nil)
create a directory and all the directories necessary to reach it.
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/compass/actions.rb', line 18 def directory(dir, = nil) ||= self. if self.respond_to?(:options) ||= {} if File.exists?(dir) && File.directory?(dir) # do nothing elsif File.exists?(dir) msg = "#{basename(dir)} already exists and is not a directory." raise Compass::FilesystemConflict.new(msg) else log_action :directory, separate("#{basename(dir)}/"), FileUtils.mkdir_p(dir) unless [:dry_run] end end |
- (Object) log_action(action, file, options)
102 103 104 105 106 107 108 109 |
# File 'lib/compass/actions.rb', line 102 def log_action(action, file, ) quiet = !![:quiet] quiet = false if [:loud] && [:loud] == true quiet = false if [:loud] && [:loud].is_a?(Array) && [:loud].include?(action) unless quiet logger.record(action, file, [:extra].to_s) end end |
- (Object) process_erb(contents, ctx = nil)
62 63 64 65 |
# File 'lib/compass/actions.rb', line 62 def process_erb(contents, ctx = nil) ctx = Object.new.instance_eval("binding") unless ctx.is_a? Binding ERB.new(contents).result(ctx) end |
- (Object) relativize(path)
82 83 84 85 86 87 88 89 90 |
# File 'lib/compass/actions.rb', line 82 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 |
- (Object) remove(file_name)
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/compass/actions.rb', line 67 def remove(file_name) file_name ||= '' if File.directory?(file_name) FileUtils.rm_rf file_name log_action :remove, basename(file_name)+"/", elsif File.exists?(file_name) File.unlink file_name log_action :remove, basename(file_name), end end |
- (Object) separate(path)
Write paths like we're on unix and then fix it
93 94 95 |
# File 'lib/compass/actions.rb', line 93 def separate(path) path.gsub(%r{/}, File::SEPARATOR) end |
- (Object) strip_trailing_separator(path)
Removes the trailing separator, if any, from a path.
98 99 100 |
# File 'lib/compass/actions.rb', line 98 def strip_trailing_separator(path) (path[-1..-1] == File::SEPARATOR) ? path[0..-2] : path end |
- (Object) write_file(file_name, contents, options = nil, binary = false)
Write a file given the file contents as a string
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/compass/actions.rb', line 33 def write_file(file_name, contents, = nil, binary = false) ||= self. if self.respond_to?(:options) skip_write = [:dry_run] contents = process_erb(contents, [:erb]) if [:erb] if File.exists?(file_name) existing_contents = IO.read(file_name) if existing_contents == contents log_action :identical, basename(file_name), skip_write = true elsif [:force] log_action :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 log_action :create, basename(file_name), end if skip_write FileUtils.touch file_name unless [:dry_run] else mode = "w" mode << "b" if binary open(file_name, mode) do |file| file.write(contents) end end end |