Class: MagLove::Commands::Assets

Inherits:
Base
  • Object
show all
Defined in:
lib/maglove/commands/assets.rb

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods included from Helper::LogHelper

#debug, #error, #error!, #info, #logger

Constructor Details

This class inherits a constructor from MagLove::Commands::Base

Instance Method Details

#blocksObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/maglove/commands/assets.rb', line 130

def blocks
  info("▸ Compiling HAML Blocks")
  if theme.src_dir.dir("blocks").exists?
    Maglove::Engine.configure do |config|
      config.asset_uri = "file://#{Workspace::Dir.new(Dir.pwd, 'dist').absolute_path}"
      config.block_path = theme.src_dir.dir("blocks").to_s
    end
    theme.src_dir.dir("blocks").children("**/*.haml").each do |block_file|
      debug "~> processing block #{block_file.basename}"
      contents = MagLove::Asset::Theme.new(block_file.path, { base: false }).contents
      html_contents = Maglove::Engine.render(Maglove.assets_dir.file("export.haml").read, theme: theme.identifier, contents: contents)
      theme.dist_dir.file("blocks/#{block_file.basename}.html").write(html_contents)
    end
  else
    debug "~> no blocks available"
  end
end

#cleanObject



19
20
21
22
# File 'lib/maglove/commands/assets.rb', line 19

def clean
  info("▸ Cleaning up Theme Directory")
  theme.dist_dir.clean
end

#compileObject



7
8
9
10
11
12
13
14
15
16
# File 'lib/maglove/commands/assets.rb', line 7

def compile
  invoke(:clean)
  invoke(:images)
  invoke(:videos)
  invoke(:javascript)
  invoke(:stylesheet)
  invoke(:yaml)
  invoke(:templates)
  invoke(:blocks)
end

#compressObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/maglove/commands/assets.rb', line 95

def compress
  invoke(:javascript)
  invoke(:stylesheet)
  require 'cssminify'
  require 'closure-compiler'

  info("▸ Compressing JavaScript")
  js_file = theme.dist_dir.file("theme.js")
  js_file.write(Closure::Compiler.new.compile(js_file.read))

  info("▸ Compressing Stylesheet")
  css_file = theme.dist_dir.file("theme.css")
  runtime = ExecJS.compile(Maglove.assets_dir.file("autoprefixer.js").read)
  results = runtime.call("(function(css) { return autoprefixer.process(css).css; })", [css_file.read])
  css_file.write(CSSminify.compress(results))
end

#imagesObject



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/maglove/commands/assets.rb', line 25

def images
  info("▸ Copying Images")
  if theme.base_version
    theme.base_dir.children("images/**/*.{jpg,png,gif,svg}").each do |file|
      debug("~> Copying #{file}")
      MagLove::Asset::Theme.new(file.path, { base: true }).write!
    end
  end
  theme.src_dir.children("images/**/*.{jpg,png,gif,svg}").each do |file|
    debug("~> Copying #{file}")
    MagLove::Asset::Theme.new(file.path, { base: false }).write!
  end
end

#javascriptObject



72
73
74
75
76
# File 'lib/maglove/commands/assets.rb', line 72

def javascript
  info("▸ Compiling JavaScript")
  file = theme.src_dir.children("theme.{coffee,js}").first
  MagLove::Asset::Theme.new(file.path, { base: false }).write!
end

#optimizeimagesObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/maglove/commands/assets.rb', line 50

def optimizeimages
  info("▸ Optimizing Images")
  require "workspace-media"
  Workspace.tmpdir do |tmpdir|
    theme.src_dir.children("images/**/*.{jpg,png,gif}").each do |file|
      tmp_file = tmpdir.file(file.name)
      file.copy(tmp_file)
      old_size = tmp_file.size
      tmp_file.optimize!(image_max_width: 1536, quality: 75, convert_jpg: false, optimize: true)
      new_size = tmp_file.size
      compression = new_size.to_f / old_size.to_f
      if compression < 0.99
        info("~> #{file.relative_path} optimized #{((1 - compression) * 100).to_i}% (#{old_size / 1000}kb to #{new_size / 1000}kb)")
        tmp_file.move(file)
      else
        debug("~> #{file.relative_path} skipped #{((1 - compression) * 100).to_i}% (#{old_size / 1000}kb to #{new_size / 1000}kb)")
      end
    end
  end
end

#stylesheetObject



79
80
81
82
83
84
85
86
# File 'lib/maglove/commands/assets.rb', line 79

def stylesheet
  info("▸ Compiling Stylesheet")
  Maglove::Engine.configure do |config|
    config.asset_uri = "."
  end
  file = theme.src_dir.children("theme.{scss,less}").first
  MagLove::Asset::Theme.new(file.path, { base: false }).write!
end

#templatesObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/maglove/commands/assets.rb', line 113

def templates
  info("▸ Compiling HAML Templates")
  Maglove::Engine.configure do |config|
    config.asset_uri = "file://#{Workspace::Dir.new(Dir.pwd, 'dist').absolute_path}"
    config.block_path = theme.src_dir.dir("blocks").to_s
  end
  theme.templates.each do |template|
    debug "~> processing template #{template}"
    template_file = theme.src_dir.file("templates/#{template}.haml")
    error!("~> Template '#{template}' does not exist!") if template_file.nil?
    contents = MagLove::Asset::Theme.new(template_file.path, { base: false }).contents
    html_contents = Maglove::Engine.render(Maglove.assets_dir.file("export.haml").read, theme: theme.identifier, contents: contents)
    theme.dist_dir.file("templates/#{template}.html").write(html_contents)
  end
end

#videosObject



40
41
42
43
44
45
46
# File 'lib/maglove/commands/assets.rb', line 40

def videos
  info("▸ Copying Videos")
  theme.src_dir.children("videos/**/*.{mp4,webm,ogg}").each do |file|
    debug("~> Copying #{file}")
    MagLove::Asset::Theme.new(file.path, { base: false }).write!
  end
end

#yamlObject



89
90
91
92
# File 'lib/maglove/commands/assets.rb', line 89

def yaml
  info("▸ Compiling YAML Manifest")
  MagLove::Asset::Theme.new("theme.yml", { base: false }).write!
end