Class: Slinky::Compilers

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

Constant Summary collapse

DEPENDENCY_NOT_MET =
"Missing dependency %s (version %s), which is\n" +
"necessary to compile %s files\n\n" +
"Running the following should rectify this problem:\n" +
"  $ gem install --version '%s' %s"

Class Method Summary collapse

Class Method Details

.cfile_for_file(path) ⇒ Object

Like cfile_for_request, but for the actual file to be compiled (so script.coffee, not script.js).



96
97
98
99
100
101
102
103
104
# File 'lib/slinky/compilers.rb', line 96

def cfile_for_file path
  _, file, ext = path.match(EXTENSION_REGEX).to_a

  if ext && ext != "" && compiler = @compilers_by_input[ext]
    get_cfile(path, compiler, ext, compiler[:outputs].first)
  else
    nil
  end
end

.cfile_for_request(path) ⇒ Object

Produces a CompiledFile for an input file if the file needs to be compiled, or nil otherwise. Note that path is the path of the compiled file, so script.js not script.coffee.



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
# File 'lib/slinky/compilers.rb', line 60

def cfile_for_request path
  _, file, extension = path.match(EXTENSION_REGEX).to_a

  compilers = @compilers_by_ext

  # if there's a file extension and we have a compiler that
  # outputs that kind of file, look for an input with the same
  # name and an extension in our list
  if extension && extension != "" && compilers[extension]
    files_by_ext = {}
    # find possible source files
    Dir.glob("#{file}.*").each do |f|
      _, _, ext = f.match(EXTENSION_REGEX).to_a
      files_by_ext[ext] = f
    end

    cfile = nil
    # find a compiler that outputs the request kind of file and
    # which has an input file type that exists on the file system
    compilers[extension].each do |c|
      c[:inputs].each do |i|
        if files_by_ext[i]
          cfile = get_cfile(files_by_ext[i], c, ext, extension)
          break
        end
      end
      break if cfile
    end
    cfile
  else
    nil
  end
end

.get_cfile(source, compiler, input_ext, output_ext) ⇒ Object



30
31
32
33
34
# File 'lib/slinky/compilers.rb', line 30

def get_cfile source, compiler, input_ext, output_ext
  if has_dependencies compiler, input_ext
    CompiledFile.new source, compiler[:klass], output_ext
  end
end

.has_dependencies(compiler, ext) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/slinky/compilers.rb', line 36

def has_dependencies compiler, ext
  ds = (compiler[:dependencies] || []).all? {|d|
    if @checked_dependencies.include?(d)
      true
    else
      begin
        gem(d[0], d[1])
        @checked_dependencies.add(d)
        true
      rescue Gem::LoadError
        $stderr.puts((DEPENDENCY_NOT_MET % [d[0], d[1], ext, d[1], d[0]]).foreground(:red))
        false
      end
    end
  }
  (compiler[:requires] || []).each {|d|
    require d
  }
  ds
end

.register_compiler(klass, options) ⇒ Object



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

def register_compiler klass, options
  options[:klass] = klass
  @compilers << options
  options[:outputs].each do |output|
    @compilers_by_ext[output] ||= []
    @compilers_by_ext[output] << options
  end
  options[:inputs].each do |input|
    @compilers_by_input[input] = options
  end
end