Class: Synthesis::AssetPackage

Inherits:
Object
  • Object
show all
Defined in:
lib/synthesis/asset_package.rb

Constant Summary collapse

@@asset_packages_yml =

class variables

$asset_packages_yml || 
(File.exists?("#{config.root}/config/asset_packages.yml") ? YAML.load_file("#{config.root}/config/asset_packages.yml") : nil)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(asset_type, package_hash) ⇒ AssetPackage

Returns a new instance of AssetPackage.



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/synthesis/asset_package.rb', line 96

def initialize(asset_type, package_hash)
  target_parts = self.class.parse_path(package_hash.keys.first)
  @target_dir = target_parts[1].to_s
  @target = target_parts[2].to_s
  @sources = package_hash[package_hash.keys.first]
  @asset_type = asset_type
  @asset_path = ($asset_base_path ? "#{$asset_base_path}/" : "#{RAILS_ROOT}/public/") +
      "#{@asset_type}#{@target_dir.gsub(/^(.+)$/, '/\1')}"
  @extension = get_extension
  @file_name = "#{@target}_packaged.#{@extension}"
  @full_path = File.join(@asset_path, @file_name)
end

Instance Attribute Details

#asset_typeObject

instance methods



94
95
96
# File 'lib/synthesis/asset_package.rb', line 94

def asset_type
  @asset_type
end

#sourcesObject

instance methods



94
95
96
# File 'lib/synthesis/asset_package.rb', line 94

def sources
  @sources
end

#targetObject

instance methods



94
95
96
# File 'lib/synthesis/asset_package.rb', line 94

def target
  @target
end

#target_dirObject

instance methods



94
95
96
# File 'lib/synthesis/asset_package.rb', line 94

def target_dir
  @target_dir
end

Class Method Details

.build_allObject



61
62
63
64
65
# File 'lib/synthesis/asset_package.rb', line 61

def build_all
  @@asset_packages_yml.keys.each do |asset_type|
    @@asset_packages_yml[asset_type].each { |p| self.new(asset_type, p).build }
  end
end

.create_ymlObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/synthesis/asset_package.rb', line 73

def create_yml
  unless File.exists?("#{config.root}/config/asset_packages.yml")
    asset_yml = Hash.new

    asset_yml['javascripts'] = [{"base" => build_file_list("#{config.root}/public/javascripts", "js")}]
    asset_yml['stylesheets'] = [{"base" => build_file_list("#{config.root}/public/stylesheets", "css")}]

    File.open("#{config.root}/config/asset_packages.yml", "w") do |out|
      YAML.dump(asset_yml, out)
    end

    log "config/asset_packages.yml example file created!"
    log "Please reorder files under 'base' so dependencies are loaded in correct order."
  else
    log "config/asset_packages.yml already exists. Aborting task..."
  end
end

.delete_allObject



67
68
69
70
71
# File 'lib/synthesis/asset_package.rb', line 67

def delete_all
  @@asset_packages_yml.keys.each do |asset_type|
    @@asset_packages_yml[asset_type].each { |p| self.new(asset_type, p).delete_previous_build }
  end
end

.find_by_source(asset_type, source) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/synthesis/asset_package.rb', line 32

def find_by_source(asset_type, source)
  path_parts = parse_path(source)
  package_hash = @@asset_packages_yml[asset_type].find do |p|
    key = p.keys.first
    p[key].include?(path_parts[2]) && (parse_path(key)[1] == path_parts[1])
  end
  package_hash ? self.new(asset_type, package_hash) : nil
end

.find_by_target(asset_type, target) ⇒ Object



27
28
29
30
# File 'lib/synthesis/asset_package.rb', line 27

def find_by_target(asset_type, target)
  package_hash = @@asset_packages_yml[asset_type].find {|p| p.keys.first == target }
  package_hash ? self.new(asset_type, package_hash) : nil
end

.find_by_type(asset_type) ⇒ Object



23
24
25
# File 'lib/synthesis/asset_package.rb', line 23

def find_by_type(asset_type)
  @@asset_packages_yml[asset_type].map { |p| self.new(asset_type, p) }
end

.merge_environmentsObject



15
16
17
# File 'lib/synthesis/asset_package.rb', line 15

def merge_environments
  @@merge_environments ||= ["production"]
end

.merge_environments=(environments) ⇒ Object



11
12
13
# File 'lib/synthesis/asset_package.rb', line 11

def merge_environments=(environments)
  @@merge_environments = environments
end

.parse_path(path) ⇒ Object



19
20
21
# File 'lib/synthesis/asset_package.rb', line 19

def parse_path(path)
  /^(?:(.*)\/)?([^\/]+)$/.match(path).to_a
end

.sources_from_targets(asset_type, targets) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/synthesis/asset_package.rb', line 50

def sources_from_targets(asset_type, targets)
  source_names = Array.new
  targets.each do |target|
    package = find_by_target(asset_type, target)
    source_names += (package ? package.sources.collect do |src|
      package.target_dir.gsub(/^(.+)$/, '\1/') + src
    end : target.to_a)
  end
  source_names.uniq
end

.targets_from_sources(asset_type, sources) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/synthesis/asset_package.rb', line 41

def targets_from_sources(asset_type, sources)
  package_names = Array.new
  sources.each do |source|
    package = find_by_target(asset_type, source) || find_by_source(asset_type, source)
    package_names << (package ? package.current_file : source)
  end
  package_names.uniq
end

Instance Method Details

#buildObject



120
121
122
123
# File 'lib/synthesis/asset_package.rb', line 120

def build
  delete_previous_build
  create_new_build
end

#current_fileObject



113
114
115
116
117
118
# File 'lib/synthesis/asset_package.rb', line 113

def current_file
  build unless package_exists?

  path = @target_dir.gsub(/^(.+)$/, '\1/')
  "#{path}#{@target}_packaged"
end

#delete_previous_buildObject



125
126
127
# File 'lib/synthesis/asset_package.rb', line 125

def delete_previous_build
  File.delete(@full_path) if File.exists?(@full_path)
end

#package_exists?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/synthesis/asset_package.rb', line 109

def package_exists?
  File.exists?(@full_path)
end