Module: Gluey::Tools

Defined in:
lib/gluey/tools/tools.rb,
lib/gluey/tools/local_build.rb

Defined Under Namespace

Classes: Asset

Class Method Summary collapse

Class Method Details

.build_into_public_dir(workshop, warehouse, public_dir = 'public/assets', **builders) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/gluey/tools/local_build.rb', line 5

def self.build_into_public_dir(workshop, warehouse, public_dir='public/assets', **builders)
  public_dirs = workshop.materials.values.inject({}) do |h, m|
    h[m.name] = "#{workshop.root_path}/#{m.public_dir || public_dir}"
    h
  end

  built = []
  move_it = -> (cache_file, file) { FileUtils.mv cache_file, file }
  self.each_asset_file workshop, warehouse do |cache_file, type, path|
    file = "#{public_dirs[type]}/#{path}"
    next if File.exists? file
    FileUtils.mkdir_p file[0..(file.rindex('/')-1)]
    (builders[type] || move_it)[cache_file, file]
    built << file
    puts "created #{file}"
  end
  return built
end

.clear_public_dir(workshop, warehouse, versions = 2, public_dir = 'public/assets') ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/gluey/tools/local_build.rb', line 24

def self.clear_public_dir(workshop, warehouse, versions=2, public_dir='public/assets')
  public_dirs = workshop.materials.values.inject({}) do |h, m|
    h[m.name] = "#{workshop.root_path}/#{m.public_dir || public_dir}"
    h
  end

  # process existing files into assets
  eas_regexp = /^#{workshop.root_path}\/(.+)$/
  assets = public_dirs.values.uniq.map{|dir| Dir["#{dir}/**/*.*.*"]}.flatten.
      map{|f| Asset.try_create f[eas_regexp, 1]}.compact
  assets = assets.inject({}){|h, asset| (h[asset.path] ||= []) << asset; h }

  # items not on list
  on_list = []
  warehouse.assets.each do |type, mater_assets|
    mater_assets.each do |_, real_path|
      file = "#{public_dirs[type]}/#{real_path}"
      asset = Asset.try_create file[eas_regexp, 1]
      on_list << asset
    end
  end
  on_list.map! &:path
  assets.delete_if do |path, asseets_arr|
    unless on_list.include? path
      asseets_arr.each do |some_asset|
        file = "#{workshop.root_path}/#{some_asset.orig_path}"
        File.delete file
        puts "deleted unknown #{file}"
      end
      true
    end
    false
  end

  # older versions
  assets.values.select{|arr| arr.length > versions}.
      map{|arr| arr.sort.slice 0..(-versions-1) }.compact.flatten.each do |old_asset|
    file = "#{workshop.root_path}/#{old_asset.orig_path}"
    File.delete file
    puts "deleted old #{file}"
  end
end

.create_uglifier_builder(**opts) ⇒ Object



14
15
16
17
# File 'lib/gluey/tools/tools.rb', line 14

def self.create_uglifier_builder(**opts)
  require 'uglifier'
  ->(a, b){File.write b, ::Uglifier.new(opts.merge! copyright: :none).compile(File.read a)}
end

.each_asset_file(workshop, warehouse) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/gluey/tools/tools.rb', line 5

def self.each_asset_file(workshop, warehouse)
  warehouse.assets.each do |type, assets|
    assets.each do |path, real_path|
      cache_file = workshop.fetch_file type, path
      yield cache_file, type, real_path
    end
  end
end