Skip to content

Commit 1a51bbc

Browse files
authored
Merge pull request #297 from phase2/feature/package_rewrite_composer
Allow changing vendor path for package output
2 parents ad182c4 + 2c05b33 commit 1a51bbc

File tree

4 files changed

+61
-8
lines changed

4 files changed

+61
-8
lines changed

example/Gruntconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"srcFiles": ["!sites/*/files/**", "!xmlrpc.php", "!modules/php/*"],
1212
"projFiles": ["README*", "bin/**", "hooks/**", "src/*.make", "vendor/**"],
1313
"dest": {
14-
"docroot": "build/html",
14+
"docroot": "html",
1515
"devResources": ""
1616
}
1717
},

tasks/package.js

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,68 @@ module.exports = function(grunt) {
99
var Help = require('../lib/help')(grunt);
1010
var path = require('path');
1111

12+
grunt.registerTask('packageRewriteComposer', '', function() {
13+
var pathBuild = grunt.config('config.buildPaths.html');
14+
var pathPackage = grunt.config('config.packages.dest.docroot');
15+
var pathVendor = grunt.config('config.packages.dest.vendor');
16+
// Check if we are packaging to a custom destination.
17+
if ((pathBuild !== pathPackage) || pathVendor) {
18+
// Load `composer.json` as JSON, convert to object.
19+
var destPath = grunt.option('package-dest');
20+
var composer = grunt.file.readJSON(destPath + '/composer.json');
21+
// Determine new installer-paths
22+
var pathInstall = pathPackage ? pathPackage + '/' : '';
23+
if (pathVendor) {
24+
// Make sure install path is relative to vendor location.
25+
var regexVendor = new RegExp('^' + pathVendor + '/');
26+
pathInstall = pathInstall.replace(regexVendor, '');
27+
}
28+
var regex = new RegExp('^' + pathBuild + '/');
29+
for (var key in composer.extra['installer-paths']) {
30+
// Add an unnecessary if check just for eslint rules.
31+
if (composer.extra['installer-paths'].hasOwnProperty(key)) {
32+
var newKey = key.replace(regex, pathInstall);
33+
if (newKey !== key) {
34+
// Alter keys in `extra.installer-paths` object to change `build/html`
35+
// to `html` or an alternative path from the config.
36+
var value = composer.extra['installer-paths'][key];
37+
delete composer.extra['installer-paths'][key];
38+
composer.extra['installer-paths'][newKey] = value;
39+
}
40+
}
41+
}
42+
43+
// Next, generate the composer.json in the correct path.
44+
var destComposer = (pathVendor) ? destPath + '/' + pathVendor : destPath;
45+
// Write out data to `composer.json` in the package output.
46+
var composerString = JSON.stringify(composer, null, 2);
47+
grunt.file.write(destComposer + '/composer.json', composerString);
48+
if (pathVendor) {
49+
// Remove the original file if we moved it.
50+
grunt.file.delete(destPath + '/composer.json');
51+
// Change working directory for later `composer install`.
52+
grunt.config(['composer'], {
53+
options: {
54+
flags: ['no-dev'],
55+
cwd: destComposer
56+
}
57+
});
58+
}
59+
}
60+
});
61+
1262
grunt.registerTask('package', 'Package the operational codebase for deployment. Use package:compress to create an archive.', function() {
1363
grunt.loadNpmTasks('grunt-contrib-copy');
1464

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

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

2575
grunt.config('copy.package', {
2676
files: [
@@ -55,13 +105,16 @@ module.exports = function(grunt) {
55105
if (projFiles.find(function(pattern) {
56106
return pattern.startsWith('composer');
57107
})) {
108+
tasks.push('packageRewriteComposer');
58109
grunt.config(['composer'], {
59110
options: {
60111
flags: ['no-dev'],
61112
cwd: destPath
62113
}
63114
});
64115
tasks.push('composer:install');
116+
grunt.config(['composer', 'drupal-scaffold'], {});
117+
tasks.push('composer:drupal-scaffold');
65118
}
66119

67120
if (this.args[0] && this.args[0] === 'compress') {

test/build.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ describe('grunt', function() {
114114
});
115115

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

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

134134
// Ensure a custom theme file is available under package.
135-
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';
135+
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';
136136
it('custom theme file should exist in package', function(done) {
137137
fs.exists(themesPackageDest, function(exists) {
138138
assert.ok(exists);
@@ -202,7 +202,7 @@ describe('grunt', function() {
202202
it('should place the build codebase in build/packages/package by default', function(done) {
203203
this.timeout(180000);
204204
exec('grunt package', function(error) {
205-
var indexPackageDest = (drupalCore === '8') ? 'build/packages/package/build/html/index.php' : 'build/packages/package/index.php';
205+
var indexPackageDest = (drupalCore === '8') ? 'build/packages/package/html/index.php' : 'build/packages/package/index.php';
206206
fs.exists(indexPackageDest, function(exists) {
207207
assert.ok(!error && exists);
208208
done();
@@ -213,7 +213,7 @@ describe('grunt', function() {
213213
it('should allow override of grunt package destination with --name', function(done) {
214214
this.timeout(180000);
215215
exec('grunt package --name=upstream', function(error) {
216-
var indexPackageDest = (drupalCore === '8') ? 'build/packages/upstream/build/html/index.php' : 'build/packages/upstream/index.php';
216+
var indexPackageDest = (drupalCore === '8') ? 'build/packages/upstream/html/index.php' : 'build/packages/upstream/index.php';
217217
fs.exists(indexPackageDest, function(exists) {
218218
assert.ok(!error && exists);
219219
done();

test/test_assets_d8/Gruntconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"srcFiles": ["!sites/*/files/**", "!xmlrpc.php", "!modules/php/*"],
1111
"projFiles": ["README*", "bin/**", "hooks/**", "src/*.make", "vendor/**", "composer.*"],
1212
"dest": {
13-
"docroot": "build/html",
13+
"docroot": "html",
1414
"devResources": ""
1515
}
1616
},

0 commit comments

Comments
 (0)