Module: Condenser::Resolve

Included in:
Condenser
Defined in:
lib/condenser/resolve.rb

Instance Method Summary collapse

Instance Method Details

#[](filename) ⇒ Object



120
121
122
123
124
# File 'lib/condenser/resolve.rb', line 120

def [](filename)
  build do
    find!(filename).export
  end
end

#buildObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/condenser/resolve.rb', line 133

def build
  if build_cache.listening && !build_cache.semaphore.owned?
    build_cache.semaphore.lock 
    logger.debug { "build cache semaphore locked by #{Thread.current.object_id}" }
  end
  
  @build_cc += 1
  yield
ensure
  @build_cc -= 1
  if @build_cc == 0
    if build_cache.listening && build_cache.semaphore.owned?
      logger.debug { "build cache semaphore unlocked by #{Thread.current.object_id}" }
      build_cache.semaphore.unlock
    end
  end
end

#build_cacheObject



10
11
12
13
# File 'lib/condenser/resolve.rb', line 10

def build_cache
  return @build_cache if instance_variable_defined?(:@build_cache)
  @build_cache = BuildCache.new(path, logger: logger)
end

#decompose_path(path, base = nil) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/condenser/resolve.rb', line 164

def decompose_path(path, base=nil)
  dirname = if base && path.start_with?('.')
    if has_dir?(base)
      if has_dir?(path)
        expand_path(File.join(File.dirname(base), File.dirname(path)))
      else
        expand_path(File.dirname(base))
      end
    else
      expand_path(File.dirname(path))
    end
  else
    path.index('/') ? File.dirname(path) : nil
  end

  
  _, star, basename, extensions = path.match(/(([^\.\/]+)(\.[^\/]+)|\*|[^\/]+)$/).to_a
  if extensions == '.*'
    extensions = nil
  end
  if basename.nil? && extensions.nil?
    basename = star
  end
    
  if extensions.nil? && basename == '*'
    extensions = nil
    mime_types = []
  elsif extensions.nil?
    mime_types = []
  else
    exts = []
    while !extensions.empty?
      matching_extensions = @extensions.keys.select { |e| extensions.end_with?(e) }
      if matching_extensions.empty?
        basename << extensions
        break
        # raise 'unkown mime'
      else
        matching_extensions.sort_by! { |e| -e.length }
        exts.unshift(matching_extensions.first)
        extensions.delete_suffix!(matching_extensions.first)
      end
    end
    extensions = exts
    mime_types = extensions.map { |k| @extensions[k] }
  end
  
  
  [ dirname, basename, extensions, mime_types ]
end

#expand_path(path) ⇒ Object



155
156
157
158
159
160
161
162
# File 'lib/condenser/resolve.rb', line 155

def expand_path(path)
  dir = if path.start_with?('/')
    File.expand_path(path)
  else
    File.expand_path("/#{path}").delete_prefix('/')
  end
  dir.empty? ? nil : dir
end

#find(filename, base = nil, accept: nil) ⇒ Object



108
109
110
111
112
# File 'lib/condenser/resolve.rb', line 108

def find(filename, base=nil, accept: nil)
  build do
    resolve(filename, base, accept: accept).first
  end
end

#find!(filename, base = nil, **kargs) ⇒ Object



114
115
116
117
118
# File 'lib/condenser/resolve.rb', line 114

def find!(filename, base=nil, **kargs)
  build do
    resolve!(filename, base, **kargs).first
  end
end

#find_export(filename, base = nil, **kargs) ⇒ Object



126
127
128
129
130
131
# File 'lib/condenser/resolve.rb', line 126

def find_export(filename, base=nil, **kargs)
  build do
    asset = resolve(filename, base, **kargs).first
    asset&.export
  end
end

#has_dir?(path) ⇒ Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/condenser/resolve.rb', line 151

def has_dir?(path)
  path.count('/') > (path.start_with?('/') ? 1 : 0)
end

#initialize(*args, **kws, &block) ⇒ Object



4
5
6
7
8
# File 'lib/condenser/resolve.rb', line 4

def initialize(*args, **kws, &block)
  @reverse_mapping = nil
  @build_cache_options = kws[:listen]
  super
end

#match_mime_type?(value, matcher) ⇒ Boolean

Public: Test mime type against mime range.

match_mime_type?('text/html', 'text/*') => true
match_mime_type?('text/plain', '*') => true
match_mime_type?('text/html', 'application/json') => false

Returns true if the given value is a mime match for the given mime match specification, false otherwise.

Returns:

  • (Boolean)


267
268
269
270
271
# File 'lib/condenser/resolve.rb', line 267

def match_mime_type?(value, matcher)
  v1, v2 = value.split('/'.freeze, 2)
  m1, m2 = matcher.split('/'.freeze, 2)
  (m1 == '*'.freeze || v1 == m1) && (m2.nil? || m2 == '*'.freeze || m2 == v2)
end

#match_mime_types?(value, matcher) ⇒ Boolean

Returns:

  • (Boolean)


242
243
244
245
246
247
248
249
250
251
# File 'lib/condenser/resolve.rb', line 242

def match_mime_types?(value, matcher)
  matcher = Array(matcher)
  value = Array(value)
  
  if matcher.length == 1 && matcher.last == '*/*'
    true
  else
    value.length == matcher.length && value.zip(matcher).all? { |v, m| match_mime_type?(v, m) }
  end
end

#mime_type_match_accept?(value, accept) ⇒ Boolean

Returns:

  • (Boolean)


253
254
255
256
257
# File 'lib/condenser/resolve.rb', line 253

def mime_type_match_accept?(value, accept)
  accept.any? do |a|
    match_mime_types?(value, Array(a))
  end
end

#resolve(filename, base = nil, accept: nil) ⇒ Object



15
16
17
18
19
20
21
22
23
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/condenser/resolve.rb', line 15

def resolve(filename, base=nil, accept: nil)
  filename = filename.delete_prefix("/") if path.none? { |p| filename.start_with?(p) }
  dirname, basename, extensions, mime_types = decompose_path(filename, base)
  accept ||= mime_types.empty? ? ['*/*'] : mime_types
  accept = Array(accept)
  
  cache_key = [dirname, basename].flatten.join('/')
  cache_key = "/#{cache_key}" if !cache_key.starts_with?('/')
  cache_key << "@#{accept.join(',')}" if accept
  build_cache.fetch(cache_key) do
    build do
      results = []

      paths = if dirname&.start_with?('/')
        if pat = path.find { |pa| dirname.start_with?(pa) }
          dirname.delete_prefix!(pat)
          dirname.delete_prefix!('/')
          [pat]
        else
          []
        end
      else
        path
      end
    
      paths.each do |path|
        glob = File.join(*[path, dirname, basename].compact)
        glob << '.*' unless glob.end_with?('*')
      
        Dir.glob(glob).sort.each do |f|
          next if !File.file?(f)
      
          f_dirname, f_basename, f_extensions, f_mime_types = decompose_path(f)
          if (basename == '*' || basename == f_basename)
            if accept == ['*/*'] || mime_type_match_accept?(f_mime_types, accept)
              asset_dir = f_dirname.delete_prefix(path).delete_prefix('/')
              asset_basename = f_basename + f_extensions.join('')
              asset_filename = asset_dir.empty? ? asset_basename : File.join(asset_dir, asset_basename)
              results << build_cache.map("#{asset_filename}@#{f_mime_types.join('')}") do
                Asset.new(self, {
                  filename: asset_filename,
                  content_types: f_mime_types,
                  source_file: f,
                  source_path: path
                })
              end
            else
              reverse_mapping[f_mime_types]&.each do |derivative_mime_types|
                if accept == ['*/*'] || mime_type_match_accept?(derivative_mime_types, accept)
                  asset_dir = f_dirname.delete_prefix(path).delete_prefix('/')
                  asset_basename = f_basename + derivative_mime_types.map { |t| @mime_types[t][:extensions].first }.join('')
                  asset_filename = asset_dir.empty? ? asset_basename : File.join(asset_dir, asset_basename)
                  results << build_cache.map("#{asset_filename}@#{derivative_mime_types.join('')}") do
                    Asset.new(self, {
                      filename: asset_filename,
                      content_types: derivative_mime_types,
                      source_file: f,
                      source_path: path
                    })
                  end
                end
              end
            end

          end
        end
      end
    
      results = results.group_by do |a|
        accept.find_index { |m| match_mime_types?(a.content_types, m) }
      end
    
      results = results.keys.sort.reduce([]) do |c, key|
        c += results[key].sort_by(&:filename).uniq { |f| f.filename }
      end

      results.sort_by!(&:filename)
      results
    end
  end
end

#resolve!(filename, base = nil, **kargs) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/condenser/resolve.rb', line 97

def resolve!(filename, base=nil, **kargs)
  build do
    assets = resolve(filename, base, **kargs)
    if assets.empty?
      raise FileNotFound, "couldn't find file '#{filename}'"
    else
      assets
    end
  end
end

#reverse_mappingObject



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/condenser/resolve.rb', line 215

def reverse_mapping
  return @reverse_mapping if @reverse_mapping
  map = {}
  @mime_types.each_key do |source_mime_type|
    to_mime_type = source_mime_type

    ([nil] + (@transformers[source_mime_type]&.keys || [])).each do |transform_mime_type|
      to_mime_type = transform_mime_type if transform_mime_type
      
      ([nil] + @templates.keys).each do |template_mime_type|
        from_mimes = [source_mime_type, template_mime_type].compact
        to_mime_types = [to_mime_type].compact
        if from_mimes != to_mime_types
          map[from_mimes] ||= Set.new
          map[from_mimes] << to_mime_types
        end
      end

    end
  end
  $map = map
end

#writers_for_mime_type(mime_type) ⇒ Object



238
239
240
# File 'lib/condenser/resolve.rb', line 238

def writers_for_mime_type(mime_type)
  @writers.select { |m, e| match_mime_type?(mime_type, m) }.values.reduce(&:+)
end