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