Class: SparkEngine::Assets::Svgs

Inherits:
AssetType show all
Defined in:
lib/spark_engine/plugin/assets/svgs.rb

Instance Attribute Summary

Attributes inherited from AssetType

#base, #plugin

Instance Method Summary collapse

Methods inherited from AssetType

#build_failure, #build_success, #change, #compress, #destination, #file_event, #filter_files, #find_files, #find_node_module, #log_error, #log_success, #npm_command, #pathname, #url, #urls, #versioned, #watch

Constructor Details

#initialize(plugin, path) ⇒ Svgs

Returns a new instance of Svgs.



5
6
7
8
9
10
11
12
# File 'lib/spark_engine/plugin/assets/svgs.rb', line 5

def initialize(plugin, path)

  require 'esvg'

  @plugin = plugin
  @base = path
  @svg = esvg
end

Instance Method Details

#buildObject



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
# File 'lib/spark_engine/plugin/assets/svgs.rb', line 50

def build
  begin
    esvg.load_files

    return if esvg.symbols.empty?

    if files = esvg.build
      files.each do |file|
        puts build_success(file)
      end
    else
      log_error "FAILED TO BUILD SVGs"
    end
  rescue Exception => e
    log_error "\nFAILED TO BUILD SVGs"

    if e.backtrace && e.backtrace.is_a?(Array)
      log_error "Error in file: #{local_path(e.backtrace.shift)}"

      e.backtrace.each do |line|
        log_error local_path(line)
      end
    end

    log_error("  #{e.message}\n") if e.message

  end
end

#build_pathsObject



46
47
48
# File 'lib/spark_engine/plugin/assets/svgs.rb', line 46

def build_paths
  esvg.build_paths.map { |file| file.sub("-#{plugin.version}",'') }
end

#esvgObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/spark_engine/plugin/assets/svgs.rb', line 14

def esvg
  @svg || Esvg.new({
    root: plugin.root,
    config_file: File.join(plugin.root, 'config', 'esvg.yml'),
    source: @base,
    assets: plugin.paths[:javascripts],
    version: plugin.version,
    build: plugin.destination,
    temp: SparkEngine.rails_path('tmp/cache/assets'),
    filename: '_icons.js',
    compress: SparkEngine.production?,
    optimize: true,
    print: false
  })
end

#extObject



30
31
32
# File 'lib/spark_engine/plugin/assets/svgs.rb', line 30

def ext
  "svg"
end

#local_path(path) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/spark_engine/plugin/assets/svgs.rb', line 34

def local_path(path)
  path = File.expand_path(path)

  # Strip all irrelevant sections of the path
  path.sub(plugin.paths[:javascripts]+'/', '') # written to assets dir
      .sub(plugin.root+'/','')                 # writtent to public dir
end

#use(*args) ⇒ Object



42
43
44
# File 'lib/spark_engine/plugin/assets/svgs.rb', line 42

def use(*args)
  esvg.use(*args)
end