forked from CodeNarc/CodeNarc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codenarc.groovy
executable file
·168 lines (133 loc) · 4.31 KB
/
codenarc.groovy
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env groovy
import groovy.text.SimpleTemplateEngine
import static java.nio.charset.StandardCharsets.UTF_8
if (!args) {
println usage()
return
}
if (args[0] == 'create-rule') {
print "Enter your name:"
def authorName = getUserInput()
print "Enter the rule name:"
def ruleName = removeRuleSuffix(getUserInput())
def ruleCategory = getRuleCategory()
print "Enter the rule description:"
def ruleDescription = getUserInput()
def binding = ['ruleName':ruleName, 'ruleCategory':ruleCategory, 'authorName': authorName, 'ruleDescription': ruleDescription]
def ruleFile = makeRule(binding)
def testFile = makeRuleTest(binding)
updatePropertiesFile(ruleName, ruleDescription)
updateRuleList(ruleName, ruleCategory)
updateSiteDocumentation(ruleName, ruleCategory, ruleDescription)
updateChangelog(ruleName, ruleCategory, ruleDescription)
print "\tadding to git... "
print "git add -v $ruleFile".execute().text
print "\tadding to git... "
print "git add -v $testFile".execute().text
println "\tFinished"
} else {
println usage()
}
/*
* The rest of the code is here to support create-rule.
*/
def makeRule(binding) {
makeFromTemplate(
binding, 'Rule.groovy',
"./src/main/groovy/org/codenarc/rule/${binding.ruleCategory}/${binding.ruleName}Rule.groovy")
}
def makeRuleTest(binding) {
makeFromTemplate(
binding,
'Test.groovy',
"./src/test/groovy/org/codenarc/rule/${binding.ruleCategory}/${binding.ruleName}RuleTest.groovy")
}
def makeFromTemplate(binding, templateName, targetPath) {
def file = new File(targetPath)
file.createNewFile()
def ruleTemplate = new File("./templates/$templateName").text
def engine = new SimpleTemplateEngine()
def rule = engine.createTemplate(ruleTemplate).make(binding)
file.text = rule.toString()
println "\tCreated $targetPath"
targetPath
}
def updateRuleList(ruleName, ruleCategory) {
def path = "./src/main/resources/rulesets/${ruleCategory}.xml"
def file = new File(path)
file.text = file.text.replaceAll(
'</ruleset>',
" <rule class='org.codenarc.rule.${ruleCategory}.${ruleName}Rule'/>\n</ruleset>")
println "\tUpdated $path"
}
def updateSiteDocumentation(ruleName, ruleCategory, ruleDescription) {
def path = "./docs/codenarc-rules-${ruleCategory}.md"
new File(path).append """
## $ruleName Rule
<Since CodeNarc 3.4.0>
$ruleDescription
Example of violations:
```
// TODO: Add examples
```
"""
println "\tUpdated $path"
}
def updateChangelog(ruleName, ruleCategory, ruleDescription) {
def path = './CHANGELOG.md'
File file = new File(path)
String original = file.getText(UTF_8.name())
file.setText("""
#TODO: Sort the following line into the file
- $ruleName rule ($ruleCategory) - $ruleDescription
$original""", UTF_8.name())
println "\tUpdated $path"
}
def updatePropertiesFile(ruleName, ruleDescription) {
def path = './src/main/resources/codenarc-base-messages.properties'
def file = new File(path)
file.text = """
# todo: manually sort your messages into the correct location
${ruleName}.description=$ruleDescription
${ruleName}.description.html=$ruleDescription
""" + file.text
println "\tUpdated $path"
}
def getRuleCategory() {
def categories = getValidRuleCategories()
println "Enter the rule category. Valid categories are:\n ${categories.sort().join("\n ")}"
while(true) {
def input = getUserInput()
if (categories.contains(input)) {
return input
}
println "Invalid category. Valid categories are:\n ${categories.sort().join("\n ")}"
}
}
def getUserInput() {
def input = new java.util.Scanner(System.in)
input.nextLine()
}
def getValidRuleCategories() {
def categories = []
new File("./src/main/groovy/org/codenarc/rule/").eachDir() { dir->
if (dir.name != '.svn') categories << dir.name
}
categories
}
String removeRuleSuffix(String initialRuleName) {
removeSuffix(initialRuleName, 'Rule')
}
String removeSuffix(String input, String suffix) {
if (input.endsWith(suffix)) {
return input.substring(0, input.lastIndexOf(suffix))
}
return input
}
def usage() {
"""
Usage: codenarc [OPTION]
Valid Options:
create-rule - Creates a new CodeNarc rule
"""
}