4 coffee = require('gulp-coffee')
5 coffeelint = require('gulp-coffeelint')
6 git = require('gulp-git')
7 header = require('gulp-header')
8 mustache = require('gulp-mustache')
9 preprocess = require('gulp-preprocess')
10 sloc = require('gulp-sloc')
11 tap = require('gulp-tap')
12 zip = require('gulp-zip')
13 marked = require('marked')
14 merge = require('merge2')
15 precompute = require('require-precompute')
16 request = require('request')
17 rimraf = require('rimraf')
18 pkg = require('./package.json')
22 LOCALE = 'extension/locale'
23 TEST = 'extension/test'
26 UPDATE_ALL = /\s*UPDATE_ALL$/
28 ADDON_PATH = 'chrome://vimfx'
29 BUILD_TIME = Date.now()
31 argv = process.argv.slice(2)
34 read = (filepath) -> fs.readFileSync(filepath).toString()
35 template = (data) -> mustache(data, {extension: ''})
37 gulp.task('clean', (callback) ->
38 rimraf(DEST, callback)
42 gulp.src(['extension/**/!(*.coffee|*.tmpl)', 'LICENSE', 'LICENSE-MIT'])
43 .pipe(gulp.dest(DEST))
46 gulp.task('node_modules', ->
47 dependencies = (name for name of pkg.dependencies)
48 # Note: When installing or updating node modules, make sure that the following
49 # glob does not include too much or too little!
51 "node_modules/+(#{dependencies.join('|')})/\
52 {LICENSE*,{,**/!(test|examples)/}!(*min|*test*|*bench*).js}"
54 .pipe(gulp.dest("#{DEST}/node_modules"))
57 gulp.task('coffee', ->
58 test = '--test' in argv or '-t' in argv
61 'extension/bootstrap.coffee'
62 'extension/lib/**/*.coffee'
63 ].concat(if test then 'extension/test/**/*.coffee' else []),
66 .pipe(preprocess({context: {
68 ADDON_PATH: JSON.stringify(ADDON_PATH)
69 HOMEPAGE: JSON.stringify(pkg.homepage)
70 REQUIRE_DATA: JSON.stringify(precompute('.'), null, 2)
73 JSON.stringify(fs.readdirSync(TEST)
74 .map((name) -> name.match(/^(test-.+)\.coffee$/)?[1])
80 .pipe(coffee({bare: true}))
81 .pipe(gulp.dest(DEST))
84 gulp.task('bootstrap-frame.js', ->
85 gulp.src('extension/bootstrap-frame.js.tmpl')
86 .pipe(mustache({ADDON_PATH}))
88 file.path = file.path.replace(/\.js\.tmpl$/, "-#{BUILD_TIME}.js")
90 .pipe(gulp.dest(DEST))
93 gulp.task('chrome.manifest', ->
94 gulp.src('extension/chrome.manifest.tmpl')
95 .pipe(template({locales: fs.readdirSync(LOCALE).map((locale) -> {locale})}))
96 .pipe(gulp.dest(DEST))
99 gulp.task('install.rdf', ->
100 [[{name: creator}], developers, contributors, translators] =
101 read('PEOPLE.md').trim().replace(/^#.+\n|^\s*-\s*/mg, '').split('\n\n')
102 .map((block) -> block.split('\n').map((name) -> {name}))
104 getDescription = (locale) -> read(join(LOCALE, locale, 'description')).trim()
106 descriptions = fs.readdirSync(LOCALE)
107 .filter((locale) -> locale != BASE_LOCALE)
108 .map((locale) -> {locale, description: getDescription(locale)})
110 gulp.src('extension/install.rdf.tmpl')
112 idSuffix: if '--unlisted' in argv or '-u' in argv then '-unlisted' else ''
114 homepage: pkg.homepage
115 minVersion: pkg.firefoxVersions.min
116 maxVersion: pkg.firefoxVersions.max
117 creator, developers, contributors, translators
118 defaultDescription: getDescription(BASE_LOCALE)
121 .pipe(gulp.dest(DEST))
124 gulp.task('templates', gulp.parallel(
130 gulp.task('build', gulp.series(
132 gulp.parallel('copy', 'node_modules', 'coffee', 'templates')
135 gulp.task('xpi-only', ->
136 gulp.src("#{DEST}/**/*")
137 .pipe(zip(XPI, {compress: false}))
138 .pipe(gulp.dest(DEST))
141 gulp.task('xpi', gulp.series('build', 'xpi-only'))
143 gulp.task('push-only', ->
144 body = fs.readFileSync(join(DEST, XPI))
145 request.post({url: 'http://localhost:8888', body})
148 gulp.task('push', gulp.series('xpi', 'push-only'))
150 gulp.task('default', gulp.series('push'))
152 # coffeelint-forbidden-keywords has `require('coffee-script/register');` in its
154 gulp.task('lint-workaround', ->
155 gulp.src('node_modules/coffeescript/')
156 .pipe(gulp.symlink('node_modules/coffee-script'))
159 gulp.task('lint-only', ->
160 gulp.src(['extension/**/*.coffee', 'gulpfile.coffee'])
162 .pipe(coffeelint.reporter())
163 .pipe(coffeelint.reporter('fail'))
166 gulp.task('lint', gulp.series('lint-workaround', 'lint-only'))
170 'extension/bootstrap.coffee'
171 'extension/lib/!(migrations|legacy).coffee'
176 gulp.task('release', (callback) ->
178 message = "VimFx v#{version}"
179 today = new Date().toISOString()[...10]
181 gulp.src('package.json')
182 gulp.src('CHANGELOG.md')
183 .pipe(header("### #{version} (#{today})\n\n"))
184 .pipe(gulp.dest('.'))
186 .pipe(git.commit(message))
188 git.tag("v#{version}", message, callback)
193 gulp.task('changelog', (callback) ->
195 for arg in argv when /^-[1-9]$/.test(arg)
197 entries = read('CHANGELOG.md').split(/^### .+/m)[1..num].join('')
198 process.stdout.write(html(entries))
202 gulp.task('readme', (callback) ->
203 process.stdout.write(html(read('README.md')))
207 # Reduce markdown to the small subset of HTML that AMO allows. Note that AMO
208 # converts newlines to `<br>`.
210 return marked(string)
211 .replace(/// <h\d [^>]*> ([^<>]+) </h\d> ///g, '\n\n<b>$1</b>')
212 .replace(///\s* <p> ((?: [^<] | <(?!/p>) )+) </p>///g, (match, text) ->
213 return "\n#{text.replace(/\s*\n\s*/g, ' ')}\n\n"
215 .replace(///<li> ((?: [^<] | <(?!/li>) )+) </li>///g, (match, text) ->
216 return "<li>#{text.replace(/\s*\n\s*/g, ' ')}</li>"
218 .replace(/<br>/g, '\n')
219 .replace(///<(/?)kbd>///g, '<$1code>')
220 .replace(/<img[^>]*>\s*/g, '')
221 .replace(/\n\s*\n/g, '\n\n')
224 gulp.task('faster', ->
225 gulp.src('gulpfile.coffee')
226 .pipe(coffee({bare: true}))
227 .pipe(gulp.dest('.'))
230 gulp.task('sync-locales', (callback) ->
231 baseLocale = BASE_LOCALE
233 for arg in argv when arg[...2] == '--'
235 if name[-1..] == '?' then compareLocale = name[...-1] else baseLocale = name
237 results = fs.readdirSync(join(LOCALE, baseLocale))
238 .filter((file) -> path.extname(file) == '.properties')
239 .map(syncLocale.bind(null, baseLocale))
241 if baseLocale == BASE_LOCALE
243 for {fileName, untranslated, total} in results
244 report.push("#{fileName}:")
245 for localeName, strings of untranslated
246 paddedName = "#{localeName}: "[...6]
247 percentage = Math.round((1 - strings.length / total) * 100)
248 if localeName == compareLocale or compareLocale == null
249 report.push(" #{paddedName} #{percentage}%")
250 if localeName == compareLocale
251 report.push(strings.map((string) -> " #{string}")...)
252 process.stdout.write(report.join('\n') + '\n')
257 syncLocale = (baseLocaleName, fileName) ->
258 basePath = join(LOCALE, baseLocaleName, fileName)
259 base = parseLocaleFile(read(basePath))
261 for localeName in fs.readdirSync(LOCALE)
262 localePath = join(LOCALE, localeName, fileName)
263 locale = parseLocaleFile(read(localePath))
264 untranslated[localeName] = []
265 newLocale = base.template.map((line, index) ->
266 if Array.isArray(line)
268 baseValue = base.keys[key]
270 if UPDATE_ALL.test(baseValue) or key not of locale.keys
271 baseValue.replace(UPDATE_ALL, '')
274 result = "#{key}=#{value}"
275 if value == base.keys[key] and value != ''
276 untranslated[localeName].push("#{index + 1}: #{result}")
281 fs.writeFileSync(localePath, newLocale.join(base.newline))
282 delete untranslated[baseLocaleName]
283 return {fileName, untranslated, total: Object.keys(base.keys).length}
285 parseLocaleFile = (fileContents) ->
288 [newline] = fileContents.match(/\r?\n/)
289 for line in fileContents.split(newline)
291 [match, key, value] = line.match(///^ ([^=]+) = (.*) $///) ? []
297 return {keys, template: lines, newline}
299 generateHTMLTask = (filename, message) ->
300 gulp.task(filename, (callback) ->
301 unless fs.existsSync(filename)
302 process.stdout.write(message(filename))
307 file.contents = new Buffer(generateTestHTML(file.contents.toString()))
309 .pipe(gulp.dest('.'))
312 generateHTMLTask('help.html', (filename) -> """
313 First enable the “Copy to clipboard” line in help.coffee, show the help
314 dialog and finally dump the clipboard into #{filename}.
317 generateHTMLTask('hints.html', (filename) -> """
318 First enable the “Copy to clipboard” line in modes.coffee, show the
319 hint markers, activate the “Increase count” command and finally dump the
320 clipboard into #{filename}.
323 testHTMLPrelude = '''
326 <title>VimFx test</title>
329 body > :first-child {min-height: 100vh; width: 100vw;}
331 <link rel=stylesheet href=extension/skin/style.css>
334 generateTestHTML = (dumpedHTML) ->
335 return testHTMLPrelude + dumpedHTML
336 .replace(/^<\w+ xmlns="[^"]+"/, '<div')
337 .replace(/\w+>$/, 'div>')
338 .replace(/<(\w+)([^>]*)\/>/g, '<$1$2></$1>')