Skip to content

Commit

Permalink
Merge branch 'v0.4.0' of https://github.com/nwittwer/shift into brows…
Browse files Browse the repository at this point in the history
…ersync
  • Loading branch information
nwittwer committed Jan 10, 2019
2 parents 2b7960e + 50f18c4 commit 264930d
Show file tree
Hide file tree
Showing 39 changed files with 12,096 additions and 362 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GITHUB_TOKEN=xxx
MAC_CERT_ID=xxxxxxxxxx
1 change: 1 addition & 0 deletions gulp/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = {
SRC: 'src/',
DIST: 'dist/',
SHIP: 'ship/',
TEST: 'test/',

SERVER: {
PORT: 8000
Expand Down
106 changes: 74 additions & 32 deletions gulp/tasks/build-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,40 +25,71 @@ let NEXT_APP_VERSION;

// Changelog variable
let CHANGELOG;
let PATH_TO_ZIP;
let PATH_TO_MAC_ZIP;

// The overall task
// Build task
gulp.task('build-app', gulp.series(
'build-app:prompt',
'build-app:prompt-version',
'build-app:prompt-version-diff',
'build-app:version',
'build-app:changelog',
'build-app:main',
'build-app:zip-app',
'build-app:draft-release',
'build-app:open-release-in-browser'
'build-app:main'
));

// Deploy tasks
// @TODO: Split this into another file? How to keep the variable values across files?
gulp.task('deploy-app', gulp.series(
'deploy-app:changelog',
'deploy-app:zip-app',
'deploy-app:draft-release',
'deploy-app:open-release-in-browser'
));

// Confirm version number
gulp.task('build-app:prompt', function (done) {
gulp.task('build-app:prompt-version', function (done) {
inquirer.prompt([{
type: 'input',
name: 'version',
message: 'What version is this? (Currently ' + CURRENT_APP_VERSION + ' ):'
message: 'What release version is this? (Currently ' + CURRENT_APP_VERSION + '):'
}])
.then(function (res) {
if (res.version) {
NEXT_APP_VERSION = res.version;
console.log(chalk.yellow(`Version updated to ${NEXT_APP_VERSION} 👍`));
} else {
NEXT_APP_VERSION = CURRENT_APP_VERSION;
console.log(chalk.yellow(`Version: ${NEXT_APP_VERSION}`));
}

// Update the final ZIP path with the version
PATH_TO_ZIP = 'ship/Shift-v' + NEXT_APP_VERSION + '.zip';
PATH_TO_MAC_ZIP = 'ship/shift-' + NEXT_APP_VERSION + '-mac.zip';

done();
});
});

// Choose what version you'd like to compare to
// This will create a link viewable in Github
// Defaults to master branch
gulp.task('build-app:prompt-version-diff', function (done) {
inquirer.prompt([{
type: 'input',
name: 'version',
message: `Branch/Git tag to compare to ${NEXT_APP_VERSION}: `
}])
.then(function (res) {
if (res.version) {
// Successful comparison to x version
COMPARE_VERSION = res.version;
console.log(chalk.yellow(`Got it, will compare changes in ${COMPARE_VERSION} to ${NEXT_APP_VERSION} 👍`));
} else {
// Nothing chosen, default to master
COMPARE_VERSION = "master";
console.log(chalk.yellow(`Comparing changes in ${COMPARE_VERSION} to master`));
}
done();
});
});

// Bumps the version number
// `src/package.json` and `dist/package.json`
gulp.task('build-app:version', function () {
Expand All @@ -73,18 +104,6 @@ gulp.task('build-app:version', function () {
return merge(src, dist);
});

// Creates a changelog from Git log
// Compares the commits from branch A that are not in branch B
// Then stores it to a variable that can later be used for a
// git log origin/branch-a..origin/branch-b --abbrev-commit --pretty=oneline
gulp.task('build-app:changelog', function (cb) {
exec('git log master.. --abbrev-commit --pretty=oneline', function (err, stdout, stderr) {
CHANGELOG = stdout;
console.log(chalk.yellow('Changelog:\n' + CHANGELOG));
cb(err);
});
});

// Build the app into a Mac/Windows executable format
// The final distributable is put into the `ship` folder
gulp.task('build-app:main', function () {
Expand All @@ -111,10 +130,31 @@ gulp.task('build-app:main', function () {
})
});

// Creates a changelog from Git log
// Compares the commits from branch A that are not in branch B
// Then stores it to a variable that can later be used for a
// git log origin/branch-a..origin/branch-b --abbrev-commit --pretty=oneline
gulp.task('deploy-app:changelog', function (cb) {

// As long as the new version isn't comparing to master...
// Prepend with a "v" ("v1.0.0") instead of just "1.0.0"
if (COMPARE_VERSION.includes("master") == false) {
COMPARE_VERSION = "v" + COMPARE_VERSION;
NEXT_APP_VERSION = "v" + NEXT_APP_VERSION;
}

exec(`git log ${COMPARE_VERSION}.. --abbrev-commit --pretty=oneline`, function (err, stdout, stderr) {
CHANGELOG = stdout;
CHANGELOG += `\n\n [View all changes since ${COMPARE_VERSION}](../../compare/${COMPARE_VERSION}..${NEXT_APP_VERSION})`
console.log(chalk.yellow('Changelog:\n' + CHANGELOG));
cb(err);
});
});

// Create a ZIP of executable file
gulp.task('build-app:zip-app', function (done) {
gulp.task('deploy-app:zip-app', function (done) {
// create a file to stream archive data to.
var output = fs.createWriteStream(PATH_TO_ZIP);
var output = fs.createWriteStream(PATH_TO_MAC_ZIP);
var archive = archiver('zip', {
zlib: {
level: 9 // Sets the compression level.
Expand All @@ -124,14 +164,16 @@ gulp.task('build-app:zip-app', function (done) {
// listen for all archive data to be written
// 'close' event is fired only when a file descriptor is involved
output.on('close', function () {
console.log(chalk.yellow('ZIP size: ' + archive.pointer() + ' total bytes'));
console.log(chalk.yellow('ZIP created: ' + archive.pointer() + ' total bytes'));
console.log(chalk.yellow(`🚀 Setting up the Github release...`));
done();
});

// Catch warnings
archive.on('warning', function (err) {
if (err.code === 'ENOENT') {
// log warning
console.log(err);
} else {
// throw error
throw err;
Expand All @@ -158,26 +200,26 @@ gulp.task('build-app:zip-app', function (done) {
// 2. You can create a Github token here: https://github.com/settings/tokens
// 3. Add: `GITHUB_TOKEN=replace_with_your_token`
// 4. Save the file
gulp.task('build-app:draft-release', function () {
gulp.task('deploy-app:draft-release', function () {
const GITHUB_TOKEN = process.env.GITHUB_TOKEN || "";
return gulp.src(PATH_TO_ZIP)
return gulp.src(PATH_TO_MAC_ZIP)
.pipe(release({
owner: 'nwittwer',
token: GITHUB_TOKEN, // Did you set already add your Github token?
tag: 'v' + NEXT_APP_VERSION, // i.e. v0.3.0 (format: v + 0.0.0)
token: GITHUB_TOKEN, // Did you set already add your Github token in a .env file?
tag: NEXT_APP_VERSION, // format: v0.0.0
draft: true, // draft or public
prerelease: false,
manifest: require('../../package.json'), // package.json from which default values will be extracted if they're missing
notes: CHANGELOG
}))
.pipe(notify({
title: "🎉 Shift v" + NEXT_APP_VERSION + " release draft created!",
title: "🎉 Shift " + NEXT_APP_VERSION + " release draft created!",
message: "Opening in browser..."
}))
});

// Opens the Github Releases page
gulp.task('build-app:open-release-in-browser', function (done) {
gulp.task('deploy-app:open-release-in-browser', function (done) {
opn('https://github.com/nwittwer/shift/releases');
done();
});
39 changes: 39 additions & 0 deletions gulp/tasks/code-sign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Update process.env based on .env file (in root directory)
require('dotenv').config();

const gulp = require('gulp');
const exec = require('child_process').exec;

const APP_NAME = "Shift"; // @TODO: un-hardcode this value
const MAC_CERTIFICATE = process.env.MAC_CERT_ID; // Mac certificate ID (xxxxxxxxxx)

function sign(callback) {
const command = `
# Go into the ship folder
cd ship/${APP_NAME}/osx64/
# Code sign your app using the variables above
codesign --force --deep --verbose --sign "${MAC_CERTIFICATE}" ${APP_NAME}.app
`;

exec(command, (err, stdout, stderr) => {
console.log(stdout);
console.log(stderr);
callback(err);
});
}

function validate(callback) {
const command = `
cd ship/${APP_NAME}/osx64/
codesign --verify -vvvv ${APP_NAME}.app & spctl -a -vvvv ${APP_NAME}.app
`;

exec(command, (err, stdout, stderr) => {
console.log(stdout);
console.log(stderr);
callback(err);
});
}

gulp.task('code-sign-mac', gulp.series(sign, validate));
20 changes: 19 additions & 1 deletion gulp/tasks/handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ var merge = require('merge-stream');

const CONFIG = require('../config.js');

gulp.task('handlebars', gulp.series('handlebars:precompile', 'handlebars:main'))
gulp.task('handlebars', gulp.series(
'handlebars:precompile',
'handlebars:main',
'handlebars:tests'
))

gulp.task('handlebars:precompile', () => {
return gulp.src(CONFIG.SRC + 'pages/*.hbs')
Expand Down Expand Up @@ -38,4 +42,18 @@ gulp.task('handlebars:main', function () {
// Output both the partials and the templates as build/js/templates.js
.pipe(concat('templates.js'))
.pipe(gulp.dest(CONFIG.DIST + 'js/'));
});

// Compile the test/index.hbs file to .html and create the partials
// This makes it easy to insert the latest content into the tests
gulp.task('handlebars:tests', function () {
return gulp.src(CONFIG.TEST + '*.hbs')
.pipe(compileHandlebars({}, {
ignorePartials: true,
batch: [CONFIG.SRC + '/partials']
}))
.pipe(rename({
extname: '.html'
}))
.pipe(gulp.dest(CONFIG.TEST));
});
28 changes: 21 additions & 7 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ gulp.registry(forwardReference());
requireDir('./gulp/tasks');

// Tasks
// Runs all of the above tasks and then waits for files to change
// build --> serve --> watch
// 1. Default
// 2. Build
// 3. Serve
// 4. App: Compiles the app
// 5. Release: Drafts a release of the app to Github
gulp.task('default', gulp.parallel('serve', 'watch'));

// Build the app
gulp.task('app', gulp.series('build', 'create-package-json:main', 'build-app'));
// Starts a BrowerSync instance
gulp.task('build', gulp.series('clean', 'sass', 'javascript', 'handlebars'));

// BrowserSync Server
gulp.task('serve', gulp.series('build', 'create-package-json:dev', function () {
Expand All @@ -28,14 +31,25 @@ gulp.task('serve', gulp.series('build', 'create-package-json:dev', function () {
});
}));

// Starts a BrowerSync instance
gulp.task('build', gulp.series('clean', 'copy', 'sass', 'javascript', 'handlebars'));
// Compile the app
gulp.task('app', gulp.series(
'build',
'create-package-json:main',
'build-app'
));

// Draft a release to Github
gulp.task('release', gulp.series(
'app',
'code-sign-mac',
'deploy-app'
));

// Watch files for changes
gulp.task('watch', function () {
gulp.watch(CONFIG.SRC + 'scss/**/*.scss', gulp.series('sass'));
gulp.watch(CONFIG.SRC + 'js/**/*.js', gulp.series('javascript', reload));
gulp.watch([CONFIG.SRC + 'pages/**/*.hbs', CONFIG.SRC + 'partials/**/*.hbs'], gulp.series('handlebars', reload));
gulp.watch([CONFIG.SRC + 'pages/**/*.hbs', CONFIG.SRC + 'partials/**/*.hbs', CONFIG.TEST + '*.hbs'], gulp.series('handlebars', reload));
});

// Reloads BrowserSync
Expand Down
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"homepage": "https://shift.nickwittwer.com",
"scripts": {
"start": "kill -9 $(lsof -i TCP:8000 | grep LISTEN | awk '{print $2}') & gulp & nw dist",
"build": "gulp app"
"build": "gulp app",
"deploy": "gulp release"
},
"bugs": {
"url": "https://github.com/nwittwer/shift/issues"
Expand All @@ -28,7 +29,7 @@
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"babel-register": "^6.24.1",
"browser-sync": "^2.24.7",
"chai": "^4.2.0",
"chalk": "^1.1.3",
"css-loader": "^0.28.1",
"dotenv": "^6.0.0",
Expand Down Expand Up @@ -70,6 +71,7 @@
"less": "^2.7.2",
"less-loader": "^4.0.3",
"merge-stream": "^1.0.1",
"mocha": "^5.2.0",
"node-sass": "^4.5.3",
"nw": "^0.33.1-sdk",
"opn": "^5.4.0",
Expand All @@ -90,5 +92,6 @@
"webpack-hot-middleware": "^2.18.0",
"webpack-merge": "^4.1.0",
"webpack-stream": "^5.1.1"
}
},
"dependencies": {}
}
Loading

0 comments on commit 264930d

Please sign in to comment.