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

Instance Attribute Details

#loggerObject



6
7
8
# File 'lib/compass/actions.rb', line 6

def logger
  @logger ||= Logger.new
end

Instance Method Details

#basename(file) ⇒ Object



76
77
78
# File 'lib/compass/actions.rb', line 76

def basename(file)
  relativize(file) {|f| File.basename(file)}
end

#compile(sass_filename, css_filename, options) ⇒ Object

Compile one Sass file



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/compass/actions.rb', line 59

def compile(sass_filename, css_filename, options)
  if options[:force] || Sass::Plugin.exact_stylesheet_needs_update?(css_filename, sass_filename)
    logger.record :compile, basename(sass_filename) unless options[:quiet]
    engine = ::Sass::Engine.new(open(sass_filename).read,
                                :filename => sass_filename,
                                :line_comments => options[:line_comments],
                                :style => options[:style],
                                :css_filename => css_filename,
                                :load_paths => options[:load_paths],
                                :cache_location => options[:cache_location])
    css_content = engine.render
    write_file(css_filename, css_content, options.merge(:force => true))
  else
    logger.record :unchanged, basename(sass_filename) unless options[:quiet]
  end
end

#copy(from, to, options = nil) ⇒ Object

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, options = nil)
  options ||= self.options if self.respond_to?(:options)
  contents = File.new(from).read
  write_file to, contents, options
end

#directory(dir, options = nil) ⇒ Object

create a directory and all the directories necessary to reach it.



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/compass/actions.rb', line 18

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) unless options[:quiet]
  elsif File.exists?(dir)
    msg = "#{basename(dir)} already exists and is not a directory."
    raise Compass::FilesystemConflict.new(msg)
  else
    logger.record :directory, separate("#{basename(dir)}/")
    FileUtils.mkdir_p(dir) unless options[:dry_run]
  end
end

#relativize(path) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/compass/actions.rb', line 80

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

#separate(path) ⇒ Object

Write paths like we’re on unix and then fix it



91
92
93
# File 'lib/compass/actions.rb', line 91

def separate(path)
  path.gsub(%r{/}, File::SEPARATOR)
end

#strip_trailing_separator(path) ⇒ Object

Removes the trailing separator, if any, from a path.



96
97
98
# File 'lib/compass/actions.rb', line 96

def strip_trailing_separator(path)
  (path[-1..-1] == File::SEPARATOR) ? path[0..-2] : path
end

#write_file(file_name, contents, options = nil) ⇒ Object

Write a file given the file contents as a string



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/compass/actions.rb', line 32

def write_file(file_name, contents, options = nil)
  options ||= self.options if self.respond_to?(:options)
  skip_write = options[:dry_run]
  if File.exists?(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
    logger.record :create, basename(file_name)
  end
  if skip_write
    FileUtils.touch file_name
  else
    open(file_name,'w') do |file|
      file.write(contents)
    end
  end
end