File: actions.rb

package info (click to toggle)
ruby-compass 1.0.1~dfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,152 kB
  • ctags: 1,795
  • sloc: ruby: 12,901; makefile: 127; perl: 43; xml: 14; sh: 4
file content (116 lines) | stat: -rw-r--r-- 3,573 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
module Compass
  module Actions

    attr_writer :logger

    def logger
      @logger ||= ::Compass::Logger.new
    end

    # copy/process a template in the compass template directory to the project directory.
    def copy(from, to, options = nil, binary = false)
      options ||= self.options if self.respond_to?(:options)
      if binary
        contents = File.new(from,"rb").read
      else
        contents = File.new(from).read
      end  
      write_file to, contents, options, binary
    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)
      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)}/"), options
        FileUtils.mkdir_p(dir)
      end
    end

    # Write a file given the file contents as a string
    def write_file(file_name, contents, options = nil, binary = false)
      options ||= self.options if self.respond_to?(:options)
      skip_write = false
      contents = process_erb(contents, options[:erb]) if options[:erb]
      if File.exists?(file_name)
        existing_contents = IO.read(file_name)
        if existing_contents == contents
          log_action :identical, basename(file_name), options
          skip_write = true
        elsif options[:force]
          log_action :overwrite, basename(file_name), options
        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), options
      end
      if skip_write
        FileUtils.touch file_name
      else
        mode = "w"
        mode << "b" if binary
        open(file_name, mode) do |file|
          file.write(contents)
        end
      end
    end

    def process_erb(contents, ctx = nil)
      ctx = Object.new.instance_eval("binding") unless ctx.is_a? Binding
      ERB.new(contents).result(ctx)
    end

    def remove(file_name)
      file_name ||= ''
      if File.directory?(file_name)
        FileUtils.rm_rf file_name
        log_action :remove, basename(file_name)+"/", options
      elsif File.exists?(file_name)
        File.unlink file_name
        log_action :remove, basename(file_name), options
      end
    end

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

    def relativize(path)
      path = File.expand_path(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

    # Write paths like we're on unix and then fix it
    def separate(path)
      path.gsub(%r{/}, File::SEPARATOR)
    end

    # Removes the trailing separator, if any, from a path.
    def strip_trailing_separator(path)
      (path[-1..-1] == File::SEPARATOR) ? path[0..-2] : path
    end

    def log_action(action, file, options)
      quiet = !!options[:quiet]
      quiet = false if options[:loud] && options[:loud] == true
      quiet = false if options[:loud] && options[:loud].is_a?(Array) && options[:loud].include?(action)
      unless quiet
        logger.record(action, file, options[:extra].to_s)
      end
    end
  end
end