forked from harvesthq/chosen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cakefile
188 lines (164 loc) · 5.8 KB
/
Cakefile
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
# Building Chosen requires coffee-script and uglify-js. For
# help installing, try:
#
# `npm -g install coffee-script uglify-js`
#
fs = require 'fs'
path = require 'path'
{spawn, exec} = require 'child_process'
CoffeeScript = require 'coffee-script'
{parser, uglify} = require 'uglify-js'
javascripts = {
'chosen/chosen.jquery.js': [
'coffee/lib/select-parser.coffee'
'coffee/lib/abstract-chosen.coffee'
'coffee/chosen.jquery.coffee'
]
'chosen/chosen.proto.js': [
'coffee/lib/select-parser.coffee'
'coffee/lib/abstract-chosen.coffee'
'coffee/chosen.proto.coffee'
]
}
Array::unique = ->
output = {}
output[@[key]] = @[key] for key in [0...@length]
value for key, value of output
# Gather a list of unique source files.
#
source_files = ->
all_sources = []
for javascript, sources of javascripts
for source in sources
all_sources.push source
all_sources.unique()
# Get the version number
#
version = ->
"#{fs.readFileSync('VERSION')}".replace /[^0-9a-zA-Z.]*/gm, ''
version_tag = ->
"v#{version()}"
# Write chosen files with a header
#
write_chosen_javascript = (filename, body, trailing='') ->
fs.writeFileSync filename, """
// Chosen, a Select Box Enhancer for jQuery and Protoype
// by Patrick Filler for Harvest, http://getharvest.com
//
// Version #{version()}
// Full source at https://github.com/harvesthq/chosen
// Copyright (c) 2011 Harvest http://getharvest.com
// MIT License, https://github.com/harvesthq/chosen/blob/master/LICENSE.md
// This file is generated by `cake build`, do not edit it by hand.
#{body}#{trailing}
"""
console.log "Wrote #{filename}"
# Build Chosen.
#
task 'build', 'build Chosen from source', build = (cb) ->
file_name = null; file_contents = null
try
for javascript, sources of javascripts
code = ''
for source in sources
file_name = source
file_contents = "#{fs.readFileSync source}"
code += CoffeeScript.compile file_contents
write_chosen_javascript javascript, code
unless process.env.MINIFY is 'false'
write_chosen_javascript javascript.replace(/\.js$/,'.min.js'), (
uglify.gen_code uglify.ast_squeeze uglify.ast_mangle parser.parse code
), ';'
package_npm () ->
package_jquery () ->
cb() if typeof cb is 'function'
catch e
print_error e, file_name, file_contents
task 'watch', 'watch coffee/ for changes and build Chosen', ->
console.log "Watching for changes in coffee/"
for file in source_files()
# Coffeescript wasn't scoping file correctly-
# without this closure the file name displayed
# is incorrect.
((file) ->
fs.watchFile file, (curr, prev) ->
if +curr.mtime isnt +prev.mtime
console.log "Saw change in #{file}"
invoke 'build'
)(file)
task 'package_npm', 'generate the package.json file for npm', package_npm = (cb) ->
try
package_file = 'package.json'
package_obj = JSON.parse("#{fs.readFileSync package_file}")
package_obj['version'] = version()
fs.writeFileSync package_file, JSON.stringify(package_obj, null, 2)
console.log "Wrote #{package_file}"
cb() if typeof cb is 'function'
catch e
print_error e, package_file
task 'package_jquery', 'generate the chosen.jquery.json file for the jQuery plugin website', package_jquery = (cb) ->
try
package_file = 'chosen.jquery.json'
package_obj = JSON.parse("#{fs.readFileSync package_file}")
package_obj['version'] = version()
fs.writeFileSync package_file, JSON.stringify(package_obj, null, 2)
console.log "Wrote #{package_file}"
cb() if typeof cb is 'function'
catch e
print_error e, package_file
run = (cmd, args, cb, err_cb) ->
exec "#{cmd} #{args.join(' ')}", (err, stdout, stderr) ->
if err isnt null
console.error stderr
if typeof err_cb is 'function'
err_cb()
else
throw "Failed command execution (#{err})."
else
cb(stdout) if typeof cb is 'function'
with_clean_repo = (cb) ->
run 'git', ['diff', '--exit-code'], cb, ->
throw 'There are files that need to be committed first.'
without_existing_tag = (cb) ->
run 'git', ['tag'], (stdout) ->
if stdout.split("\n").indexOf( version_tag() ) >= 0
throw 'This tag has already been committed to the repo.'
else
cb()
tag_release = (cb, cb_err) ->
run 'git', ['tag', '-a', '-m', "\"Version #{version()}\"", version_tag()], cb, cb_err
untag_release = (e) ->
console.log "Failure to tag caught: #{e}"
console.log "Removing tag #{version_tag()}"
run 'git', ['tag', '-d', version_tag()]
push_repo = (args=[], cb, cb_err) ->
run 'git', ['push'].concat(args), cb, cb_err
print_error = (error, file_name, file_contents) ->
line = error.message.match /line ([0-9]+):/
if line && line[1] && line = parseInt(line[1])
contents_lines = file_contents.split "\n"
first = if line-4 < 0 then 0 else line-4
last = if line+3 > contents_lines.size then contents_lines.size else line+3
console.log "Error compiling #{file_name}. \"#{error.message}\"\n"
index = 0
for line in contents_lines[first...last]
index++
line_number = first + 1 + index
console.log "#{(' ' for [0..(3-(line_number.toString().length))]).join('')} #{line}"
else
console.log """
Error compiling #{file_name}:
#{error.message}
"""
task 'release', 'build, tag the current release, and push', ->
console.log "Trying to tag #{version_tag()}..."
with_clean_repo ->
without_existing_tag ->
build ->
tag_release ->
push_repo [], ->
push_repo ['--tags'], ->
console.log "Successfully tagged #{version_tag()}: https://github.com/harvesthq/chosen/tree/#{version_tag()}"
, untag_release ('push repo with tags')
, untag_release ('push repo')
, untag_release ('tag release')