Module: SinatraAssetPackager

Extended by:
SinatraAssetPackager
Included in:
SinatraAssetPackager
Defined in:
lib/sinatra_asset_packager.rb,
lib/sinatra_asset_packager/routes.rb,
lib/sinatra_asset_packager/helpers.rb,
lib/sinatra_asset_packager/version.rb

Defined Under Namespace

Modules: Helpers Classes: Routes

Constant Summary collapse

PUBLIC_ASSETS_DIRECTORY =
"public/assets"
CSS_EXTENSIONS =
[".less", ".scss", ".sass", ".css"]
JS_EXTENSIONS =
[".js", ".coffee"]
VERSION =
"0.0.7"

Instance Method Summary collapse

Instance Method Details

#environment(compress = false) ⇒ Object



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

def environment(compress=false)
  @@environment ||= (Sprockets::Environment.new { |env|
    env.append_path("app/assets/images")
    env.append_path("app/assets/javascripts")
    env.append_path("app/assets/stylesheets")
    env.append_path("app/assets/templates")
    
    if !!compress || ["production", "staging"].include?(ENV["RACK_ENV"])
      env.js_compressor  = Uglifier.new(mangle: true)
      env.css_compressor = YUI::CssCompressor.new
    end
  })
end

#output_path(input_path, extension) ⇒ Object



62
63
64
65
66
# File 'lib/sinatra_asset_packager.rb', line 62

def output_path(input_path, extension)
  replacement_extension = '.js'
  replacement_extension = '.css' if CSS_EXTENSIONS.include?(extension)
  input_path.gsub(extension,  replacement_extension)
end

#precompileObject



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

def precompile
  FileUtils.rm_rf(PUBLIC_ASSETS_DIRECTORY, secure: true)
  FileUtils.mkdir_p(PUBLIC_ASSETS_DIRECTORY)
  
  (CSS_EXTENSIONS + JS_EXTENSIONS).each do |extension|
    files = Dir.glob("app/assets/**/**#{extension}")
    files.each do |full_filepath|
      regex_path            = /app\/assets\/javascripts\/|app\/assets\/stylesheets\//
      input_path            = full_filepath.gsub(regex_path, '')

      write_asset input_path, output_path(input_path, extension)
    end
  end

  FileUtils.cp_r(Dir['app/assets/images/*'], PUBLIC_ASSETS_DIRECTORY) 
  FileUtils.cp_r(Dir['app/assets/templates/*'], PUBLIC_ASSETS_DIRECTORY) 
end

#write_asset(input_path, output_path) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/sinatra_asset_packager.rb', line 53

def write_asset(input_path, output_path)
  puts output_path
  final_output_path = [PUBLIC_ASSETS_DIRECTORY, output_path].join("/")
  environment(true)[input_path].write_to(final_output_path)
rescue => e
  puts e
  puts "Asset #{input_path} failed to compile to #{output_path}"
end