2015-10-16 08:54:09 +08:00
|
|
|
_ = require 'underscore'
|
|
|
|
s3 = require 's3'
|
|
|
|
fs = require 'fs'
|
|
|
|
path = require 'path'
|
|
|
|
request = require 'request'
|
|
|
|
Promise = require 'bluebird'
|
|
|
|
|
|
|
|
s3Client = null
|
2015-10-17 05:01:29 +08:00
|
|
|
packageVersion = null
|
|
|
|
fullVersion = null
|
2015-10-16 08:54:09 +08:00
|
|
|
|
|
|
|
module.exports = (grunt) ->
|
|
|
|
{cp, spawn, rm} = require('./task-helpers')(grunt)
|
|
|
|
|
2015-11-12 02:25:11 +08:00
|
|
|
appName = -> grunt.config.get('nylasGruntConfig.appName')
|
2015-10-16 08:54:09 +08:00
|
|
|
dmgName = -> "#{appName().split('.')[0]}.dmg"
|
|
|
|
zipName = -> "#{appName().split('.')[0]}.zip"
|
|
|
|
winReleasesName = -> "RELEASES"
|
|
|
|
winSetupName = -> "Nylas N1Setup.exe"
|
2015-10-17 05:01:29 +08:00
|
|
|
winNupkgName = -> "nylas-#{packageVersion}-full.nupkg"
|
|
|
|
|
|
|
|
populateVersion = ->
|
|
|
|
new Promise (resolve, reject) ->
|
2015-11-12 02:25:11 +08:00
|
|
|
json = require(path.join(grunt.config.get('nylasGruntConfig.appDir'), 'package.json'))
|
2015-10-17 05:01:29 +08:00
|
|
|
cmd = 'git'
|
|
|
|
args = ['rev-parse', '--short', 'HEAD']
|
|
|
|
spawn {cmd, args}, (error, {stdout}={}, code) ->
|
|
|
|
return reject() if error
|
|
|
|
commitHash = stdout?.trim?()
|
|
|
|
packageVersion = json.version
|
2015-10-17 06:26:23 +08:00
|
|
|
if packageVersion.indexOf('-') > 0
|
|
|
|
fullVersion = packageVersion
|
|
|
|
else
|
|
|
|
fullVersion = "#{packageVersion}-#{commitHash}"
|
2015-10-17 05:01:29 +08:00
|
|
|
resolve()
|
2015-10-16 08:54:09 +08:00
|
|
|
|
|
|
|
runEmailIntegrationTest = ->
|
|
|
|
return Promise.resolve() unless process.platform is 'darwin'
|
|
|
|
|
2015-11-12 02:25:11 +08:00
|
|
|
buildDir = grunt.config.get('nylasGruntConfig.buildDir')
|
2015-10-16 08:54:09 +08:00
|
|
|
new Promise (resolve, reject) ->
|
|
|
|
appToRun = path.join(buildDir, appName())
|
|
|
|
scriptToRun = "./build/run-build-and-send-screenshot.scpt"
|
|
|
|
spawn
|
|
|
|
cmd: "osascript"
|
2015-10-17 05:01:29 +08:00
|
|
|
args: [scriptToRun, appToRun, fullVersion]
|
2015-10-16 08:54:09 +08:00
|
|
|
, (error) ->
|
|
|
|
if error
|
|
|
|
reject(error)
|
|
|
|
return
|
|
|
|
resolve()
|
|
|
|
|
|
|
|
postToSlack = (msg) ->
|
2015-12-04 03:53:51 +08:00
|
|
|
return Promise.resolve() unless process.env.NYLAS_INTERNAL_HOOK_URL
|
2015-10-16 08:54:09 +08:00
|
|
|
new Promise (resolve, reject) ->
|
|
|
|
request.post
|
2015-12-04 03:53:51 +08:00
|
|
|
url: process.env.NYLAS_INTERNAL_HOOK_URL
|
2015-10-16 08:54:09 +08:00
|
|
|
json:
|
|
|
|
username: "Edgehill Builds"
|
|
|
|
text: msg
|
|
|
|
, (err, httpResponse, body) ->
|
2015-10-17 06:46:09 +08:00
|
|
|
return reject(err) if err
|
|
|
|
resolve()
|
2015-10-16 08:54:09 +08:00
|
|
|
|
|
|
|
put = (localSource, destName) ->
|
|
|
|
grunt.log.writeln ">> Uploading #{localSource} to S3…"
|
|
|
|
|
|
|
|
write = grunt.log.writeln
|
|
|
|
ext = path.extname(destName)
|
|
|
|
lastPc = 0
|
|
|
|
|
|
|
|
new Promise (resolve, reject) ->
|
|
|
|
uploader = s3Client.uploadFile
|
|
|
|
localFile: localSource
|
|
|
|
s3Params:
|
|
|
|
Key: destName
|
|
|
|
ACL: "public-read"
|
|
|
|
Bucket: "edgehill"
|
|
|
|
|
|
|
|
uploader.on "error", (err) ->
|
|
|
|
reject(err)
|
|
|
|
uploader.on "progress", ->
|
|
|
|
pc = Math.round(uploader.progressAmount / uploader.progressTotal * 100.0)
|
|
|
|
if pc isnt lastPc
|
|
|
|
lastPc = pc
|
|
|
|
write(">> Uploading #{destName} #{pc}%")
|
|
|
|
uploader.on "end", (data) ->
|
|
|
|
resolve(data)
|
|
|
|
|
|
|
|
uploadToS3 = (filename, key) ->
|
2015-11-12 02:25:11 +08:00
|
|
|
buildDir = grunt.config.get('nylasGruntConfig.buildDir')
|
2015-10-17 05:01:29 +08:00
|
|
|
filepath = path.join(buildDir, filename)
|
2015-10-16 08:54:09 +08:00
|
|
|
|
|
|
|
grunt.log.writeln ">> Uploading #{filename} to #{key}…"
|
|
|
|
put(filepath, key).then (data) ->
|
2015-10-17 06:46:09 +08:00
|
|
|
msg = "N1 release asset uploaded: <#{data.Location}|#{key}>"
|
2015-10-16 08:54:09 +08:00
|
|
|
postToSlack(msg).then ->
|
|
|
|
Promise.resolve(data)
|
|
|
|
|
2015-10-21 05:24:51 +08:00
|
|
|
uploadZipToS3 = (filenameToZip, key) ->
|
2015-11-12 02:25:11 +08:00
|
|
|
buildDir = grunt.config.get('nylasGruntConfig.buildDir')
|
2015-10-21 05:24:51 +08:00
|
|
|
buildZipFilename = "#{filenameToZip}.zip"
|
|
|
|
buildZipPath = path.join(buildDir, buildZipFilename)
|
2015-10-16 08:54:09 +08:00
|
|
|
|
|
|
|
grunt.log.writeln ">> Creating zip file…"
|
2015-10-21 05:24:51 +08:00
|
|
|
|
2015-10-16 08:54:09 +08:00
|
|
|
new Promise (resolve, reject) ->
|
2015-10-21 05:24:51 +08:00
|
|
|
rm(buildZipPath)
|
2015-10-16 08:54:09 +08:00
|
|
|
orig = process.cwd()
|
|
|
|
process.chdir(buildDir)
|
|
|
|
|
|
|
|
spawn
|
|
|
|
cmd: "zip"
|
2015-10-21 05:24:51 +08:00
|
|
|
args: ["-9", "-y", "-r", buildZipPath, filenameToZip]
|
2015-10-16 08:54:09 +08:00
|
|
|
, (error) ->
|
|
|
|
process.chdir(orig)
|
|
|
|
if error
|
|
|
|
reject(error)
|
|
|
|
return
|
|
|
|
|
2015-10-21 05:24:51 +08:00
|
|
|
grunt.log.writeln ">> Created #{buildZipPath}"
|
|
|
|
uploadToS3(buildZipFilename, key).then(resolve).catch(reject)
|
2015-10-16 08:54:09 +08:00
|
|
|
|
|
|
|
grunt.registerTask "publish-nylas-build", "Publish Nylas build", ->
|
|
|
|
awsKey = process.env.AWS_ACCESS_KEY_ID ? ""
|
|
|
|
awsSecret = process.env.AWS_SECRET_ACCESS_KEY ? ""
|
|
|
|
|
|
|
|
if awsKey.length is 0
|
2015-12-04 03:53:51 +08:00
|
|
|
grunt.fail.fatal "Please set the AWS_ACCESS_KEY_ID environment variable"
|
2015-10-16 08:54:09 +08:00
|
|
|
if awsSecret.length is 0
|
2015-12-04 03:53:51 +08:00
|
|
|
grunt.fail.fatal "Please set the AWS_SECRET_ACCESS_KEY environment variable"
|
2015-10-16 08:54:09 +08:00
|
|
|
|
|
|
|
s3Client = s3.createClient
|
|
|
|
s3Options:
|
|
|
|
accessKeyId: process.env.AWS_ACCESS_KEY_ID
|
|
|
|
scretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
|
|
|
|
|
|
|
|
done = @async()
|
|
|
|
|
2015-12-04 03:53:51 +08:00
|
|
|
populateVersion()
|
|
|
|
.then ->
|
|
|
|
if process.env.RUN_APPLE_SCRIPT_INTEGRATION
|
|
|
|
runEmailIntegrationTest()
|
|
|
|
else Promise.resolve()
|
|
|
|
.then ->
|
|
|
|
uploadPromises = []
|
|
|
|
if process.platform is 'darwin'
|
|
|
|
uploadPromises.push uploadToS3(dmgName(), "#{fullVersion}/#{process.platform}/#{process.arch}/N1.dmg")
|
|
|
|
uploadPromises.push uploadZipToS3(appName(), "#{fullVersion}/#{process.platform}/#{process.arch}/N1.zip")
|
|
|
|
|
|
|
|
else if process.platform is 'win32'
|
|
|
|
uploadPromises.push uploadToS3("installer/"+winReleasesName(), "#{fullVersion}/#{process.platform}/#{process.arch}/RELEASES")
|
|
|
|
uploadPromises.push uploadToS3("installer/"+winSetupName(), "#{fullVersion}/#{process.platform}/#{process.arch}/N1Setup.exe")
|
|
|
|
uploadPromises.push uploadToS3("installer/"+winNupkgName(), "#{fullVersion}/#{process.platform}/#{process.arch}/#{winNupkgName()}")
|
|
|
|
|
|
|
|
else if process.platform is 'linux'
|
|
|
|
buildDir = grunt.config.get('nylasGruntConfig.buildDir')
|
|
|
|
files = fs.readdirSync(buildDir)
|
|
|
|
for file in files
|
|
|
|
if path.extname(file) is '.deb'
|
|
|
|
uploadPromises.push uploadToS3(file, "#{fullVersion}/#{process.platform}/#{process.arch}/N1.deb")
|
|
|
|
if path.extname(file) is '.rpm'
|
|
|
|
uploadPromises.push uploadToS3(file, "#{fullVersion}/#{process.platform}/#{process.arch}/N1.rpm")
|
|
|
|
|
|
|
|
else
|
|
|
|
grunt.fail.fatal "Unsupported platform: '#{process.platform}'"
|
|
|
|
|
|
|
|
Promise.all(uploadPromises).then(done).catch (err) ->
|
|
|
|
grunt.log.error(err)
|
|
|
|
return false
|