Class: Slinky::CompiledFile

Inherits:
Object
  • Object
show all
Defined in:
lib/slinky/compiled_file.rb

Overview

Stores information about compiled files, including location, source file and last modified time

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, compiler, output_ext) ⇒ CompiledFile

Creates a new CompiledFile, compiling the provided source file with the provided compiler class.



10
11
12
13
14
15
# File 'lib/slinky/compiled_file.rb', line 10

def initialize source, compiler, output_ext
  @source = source
  @compiler = compiler
  @last_compiled = Time.at(0)
  @output_ext = output_ext
end

Instance Attribute Details

#last_compiledObject (readonly)

Returns the value of attribute last_compiled.



6
7
8
# File 'lib/slinky/compiled_file.rb', line 6

def last_compiled
  @last_compiled
end

#output_extObject (readonly)

Returns the value of attribute output_ext.



6
7
8
# File 'lib/slinky/compiled_file.rb', line 6

def output_ext
  @output_ext
end

#output_pathObject

Returns the value of attribute output_path.



5
6
7
# File 'lib/slinky/compiled_file.rb', line 5

def output_path
  @output_path
end

Returns the value of attribute print_name.



5
6
7
# File 'lib/slinky/compiled_file.rb', line 5

def print_name
  @print_name
end

#sourceObject

Returns the value of attribute source.



5
6
7
# File 'lib/slinky/compiled_file.rb', line 5

def source
  @source
end

Instance Method Details

#build(path) ⇒ Object



17
18
19
# File 'lib/slinky/compiled_file.rb', line 17

def build path
  compiler_compile path, EM::DefaultDeferrable
end

#compile(&cb) ⇒ Object

Compiles the source file to a temporary location



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/slinky/compiled_file.rb', line 22

def compile &cb
  path = @output_path || tmp_path
  @last_compiled = Time.now
  if @compiler.respond_to? :compile
    compiler_compile path, cb
  else
    compile_failed Exception.new("invalid compiler")
    cb.call @output_path, nil, nil, "invalid compiler"
    # compiler_command path, cb
  end
end

#compile_failed(e) ⇒ Object



76
77
78
# File 'lib/slinky/compiled_file.rb', line 76

def compile_failed e
  SlinkyError.raise BuildFailedError, "Compilation failed on #{name}: #{e}"
end

#compile_succeededObject



72
73
74
# File 'lib/slinky/compiled_file.rb', line 72

def compile_succeeded
  $stdout.puts "Compiled #{name}".foreground(:green)
end

#compiler_compile(path, cb) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/slinky/compiled_file.rb', line 34

def compiler_compile path, cb
  begin
    out = File.open @source do |f|
      @compiler.compile f.read, @source
    end

    compile_succeeded
    File.open(path, "w+") do |f|
      f.write out
    end
  rescue Exception
    compile_failed $!
    path = nil
  end
  cb.call((path ? path.to_s : nil), nil, nil, $!)
end

#file(&cb) ⇒ Object

Calls the supplied callback with the path of the compiled file, compiling the source file first if necessary.



82
83
84
85
86
87
88
# File 'lib/slinky/compiled_file.rb', line 82

def file &cb
  #if needs_update?
  compile &cb
  #else
  #  cb.call @output_path, nil, nil, nil
  #end
end

#nameObject

EM.system3 command do |stdout, stderr, status|

  if status.exitstatus != 0
    compile_failed stderr.strip
  else
    compile_succeeded
    @output_path = path
  end
  cb.call(@output_path, status, stdout, stderr)
end

end



68
69
70
# File 'lib/slinky/compiled_file.rb', line 68

def name
  @print_name || @source
end

#needs_update?Boolean

Returns whether the source file has changed since it was last compiled.

Returns:

  • (Boolean)


92
93
94
95
# File 'lib/slinky/compiled_file.rb', line 92

def needs_update?
  return true if @compiler.to_s.match "SassCompiler"
  File.new(source).mtime > @last_compiled rescue true
end