class Guard::MtHaml

Public Class Methods

new(options = {}) click to toggle source

Initializer

Calls superclass method
# File lib/guard/mthaml.rb, line 12
def initialize(options = {})
    options = {
        input: "views/src",
        output: "views",
        environment: "php",
        extension: nil,
        notifications: true,
        compress_output: false,
        static_files: false,
        run_at_start: true
    }.merge(options)

    # Define extension if environment is nil
    if options[:extension].nil?
        options[:extension] = if options[:static_files] then "html" else options[:environment] end
    end

    super(options)

    if options[:input]
        watchers << ::Guard::Watcher.new(%r{^#{options[:input]}/(.+\.haml)$})
    end
end

Public Instance Methods

reload() click to toggle source

On Guard reload

# File lib/guard/mthaml.rb, line 53
def reload
    run_all
end
run_all() click to toggle source

Run all

# File lib/guard/mthaml.rb, line 60
def run_all
    run_on_changes Watcher.match_files(self, Dir.glob(File.join("**", "*.*")).reject { |f| f[%r{(\.php)$}] })
end
run_on_changes(paths) click to toggle source

Run on changes to watched files

@param {Array} paths

Paths of changed files
# File lib/guard/mthaml.rb, line 70
def run_on_changes(paths)
    paths.each do |file|

        file = Pathname.new(file)
        file_dir = file.dirname.realpath
        input_dir = Pathname.new(options[:input]).realpath
        input_file = file.realpath

        # Simple check to see if we need to create any directories in the output
        if file_dir == input_dir
            # File looks like it's in the base directory
            output_dir = Pathname.new(options[:output]).realpath
        else
            # Looks like we need to create a directory or two
            output_dir = Pathname.new(options[:output]).realpath + file_dir.to_s.gsub(input_dir.to_s, "")[1..-1]
        end

        # Make directories if they don't already exist
        make_directory(output_dir)

        # Initiate compiler
        compile_haml(input_file, output_dir)
    end
end
run_on_removals(paths) click to toggle source

Called when a watched file is removed

@param {Array} paths

Paths of changed files
# File lib/guard/mthaml.rb, line 101
def run_on_removals(paths)
end
start() click to toggle source

Run at start

# File lib/guard/mthaml.rb, line 39
def start
    run_all if options[:run_at_start]
end
stop() click to toggle source

Stop running

# File lib/guard/mthaml.rb, line 46
def stop
    true
end

Private Instance Methods

color(message, color) click to toggle source

Set message color

@param {String} message

Text to color

@param {String} color

Color code
# File lib/guard/mthaml.rb, line 155
def color(message, color)
    if ::Guard::UI.send(:color_enabled?)
        "\e[0#{color}m#{message}\e[0m"
    else
        message
    end
end
compile_haml(input, output) click to toggle source

Wrapper to run through PHP Haml compiler, which creates new files itself

@param {String} input

Input file to pass to compiler

@param {String} output

Output directory to pass to compiler
# File lib/guard/mthaml.rb, line 114
def compile_haml(input, output)

    command = [
        "php #{File.dirname(__FILE__)}/mthaml/compiler/MtHaml.php",
        "--input #{input}",
        "--output #{output}",
        "--environment #{options[:environment]}",
        "--extension #{options[:extension]}",
        "--static_files #{options[:static_files]}",
        "--compress_output #{options[:compress_output]}",
    ].join " "

    begin
        throw :task_has_failed unless system command
        ::Guard::UI.info(color("write #{File.basename(input, ".*")}.#{options[:extension]}", ";32")) if options[:notifications]
    rescue StandardError => error
        ::Guard::UI.error(color("error #{File.basename(input, ".*")}.#{options[:extension]} : #{error}", ";31")) if options[:notifications]
    end
end
make_directory(dir) click to toggle source

Create dir if it doesn't already exist, used to make sure the

output file structure matches the input structure

@param {String} dir

Directory to create
# File lib/guard/mthaml.rb, line 141
def make_directory(dir)
    unless File.directory? dir
        FileUtils.mkdir_p(dir)
    end
end