forked from emaiax/jquery-raty-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
212 lines (177 loc) · 4.96 KB
/
Rakefile
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
#!/usr/bin/env rake
begin
require 'bundler/setup'
rescue LoadError
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
end
begin
require 'rdoc/task'
rescue LoadError
require 'rdoc/rdoc'
require 'rake/rdoctask'
RDoc::Task = Rake::RDocTask
end
RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'JqueryRatyRails'
rdoc.options << '--line-numbers'
rdoc.rdoc_files.include('README.rdoc')
rdoc.rdoc_files.include('lib/**/*.rb')
end
Bundler::GemHelper.install_tasks
require 'rake/testtask'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = false
end
task :default => :test
# ---------------------------------------------------------------------------
# Local additions
# ---------------------------------------------------------------------------
require 'fileutils'
require 'pathname'
include FileUtils
ROOT = Pathname(File.dirname(__FILE__))
DOWNLOAD_DIR = ROOT + "tmp/download"
ASSETS = ROOT + 'vendor/assets'
PACKAGE = "jquery-raty-rails"
GEMSPEC = "#{PACKAGE}.gemspec"
def load_gem
eval File.open(GEMSPEC).read
end
def gem_name
gem = load_gem
version = gem.version.to_s
"#{PACKAGE}-#{version}.gem"
end
# Tasks
desc "Build the gem"
task :gem => gem_name
desc "Build the gem"
file gem_name do
require 'rubygems/builder'
raise "Gem package not defined" unless defined? Gem
spec = eval File.open(GEMSPEC).read
Gem::Builder.new(spec).build
end
desc "Publish the gem"
task :publish => :gem do
sh "gem push #{gem_name}"
end
desc "Update to the most recently released version of jQuery Raty"
task :update => [:download_raty, :copy_raty] do
rm_rf "tmp"
end
desc "Download and unpack the latest version of jquery-raty"
task :download_raty do
require 'octokit'
require 'time'
gh = Octokit::Client.new
newest_time = 0
newest = nil
download = gh.downloads('wbotelhos/raty').select do |dl|
dl["content_type"] == "application/zip"
end.map do |dl|
dl["timestamp"] = Time.iso8601 dl["created_at"]
dl
end.sort do |dl1, dl2|
dl1["timestamp"].to_i <=> dl2["timestamp"].to_i
end.last
raise "Can't find suitable download" if download.nil?
rm_rf DOWNLOAD_DIR
mkdir_p DOWNLOAD_DIR
url = download["html_url"]
uri = URI(url)
filename = File.basename(uri.path)
local_path = DOWNLOAD_DIR + filename
download(uri, local_path)
unzip local_path
end
desc "Copy the jquery-raty assets in place"
task :copy_raty do
%w[javascripts images/jquery.raty].each do |d|
mkdir_p ASSETS + d
end
Dir.glob(DOWNLOAD_DIR + 'img/*') do |f|
cp(f, ASSETS + "images/jquery.raty/")
end
js_file = DOWNLOAD_DIR + 'js/jquery.raty.js'
puts "Editing and copying #{js_file}"
js = File.open(js_file).readlines.map do |line|
case line
when /^(\s+path\s+:)\s'([^']+)'(.*)$/
line = "#{$1}'/assets/jquery.raty/'#{$3}\n"
end
line.gsub(/\r/, "").gsub(%r{^/\*!}, "/**")
end
full_js = ASSETS + "javascripts/jquery.raty.js"
File.open(full_js, "wb") { |f| f.write(js.join('')) }
min_js = ASSETS + "javascripts/jquery.raty.min.js"
minify full_js, min_js
end
# Download a URL, handling redirects and SSL.
#
# Parameters:
#
# uri - URI object
# dest - destination path
def download(uri, dest)
require 'net/http'
# Would use open-uri, as shown, but it pukes on the GitHub redirection
# from an https URL to a cloud http URL.
#
# open(local_path, 'wb') do |file|
# file << open(url).read
# end
keep_going = true
while keep_going
use_ssl = uri.scheme == 'https'
Net::HTTP.start(uri.host, uri.port, use_ssl: use_ssl) do |http|
request = Net::HTTP::Get.new uri.request_uri
puts "Downloading #{uri.to_s}"
http.request request do |response|
if response.is_a? Net::HTTPSuccess
File.open(dest, 'wb') do |f|
response.read_body do |chunk|
f.write chunk
end
end
keep_going = false
elsif (response.code[0] == '3') && response['location']
# Redirect.
puts "(Redirected to #{response['location']}"
uri = URI(response['location'])
else
raise "Download failed: #{response.code} #{response.message}"
end
end
end
end
end
def unzip(path)
require 'zip/zip'
puts "Unpacking #{path}"
Zip::ZipFile.open(path) do |zip_file|
zip_file.each do |entry|
dest = File.join(DOWNLOAD_DIR, entry.name)
mkdir_p File.dirname(dest), verbose: false
zip_file.extract(entry, dest)
end
end
end
# Minify JS. Currently expects Google Closure to be installed.
def minify(input_file, output_file)
#require 'uglifier'
require 'tempfile'
puts "Minifying #{input_file} to #{output_file}."
Tempfile.open('jquery-raty') do |temp|
sh 'closure', input_file.to_s, temp.path
temp.rewind
File.open(output_file, 'wb') do |out|
out.puts "/* Minified by Google Closure */"
out.write(temp.read)
end
end
end