forked from westonruter/syntax-highlighting-code-block
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
119 lines (101 loc) · 2.92 KB
/
Gruntfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* eslint-env node */
/* eslint-disable camelcase, no-console, no-param-reassign */
module.exports = function( grunt ) {
'use strict';
grunt.initConfig( {
pkg: grunt.file.readJSON( 'package.json' ),
// Deploys a git Repo to the WordPress SVN repo.
wp_deploy: {
deploy: {
options: {
plugin_slug: 'syntax-highlighting-code-block',
build_dir: 'dist',
assets_dir: 'wp-assets',
},
},
},
} );
// Load tasks.
grunt.loadNpmTasks( 'grunt-contrib-copy' );
grunt.loadNpmTasks( 'grunt-wp-deploy' );
// Register tasks.
grunt.registerTask( 'default', [
'dist',
] );
grunt.registerTask( 'dist', function() {
const done = this.async();
const spawnQueue = [];
const stdout = [];
spawnQueue.push(
{
cmd: 'git',
args: [ '--no-pager', 'log', '-1', '--format=%h', '--date=short' ],
}
);
function finalize() {
const commitHash = stdout.shift();
const versionAppend = new Date().toISOString().replace( /\.\d+/, '' ).replace( /-|:/g, '' ) + '-' + commitHash;
const paths = [
'syntax-highlighting-code-block.php',
'line-numbers.css',
'readme.txt',
'LICENSE',
'build/*',
'wp-assets/*',
'vendor/autoload.php',
'vendor/composer/**',
'vendor/scrivo/highlight.php/Highlight/**',
'vendor/scrivo/highlight.php/HighlightUtilities/**',
'vendor/scrivo/highlight.php/styles/*',
];
grunt.config.set( 'copy', {
build: {
src: paths,
dest: 'dist',
expand: true,
options: {
noProcess: [ '*/**', 'LICENSE' ], // That is, only process syntax-highlighting-code-block.php and readme.txt.
process( content, srcpath ) {
if ( ! /syntax-highlighting-code-block\.php$/.test( srcpath ) ) {
return content;
}
let updatedContent = content;
const versionRegex = /(\*\s+Version:\s+)(\d+(\.\d+)+-\w+)/;
let version;
// If not a stable build (e.g. 0.7.0-beta), amend the version with the git commit and current timestamp.
const matches = content.match( versionRegex );
if ( matches ) {
version = matches[ 2 ] + '-' + versionAppend;
console.log( 'Updating version in plugin version to ' + version );
updatedContent = updatedContent.replace( versionRegex, '$1' + version );
updatedContent = updatedContent.replace( /(const PLUGIN_VERSION = ')(.+?)(?=')/, '$1' + version );
}
updatedContent = updatedContent.replace( /const DEVELOPMENT_MODE = true;.*/, 'const DEVELOPMENT_MODE = false;' );
return updatedContent;
},
},
},
} );
grunt.task.run( 'copy' );
done();
}
function doNext() {
const nextSpawnArgs = spawnQueue.shift();
if ( ! nextSpawnArgs ) {
finalize();
} else {
grunt.util.spawn(
nextSpawnArgs,
function( err, res ) {
if ( err ) {
throw new Error( err.message );
}
stdout.push( res.stdout );
doNext();
}
);
}
}
doNext();
} );
};