forked from westonruter/syntax-highlighting-code-block
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGruntfile.js
149 lines (133 loc) · 3.34 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* 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: '.wordpress-org',
},
},
},
});
// 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',
'language-names.php',
'editor-styles.css',
'style.css',
'readme.txt',
'LICENSE',
'build/*',
];
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.md.
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;
},
},
},
composer: {
src: ['vendor/autoload.php', 'vendor/composer/**'],
dest: 'dist',
expand: true,
options: {
noProcess: ['vendor/composer/installed.json'],
process(content) {
return content.replace(
/\/highlight\.php/g,
'/highlight-php'
);
},
},
},
vendor: {
src: ['Highlight/**', 'HighlightUtilities/**', 'styles/*'],
expand: true,
cwd: 'vendor/scrivo/highlight.php/',
dest: 'dist/vendor/scrivo/highlight-php/',
},
});
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();
});
};