middleman-s3_sync 3.0.9 → 3.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,92 +14,49 @@ module Middleman
14
14
  module S3Sync
15
15
  class << self
16
16
  def sync
17
- puts "Gathering local files."
18
-
19
- local_files = (Dir[options.build_dir + "/**/*"] + Dir[options.build_dir + "/**/.*"])
20
- .reject { |f| File.directory?(f) }
21
- .map { |f| f.gsub(/^#{options.build_dir}\//, '') }
22
- puts "Gathering remote files from #{options.bucket}"
23
- remote_files = bucket.files.map { |f| f.key }
24
-
25
- if options.force
26
- files_to_push = local_files
27
- else
28
- # First pass on the set of files to work with.
29
- puts "Determine files to add to #{options.bucket}."
30
- files_to_push = local_files - remote_files
31
- if options.delete
32
- puts "Determine which files to delete from #{options.bucket}"
33
- files_to_delete = remote_files - local_files
34
- end
35
- files_to_evaluate = local_files - files_to_push
36
-
37
- # No need to evaluate the files that are newer on S3 than the local files.
38
- puts "Determine which local files are newer than their S3 counterparts"
39
- files_to_reject = []
40
- Parallel.each(files_to_evaluate, :in_threads => 4) do |f|
41
- print '.'
42
- local_mtime = File.mtime("#{options.build_dir}/#{f}")
43
- remote_mtime = s3_files.get(f).last_modified
44
- files_to_reject << f if remote_mtime >= local_mtime
45
- end
17
+ if files_to_create.empty? && files_to_update.empty? && files_to_delete.empty?
18
+ puts "\nAll S3 files are up to date."
19
+ return
20
+ end
46
21
 
47
- files_to_evaluate = files_to_evaluate - files_to_reject
48
-
49
- # Are the files different? Use MD5 to see
50
- if (files_to_evaluate.size > 0)
51
- puts "\n\nDetermine which remaining files are actually different than their S3 counterpart."
52
- Parallel.each(files_to_evaluate, :in_threads => 4) do |f|
53
- print '.'
54
- local_md5 = Digest::MD5.hexdigest(File.read("#{options.build_dir}/#{f}"))
55
- remote_md5 = s3_files.get(f).etag
56
- files_to_push << f if local_md5 != remote_md5
57
- end
22
+ puts "\nReady to apply updates to #{options.bucket}."
23
+
24
+ files_to_create.each do |f|
25
+ puts "Creating #{f}"
26
+ file_hash = {
27
+ :key => f,
28
+ :body => File.open(local_path(f)),
29
+ :public => true,
30
+ :acl => 'public-read',
31
+ :content_type => MIME::Types.of(f).first
32
+ }
33
+
34
+ # Add cache-control headers
35
+ if policy = options.caching_policy_for(file_hash[:content_type])
36
+ file_hash[:cache_control] = policy.cache_control if policy.cache_control
37
+ file_hash[:expires] = policy.expires if policy.expires
58
38
  end
39
+
40
+ bucket.files.create(file_hash)
59
41
  end
60
42
 
61
- if files_to_push.size > 0
62
- puts "\n\nReady to apply updates to #{options.bucket}."
63
- files_to_push.each do |f|
64
- if remote_files.include?(f)
65
- puts "Updating #{f}"
66
- file = s3_files.get(f)
67
- file.body = File.open("#{options.build_dir}/#{f}")
68
- file.public = true
69
- file.content_type = MIME::Types.of(f).first
70
- if policy = options.caching_policy_for(file.content_type)
71
- file.cache_control = policy.cache_control if policy.cache_control
72
- file.expires = policy.expires if policy.expires
73
- end
74
-
75
- file.save
76
- else
77
- puts "Creating #{f}"
78
- file_hash = {
79
- :key => f,
80
- :body => File.open("#{options.build_dir}/#{f}"),
81
- :public => true,
82
- :acl => 'public-read',
83
- :content_type => MIME::Types.of(f).first
84
- }
85
-
86
- # Add cache-control headers
87
- if policy = options.caching_policy_for(file_hash[:content_type])
88
- file_hash[:cache_control] = policy.cache_control if policy.cache_control
89
- file_hash[:expires] = policy.expires if policy.expires
90
- end
91
-
92
- file = bucket.files.create(file_hash)
93
- end
43
+ files_to_update.each do |f|
44
+ puts "Updating #{f}"
45
+ file = s3_files.get(f)
46
+ file.body = File.open(local_path(f))
47
+ file.public = true
48
+ file.content_type = MIME::Types.of(f).first
49
+ if policy = options.caching_policy_for(file.content_type)
50
+ file.cache_control = policy.cache_control if policy.cache_control
51
+ file.expires = policy.expires if policy.expires
94
52
  end
95
- else
96
- puts "\n\nNo files to update."
53
+
54
+ file.save
97
55
  end
98
56
 
99
- if options.delete
100
- files_to_delete.each do |f|
101
- puts "Deleting #{f}"
102
- file = s3_files.get(f)
57
+ files_to_delete.each do |f|
58
+ puts "Deleting #{f}"
59
+ if file = s3_files.get(f)
103
60
  file.destroy
104
61
  end
105
62
  end
@@ -122,6 +79,75 @@ module Middleman
122
79
  def s3_files
123
80
  @s3_files ||= bucket.files
124
81
  end
82
+
83
+ def remote_files
84
+ @remote_files ||= begin
85
+ puts "Gathering remote files from #{options.bucket}"
86
+ bucket.files.map { |f| f.key }
87
+ end
88
+ end
89
+ def local_files
90
+ @local_files ||= begin
91
+ puts "Gathering local files."
92
+ (Dir[build_dir + "/**/*"] + Dir[build_dir + "/**/.*"])
93
+ .reject { |f| File.directory?(f) }
94
+ .map { |f| f.gsub(/^#{build_dir}\//, '') }
95
+ end
96
+ end
97
+
98
+ def files_to_delete
99
+ @files_to_delete ||= begin
100
+ if options.delete
101
+ puts "\nDetermine which files to delete from #{options.bucket}"
102
+ remote_files - local_files
103
+ else
104
+ []
105
+ end
106
+ end
107
+ end
108
+
109
+ def files_to_create
110
+ @files_to_create ||= begin
111
+ puts "Determine files to add to #{options.bucket}."
112
+ local_files - remote_files
113
+ end
114
+ end
115
+
116
+ def files_to_evaluate
117
+ @files_to_evaluate ||= begin
118
+ local_files - files_to_create
119
+ end
120
+ end
121
+
122
+ def files_to_update
123
+ return files_to_evaluate if options.force
124
+
125
+ @files_to_update ||= begin
126
+ puts "Determine which local files to update their S3 counterparts"
127
+ files_to_update = []
128
+ Parallel.each(files_to_evaluate, :in_threads => 4) do |f|
129
+ print '.'
130
+ remote_file = s3_files.get(f)
131
+ local_mtime = File.mtime(local_path(f))
132
+ remote_mtime = remote_file.last_modified
133
+ if remote_mtime < local_mtime
134
+ local_md5 = Digest::MD5.hexdigest(File.read(local_path(f)))
135
+ remote_md5 = remote_file.etag
136
+ files_to_update << f if local_md5 != remote_md5
137
+ end
138
+ end
139
+ puts ""
140
+ files_to_update
141
+ end
142
+ end
143
+
144
+ def local_path(f)
145
+ "#{build_dir}/#{f}"
146
+ end
147
+
148
+ def build_dir
149
+ @build_dir ||= options.build_dir
150
+ end
125
151
  end
126
152
  end
127
153
  end
@@ -11,10 +11,14 @@ module Middleman
11
11
  true
12
12
  end
13
13
 
14
- desc "s3_sync", "Builds and push the minimum set of files needed to S3"
14
+ desc "s3_sync", "Pushes the minimum set of files needed to S3"
15
15
  option :force, type: :boolean,
16
16
  desc: "Push all local files to the server",
17
17
  aliases: :f
18
+ option :bucket, type: :string,
19
+ desc: "Specify which bucket to use, overrides the configured bucket.",
20
+ aliases: :b
21
+
18
22
  def s3_sync
19
23
  shared_inst = ::Middleman::Application.server.inst
20
24
  bucket = shared_inst.options.bucket
@@ -23,6 +27,7 @@ module Middleman
23
27
  end
24
28
 
25
29
  shared_inst.options.force = options[:force] if options[:force]
30
+ shared_inst.options.bucket = options[:bucket] if options[:bucket]
26
31
 
27
32
  ::Middleman::S3Sync.sync
28
33
  end
@@ -1,5 +1,5 @@
1
1
  module Middleman
2
2
  module S3Sync
3
- VERSION = "3.0.9"
3
+ VERSION = "3.0.10"
4
4
  end
5
5
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: middleman-s3_sync
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 3.0.9
5
+ version: 3.0.10
6
6
  platform: ruby
7
7
  authors:
8
8
  - Frederic Jean
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-04-03 00:00:00.000000000 Z
13
+ date: 2013-04-17 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  version_requirements: !ruby/object:Gem::Requirement
@@ -155,7 +155,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
155
155
  - !ruby/object:Gem::Version
156
156
  segments:
157
157
  - 0
158
- hash: 1248558476700600034
158
+ hash: 2846065098887891118
159
159
  version: '0'
160
160
  required_rubygems_version: !ruby/object:Gem::Requirement
161
161
  none: false
@@ -164,7 +164,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
164
164
  - !ruby/object:Gem::Version
165
165
  segments:
166
166
  - 0
167
- hash: 1248558476700600034
167
+ hash: 2846065098887891118
168
168
  version: '0'
169
169
  requirements: []
170
170
  rubyforge_project: