From 38fff2d83fd8358250a5f0c83cdbbbdc5c660eef Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Mon, 18 Apr 2022 15:25:56 +0800 Subject: [PATCH] MDL-74511 core: Add phpcs config generator --- .gitignore | 1 + .grunt/tasks/ignorefiles.js | 39 +++++++++++++++++++++++++++++++++++++ npm-shrinkwrap.json | 13 +++++++++++++ package.json | 1 + 4 files changed, 54 insertions(+) diff --git a/.gitignore b/.gitignore index 780bcc878fe..cc04ae26f86 100644 --- a/.gitignore +++ b/.gitignore @@ -54,3 +54,4 @@ moodle-plugin-ci.phar /admin/tool/componentlibrary/docs /admin/tool/componentlibrary/hugo/site/data/my-index.json .hugo_build.lock +.phpcs.xml diff --git a/.grunt/tasks/ignorefiles.js b/.grunt/tasks/ignorefiles.js index 43cf56d0147..5d1b83e2722 100644 --- a/.grunt/tasks/ignorefiles.js +++ b/.grunt/tasks/ignorefiles.js @@ -21,6 +21,43 @@ */ module.exports = grunt => { + + /** + * Generate the PHPCS configuration. + * + * @param {Object} thirdPartyPaths + */ + const phpcsIgnore = (thirdPartyPaths) => { + const {toXML} = require('jstoxml'); + + const config = { + _name: 'ruleset', + _attrs: { + name: "MoodleCore", + }, + _content: [ + { + rule: { + _attrs: { + ref: 'moodle', + }, + }, + }, + ], + }; + + thirdPartyPaths.forEach(library => { + config._content.push({ + 'exclude-pattern': library, + }); + }); + + grunt.file.write('.phpcs.xml', toXML(config, { + header: true, + indent: ' ', + }) + "\n"); + }; + /** * Generate ignore files (utilising thirdpartylibs.xml data) */ @@ -53,6 +90,8 @@ module.exports = grunt => { 'admin/tool/componentlibrary/hugo/dist/css/docs.css', ].concat(thirdPartyPaths); grunt.file.write('.stylelintignore', stylelintIgnores.join('\n') + '\n'); + + phpcsIgnore(thirdPartyPaths); }; grunt.registerTask('ignorefiles', 'Generate ignore files for linters', handler); diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 8a18c5cd901..3c53398cf18 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -43,6 +43,7 @@ "jsdoc": "^3.6.10", "jsdoc-to-markdown": "^7.1.1", "jshint": "^2.13.4", + "jstoxml": "^3.2.3", "node-sass": "^7.0.1", "npm-run-all": "^4.1.5", "rollup-plugin-terser": "^7.0.2", @@ -8566,6 +8567,12 @@ "node": ">=0.6.0" } }, + "node_modules/jstoxml": { + "version": "3.2.3", + "resolved": "https://registry.npmjs.org/jstoxml/-/jstoxml-3.2.3.tgz", + "integrity": "sha512-IxoZkdFcKm1dO4g+JcZBB7z1p/vPXEZPV3APRDd3/zcVtthg2kfTmpzKNZMl37/MrE/Uoc2p6ZnLnv3P5HVMTw==", + "dev": true + }, "node_modules/keyv": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/keyv/-/keyv-3.0.0.tgz", @@ -20632,6 +20639,12 @@ "verror": "1.10.0" } }, + "jstoxml": { + "version": "3.2.3", + "resolved": "https://registry.npmjs.org/jstoxml/-/jstoxml-3.2.3.tgz", + "integrity": "sha512-IxoZkdFcKm1dO4g+JcZBB7z1p/vPXEZPV3APRDd3/zcVtthg2kfTmpzKNZMl37/MrE/Uoc2p6ZnLnv3P5HVMTw==", + "dev": true + }, "keyv": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/keyv/-/keyv-3.0.0.tgz", diff --git a/package.json b/package.json index e1954f02ccf..5baef68aab9 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "jsdoc": "^3.6.10", "jsdoc-to-markdown": "^7.1.1", "jshint": "^2.13.4", + "jstoxml": "^3.2.3", "node-sass": "^7.0.1", "npm-run-all": "^4.1.5", "rollup-plugin-terser": "^7.0.2",