|
| 1 | +module.exports = function(grunt) { |
| 2 | + 'use strict'; |
| 3 | + |
| 4 | + require('load-grunt-tasks')(grunt); |
| 5 | + |
| 6 | + // Project configuration. |
| 7 | + grunt.initConfig({ |
| 8 | + pkg: grunt.file.readJSON('package.json'), |
| 9 | + |
| 10 | + // Update developer dependencies |
| 11 | + devUpdate: { |
| 12 | + packages: { |
| 13 | + options: { |
| 14 | + packageJson: null, |
| 15 | + packages: { |
| 16 | + devDependencies: true, |
| 17 | + dependencies: false |
| 18 | + }, |
| 19 | + reportOnlyPkgs: [], |
| 20 | + reportUpdated: false, |
| 21 | + semver: true, |
| 22 | + updateType: 'force' |
| 23 | + } |
| 24 | + } |
| 25 | + }, |
| 26 | + |
| 27 | + // Generate .pot file |
| 28 | + makepot: { |
| 29 | + target: { |
| 30 | + options: { |
| 31 | + cwd: '', |
| 32 | + domainPath: 'languages', // Where to save the POT file. |
| 33 | + exclude: [ |
| 34 | + 'releases', |
| 35 | + 'woo-dependencies/.*', |
| 36 | + 'node_modules' |
| 37 | + ], |
| 38 | + mainFile: '<%= pkg.name %>.php', // Main project file. |
| 39 | + potComments: 'Copyright (c) {year} Sébastien Dumont\nThis file is distributed under the same license as the CoCart Tools package.', // The copyright at the beginning of the POT file. |
| 40 | + domainPath: 'languages', // Where to save the POT file. |
| 41 | + potFilename: '<%= pkg.name %>.pot', // Name of the POT file. |
| 42 | + potHeaders: { |
| 43 | + 'poedit': true, // Includes common Poedit headers. |
| 44 | + 'x-poedit-keywordslist': true, // Include a list of all possible gettext functions. |
| 45 | + 'Report-Msgid-Bugs-To': 'https://cocart.xyz/feedback/', |
| 46 | + 'language-team': 'Sébastien Dumont <[email protected]>', |
| 47 | + 'language': 'en_US' |
| 48 | + }, |
| 49 | + processPot: function( pot ) { |
| 50 | + var translation, |
| 51 | + excluded_meta = [ |
| 52 | + 'Plugin Name of the plugin/theme', |
| 53 | + 'Plugin URI of the plugin/theme', |
| 54 | + 'Description of the plugin/theme', |
| 55 | + 'Author of the plugin/theme', |
| 56 | + 'Author URI of the plugin/theme' |
| 57 | + ]; |
| 58 | + |
| 59 | + for ( translation in pot.translations[''] ) { |
| 60 | + if ( 'undefined' !== typeof pot.translations[''][ translation ].comments.extracted ) { |
| 61 | + if ( excluded_meta.indexOf( pot.translations[''][ translation ].comments.extracted ) >= 0 ) { |
| 62 | + console.log( 'Excluded meta: ' + pot.translations[''][ translation ].comments.extracted ); |
| 63 | + delete pot.translations[''][ translation ]; |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return pot; |
| 69 | + }, |
| 70 | + type: 'wp-plugin', // Type of project. |
| 71 | + updateTimestamp: true, // Whether the POT-Creation-Date should be updated without other changes. |
| 72 | + } |
| 73 | + } |
| 74 | + }, |
| 75 | + |
| 76 | + // Check strings for localization issues |
| 77 | + checktextdomain: { |
| 78 | + options:{ |
| 79 | + text_domain: '<%= pkg.name %>', // Project text domain. |
| 80 | + keywords: [ |
| 81 | + '__:1,2d', |
| 82 | + '_e:1,2d', |
| 83 | + '_x:1,2c,3d', |
| 84 | + 'esc_html__:1,2d', |
| 85 | + 'esc_html_e:1,2d', |
| 86 | + 'esc_html_x:1,2c,3d', |
| 87 | + 'esc_attr__:1,2d', |
| 88 | + 'esc_attr_e:1,2d', |
| 89 | + 'esc_attr_x:1,2c,3d', |
| 90 | + '_ex:1,2c,3d', |
| 91 | + '_n:1,2,4d', |
| 92 | + '_nx:1,2,4c,5d', |
| 93 | + '_n_noop:1,2,3d', |
| 94 | + '_nx_noop:1,2,3c,4d' |
| 95 | + ] |
| 96 | + }, |
| 97 | + files: { |
| 98 | + src: [ |
| 99 | + '*.php', |
| 100 | + '**/*.php', // Include all files |
| 101 | + '!node_modules/**' // Exclude node_modules/ |
| 102 | + ], |
| 103 | + expand: true |
| 104 | + }, |
| 105 | + }, |
| 106 | + |
| 107 | + potomo: { |
| 108 | + dist: { |
| 109 | + options: { |
| 110 | + poDel: false |
| 111 | + }, |
| 112 | + files: [{ |
| 113 | + expand: true, |
| 114 | + cwd: 'languages', |
| 115 | + src: ['*.po'], |
| 116 | + dest: 'languages', |
| 117 | + ext: '.mo', |
| 118 | + nonull: false |
| 119 | + }] |
| 120 | + } |
| 121 | + }, |
| 122 | + |
| 123 | + // Bump version numbers (replace with version in package.json) |
| 124 | + replace: { |
| 125 | + php: { |
| 126 | + src: [ '<%= pkg.name %>.php' ], |
| 127 | + overwrite: true, |
| 128 | + replacements: [ |
| 129 | + { |
| 130 | + from: /Description:.*$/m, |
| 131 | + to: "Description: <%= pkg.description %>" |
| 132 | + }, |
| 133 | + { |
| 134 | + from: /WC requires at least:.*$/m, |
| 135 | + to: "WC requires at least: <%= pkg.wc_requires %>" |
| 136 | + }, |
| 137 | + { |
| 138 | + from: /WC tested up to:.*$/m, |
| 139 | + to: "WC tested up to: <%= pkg.wc_tested_up_to %>" |
| 140 | + }, |
| 141 | + { |
| 142 | + from: /Version:.*$/m, |
| 143 | + to: "Version: <%= pkg.version %>" |
| 144 | + }, |
| 145 | + { |
| 146 | + from: /public static \$version = \'.*.'/m, |
| 147 | + to: "public static $version = '<%= pkg.version %>'" |
| 148 | + } |
| 149 | + ] |
| 150 | + }, |
| 151 | + readme: { |
| 152 | + src: [ |
| 153 | + 'readme.txt', |
| 154 | + 'README.md' |
| 155 | + ], |
| 156 | + overwrite: true, |
| 157 | + replacements: [ |
| 158 | + { |
| 159 | + from: /Requires at least:(\*\*|)(\s*?)[0-9.-]+(\s*?)$/mi, |
| 160 | + to: 'Requires at least:$1$2<%= pkg.requires %>$3' |
| 161 | + }, |
| 162 | + { |
| 163 | + from: /Requires PHP:(\*\*|)(\s*?)[0-9.-]+(\s*?)$/mi, |
| 164 | + to: 'Requires PHP:$1$2<%= pkg.requires_php %>$3' |
| 165 | + }, |
| 166 | + { |
| 167 | + from: /Stable tag:(\*\*|)(\s*?)[0-9.-]+(\s*?)$/mi, |
| 168 | + to: 'Stable tag:$1$2<%= pkg.version %>$3' |
| 169 | + }, |
| 170 | + { |
| 171 | + from: /Tested up to:(\*\*|)(\s*?)[0-9.-]+(\s*?)$/mi, |
| 172 | + to: 'Tested up to:$1$2<%= pkg.tested_up_to %>$3' |
| 173 | + }, |
| 174 | + { |
| 175 | + from: /WC requires at least:(\*\*|)(\s*?)[0-9.-]+(\s*?)$/mi, |
| 176 | + to: 'WC requires at least:$1$2<%= pkg.wc_requires %>$3' |
| 177 | + }, |
| 178 | + { |
| 179 | + from: /WC tested up to:(\*\*|)(\s*?)[a-zA-Z0-9.-]+(\s*?)$/mi, |
| 180 | + to: 'WC tested up to:$1$2<%= pkg.wc_tested_up_to %>$3' |
| 181 | + }, |
| 182 | + ] |
| 183 | + } |
| 184 | + }, |
| 185 | + |
| 186 | + // Copies the plugin to create deployable plugin. |
| 187 | + copy: { |
| 188 | + build: { |
| 189 | + files: [ |
| 190 | + { |
| 191 | + expand: true, |
| 192 | + src: [ |
| 193 | + '**', |
| 194 | + '!.*', |
| 195 | + '!**/*.{gif,jpg,jpeg,js,json,log,md,png,scss,sh,txt,xml,zip}', |
| 196 | + '!.*/**', |
| 197 | + '!.DS_Store', |
| 198 | + '!.htaccess', |
| 199 | + '!assets/scss/**', |
| 200 | + '!assets/**/*.scss', |
| 201 | + '!<%= pkg.name %>-git/**', |
| 202 | + '!<%= pkg.name %>-svn/**', |
| 203 | + '!node_modules/**', |
| 204 | + '!releases/**', |
| 205 | + 'readme.txt', |
| 206 | + 'assets/images/**', |
| 207 | + 'assets/js/**' |
| 208 | + ], |
| 209 | + dest: 'build/', |
| 210 | + dot: true |
| 211 | + } |
| 212 | + ] |
| 213 | + } |
| 214 | + }, |
| 215 | + |
| 216 | + // Compresses the deployable plugin folder. |
| 217 | + compress: { |
| 218 | + zip: { |
| 219 | + options: { |
| 220 | + archive: './releases/<%= pkg.name %>-v<%= pkg.version %>.zip', |
| 221 | + mode: 'zip' |
| 222 | + }, |
| 223 | + files: [ |
| 224 | + { |
| 225 | + expand: true, |
| 226 | + cwd: './build/', |
| 227 | + src: '**', |
| 228 | + dest: '<%= pkg.name %>' |
| 229 | + } |
| 230 | + ] |
| 231 | + } |
| 232 | + }, |
| 233 | + |
| 234 | + // Deletes the deployable plugin folder once zipped up. |
| 235 | + clean: { |
| 236 | + build: [ 'build/' ] |
| 237 | + } |
| 238 | + }); |
| 239 | + |
| 240 | + // Set the default grunt command to run test cases. |
| 241 | + grunt.registerTask( 'default', [ 'test' ] ); |
| 242 | + |
| 243 | + // Checks for developer dependencies updates. |
| 244 | + grunt.registerTask( 'check', [ 'devUpdate' ] ); |
| 245 | + |
| 246 | + // Checks for errors. |
| 247 | + grunt.registerTask( 'test', [ 'checktextdomain' ]); |
| 248 | + |
| 249 | + // Update version of plugin. |
| 250 | + grunt.registerTask( 'version', [ 'replace' ] ); |
| 251 | + |
| 252 | + /** |
| 253 | + * Run i18n related tasks. |
| 254 | + * |
| 255 | + * This includes extracting translatable strings, updating the master pot file. |
| 256 | + * If this is part of a deploy process, it should come before zipping everything up. |
| 257 | + */ |
| 258 | + grunt.registerTask( 'update-pot', [ 'checktextdomain', 'makepot' ]); |
| 259 | + |
| 260 | + /** |
| 261 | + * Creates a deployable plugin zipped up ready to upload |
| 262 | + * and install on a WordPress installation. |
| 263 | + */ |
| 264 | + grunt.registerTask( 'zip', [ 'copy:build', 'compress', 'clean:build' ]); |
| 265 | +}; |
0 commit comments