2 # Copyright Simon Lydell 2014.
4 # This file is part of VimFx.
6 # VimFx is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # VimFx is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
21 path = require('path')
22 gulp = require('gulp')
23 coffee = require('gulp-coffee')
24 coffeelint = require('gulp-coffeelint')
25 git = require('gulp-git')
26 header = require('gulp-header')
27 mustache = require('gulp-mustache')
28 sloc = require('gulp-sloc')
29 tap = require('gulp-tap')
30 zip = require('gulp-zip')
31 marked = require('marked')
32 merge = require('merge2')
33 precompute = require('require-precompute')
34 request = require('request')
35 rimraf = require('rimraf')
36 runSequence = require('run-sequence')
37 pkg = require('./package.json')
41 LOCALE = 'extension/locale'
42 TEST = 'extension/test'
46 test = '--test' in process.argv or '-t' in process.argv
47 ifTest = (value) -> if test then [value] else []
50 read = (filepath) -> fs.readFileSync(filepath).toString()
51 template = (data) -> mustache(data, {extension: ''})
53 gulp.task('default', ['push'])
55 gulp.task('clean', (callback) ->
56 rimraf(DEST, callback)
60 gulp.src(['extension/**/!(*.coffee|*.tmpl)', 'COPYING', 'LICENSE'])
61 .pipe(gulp.dest(DEST))
64 gulp.task('node_modules', ->
65 dependencies = (name for name of pkg.dependencies)
66 # Note! When installing or updating node modules, make sure that the following
67 # glob does not include too much or too little!
68 gulp.src("node_modules/+(#{ dependencies.join('|') })/\
69 {LICENSE*,{,**/!(test|examples)/}!(*min|*test*|*bench*).js}")
70 .pipe(gulp.dest("#{ DEST }/node_modules"))
73 gulp.task('coffee', ->
75 'extension/bootstrap.coffee'
76 'extension/lib/**/*.coffee'
77 ifTest('extension/test/**/*.coffee')...
78 ], {base: 'extension'})
79 .pipe(coffee({bare: true}))
80 .pipe(gulp.dest(DEST))
83 gulp.task('chrome.manifest', ->
84 gulp.src('extension/chrome.manifest.tmpl')
85 .pipe(template({locales: fs.readdirSync(LOCALE).map((locale) -> {locale})}))
86 .pipe(gulp.dest(DEST))
89 gulp.task('install.rdf', ->
90 [ [ { name: creator } ], developers, contributors, translators ] =
91 read('PEOPLE.md').trim().replace(/^#.+\n|^\s*-\s*/mg, '').split('\n\n')
92 .map((block) -> block.split('\n').map((name) -> {name}))
94 getDescription = (locale) -> read(join(LOCALE, locale, 'description')).trim()
96 descriptions = fs.readdirSync(LOCALE)
97 .filter((locale) -> locale != BASE_LOCALE)
98 .map((locale) -> {locale, description: getDescription(locale)})
100 gulp.src('extension/install.rdf.tmpl')
103 minVersion: pkg.firefoxVersions.min
104 maxVersion: pkg.firefoxVersions.max
105 creator, developers, contributors, translators
106 defaultDescription: getDescription(BASE_LOCALE)
109 .pipe(gulp.dest(DEST))
112 gulp.task('require-data', ['node_modules'], ->
113 data = JSON.stringify(precompute('.'), null, 2)
114 gulp.src('extension/require-data.js.tmpl')
115 .pipe(template({data}))
116 .pipe(gulp.dest(DEST))
119 gulp.task('tests-list', ->
120 list = JSON.stringify(fs.readdirSync(TEST)
121 .map((name) -> name.match(/^(test-.+)\.coffee$/)?[1])
124 gulp.src("#{ TEST }/tests-list.js.tmpl", {base: 'extension'})
125 .pipe(template({list}))
126 .pipe(gulp.dest(DEST))
129 gulp.task('templates', [
133 ifTest('tests-list')...
136 gulp.task('build', (callback) ->
139 ['copy', 'node_modules', 'coffee', 'templates'],
144 gulp.task('xpi', ['build'], ->
145 gulp.src("#{ DEST }/**/*")
146 .pipe(zip(XPI, {compress: false}))
147 .pipe(gulp.dest(DEST))
150 gulp.task('push', ['xpi'], ->
151 body = fs.readFileSync(join(DEST, XPI))
152 request.post({url: 'http://localhost:8888', body})
156 gulp.src(['extension/**/*.coffee', 'gulpfile.coffee'])
158 .pipe(coffeelint.reporter())
163 'extension/bootstrap.coffee'
164 'extension/lib/!(migrations|legacy).coffee']
169 gulp.task('release', (callback) ->
171 message = "VimFx v#{ version }"
172 today = new Date().toISOString()[...10]
174 gulp.src('package.json'),
175 gulp.src('CHANGELOG.md')
176 .pipe(header("### #{ version } (#{ today })\n\n"))
177 .pipe(gulp.dest('.'))
179 .pipe(git.commit(message))
181 git.tag("v#{ version }", message, callback)
185 gulp.task('changelog', ->
187 for arg in process.argv when /^-[1-9]$/.test(arg)
189 entries = read('CHANGELOG.md').split(/^### .+/m)[1..num].join('')
190 process.stdout.write(marked(entries))
193 gulp.task('readme', ->
194 process.stdout.write(marked(read('README.md')))
197 gulp.task('faster', ->
198 gulp.src('gulpfile.coffee')
199 .pipe(coffee({bare: true}))
200 .pipe(gulp.dest('.'))
203 gulp.task('sync-locales', ->
204 baseLocale = BASE_LOCALE
205 for arg in process.argv when arg[...2] == '--'
206 baseLocale = arg[2..]
207 results = fs.readdirSync(join(LOCALE, baseLocale))
208 .filter((file) -> path.extname(file) == '.properties')
209 .map(syncLocale.bind(null, baseLocale))
210 if baseLocale == BASE_LOCALE
212 for {fileName, translatedCount, total} in results
213 report.push("#{ fileName }:")
214 for localeName, count of translatedCount
215 paddedName = "#{ localeName }: "[...6]
216 percentage = Math.round((count / total) * 100)
217 report.push(" #{ paddedName } #{ percentage }%")
218 process.stdout.write(report.join('\n') + '\n')
221 syncLocale = (baseLocaleName, fileName) ->
222 basePath = join(LOCALE, baseLocaleName, fileName)
223 base = parseLocaleFile(read(basePath))
224 oldBasePath = "#{basePath}.old"
225 if fs.existsSync(oldBasePath)
226 oldBase = parseLocaleFile(read(oldBasePath))
228 for localeName in fs.readdirSync(LOCALE) when localeName != baseLocaleName
229 localePath = join(LOCALE, localeName, fileName)
230 locale = parseLocaleFile(read(localePath))
231 translatedCount[localeName] = 0
232 newLocale = base.template.map((line) ->
233 if Array.isArray(line)
235 oldValue = oldBase?.keys[key]
237 if (oldValue? and oldValue != base.keys[key]) or
238 key not of locale.keys
242 translatedCount[localeName]++ if value != base.keys[key] or value == ''
243 return "#{ key }=#{ value }"
247 fs.writeFileSync(localePath, newLocale.join(base.newline))
248 return {fileName, translatedCount, total: Object.keys(base.keys).length}
250 parseLocaleFile = (fileContents) ->
253 [ newline ] = fileContents.match(/\r?\n/)
254 for line in fileContents.split(newline)
256 [ match, key, value ] = line.match(///^ ([^=]+) = (.*) $///) ? []
262 return {keys, template: lines, newline}
264 helpHTMLFile = 'help.html'
265 gulp.task(helpHTMLFile, ->
266 unless fs.existsSync(helpHTMLFile)
267 process.stdout.write("""
268 First enable the “Copy to clipboard” line in help.coffee, show the help
269 dialog and finally dump the clipboard into #{ helpHTMLFile }.
272 gulp.src('help.html')
274 file.contents = new Buffer(generateHelpHTML(file.contents.toString()))
276 .pipe(gulp.dest('.'))
279 helpHTMLPrelude = '''
282 <title>VimFx help</title>
285 body > :first-child { min-height: 100vh; }
287 <link rel=stylesheet href=extension/skin/style.css>
290 generateHelpHTML = (dumpedHTML) ->
291 return helpHTMLPrelude + dumpedHTML
292 .replace(/^<\w+ xmlns="[^"]+"/, '<div')
293 .replace(/\w+>$/, 'div>')
294 .replace(/<(\w+)([^>]*)\/>/g, '<$1$2></$1>')