Skip to content

Commit

Permalink
#464: Get pantheon app to build its own dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
pirog committed Sep 14, 2015
1 parent 22c561f commit de206fe
Show file tree
Hide file tree
Showing 5 changed files with 263 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ cache
coverage
doc
deploy
node_modules
node_modules/*
cids
.boot2docker
built
Expand Down
3 changes: 2 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ module.exports = function(grunt) {
src: [
'*.js',
'lib/*.js',
'scripts/*.js'
'scripts/*.js',
'app/scripts/*.js'
]
}
},
Expand Down
1 change: 1 addition & 0 deletions app/kalabox.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"appName": "panth",
"appType": "kalabox-app-pantheon",
"appPlugins": [
"kalabox-plugin-git",
"kalabox-plugin-rsync",
Expand Down
24 changes: 11 additions & 13 deletions app/package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
{
"name": "solr",
"dependencies": {
"bluebird": "^2.9.32",
"fs-extra": "^0.18.2",
"inquirer": "^0.9.0",
"kalabox-plugin-drush": "^0.10.0",
"kalabox-plugin-git": "^0.10.0",
"kalabox-plugin-pantheon": "^0.10.0",
"kalabox-plugin-rsync": "^0.10.0",
"ssh-fingerprint": "0.0.1",
"ssh-keygen": "^0.2.1",
"lodash": "^3.9.3",
"restler": "^3.3.0"
}
"scripts": {
"postinstall": "node ./scripts/downloadLibs.js"
},
"version": "0.10.0",
"postInstallPkgs": [
"kalabox-plugin-pantheon@^0.10.0",
"kalabox-plugin-drush@^0.10.0",
"kalabox-plugin-git@^0.10.0",
"kalabox-plugin-rsync@^0.10.0"
],
"dependencies": {}
}
248 changes: 248 additions & 0 deletions app/scripts/downloadLibs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
/**
* Download vendor deps
*/

'use strict';

// Intrinsic mods
var path = require('path');
var https = require('https');
var url = require('url');

// Npm mods
var _ = require('lodash');
var fs = require('fs-extra');

// Pkg.json
var pkgJson = require('./../package.json');

// Files we need and the projects from whence they came.
// @todo: check whether this actually is set?
var pkgs = pkgJson.postInstallPkgs;

/*
* Get the minor version of the current project
*/
var getMinorVersion = function() {

// Should already be loaded but just in case
if (!pkgJson) {
pkgJson = require('./../package.json');
}

// Split the version and send back the minor part
var parts = pkgJson.version.split('.');
return parts[1];

};

/*
* Simple function to make a github api request
*/
var request = function(options, callback) {

// Request object
var req = https.request(options, function(res) {

// Need a string not a buffer
res.setEncoding('utf8');

// Collector
var responseString = '';

// Collect the stream
res.on('data', function(data) {
responseString += data;
});

// Return the JSON parsed stream
res.on('end', function() {
callback(JSON.parse(responseString));
});

});

// Handle errors
req.on('error', function(err) {
console.error(err);
});

// End request
req.end();

};

/*
* We can't rely on Kalabox stuff in this file so we need to do this
* to get the users home dir
*/
var getHomeDir = function() {

// Check the env for the home path
var platformIsWindows = process.platform === 'win32';
var envKey = platformIsWindows ? 'USERPROFILE' : 'HOME';

// Homeward bound
return process.env[envKey];

};

/*
* We can't rely on Kalabox stuff in this file so we need to do this
* to get dev mod
*/
var getDevMode = function() {

// Set for usage later
var devMode;

// If the environment is a no go try to load from custom kalabox.json
if (process.env.KALABOX_DEV === undefined) {

// Construct kbox.json path
var kboxJsonFile = path.join(getHomeDir(), '.kalabox', 'kalabox.json');

// Check it kbox json exists
if (fs.existsSync(kboxJsonFile)) {

// Load it
var kboxJson = require(kboxJsonFile);

// Use its dev mode if its set
devMode = (kboxJson.devMode) ? kboxJson.devMode : false;

}
}

// Use the envvar if its set
else {

// Set it
devMode = (process.env.KALABOX_DEV) ? process.env.KALABOX_DEV : false;

}

// Return something
return devMode;

};

/*
* Get the project tag/branch we should be pulling from
*/
var getProjectVersion = function(project, callback) {

// If we are in dev mode this is trivial
if (getDevMode() === true || getDevMode() === 'true') {
callback('master');
}
// If not we need to do some exploration on the github API
else {
// Request opts to find the github tags for a project
var options = {
hostname: 'api.github.com',
port: 443,
path: '/repos/kalabox/' + project + '/tags',
method: 'GET',
json: true,
headers: {'User-Agent': 'Kalabox'}
};

// Make teh request
request(options, function(data) {

// Get the minor version of the current project
var projectVer = getMinorVersion();

// Grab the first tag that shares the minor version
// we assume this lists the most recent tags first
var minorVersion = _.result(_.find(data, function(release) {
if (release.name) {
var minorVersionParts = release.name.split('.');
return projectVer.toString() === minorVersionParts[1];
}
// @todo: What happens if we have no project releases for this version?
}), 'name');

callback(minorVersion);

});
}

};

/**
* Returns a string with the url of the master branch
* tarball. The package must be on github and have a repository
* field in its pacakge.json. It must also have a master branch that is
* considered the development branch on github.
*/
var getMasterTarball = function(pkg) {

// Build our tarball URL
// https://github.com/kalabox/kalabox-plugin-dbenv/tarball/master
var tarUrl = {
protocol: 'https:',
host: 'github.com',
pathname: ['kalabox', pkg, 'tarball', 'master'].join('/')
};

// Return the formatted tar URL
return url.format(tarUrl);

};

/*
* Replaces the version part of a npm pkg@version string with
* the master branch tarball from github if that package is a kalabox
* plugin
*
* If you are using your own external app or plugin it needs to live on github
* and have a master branch or this is not going to work.
*
*/
var pkgToDev = function(pkg) {

// Split our package so we can reassemble later
var parts = pkg.split('@');

// Grab the dev tarball if ths is a kalabox plugin
if (_.includes(pkg, 'kalabox-')) {

// Get the tarball location
var masterTar = getMasterTarball(parts[0]);
return [parts[0], masterTar].join('@');

}
// Otherwise just return what we have
else {
return pkg;
}

};

// Downlaod our files and put them in their place
_.forEach(pkgs, function(pkg) {
getProjectVersion(pkg, function(version) {

if (version === 'master') {
pkg = pkgToDev(pkg);
}

var spawn = require('child_process').spawn;
var npm = spawn('npm', ['install', pkg]);

npm.stdout.on('data', function(data) {
console.log('stdout: ' + data);
});

npm.stderr.on('data', function(data) {
console.log('stderr: ' + data);
});

npm.on('close', function(code) {
console.log('child process exited with code ' + code);
});

});
});

0 comments on commit de206fe

Please sign in to comment.