Class: FrontendLoader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFrontendLoader

Returns a new instance of FrontendLoader.



9
10
11
12
13
14
# File 'lib/FrontEndLoader.rb', line 9

def initialize
  version = '0.0.7'
  @gem_path = Gem.path[0]+"/gems/frontendloader-"+version
  @resources_path = Gem.path[0]+"/gems/frontendloader-"+version+"/resources"
  @processors = {}
end

Instance Attribute Details

#resources_pathObject

Returns the value of attribute resources_path.



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

def resources_path
  @resources_path
end

#settingsObject

Returns the value of attribute settings.



7
8
9
# File 'lib/FrontEndLoader.rb', line 7

def settings
  @settings
end

Instance Method Details

#boilerplateObject



29
30
31
# File 'lib/FrontEndLoader.rb', line 29

def boilerplate
  load_settings
end

#clean_ignored_files(files, ignored_files = [], path = "") ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/FrontEndLoader.rb', line 160

def clean_ignored_files(files, ignored_files=[],path="")
  if ignored_files.class != Array then
    ignored_files = []
  end
  cleaned_list = []
  ignored_files.uniq.each {|file|
    if files.include? file then
      files.delete file
    end
  }
  return files
end

#compile(dir = false) ⇒ 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/FrontEndLoader.rb', line 48

def compile(dir=false)
  if dir then
    Dir.chdir(dir)
  end

  return false unless load_settings

  #CSS
  if @settings['css']['enabled'] then
    css_source_files = Dir.glob("*.#{@settings['css']['format']}")
    if @settings['css']['prioritize'] then
      css_source_files = prioritize_files(css_source_files,@settings['css']['prioritize'])
    end
    if @settings['css']['ignore']
      css_source_files = clean_ignored_files(css_source_files,@settings['css']['ignore'])
    end
    begin
      css_processor_found = require "processors/#{@settings['css']['format']}.rb"
    rescue Exception => e
      puts "No processor for #{@settings['css']['format']} found"
      css_processor_found = false
    end
    if css_processor_found then
      css_processor = CSS_Processor.new
      css_processor.process(css_source_files)
    end
    puts "Compiled css into css.css"
  end

  #TEMPLATES
  if @settings['templates']['enabled'] then
    template_files = Dir.glob("*.#{@settings['templates']['format']}")
    if @settings['templates']['prioritize'] then
      template_files = prioritize_files(template_files,@settings['templates']['prioritize'])
    end
    if @settings['templates']['ignore'] then
      template_files = clean_ignored_files(template_files,@settings['templates']['ignore'])
    end
    templates_source = "#{@settings['templates']['varname']} = {};\n"
    template_files.each {|file|
      template_markup = File.open(file, :encoding => "UTF-8").read
      template_markup.gsub!("\n","")
      template_markup.gsub!("\"","\\\"")
      template_markup = template_markup.strip.gsub(/\s{2,}/, ' ')
      templates_source << "#{@settings['templates']['varname']}['#{file.split('.')[0]}'] = \"#{template_markup}\";\n"
    }
    puts "Compiled templates into #{@settings['templates']['varname']} variable"
  else
    templates_source = ""
  end

  #JS
  if @settings['javascript']['enabled'] then
    js_source_files = Dir.glob("*.js")
    if @settings['javascript']['prioritize'] then
      js_source_files = prioritize_files(js_source_files,@settings['javascript']['prioritize'])
    end
    # if @settings['javascript']['ignore'].class != Array then
    #   @settings['javascript']['ignore'] = []
    # end
    if @settings['javascript']['ignore'] then
      js_source_files = clean_ignored_files(js_source_files,(@settings['javascript']['ignore'] << 'js.js'))
    end
    js_source = ""
    js_source_files.each { |file|
      js_source << File.open(file,:encoding => "UTF-8").read+"\n"
    }
    js_source << templates_source
    if @settings['javascript']['jsmin'] then
      begin
        jsmin = require "jsmin"
      rescue Exception => e
        puts "JSmin not installed"
      end
      if jsmin then
        js_source = JSMin.minify(js_source)
      end
    end
    if @settings['javascript']['yui'] then
      begin
        yui = require "yui/compressor"
      rescue Exception => e
        puts "YUI compressor gem not installed"
      end
      if yui then
        compressor = YUI::JavaScriptCompressor.new
        js_source = compressor.compress(js_source)
      end
    end
    File.open('js.js','w') { |f|
     f.write(js_source)
    }
    puts "Compiled javascript into js.js"
  end

end

#init_appObject



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/FrontEndLoader.rb', line 16

def init_app
  if File.exists?  'FrontendLoader.yml' then
    puts "Frontend Loader already initialized"
    return false
  end
  config_path = @resources_path+"/FrontendLoader.yml"
  # guard_path = @resources_path+"/Guardfile"
  %x[cp #{config_path} FrontendLoader.yml]
  # %x[cp #{guard_path} Guardfile]
  puts "Created basic FrontendLoader app, check FrontendLoader.yml for config"
  puts "To automatically compile upon file saves, run \"fel listen\""
end

#listen(dir = false) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/FrontEndLoader.rb', line 174

def listen(dir=false)
  if dir then
    Dir.chdir(dir)
  end
  begin
    listener = Listen.to("./", :filter => %r{(.*).(js|css|less|scss|mustache|handlebars|html)}, :ignore => [/js.js/,/style.css/]) do
      compile
    end
    listener = listener.ignore(/js.js/,/style.css/)
  rescue Exception => e
    puts "\nListening stopped"
  end
end

#load_settingsObject



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/FrontEndLoader.rb', line 33

def load_settings
  if  ! File.exists?  'FrontendLoader.yml' then
    puts 'FrontendLoader not yet initialized. Please run "fel init" first'
    return false
  end
  begin
    @settings = YAML.load_file('FrontendLoader.yml')
    return true
  rescue Exception => e
    puts "FrontendLoader.yml could not be parsed as valid YAML, please check your syntax"
    return false
  end
end

#prioritize_files(files, priority_files = [], path = "") ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/FrontEndLoader.rb', line 145

def prioritize_files(files, priority_files=[],path="")
  if priority_files.class != Array then
    priority_files = []
  end
  priority_files.uniq.each {|file|
    if files.include? file then
      files.delete file
    else
      priority_files.delete file
    end
  }
  prioritized_files = priority_files + files
  return prioritized_files
end