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 regexEscape = require('escape-string-regexp')
23 gulp = require('gulp')
24 coffee = require('gulp-coffee')
25 coffeelint = require('gulp-coffeelint')
26 git = require('gulp-git')
27 header = require('gulp-header')
28 mustache = require('gulp-mustache')
29 zip = require('gulp-zip')
30 merge = require('merge2')
31 precompute = require('require-precompute')
32 request = require('request')
33 rimraf = require('rimraf')
34 runSequence = require('run-sequence')
35 pkg = require('./package.json')
39 LOCALE = 'extension/locale'
40 TEST = 'extension/test'
42 test = '--test' in process.argv or '-t' in process.argv
43 ifTest = (value) -> if test then [value] else []
46 read = (filepath) -> fs.readFileSync(filepath).toString()
47 template = (data) -> mustache(data, {extension: ''})
49 gulp.task('default', ['push'])
51 gulp.task('clean', (callback) ->
52 rimraf(DEST, callback)
56 gulp.src(['extension/**/!(*.coffee|*.tmpl)', 'COPYING'])
57 .pipe(gulp.dest(DEST))
60 gulp.task('node_modules', ->
61 dependencies = (name for name of pkg.dependencies)
62 # Note! When installing or updating node modules, make sure that the following
63 # glob does not include too much or too little!
64 gulp.src("node_modules/+(#{ dependencies.join('|') })/\
65 {LICENSE,{,**/!(test)/}*.js}")
66 .pipe(gulp.dest("#{ DEST }/node_modules"))
69 gulp.task('coffee', ->
71 'extension/bootstrap.coffee'
72 'extension/lib/**/*.coffee'
73 ifTest('extension/test/**/*.coffee')...
74 ], {base: 'extension'})
75 .pipe(coffee({bare: true}))
76 .pipe(gulp.dest(DEST))
79 gulp.task('chrome.manifest', ->
80 gulp.src('extension/chrome.manifest.tmpl')
81 .pipe(template({locales: fs.readdirSync(LOCALE).map((locale) -> {locale})}))
82 .pipe(gulp.dest(DEST))
85 gulp.task('install.rdf', ->
86 [ [ { name: creator } ], developers, contributors, translators ] =
87 read('PEOPLE.md').trim().replace(/^#.+\n|^\s*-\s*/mg, '').split('\n\n')
88 .map((block) -> block.split('\n').map((name) -> {name}))
90 getDescription = (locale) -> read(join(LOCALE, locale, 'description')).trim()
92 descriptions = fs.readdirSync(LOCALE)
95 description: getDescription(locale)
98 gulp.src('extension/install.rdf.tmpl')
101 minVersion: pkg.firefoxVersions.min
102 maxVersion: pkg.firefoxVersions.max
103 creator, developers, contributors, translators
104 defaultDescription: getDescription('en-US')
107 .pipe(gulp.dest(DEST))
110 gulp.task('require-data', ['node_modules'], ->
111 data = JSON.stringify(precompute('.'), null, 2)
112 gulp.src('extension/require-data.js.tmpl')
113 .pipe(template({data}))
114 .pipe(gulp.dest(DEST))
117 gulp.task('tests-list', ->
118 list = JSON.stringify(fs.readdirSync(TEST)
119 .map((name) -> name.match(/^(test-.+)\.coffee$/)?[1])
122 gulp.src("#{ TEST }/tests-list.js.tmpl", {base: 'extension'})
123 .pipe(template({list}))
124 .pipe(gulp.dest(DEST))
127 gulp.task('templates', [
131 ifTest('tests-list')...
134 gulp.task('build', (callback) ->
137 ['copy', 'node_modules', 'coffee', 'templates'],
142 gulp.task('xpi', ['build'], ->
143 gulp.src("#{ DEST }/**/*")
144 .pipe(zip(XPI, {compress: false}))
145 .pipe(gulp.dest(DEST))
148 gulp.task('push', ['xpi'], ->
149 body = fs.readFileSync(join(DEST, XPI))
150 request.post({url: 'http://localhost:8888', body })
154 gulp.src(['extension/**/*.coffee', 'gulpfile.coffee'])
156 .pipe(coffeelint.reporter())
159 gulp.task('release', ->
161 message = "VimFx v#{ version }"
162 today = new Date().toISOString()[...10]
164 gulp.src('package.json'),
165 gulp.src('CHANGELOG.md')
166 .pipe(header("### #{ version } (#{ today })\n\n"))
167 .pipe(gulp.dest('.'))
168 ]).pipe(git.commit(message))
169 git.tag("v#{ version }", message, (error) -> throw error if error)
172 gulp.task('sync-locales', ->
174 for arg in process.argv when arg[...2] == '--'
175 baseLocale = arg[2..]
176 for file in fs.readdirSync(join(LOCALE, baseLocale))
177 templateString = switch path.extname(file)
178 when '.properties' then '%key=%value'
179 when '.dtd' then '<!ENTITY %key "%value">'
180 syncLocale(file, baseLocale, templateString) if templateString
183 syncLocale = (fileName, baseLocaleName, templateString) ->
185 regexEscape(templateString)
186 .replace(/%key/, '([^\\s=]+)')
187 .replace(/%value/, '(.+)')
189 basePath = join(LOCALE, baseLocaleName, fileName)
190 base = parseLocaleFile(read(basePath), regex)
191 oldBasePath = "#{basePath}.old"
192 if fs.existsSync(oldBasePath)
193 oldBase = parseLocaleFile(read(oldBasePath), regex)
194 for localeName in fs.readdirSync(LOCALE) when localeName != baseLocaleName
195 localePath = join(LOCALE, localeName, fileName)
196 locale = parseLocaleFile(read(localePath), regex)
197 newLocale = base.template.map((line) ->
198 if Array.isArray(line)
200 oldValue = oldBase?.keys[key]
202 if (oldValue? and oldValue != base.keys[key]) or
203 key not of locale.keys
207 return templateString.replace(/%key/, key).replace(/%value/, value)
211 fs.writeFileSync(localePath, newLocale.join(base.newline))
214 parseLocaleFile = (fileContents, regex) ->
217 [ newline ] = fileContents.match(/\r?\n/)
218 for line in fileContents.split(newline)
220 [ match, key, value ] = line.match(regex) ? []
226 return {keys, template: lines, newline}