Class: RailsAssetPackager::AssetPackage

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

Class Attribute Summary collapse

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.



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rails_asset_packager/asset_package.rb', line 106

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 = "#{self.class.asset_base_path}/#{@asset_type}#{@target_dir.gsub(/^(.+)$/, '/\1')}"
  @extension = get_extension
  @file_name = "#{@target}_packaged.#{@extension}"
  @full_path = File.join(@asset_path, @file_name)
end

Class Attribute Details

.merge_environmentsObject



24
25
26
# File 'lib/rails_asset_packager/asset_package.rb', line 24

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

Instance Attribute Details

#asset_typeObject

instance methods



104
105
106
# File 'lib/rails_asset_packager/asset_package.rb', line 104

def asset_type
  @asset_type
end

#sourcesObject

instance methods



104
105
106
# File 'lib/rails_asset_packager/asset_package.rb', line 104

def sources
  @sources
end

#targetObject

instance methods



104
105
106
# File 'lib/rails_asset_packager/asset_package.rb', line 104

def target
  @target
end

#target_dirObject

instance methods



104
105
106
# File 'lib/rails_asset_packager/asset_package.rb', line 104

def target_dir
  @target_dir
end

Class Method Details

.asset_base_pathObject



8
9
10
# File 'lib/rails_asset_packager/asset_package.rb', line 8

def asset_base_path
  @@asset_base_path ||= "#{Rails.root}/public"
end

.asset_base_path=(path) ⇒ Object



12
13
14
# File 'lib/rails_asset_packager/asset_package.rb', line 12

def asset_base_path=(path)
  @@asset_base_path = path
end

.asset_packages_ymlObject



16
17
18
# File 'lib/rails_asset_packager/asset_package.rb', line 16

def asset_packages_yml
  @@asset_packages_yml ||= File.exists?("#{Rails.root}/config/asset_packages.yml") ? YAML.load_file("#{Rails.root}/config/asset_packages.yml") : nil
end

.asset_packages_yml=(path) ⇒ Object



20
21
22
# File 'lib/rails_asset_packager/asset_package.rb', line 20

def asset_packages_yml=(path)
  @@asset_packages_yml = path
end

.build_allObject



70
71
72
73
74
# File 'lib/rails_asset_packager/asset_package.rb', line 70

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

.create_ymlObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rails_asset_packager/asset_package.rb', line 82

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

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

    File.open("#{Rails.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



76
77
78
79
80
# File 'lib/rails_asset_packager/asset_package.rb', line 76

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 } unless asset_type == 'uploads'
  end
end

.find_by_source(asset_type, source) ⇒ Object



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

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



36
37
38
39
# File 'lib/rails_asset_packager/asset_package.rb', line 36

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



32
33
34
# File 'lib/rails_asset_packager/asset_package.rb', line 32

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

.parse_path(path) ⇒ Object



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

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

.sources_from_targets(asset_type, targets) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/rails_asset_packager/asset_package.rb', line 59

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



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

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



129
130
131
132
# File 'lib/rails_asset_packager/asset_package.rb', line 129

def build
  delete_previous_build
  create_new_build
end

#current_fileObject



122
123
124
125
126
127
# File 'lib/rails_asset_packager/asset_package.rb', line 122

def current_file
  build unless package_exists?

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

#delete_previous_buildObject



134
135
136
# File 'lib/rails_asset_packager/asset_package.rb', line 134

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

#package_exists?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/rails_asset_packager/asset_package.rb', line 118

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