-
Notifications
You must be signed in to change notification settings - Fork 13
/
Gruntfile.js
142 lines (116 loc) · 3.05 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
/* eslint-env node */
module.exports = function( grunt ) {
// Load all Grunt plugins.
require( 'load-grunt-tasks' )( grunt );
// TODO: Move to own Grunt plugin.
grunt.registerTask( 'readmeMdToTxt', 'Log some stuff.', function() {
const formatReadme = ( content ) => {
const replaceRules = {
'#': '=== $1 ===',
'##': '== $1 ==',
'#{3,}': '= $1 =',
};
// Replace Markdown headings with WP.org style headings
Object.keys( replaceRules ).forEach( function( pattern ) {
const patternRegExp = [ '^', pattern, '\\s(.+)$' ].join( '' );
content = content.replace(
new RegExp( patternRegExp, 'gm' ),
replaceRules[ pattern ]
);
} );
return content;
};
const replaceVars = ( content, vars ) => {
const handlebars = require( 'handlebars' );
const template = handlebars.compile( content );
return template( vars );
};
const getPluginVersion = ( pluginFile ) => {
const pluginSource = grunt.file.read( pluginFile );
const pattern = new RegExp( 'Version:\\s*(.+)$', 'mi' );
const match = pluginSource.match( pattern );
if ( match.length ) {
return match[ 1 ];
}
return null;
};
const path = require( 'path' );
const pkgConfig = grunt.config.get( 'pkg' );
const options = this.options( {
src: 'readme.md',
dest: 'readme.txt',
} );
const srcFile = grunt.file.read( options.src );
const destDir = path.dirname( options.dest );
// Extract the version from the main plugin file.
if ( 'undefined' === typeof pkgConfig.version ) {
const pluginVersion = getPluginVersion( 'widget-context.php' );
if ( ! pluginVersion ) {
grunt.warn(
'Failed to parse the plugin version in the plugin file.'
);
}
pkgConfig.version = pluginVersion;
}
// Replace all variables.
const readmeTxt = replaceVars( srcFile, pkgConfig );
// Ensure we have the destination directory.
if ( destDir ) {
grunt.file.mkdir( destDir );
}
// Write the readme.txt.
grunt.file.write( options.dest, formatReadme( readmeTxt ) );
} );
grunt.initConfig( {
pkg: grunt.file.readJSON( 'package.json' ),
dist_dir: 'dist',
clean: {
build: [ '<%= dist_dir %>' ],
},
readmeMdToTxt: {
options: {
src: 'readme.txt.md',
dest: '<%= dist_dir %>/readme.txt',
},
},
copy: {
dist: {
src: [
'src/**',
'vendor/**',
'assets/css/**',
'assets/js/**',
'widget-context.php',
'LICENSE',
'composer.json',
'composer.lock',
'screenshot-*.png',
],
dest: '<%= dist_dir %>',
expand: true,
},
},
wp_deploy: {
options: {
plugin_slug: 'widget-context',
svn_user: 'kasparsd',
build_dir: '<%= dist_dir %>',
assets_dir: 'assets/wporg',
},
all: {
options: {
deploy_tag: true,
deploy_trunk: true,
},
},
trunk: {
options: {
deploy_tag: false,
},
},
},
} );
grunt.registerTask( 'build', [ 'clean', 'copy', 'readmeMdToTxt' ] );
grunt.registerTask( 'deploy', [ 'build', 'wp_deploy:all' ] );
grunt.registerTask( 'deploy-trunk', [ 'build', 'wp_deploy:trunk' ] );
};