forked from quirkey/sammy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRakefile
176 lines (145 loc) · 4.91 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
desc 'Pulls the current version from lib/sammy.js'
task :version do
f = File.read('lib/sammy.js')
@version = f.match(/Sammy.VERSION \= \'([\d\w\.]+)\'/)[1]
puts "VERSION: " + @version
end
desc 'Uses the yui-compressor to minify lib/sammy.js'
task :minify => :version do
yui_path = ENV['YUI_PATH'] || '~/Sites/yui/yuicompressor-2.4.2.jar'
java_path = ENV['JAVA_PATH'] || '/usr/bin/java'
puts "Minify-ing"
# compress each file
Dir['lib/**/*.js'].each do |path|
if path =~ /\.min\.js$/
File.unlink(path)
next
end
path.gsub!('lib','')
dir = 'lib/min'
min_path = File.join(dir, path.gsub(/\.js$/, "-#{@version}.min.js"))
latest_min_path = File.join(dir, path.gsub(/\.js$/, "-latest.min.js"))
sh "#{java_path} -jar #{yui_path} -o #{min_path} lib/#{path}"
minified = File.read(min_path)
prefix = []
prefix << "// -- Sammy -- #{path}"
prefix << "// http://code.quirkey.com/sammy"
prefix << "// Version: #{@version}"
prefix << "// Built: #{Time.now}"
File.open(min_path, 'w') do |f|
f << prefix.join("\n") << "\n"
f << minified
end
FileUtils.copy(min_path, latest_min_path)
end
end
# Modified from peterc: http://gist.github.com/113226
desc "Automatically run something when code is changed"
task :autotest do
require 'find'
files = {}
test_path = ENV['TEST'] || File.join(File.dirname(__FILE__), 'test', 'index.html')
loop do
changed = false
Find.find(File.dirname(__FILE__)) do |file|
next unless file =~ /\.js$/
ctime = File.ctime(file).to_i
if ctime != files[file]
files[file] = ctime
changed = true
end
end
if changed
puts "Running #{test_path} at #{Time.now}"
system "open #{test_path}"
puts "\nWaiting for a *.js change"
end
sleep 1
end
end
desc 'launch the test file in the browser'
task :test do
system "open #{File.join(File.dirname(__FILE__), 'test', 'index.html')}"
end
desc 'generate the API documentation'
task :api do
tmp_doc_path = '/tmp/sammy.api.html'
api_template_path = 'site/docs/api_template.html'
final_path = 'site/docs/api.html'
File.unlink(tmp_doc_path) if File.readable?(tmp_doc_path)
sh "ruby vendor/jsdoc/jsdoc.rb lib/sammy.js lib/plugins/ > #{tmp_doc_path}"
sh "cat #{api_template_path} #{tmp_doc_path} > #{final_path}"
end
desc 'copy files into the site branch'
task :copy_test_and_examples do
sh "mkdir -p site/examples site/test site/lib site/vendor"
sh "cp -r examples/* site/examples/"
sh "cp -r test/* site/test/"
sh "cp -r lib/* site/lib/"
sh "cp -r vendor/* site/vendor/"
end
desc 'update the current version # in the pages'
task :update_version => :version do
Dir['site/**/*.*'].each do |file|
File.open(file, 'r+') do |f|
contents = f.read
contents.gsub!(/current_version\: ([\w\d\.]+)/, "current_version: #{@version}")
f.truncate(0)
f.rewind
f << contents
end
end
end
task :push_site do
sh "cd site && git add ."
sh "cd site && git commit -am 'Updated Site via Rake'"
sh "cd site && git push upstream gh-pages"
end
desc 'Build the site'
task :build_site => [:minify, :api, :copy_test_and_examples, :update_version]
desc 'Build the site, then push it to github'
task :site => [:build_site, :push_site]
desc "generate a simple sammy app structure at DIR"
task :generate do
dir = ENV['DIR']
name = File.basename(dir)
puts "Generating an app #{name} at #{dir}"
sammy_root = File.expand_path(File.dirname(__FILE__))
include FileUtils
path = File.expand_path(dir)
mkdir_p(path)
mkdir_p(File.join(path, 'javascripts'))
mkdir_p(File.join(path, 'stylesheets'))
mkdir_p(File.join(path, 'images'))
cp_r(File.join(sammy_root, 'lib'), File.join(path, 'javascripts', 'sammy'))
cp_r(File.join(sammy_root, 'vendor', 'jquery-1.3.2.js'), File.join(path, 'javascripts', 'jquery-1.3.2.js'))
index = <<-EOT
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>#{name}</title>
<link rel="stylesheet" href="/stylesheets/styles.css" type="text/css" media="screen" charset="utf-8"/>
<script src="javascripts/jquery-1.3.2.js" type="text/javascript" charset="utf-8"></script>
<script src="javascripts/sammy/sammy.js" type="text/javascript" charset="utf-8"></script>
<script src="javascripts/#{name}.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
</body>
</html>
EOT
js = <<-EOT
(function($) {
var app = $.sammy(function() {
});
$(function() {
app.run();
});
})(jQuery);
EOT
touch(File.join(path, 'stylesheets', 'style.css'))
File.open(File.join(path, 'javascripts', "#{name}.js"), 'w') {|f| f << js }
File.open(File.join(path, "index.html"), 'w') {|f| f << index }
puts "Done."
end