Skip to content
Newer
Older
100644 177 lines (153 sloc) 5.27 KB
6f0f0bd @mixonic Use npm modules for the build chain so we can concat multiple files f…
mixonic authored Jul 29, 2011
1 # Building Chosen requires coffee-script and uglify-js. For
2 # help installing, try:
3 #
4 # `npm -g install coffee-script uglify-js`
5 #
761d204 @mixonic Add cake for build. Run . Requires a coffee executable and uglifyjs e…
mixonic authored Jul 29, 2011
6 fs = require 'fs'
7 path = require 'path'
8 {spawn, exec} = require 'child_process'
6f0f0bd @mixonic Use npm modules for the build chain so we can concat multiple files f…
mixonic authored Jul 29, 2011
9 CoffeeScript = require 'coffee-script'
10 {parser, uglify} = require 'uglify-js'
761d204 @mixonic Add cake for build. Run . Requires a coffee executable and uglifyjs e…
mixonic authored Jul 29, 2011
11
6f0f0bd @mixonic Use npm modules for the build chain so we can concat multiple files f…
mixonic authored Jul 29, 2011
12 javascripts = {
13 'chosen/chosen.jquery.js': [
75b7546 @pfiller Move the location of select parser and update the build file. Move it…
pfiller authored Jul 31, 2011
14 'coffee/lib/select-parser.coffee'
4e46366 @mixonic Introduce abstract-chosen, try to start sharing more code between pla…
mixonic authored Aug 19, 2011
15 'coffee/lib/abstract-chosen.coffee'
16 'coffee/chosen.jquery.coffee'
6f0f0bd @mixonic Use npm modules for the build chain so we can concat multiple files f…
mixonic authored Jul 29, 2011
17 ]
18 'chosen/chosen.proto.js': [
75b7546 @pfiller Move the location of select parser and update the build file. Move it…
pfiller authored Jul 31, 2011
19 'coffee/lib/select-parser.coffee'
4e46366 @mixonic Introduce abstract-chosen, try to start sharing more code between pla…
mixonic authored Aug 19, 2011
20 'coffee/lib/abstract-chosen.coffee'
21 'coffee/chosen.proto.coffee'
6f0f0bd @mixonic Use npm modules for the build chain so we can concat multiple files f…
mixonic authored Jul 29, 2011
22 ]
23 }
761d204 @mixonic Add cake for build. Run . Requires a coffee executable and uglifyjs e…
mixonic authored Jul 29, 2011
24
6f0f0bd @mixonic Use npm modules for the build chain so we can concat multiple files f…
mixonic authored Jul 29, 2011
25 Array::unique = ->
26 output = {}
27 output[@[key]] = @[key] for key in [0...@length]
28 value for key, value of output
761d204 @mixonic Add cake for build. Run . Requires a coffee executable and uglifyjs e…
mixonic authored Jul 29, 2011
29
6f0f0bd @mixonic Use npm modules for the build chain so we can concat multiple files f…
mixonic authored Jul 29, 2011
30 # Gather a list of unique source files.
31 #
32 source_files = ->
33 all_sources = []
34 for javascript, sources of javascripts
35 for source in sources
36 all_sources.push source
37 all_sources.unique()
f444fb9 @mixonic Add a `cake watch` task
mixonic authored Jul 29, 2011
38
9e1e84f @mixonic Add a version file and header to generated javascripts
mixonic authored Jul 29, 2011
39 # Get the version number
40 #
41 version = ->
42 "#{fs.readFileSync('VERSION')}".replace /[^0-9a-zA-Z.]*/gm, ''
43
62e2c75 @mixonic Add a release task to cake
mixonic authored Jul 29, 2011
44 version_tag = ->
45 "v#{version()}"
46
9e1e84f @mixonic Add a version file and header to generated javascripts
mixonic authored Jul 29, 2011
47 # Write chosen files with a header
48 #
7b382d1 @pfiller Add a semicolon to minified build process.
pfiller authored Apr 19, 2012
49 write_chosen_javascript = (filename, body, trailing='') ->
9e1e84f @mixonic Add a version file and header to generated javascripts
mixonic authored Jul 29, 2011
50 fs.writeFileSync filename, """
387177a @pfiller Update title / copyright details.
pfiller authored Jul 31, 2011
51 // Chosen, a Select Box Enhancer for jQuery and Protoype
52 // by Patrick Filler for Harvest, http://getharvest.com
53 //
54 // Version #{version()}
55 // Full source at https://github.com/harvesthq/chosen
9e1e84f @mixonic Add a version file and header to generated javascripts
mixonic authored Jul 29, 2011
56 // Copyright (c) 2011 Harvest http://getharvest.com
387177a @pfiller Update title / copyright details.
pfiller authored Jul 31, 2011
57
9e1e84f @mixonic Add a version file and header to generated javascripts
mixonic authored Jul 29, 2011
58 // MIT License, https://github.com/harvesthq/chosen/blob/master/LICENSE.md
59 // This file is generated by `cake build`, do not edit it by hand.
7b382d1 @pfiller Add a semicolon to minified build process.
pfiller authored Apr 19, 2012
60 #{body}#{trailing}
9e1e84f @mixonic Add a version file and header to generated javascripts
mixonic authored Jul 29, 2011
61 """
df63454 @mixonic Better report compilation errors during build
mixonic authored Aug 19, 2011
62 console.log "Wrote #{filename}"
9e1e84f @mixonic Add a version file and header to generated javascripts
mixonic authored Jul 29, 2011
63
6f0f0bd @mixonic Use npm modules for the build chain so we can concat multiple files f…
mixonic authored Jul 29, 2011
64 # Build Chosen.
761d204 @mixonic Add cake for build. Run . Requires a coffee executable and uglifyjs e…
mixonic authored Jul 29, 2011
65 #
66 task 'build', 'build Chosen from source', build = (cb) ->
df63454 @mixonic Better report compilation errors during build
mixonic authored Aug 19, 2011
67 file_name = null; file_contents = null
68 try
69 for javascript, sources of javascripts
70 code = ''
71 for source in sources
72 file_name = source
73 file_contents = "#{fs.readFileSync source}"
74 code += CoffeeScript.compile file_contents
75 write_chosen_javascript javascript, code
76 unless process.env.MINIFY is 'false'
77 write_chosen_javascript javascript.replace(/\.js$/,'.min.js'), (
78 uglify.gen_code uglify.ast_squeeze uglify.ast_mangle parser.parse code
7b382d1 @pfiller Add a semicolon to minified build process.
pfiller authored Apr 19, 2012
79 ), ';'
71c06c7 @mixonic Add a package npm task and check in a generated file
mixonic authored Jan 13, 2012
80 package_npm () ->
81 cb() if typeof cb is 'function'
df63454 @mixonic Better report compilation errors during build
mixonic authored Aug 19, 2011
82 catch e
83 print_error e, file_name, file_contents
761d204 @mixonic Add cake for build. Run . Requires a coffee executable and uglifyjs e…
mixonic authored Jul 29, 2011
84
f444fb9 @mixonic Add a `cake watch` task
mixonic authored Jul 29, 2011
85 task 'watch', 'watch coffee/ for changes and build Chosen', ->
86 console.log "Watching for changes in coffee/"
6f0f0bd @mixonic Use npm modules for the build chain so we can concat multiple files f…
mixonic authored Jul 29, 2011
87 for file in source_files()
88 # Coffeescript wasn't scoping file correctly-
89 # without this closure the file name displayed
90 # is incorrect.
91 ((file) ->
92 fs.watchFile file, (curr, prev) ->
93 if +curr.mtime isnt +prev.mtime
94 console.log "Saw change in #{file}"
95 invoke 'build'
96 )(file)
62e2c75 @mixonic Add a release task to cake
mixonic authored Jul 29, 2011
97
71c06c7 @mixonic Add a package npm task and check in a generated file
mixonic authored Jan 13, 2012
98 task 'package_npm', 'generate the package.json file for npm', package_npm = (cb) ->
99 try
100 package_file = 'package.json'
07eb743 Fixing Cakefile to not use reserved word 'package'
Dave Ingram authored Apr 16, 2012
101 package_obj = JSON.parse("#{fs.readFileSync package_file}")
102 package_obj['version'] = version()
103 fs.writeFileSync package_file, JSON.stringify(package_obj, null, 2)
71c06c7 @mixonic Add a package npm task and check in a generated file
mixonic authored Jan 13, 2012
104 console.log "Wrote #{package_file}"
105 cb() if typeof cb is 'function'
106 catch e
107 print_error e, package_file
108
62e2c75 @mixonic Add a release task to cake
mixonic authored Jul 29, 2011
109 run = (cmd, args, cb, err_cb) ->
110 exec "#{cmd} #{args.join(' ')}", (err, stdout, stderr) ->
111 if err isnt null
112 console.error stderr
113 if typeof err_cb is 'function'
114 err_cb()
115 else
116 throw "Failed command execution (#{err})."
117 else
118 cb(stdout) if typeof cb is 'function'
119
120 with_clean_repo = (cb) ->
121 run 'git', ['diff', '--exit-code'], cb, ->
122 throw 'There are files that need to be committed first.'
123
124 without_existing_tag = (cb) ->
125 run 'git', ['tag'], (stdout) ->
126 if stdout.split("\n").indexOf( version_tag() ) >= 0
127 throw 'This tag has already been committed to the repo.'
128 else
129 cb()
130
131 tag_release = (cb, cb_err) ->
132 run 'git', ['tag', '-a', '-m', "\"Version #{version()}\"", version_tag()], cb, cb_err
133
134 untag_release = (e) ->
135 console.log "Failure to tag caught: #{e}"
136 console.log "Removing tag #{version_tag()}"
137 run 'git', ['tag', '-d', version_tag()]
138
139 push_repo = (args=[], cb, cb_err) ->
140 run 'git', ['push'].concat(args), cb, cb_err
141
df63454 @mixonic Better report compilation errors during build
mixonic authored Aug 19, 2011
142 print_error = (error, file_name, file_contents) ->
143 line = error.message.match /line ([0-9]+):/
144 if line && line[1] && line = parseInt(line[1])
145 contents_lines = file_contents.split "\n"
146 first = if line-4 < 0 then 0 else line-4
147 last = if line+3 > contents_lines.size then contents_lines.size else line+3
148 console.log "Error compiling #{file_name}. \"#{error.message}\"\n"
149 index = 0
150 for line in contents_lines[first...last]
151 index++
152 line_number = first + 1 + index
153 console.log "#{(' ' for [0..(3-(line_number.toString().length))]).join('')} #{line}"
154 else
155 console.log """
156 Error compiling #{file_name}:
157
158 #{error.message}
159
160 """
161
162
163
62e2c75 @mixonic Add a release task to cake
mixonic authored Jul 29, 2011
164 task 'release', 'build, tag the current release, and push', ->
165 console.log "Trying to tag #{version_tag()}..."
166 with_clean_repo ->
167 without_existing_tag ->
168 build ->
169 tag_release ->
170 push_repo [], ->
171 push_repo ['--tags'], ->
172 console.log "Successfully tagged #{version_tag()}: https://github.com/harvesthq/chosen/tree/#{version_tag()}"
173
174 , untag_release
175 , untag_release
176 , untag_release
Something went wrong with that request. Please try again.