class MagLove::Commands::Assets

Public Instance Methods

blocks() click to toggle source
# 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
clean() click to toggle source
# File lib/maglove/commands/assets.rb, line 19
def clean
  info("▸ Cleaning up Theme Directory")
  theme.dist_dir.clean
end
compile() click to toggle source
# 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
compress() click to toggle source
# 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
images() click to toggle source
# 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
javascript() click to toggle source
# 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
optimizeimages() click to toggle source
# 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
stylesheet() click to toggle source
# 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
templates() click to toggle source
# 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
videos() click to toggle source
# 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
yaml() click to toggle source
# File lib/maglove/commands/assets.rb, line 89
def yaml
  info("▸ Compiling YAML Manifest")
  MagLove::Asset::Theme.new("theme.yml", { base: false }).write!
end