[483] | 1 | //Checkstyle script for Dojo |
---|
| 2 | var buildTimerStart = (new Date()).getTime(); |
---|
| 3 | |
---|
| 4 | load("../buildscripts/jslib/logger.js"); |
---|
| 5 | load("../buildscripts/jslib/fileUtil.js"); |
---|
| 6 | load("checkstyleUtil.js"); |
---|
| 7 | |
---|
| 8 | //***************************************************************************** |
---|
| 9 | |
---|
| 10 | if(arguments[0] == "help"){ |
---|
| 11 | print("Usage: \n\tTo generate a HTML report on dojo, dijit and dojox folders use:\n\t\t" |
---|
| 12 | + "checkstyle \n\t" |
---|
| 13 | + "To specify a single directory to check in, including all child folders, use:\n\t\t" |
---|
| 14 | + "checkstyle dir={folderName}\n\t" |
---|
| 15 | + "To specify directories to ignore, use:\n\t\t" |
---|
| 16 | + "checkstyle ignoreDirs={folderName1},{folderName2}\n\t" |
---|
| 17 | + "To specify a list of files to process, use the 'files' parameter, passing a list of space separated files, e.g.\n\t\t" |
---|
| 18 | + "checkstyle files=\"dojo/_base/Color.js dojo/back.js\"\n\t" |
---|
| 19 | + "To force the script to fail when a specified file has failed the check, use the 'failOnError' parameter, e.g.\n\t\t" |
---|
| 20 | + "checkstyle failOnError=true files=\"dojo/_base/Color.js dojo/back.js\"\n\t" |
---|
| 21 | + "To commit fixes made by the checkstyleReport.html tool, use\n\t\t" |
---|
| 22 | + "checkstyle commit"); |
---|
| 23 | |
---|
| 24 | } else if(arguments[0] == "commit"){ |
---|
| 25 | runCommit(); |
---|
| 26 | } else{ |
---|
| 27 | |
---|
| 28 | //Convert arguments to keyword arguments. |
---|
| 29 | var kwArgs = convertArrayToObject(arguments); |
---|
| 30 | |
---|
| 31 | checkstyle(); |
---|
| 32 | |
---|
| 33 | var buildTime = ((new Date().getTime() - buildTimerStart) / 1000); |
---|
| 34 | print("Build time: " + buildTime + " seconds"); |
---|
| 35 | |
---|
| 36 | } |
---|
| 37 | //***************************************************************************** |
---|
| 38 | |
---|
| 39 | // Take from old buildUtil.js in 1.6 |
---|
| 40 | function convertArrayToObject(/*Array*/ary){ |
---|
| 41 | // summary: |
---|
| 42 | // converts an array that has String members of "name=value" |
---|
| 43 | // into an object, where the properties on the object are the names in the array |
---|
| 44 | // member name/value pairs. |
---|
| 45 | var result = {}; |
---|
| 46 | for(var i = 0; i < ary.length; i++){ |
---|
| 47 | var separatorIndex = ary[i].indexOf("="); |
---|
| 48 | if(separatorIndex == -1){ |
---|
| 49 | throw "Malformed name/value pair: [" + ary[i] + "]. Format should be name=value"; |
---|
| 50 | } |
---|
| 51 | result[ary[i].substring(0, separatorIndex)] = ary[i].substring(separatorIndex + 1, ary[i].length); |
---|
| 52 | } |
---|
| 53 | return result; //Object |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | //********* Start checkstyle ********* |
---|
| 57 | function checkstyle(){ |
---|
| 58 | |
---|
| 59 | var dirs, i, ignoreDirs; |
---|
| 60 | var reportFile = "./checkstyleData.js"; |
---|
| 61 | |
---|
| 62 | |
---|
| 63 | if(kwArgs.files){ |
---|
| 64 | var files = kwArgs.files.split(" "); |
---|
| 65 | |
---|
| 66 | for(i = 0; i < files.length; i++){ |
---|
| 67 | checkstyleUtil.applyRules("../../" + files[i], fileUtil.readFile("../../" + files[i])); |
---|
| 68 | } |
---|
| 69 | if(checkstyleUtil.errors){ |
---|
| 70 | var errors = checkstyleUtil.serializeErrors(); |
---|
| 71 | if(kwArgs.failOnError == "true"){ |
---|
| 72 | throw Error("Checkstyle failed. \n" + errors); |
---|
| 73 | } else{ |
---|
| 74 | print(errors); |
---|
| 75 | } |
---|
| 76 | } |
---|
| 77 | return; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | |
---|
| 81 | if(kwArgs.dir){ |
---|
| 82 | dirs = [kwArgs.dir]; |
---|
| 83 | } else{ |
---|
| 84 | dirs = ["dojo", "dijit", "dojox"]; |
---|
| 85 | } |
---|
| 86 | if(kwArgs.ignoreDirs){ |
---|
| 87 | ignoreDirs = kwArgs.ignoreDirs.split(","); |
---|
| 88 | }else{ |
---|
| 89 | ignoreDirs = []; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | for(i = 0; i < dirs.length; i++){ |
---|
| 93 | var fileList = fileUtil.getFilteredFileList("../../" + dirs[i], /\.js$/, true); |
---|
| 94 | for(var j = 0; j < fileList.length; j++){ |
---|
| 95 | if(fileList[j].indexOf("/test") < 0 |
---|
| 96 | && fileList[j].indexOf("/nls") < 0 |
---|
| 97 | && fileList[j].indexOf("/demos") < 0){ |
---|
| 98 | |
---|
| 99 | var ignore = false; |
---|
| 100 | if(ignoreDirs.length > 0){ |
---|
| 101 | for(var k = 0; k < ignoreDirs.length; k++){ |
---|
| 102 | if(fileList[j].indexOf(ignoreDirs[k]) > -1){ |
---|
| 103 | ignore = true; |
---|
| 104 | break; |
---|
| 105 | }else{ |
---|
| 106 | |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | if(!ignore){ |
---|
| 112 | checkstyleUtil.applyRules(fileList[j], fileUtil.readFile(fileList[j])); |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | } |
---|
| 117 | var report = checkstyleUtil.generateReport(); |
---|
| 118 | fileUtil.saveUtf8File(reportFile, report); |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | function runCommit(){ |
---|
| 122 | var dirs = ["dojo", "dijit", "dojox"]; |
---|
| 123 | |
---|
| 124 | var committedFiles = []; |
---|
| 125 | |
---|
| 126 | for(var i = 0; i < dirs.length; i++){ |
---|
| 127 | var fileList = fileUtil.getFilteredFileList("../../" + dirs[i], /\.checkstyle.js$/, true); |
---|
| 128 | |
---|
| 129 | for(var j = 0; j < fileList.length; j++){ |
---|
| 130 | if(fileList[j].indexOf("/test") < 0 |
---|
| 131 | && fileList[j].indexOf("/nls") < 0 |
---|
| 132 | && fileList[j].indexOf("/demos") < 0){ |
---|
| 133 | var fileName = fileList[j].substring(0, fileList[j].length - ".checkstyle.js".length); |
---|
| 134 | fileUtil.saveUtf8File(fileName, fileUtil.readFile(fileList[j])); |
---|
| 135 | fileUtil.deleteFile(fileList[j]); |
---|
| 136 | committedFiles.push(fileName); |
---|
| 137 | } |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | print("Committed checkstyle fixes for the following files:\n" + committedFiles.join("\n")); |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | //********* End checkstyle ********* |
---|