Class: Emberprecompile::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/emberprecompile/compiler.rb

Class Method Summary collapse

Class Method Details

.combineObject



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
# File 'lib/emberprecompile/compiler.rb', line 21

def self.combine
    cache_source = SETTINGS["_cache"]
    file_ext = SETTINGS["handlebars_ext"] + ".cache"
    output_dir = SETTINGS["output_dir"]

    if(output_dir.rindex("/") == output_dir.length-1)
        output_dir[output_dir.length - 1] = ""
    end

    if( !File.directory?(SETTINGS["output_dir"]) )
        FileUtils.mkdir SETTINGS["output_dir"]
    end

    fileName = output_dir + "/" + SETTINGS["output_file"]
    output = File.new(fileName, "w")
    
    # Iterating through each file and writing to output file
    Find.find(cache_source) do |file|
        if !FileTest.directory?(file)
            if(file.end_with?(file_ext))
                output.write(File.read(file))
            end
        end
    end
    output.close()
end

.compileObject



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/emberprecompile/compiler.rb', line 7

def self.compile
    source = SETTINGS["handlebars_dir"]
    file_ext = SETTINGS["handlebars_ext"]

    # Iterating through all files under source to identify
    Find.find(source) do |file|
        self.compileFile(file)
    end

    print "\n"
    self.combine
    print "Pre-Compiled handlebars are ready for use....\n-------------------------------------------------------\n"
end

.compileFile(file) ⇒ Object



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
# File 'lib/emberprecompile/compiler.rb', line 48

def self.compileFile(file)
    source = SETTINGS["handlebars_dir"]
    file_ext = SETTINGS["handlebars_ext"]

    if !FileTest.directory?(file)
        if(file.end_with?(file_ext))
            print "Compiling "+file

            # Extracting Template Name from file name
            templateName = file.chomp(file_ext)
            templateName.slice!(source)

            # Creating a temporary file for the handlebar to save output
            tempFile_Name = SETTINGS["_cache"] + templateName.slice(0, templateName.rindex("/"))
            FileUtils.makedirs(tempFile_Name)
            tempFile_Name = SETTINGS["_cache"] + templateName + file_ext + ".cache"
            tempFile = File.new(tempFile_Name, "w")

            if(templateName.index("/") == 0)
                templateName[0] = ""
            end
            # Compiling Handlebar using Barber plugin
            result = Barber::Ember::FilePrecompiler.call(File.read(file))

            # Writing output to temporary file
            tempFile.write('/* '+ templateName + ' Handlebar */')
            tempFile.write('Ember.TEMPLATES["' + templateName + '"] = ' + result + "\n")

            tempFile.close()
            print "\n"
        end
    end
end

.deleteFile(file) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/emberprecompile/compiler.rb', line 82

def self.deleteFile(file)
    source = SETTINGS["handlebars_dir"]
    cache_source = SETTINGS["_cache"]
    folder = false

    if FileTest.directory?(file)
        folder = true
    end

    if(source.rindex("/") == source.length-1)
        source[source.length - 1] = ""
    end

    file.slice!(source)
    cache_file = cache_source + file
    if folder
        FileUtils.remove_dir(cache_file)
    else
        cache_file = cache_file + ".cache"
        if File.exists?(cache_file)
            FileUtils.rm(cache_file)
        end
    end
end