Class: Sasquatch::Compiler

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/quatch/compiler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

#logger, #print_line

Constructor Details

#initialize(ext) ⇒ Compiler

Returns a new instance of Compiler.



7
8
9
10
# File 'lib/quatch/compiler.rb', line 7

def initialize ext
  # set the default extension for compiled files
  @ext = ext
end

Instance Attribute Details

#extObject

Returns the value of attribute ext.



5
6
7
# File 'lib/quatch/compiler.rb', line 5

def ext
  @ext
end

Instance Method Details

#compile(files, file) ⇒ Object

compiles files into the specified file



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/quatch/compiler.rb', line 13

def compile(files, file)

  # setup the output buffer with the base file
  output_buffer = File.read(File.path(file))

  # loop through import files and substitute imports
  files.each do |key, import_file|
    output_buffer = import(output_buffer, key, import_file)
  end

  File.open(output_filename(file), 'w') do |file|
    file.write(output_buffer)
  end
end

#import(output_buffer, key, import_file) ⇒ Object

search the output buffer for the key and replace it with the import_file



40
41
42
43
# File 'lib/quatch/compiler.rb', line 40

def import output_buffer, key, import_file

  output_buffer.sub(/\/\* @import ["']#{key}["']; \*\//, "/* Imports #{key} */ \n\n" + File.read(import_file.path()) + "\n")
end

#output_filename(file) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/quatch/compiler.rb', line 28

def output_filename file

  filename = ""
  file.scan(/^(.*?)\./) do |basename|
    filename = basename.first + @ext
  end

  (filename == file ? filename + @ext : filename)

end