Skip to content

Commit

Permalink
Merge pull request #297 from phase2/feature/package_rewrite_composer
Browse files Browse the repository at this point in the history
Allow changing vendor path for package output
  • Loading branch information
arithmetric authored Sep 26, 2016
2 parents ad182c4 + 2c05b33 commit 1a51bbc
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
2 changes: 1 addition & 1 deletion example/Gruntconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"srcFiles": ["!sites/*/files/**", "!xmlrpc.php", "!modules/php/*"],
"projFiles": ["README*", "bin/**", "hooks/**", "src/*.make", "vendor/**"],
"dest": {
"docroot": "build/html",
"docroot": "html",
"devResources": ""
}
},
Expand Down
55 changes: 54 additions & 1 deletion tasks/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,68 @@ module.exports = function(grunt) {
var Help = require('../lib/help')(grunt);
var path = require('path');

grunt.registerTask('packageRewriteComposer', '', function() {
var pathBuild = grunt.config('config.buildPaths.html');
var pathPackage = grunt.config('config.packages.dest.docroot');
var pathVendor = grunt.config('config.packages.dest.vendor');
// Check if we are packaging to a custom destination.
if ((pathBuild !== pathPackage) || pathVendor) {
// Load `composer.json` as JSON, convert to object.
var destPath = grunt.option('package-dest');
var composer = grunt.file.readJSON(destPath + '/composer.json');
// Determine new installer-paths
var pathInstall = pathPackage ? pathPackage + '/' : '';
if (pathVendor) {
// Make sure install path is relative to vendor location.
var regexVendor = new RegExp('^' + pathVendor + '/');
pathInstall = pathInstall.replace(regexVendor, '');
}
var regex = new RegExp('^' + pathBuild + '/');
for (var key in composer.extra['installer-paths']) {
// Add an unnecessary if check just for eslint rules.
if (composer.extra['installer-paths'].hasOwnProperty(key)) {
var newKey = key.replace(regex, pathInstall);
if (newKey !== key) {
// Alter keys in `extra.installer-paths` object to change `build/html`
// to `html` or an alternative path from the config.
var value = composer.extra['installer-paths'][key];
delete composer.extra['installer-paths'][key];
composer.extra['installer-paths'][newKey] = value;
}
}
}

// Next, generate the composer.json in the correct path.
var destComposer = (pathVendor) ? destPath + '/' + pathVendor : destPath;
// Write out data to `composer.json` in the package output.
var composerString = JSON.stringify(composer, null, 2);
grunt.file.write(destComposer + '/composer.json', composerString);
if (pathVendor) {
// Remove the original file if we moved it.
grunt.file.delete(destPath + '/composer.json');
// Change working directory for later `composer install`.
grunt.config(['composer'], {
options: {
flags: ['no-dev'],
cwd: destComposer
}
});
}
}
});

grunt.registerTask('package', 'Package the operational codebase for deployment. Use package:compress to create an archive.', function() {
grunt.loadNpmTasks('grunt-contrib-copy');

var config = grunt.config.get('config.packages');
var srcFiles = ['**', '!**/.gitkeep'].concat((config && config.srcFiles && config.srcFiles.length) ? config.srcFiles : '**');
var projFiles = (config && config.projFiles && config.projFiles.length) ? config.projFiles : [];
console.log(projFiles);

// Look for a package target spec, build destination path.
var packageName = grunt.option('name') || config.name || 'package';
var destPath = grunt.config.get('config.buildPaths.packages') + '/' + packageName;
var tasks = [];
grunt.option('package-dest', destPath);

grunt.config('copy.package', {
files: [
Expand Down Expand Up @@ -55,13 +105,16 @@ module.exports = function(grunt) {
if (projFiles.find(function(pattern) {
return pattern.startsWith('composer');
})) {
tasks.push('packageRewriteComposer');
grunt.config(['composer'], {
options: {
flags: ['no-dev'],
cwd: destPath
}
});
tasks.push('composer:install');
grunt.config(['composer', 'drupal-scaffold'], {});
tasks.push('composer:drupal-scaffold');
}

if (this.args[0] && this.args[0] === 'compress') {
Expand Down
10 changes: 5 additions & 5 deletions test/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ describe('grunt', function() {
});

// Ensure a custom library file is available under package.
var librariesPackageDest = (drupalCore === '8') ? 'build/packages/package/build/html/libraries/example_lib/example.md' : 'build/packages/package/sites/all/libraries/example_lib/example.md';
var librariesPackageDest = (drupalCore === '8') ? 'build/packages/package/html/libraries/example_lib/example.md' : 'build/packages/package/sites/all/libraries/example_lib/example.md';
it('custom library file should exist in package', function(done) {
fs.exists(librariesPackageDest, function(exists) {
assert.ok(exists);
Expand All @@ -123,7 +123,7 @@ describe('grunt', function() {
});

// Ensure a custom module file is available under package.
var modulesPackageDest = (drupalCore === '8') ? 'build/packages/package/build/html/modules/custom/test.php' : 'build/packages/package/sites/all/modules/custom/test.php';
var modulesPackageDest = (drupalCore === '8') ? 'build/packages/package/html/modules/custom/test.php' : 'build/packages/package/sites/all/modules/custom/test.php';
it('custom module file should exist in package', function(done) {
fs.exists(modulesPackageDest, function(exists) {
assert.ok(exists);
Expand All @@ -132,7 +132,7 @@ describe('grunt', function() {
});

// Ensure a custom theme file is available under package.
var themesPackageDest = (drupalCore === '8') ? 'build/packages/package/build/html/themes/custom/example_theme/example_theme.info.yml' : 'build/packages/package/sites/all/themes/custom/example_theme/example_theme.info';
var themesPackageDest = (drupalCore === '8') ? 'build/packages/package/html/themes/custom/example_theme/example_theme.info.yml' : 'build/packages/package/sites/all/themes/custom/example_theme/example_theme.info';
it('custom theme file should exist in package', function(done) {
fs.exists(themesPackageDest, function(exists) {
assert.ok(exists);
Expand Down Expand Up @@ -202,7 +202,7 @@ describe('grunt', function() {
it('should place the build codebase in build/packages/package by default', function(done) {
this.timeout(180000);
exec('grunt package', function(error) {
var indexPackageDest = (drupalCore === '8') ? 'build/packages/package/build/html/index.php' : 'build/packages/package/index.php';
var indexPackageDest = (drupalCore === '8') ? 'build/packages/package/html/index.php' : 'build/packages/package/index.php';
fs.exists(indexPackageDest, function(exists) {
assert.ok(!error && exists);
done();
Expand All @@ -213,7 +213,7 @@ describe('grunt', function() {
it('should allow override of grunt package destination with --name', function(done) {
this.timeout(180000);
exec('grunt package --name=upstream', function(error) {
var indexPackageDest = (drupalCore === '8') ? 'build/packages/upstream/build/html/index.php' : 'build/packages/upstream/index.php';
var indexPackageDest = (drupalCore === '8') ? 'build/packages/upstream/html/index.php' : 'build/packages/upstream/index.php';
fs.exists(indexPackageDest, function(exists) {
assert.ok(!error && exists);
done();
Expand Down
2 changes: 1 addition & 1 deletion test/test_assets_d8/Gruntconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"srcFiles": ["!sites/*/files/**", "!xmlrpc.php", "!modules/php/*"],
"projFiles": ["README*", "bin/**", "hooks/**", "src/*.make", "vendor/**", "composer.*"],
"dest": {
"docroot": "build/html",
"docroot": "html",
"devResources": ""
}
},
Expand Down

0 comments on commit 1a51bbc

Please sign in to comment.