-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathRakefile
More file actions
125 lines (99 loc) · 2.48 KB
/
Rakefile
File metadata and controls
125 lines (99 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
require 'rubygems'
require 'bundler'
Bundler.require
require 'digest/sha1'
task :deploy do
SimpleS3Deploy.deploy('public')
end
module SimpleS3Deploy
def self.deploy(site_path)
Site.new(site_path).deploy
end
class Site
attr_accessor :path
attr_accessor :tmp_files
def initialize(path)
@path = path
@tmp_files = []
end
def deploy
puts " ** Deploying #{path} to #{bucket.key}"
puts " ========================================================"
puts " ** Deleting existing remote files"
clear_bucket
puts " ** Uploading files"
files.each do |file|
if !File.directory?(file.path)
remote_file_name = file_base_path(file.path)
puts " Uploading #{remote_file_name} .."
bucket.files.create(
key: remote_file_name,
body: file.is_minifyable? ? open(minify(file.path)) : open(file.path),
public: true,
content_type: file.mime_type,
cache_control: 'max-age=604800, public' )
end
end
if tmp_files.any?
puts " ** Cleaning up tmp files"
cleanup
end
puts "\n ** Done"
end
private
def clear_bucket
bucket.files.each do |f|
puts " Deleting #{f.key}"
f.destroy
end
end
def cleanup
tmp_files.each do |file|
puts " Deleting #{file}"
File.unlink(file)
end
end
def files
@files ||= Dir.glob("#{path}/**/*").map { |f| SiteFile.new(f) }
end
def file_base_path(file)
file.gsub("#{path}/", "")
end
def minify(file_path)
"#{file_path}-tmp".tap do |tmp_file_name|
`yuicompressor -o #{tmp_file_name} #{file_path}`
tmp_files << tmp_file_name
end
end
def config
@config ||= YAML.load_file('config.yaml')
end
def s3
@s3 ||= Fog::Storage.new({
provider: 'AWS',
aws_secret_access_key: config['s3']['secret_access_key'],
aws_access_key_id: config['s3']['access_key_id'] })
end
def bucket
@bucket ||= s3.directories.get(config['s3']['bucket'])
end
end
class SiteFile
attr_accessor :path
def initialize(_path)
@path = _path
end
def is_minifyable?
is_css_file? or is_js_file?
end
def is_css_file?
path.end_with?('.css')
end
def is_js_file?
path.end_with?('.js')
end
def mime_type
MIME::Types.type_for(path)[0].to_s
end
end
end