Class: Savanna::Assets
- Inherits:
-
Object
- Object
- Savanna::Assets
- Defined in:
- lib/savanna/assets.rb
Defined Under Namespace
Classes: EmptyPrecompileList, FileNotFound, PrecompileFileNotFound
Constant Summary collapse
- ASSET_PATH =
[]
- ASSET_DIR =
'assets'
Instance Attribute Summary collapse
-
#root_path ⇒ Object
Returns the value of attribute root_path.
-
#sprockets ⇒ Object
Returns the value of attribute sprockets.
Class Method Summary collapse
Instance Method Summary collapse
- #copy_static_folders(*folders) ⇒ Object
-
#initialize(options) ⇒ Assets
constructor
A new instance of Assets.
- #load_precompile_files ⇒ Object
- #precompile ⇒ Object
Constructor Details
#initialize(options) ⇒ Assets
Returns a new instance of Assets.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/savanna/assets.rb', line 21 def initialize() @root_path = [:root_path] @sprockets = Sprockets::Environment.new do |env| env.logger = Logger.new(STDOUT) define_children_directories [@root_path] + ASSET_PATH do |path| env.append_path path end Sprockets::Helpers.configure do |config| config.environment = env config.prefix = "/#{ASSET_DIR}" config.digest = false end if [:precompile] env.css_compressor = :scss env.js_compressor = ::Uglifier.new mangle: true end end end |
Instance Attribute Details
#root_path ⇒ Object
Returns the value of attribute root_path.
14 15 16 |
# File 'lib/savanna/assets.rb', line 14 def root_path @root_path end |
#sprockets ⇒ Object
Returns the value of attribute sprockets.
14 15 16 |
# File 'lib/savanna/assets.rb', line 14 def sprockets @sprockets end |
Class Method Details
.add_path_from_gem(path) ⇒ Object
16 17 18 19 |
# File 'lib/savanna/assets.rb', line 16 def self.add_path_from_gem (path) gem_root_path = File.('../../', path) ASSET_PATH << gem_root_path end |
Instance Method Details
#copy_static_folders(*folders) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/savanna/assets.rb', line 61 def copy_static_folders(*folders) dirs = [] folders.each do |folder| dirs += @sprockets.paths.select { |path| path =~ /\/#{folder}$/ } end dirs.each do |dir| begin FileUtils.cp_r Dir["#{dir}/*"], @output_dir rescue nil end end end |
#load_precompile_files ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/savanna/assets.rb', line 75 def load_precompile_files precompile_files = [] list_file = File.join(@root_path, 'savanna') if File.exists? list_file File.open(list_file).each_line do |line| precompile_files << line.gsub(/\n/, '') end raise EmptyPrecompileList if precompile_files.empty? else raise PrecompileFileNotFound end return precompile_files end |
#precompile ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/savanna/assets.rb', line 43 def precompile precompile_files = load_precompile_files @output_dir = File.join @root_path, 'www', 'assets' FileUtils.rm_rf @output_dir FileUtils.mkdir_p @output_dir copy_static_folders 'images', 'fonts' precompile_files.each do |file| asset = @sprockets[file] raise FileNotFound.new file if asset.nil? output_file = Pathname.new(@output_dir).join file FileUtils.mkdir_p output_file.dirname asset.write_to output_file end end |