rack-pipeline 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fbe84807caa12bb5e61db1e0265cf6738773dfe8
4
- data.tar.gz: 2b32c7ca07cc0a78ca28d2dc26ee64513ee13cc6
3
+ metadata.gz: afe6a32a4e33df67af9184d8bb6776045d3a6311
4
+ data.tar.gz: cac12bca3ebd20ca96821e4fd81b2a403dccecb3
5
5
  SHA512:
6
- metadata.gz: 85882ed222c507c7b341cdb7d4d35cf98f738761356f217d3772bd43b308d6b853c9862ee574a4f530f963168978dfc974895659e19b23cd046fcc070e4e1547
7
- data.tar.gz: bd1791e0b871da0f89779b36336cb5c2a1f70c176d563de0bbdd54676d1f8c20815cccedaaaec4d25363951cefda73706d2c437b02471fb0bde838bab25f800e
6
+ metadata.gz: 4f695115376ef74ff9a39b81a09a491f04f4a0864884e96fa91bd19841f3c19cce7bb90d78406e04f88200be3d95bcb397bfe4c1dc35fd67cbeebe311aa43c02
7
+ data.tar.gz: 2382d1ef620099cc3d677caa5e7a4794a938d67a124e082218fcb52a58ee420d93b77ac9acc8e62684bdca5478f28c8ac888a55287ad77841204cf9494541d03
@@ -37,7 +37,7 @@ module RackPipeline
37
37
  },
38
38
  }
39
39
  @settings.merge!(args.pop) if args.last.kind_of?(Hash)
40
- create_temp_directory
40
+ ensure_temp_directory
41
41
  populate_pipelines
42
42
  @app = app
43
43
  end
@@ -47,6 +47,7 @@ module RackPipeline
47
47
  end
48
48
 
49
49
  def call(env)
50
+ @env = env
50
51
  env['rack-pipeline'] = self
51
52
  if file_path = prepare_pipe(env['PATH_INFO'])
52
53
  serve_file(file_path, env['HTTP_IF_MODIFIED_SINCE'])
@@ -60,15 +61,26 @@ module RackPipeline
60
61
 
61
62
  private
62
63
 
64
+ def busted?
65
+ result = settings[:bust_cache] && @busted
66
+ @busted = false
67
+ result
68
+ end
69
+
63
70
  def serve_file(file, mtime)
64
71
  headers = { 'Last-Modified' => File.mtime(file).httpdate }
65
72
  if mtime == headers['Last-Modified']
66
73
  [304, headers, []]
67
74
  else
68
- body = File.read file
69
- headers['Content-Type'] = "#{content_type(file)}; charset=#{body.encoding.to_s}"
70
- headers['Content-Length'] = File.size(file).to_s
71
- [200, headers, [body]]
75
+ if busted?
76
+ headers['Location'] = "#{@env['PATH_INFO']}?#{File.mtime(file).to_i}"
77
+ [302, headers, []]
78
+ else
79
+ body = File.read file
80
+ headers['Content-Type'] = "#{content_type(file)}; charset=#{body.encoding.to_s}"
81
+ headers['Content-Length'] = File.size(file).to_s
82
+ [200, headers, [body]]
83
+ end
72
84
  end
73
85
  rescue Errno::ENOENT
74
86
  raise MustRepopulate
@@ -95,7 +107,7 @@ module RackPipeline
95
107
  ready_file = combine(assets[type][pipename], File.basename(file))
96
108
  end
97
109
  end
98
- compress(ready_file, ready_file) if ready_file
110
+ compress(ready_file, File.basename(ready_file)) if ready_file
99
111
  rescue Errno::ENOENT
100
112
  raise MustRepopulate
101
113
  end
@@ -116,9 +128,11 @@ module RackPipeline
116
128
  static_type(file) ? :raw : :source
117
129
  end
118
130
 
119
- def glob_files(dirs)
120
- Dir.glob(dirs).sort.each_with_object({}) do |file,all|
121
- all[file] = file_kind(file)
131
+ def glob_files(globs)
132
+ Array(globs).each_with_object({}) do |glob,all|
133
+ Dir.glob(glob).sort.each do |file|
134
+ all[file] = file_kind(file)
135
+ end
122
136
  end
123
137
  end
124
138
 
@@ -4,32 +4,37 @@ require 'fileutils'
4
4
  module RackPipeline
5
5
  module Caching
6
6
  def cache_target(source, target)
7
- target_path = File.join(settings[:temp], "#{File.basename(target)}.#{calculate_hash(source)}#{File.extname(target)}")
7
+ ensure_temp_directory
8
+ caller_method = caller.first[/`([^']*)'/, 1]
9
+ extname = File.extname(target)
10
+ target_filename = File.basename(target).sub(/[0-9a-f]{32}\./,'').chomp(extname) << '.' << caller_method
11
+ target_path = File.join(settings[:temp], target_filename + '.' << calculate_hash(source) << extname)
8
12
  if File.file?(target_path)
9
13
  target_path
10
14
  else
11
- cleanup_cache(target)
15
+ cleanup_cache(target_filename << '.*' << extname)
12
16
  yield target_path
13
17
  end
14
18
  end
15
19
 
16
- def create_temp_directory
17
- temp = if settings[:temp]
18
- settings[:temp]
19
- else
20
+ def ensure_temp_directory
21
+ temp = settings[:temp]
22
+ return temp if temp.kind_of?(String) && File.directory?(temp)
23
+ unless temp
20
24
  require 'tmpdir'
21
- File.join(Dir.tmpdir, 'RackPipeline')
25
+ temp = File.join(Dir.tmpdir, 'RackPipeline')
22
26
  end
23
27
  FileUtils.mkdir_p temp
24
28
  settings[:temp] = temp
25
29
  end
26
30
 
27
31
  def cleanup_cache(target)
28
- FileUtils.rm Dir.glob(File.join(settings[:temp], File.basename(target)) + '.*')
32
+ @busted = true
33
+ FileUtils.rm Dir.glob(File.join(settings[:temp], target))
29
34
  end
30
35
 
31
36
  def calculate_hash(sources)
32
- Digest::MD5.hexdigest(Array(sources).inject(''){ |all,(file,_)| all << "#{file}:#{File.mtime(file).to_i}" })
37
+ Digest::MD5.hexdigest(Array(sources).inject(''){ |all,(file,_)| all << file << File.mtime(file).to_i.to_s })
33
38
  end
34
39
  end
35
40
  end
@@ -1,3 +1,5 @@
1
+ require 'fileutils'
2
+
1
3
  module RackPipeline
2
4
  module Compressing
3
5
  def self.process(source, target)
@@ -7,7 +9,8 @@ module RackPipeline
7
9
  Compressing.const_get(compressor[0]).process(source, target)
8
10
  else
9
11
  warn "no compressor found for #{source}"
10
- source
12
+ FileUtils.cp source, target
13
+ target
11
14
  end
12
15
  end
13
16
 
@@ -5,7 +5,7 @@ module RackPipeline
5
5
  def combine(sources, target)
6
6
  cache_target(sources, target) do |target_path|
7
7
  body = sources.inject('') do |all,(source,kind)|
8
- all << "/* #{source} */\n\n" + File.read(prepare_file(source, static_type(target))).encode('utf-8') + "\n\n"
8
+ all << "/*!\n * #{source}\n */\n\n" + File.read(prepare_file(source, static_type(target))).encode('utf-8') + "\n\n"
9
9
  end
10
10
  File.write(target_path, body)
11
11
  target_path
@@ -6,13 +6,13 @@ module RackPipeline
6
6
  end
7
7
 
8
8
  module Helpers
9
- def pipeline(pipes = [ :app ], types = [ :css, :js ], opts = {})
9
+ def pipeline(pipes = [ :app ], types = [ :css, :js ], options = {})
10
10
  bust_cache = respond_to?(:settings) && settings.respond_to?(:pipeline) && settings.pipeline[:bust_cache]
11
- pipeline_object = env['rack-pipeline']
11
+ @pipeline_object = env['rack-pipeline']
12
12
  Array(types).map do |type|
13
- assets = pipeline_object.assets_for(pipes, type, opts)
13
+ assets = @pipeline_object.assets_for(pipes, type, options)
14
14
  assets.map do |asset|
15
- pipe_tag(type, asset, bust_cache)
15
+ pipe_tag(type, asset + options[:postfix].to_s, bust_cache)
16
16
  end.join("\n")
17
17
  end.join("\n")
18
18
  end
@@ -28,20 +28,18 @@ module RackPipeline
28
28
  end
29
29
 
30
30
  def cache_buster(file)
31
- if File.file?(file)
31
+ compress = respond_to?(:settings) && settings.respond_to?(:pipeline) && settings.pipeline[:compress]
32
+ if !compress && File.file?(file)
32
33
  "?#{File.mtime(file).to_i}"
33
34
  else
34
- temp = if respond_to?(:settings) && settings.respond_to?(:pipeline) && settings.pipeline[:temp]
35
- settings.pipeline[:temp]
36
- else
37
- require 'tmpdir'
38
- File.join(Dir.tmpdir, 'RackPipeline')
35
+ temp = @pipeline_object.ensure_temp_directory
36
+ max_mtime = 0
37
+ Dir.glob(File.join(temp, File.basename(file,'.*') << '.*' << File.extname(file))).each do |cached_file|
38
+ mtime = File.mtime(cached_file).to_i
39
+ max_mtime = mtime if mtime > max_mtime
39
40
  end
40
- mtimes = []
41
- Dir.glob(File.join(temp,file+'.*')).each do |cached_file|
42
- mtimes << File.mtime(cached_file).to_i
43
- end
44
- "?#{mtimes.max || Time.now.to_i}"
41
+ max_mtime = Time.now.to_i if max_mtime == 0
42
+ "?#{max_mtime}"
45
43
  end
46
44
  end
47
45
  end
@@ -1,3 +1,3 @@
1
1
  module RackPipeline
2
- VERSION = "0.0.10"
2
+ VERSION = "0.0.11"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-pipeline
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Bochkariov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-09 00:00:00.000000000 Z
11
+ date: 2014-04-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler