Class: Savanna::Assets

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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(options)
  @root_path = options[: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 options[:precompile]
      env.css_compressor = :scss
      env.js_compressor  = ::Uglifier.new mangle: true
    end
  end
end

Instance Attribute Details

#root_pathObject

Returns the value of attribute root_path.



14
15
16
# File 'lib/savanna/assets.rb', line 14

def root_path
  @root_path
end

#sprocketsObject

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.expand_path('../../', 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_filesObject



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

#precompileObject



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