-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathComplex.scala copy
More file actions
149 lines (120 loc) · 5.21 KB
/
Complex.scala copy
File metadata and controls
149 lines (120 loc) · 5.21 KB
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
package codacy.detekt
import java.nio.file.{Path, Paths}
import java.util
import codacy.docker.api
import codacy.docker.api._
import codacy.dockerApi.utils.FileHelper
import io.gitlab.arturbosch.detekt.api._
import io.gitlab.arturbosch.detekt.core._
import org.yaml.snakeyaml.Yaml
import play.api.libs.json.{JsString, Json}
import scala.collection.JavaConversions._
import scala.io.Source
import scala.util.Try
object Detekt extends Tool {
case class DetektCategory(value: String) extends AnyVal
val configFiles = Set("default-detekt-config.yml", "detekt.yml")
private lazy val patternIdToCategory: Map[Pattern.Id, DetektCategory] = {
for {
(cat, rules) <- getRules
rule <- rules
} yield Pattern.Id(rule.getId) -> cat
}
override def apply(source: api.Source.Directory, configuration: Option[List[Pattern.Definition]], filesOpt: Option[Set[api.Source.File]])
(implicit specification: Tool.Specification): Try[List[Result]] = {
Try {
val sourcePath = Paths.get(source.path)
val yamlConfig = getYamlConfig(configuration, sourcePath)
val findings = getResults(sourcePath, filesOpt, yamlConfig, parallel = true)
findings.map { finding =>
Result.Issue(
api.Source.File(finding.getFile),
Result.Message(finding.compact()),
Pattern.Id(finding.getId),
codacy.docker.api.Source.Line(finding.getLocation.getSource.getLine)
)
}
}
}
private def getYamlConfig(config: Option[List[Pattern.Definition]], source: Path)
(implicit specification: Tool.Specification): YamlConfig = {
config.fold {
defaultConfig(source)
} {
mapConfig
}
}
private def defaultConfig(source: Path): YamlConfig = {
val map = FileHelper.findConfigurationFile(configFiles, source)
.fold(new util.LinkedHashMap[String, Any]()) {
configFile =>
new Yaml()
.load(Source.fromFile(configFile.toFile).getLines().mkString("\n"))
.asInstanceOf[util.LinkedHashMap[String, Any]]
}
new YamlConfig(map)
}
private def mapConfig(config: List[Pattern.Definition])(implicit specification: Tool.Specification): YamlConfig = {
val configPatternsById: Map[Pattern.Id, Pattern.Definition] =
config.map(patternDef => (patternDef.patternId, patternDef))(collection.breakOut)
val configMapGen = specification.patterns.groupBy(patDef => patternIdToCategory(patDef.patternId)).map {
case (cat, patternSpecs) =>
cat -> patternSpecs.map(spec => (spec.patternId, configPatternsById.get(spec.patternId)))
}.map { case (category, patternsRaw) =>
val patterns: Map[String, Any] = patternsRaw.map { case (patternId, patternDef) =>
val parameters: Map[String, Any] = patternDef.flatMap(_.parameters).getOrElse(Set.empty)
.map { param =>
val pValue = JsonApi.paramValueToJsValue(param.value) match {
case JsString(value) => value
case value => Json.stringify(value)
}
(param.name.value, pValue)
}(collection.breakOut)
val isInConfig = patternDef.isDefined
(patternId.value, new util.LinkedHashMap[String, Any](Map(("active", isInConfig)) ++ parameters))
}(collection.breakOut)
(category.value.toLowerCase, new util.LinkedHashMap[String, Any](Map(("active", patternsRaw.nonEmpty)) ++ patterns))
}
val ourConf = new util.LinkedHashMap[String, Any](Map(("autoCorrect", false), ("failFast", false)) ++ configMapGen)
new YamlConfig(ourConf)
}
private def getRules: Map[DetektCategory, Seq[Rule]] = {
val config = readEmptyConfigFile
_root_.codacy.helpers.ResourceHelper
.getResourceContent("META-INF/services/io.gitlab.arturbosch.detekt.api.RuleSetProvider")
.get //This is just a script, is better get the error
.map {
clazz =>
val provider: RuleSet = Class.forName(clazz)
.getDeclaredConstructor()
.newInstance()
.asInstanceOf[RuleSetProvider]
.instance(config)
(DetektCategory(provider.getId), provider.getRules
.flatMap {
case r: MultiRule =>
r.getRules
case r: Rule =>
Seq(r)
})
}(collection.breakOut)
}
private def readEmptyConfigFile: Config = {
new YamlConfig(new util.LinkedHashMap[String, Any](Map(("autoCorrect", false), ("failFast", false))))
}
private def getResults(path: Path, filesOpt: Option[Set[api.Source.File]], yamlConf: YamlConfig, parallel: Boolean): List[Finding] = {
val settings = new ProcessingSettings(path, yamlConf, List.empty[PathFilter], parallel, false, List.empty[Path])
val providers = new RuleSetLocator(settings).load()
val processors = List.empty[FileProcessListener]
val detektor = new Detektor(settings, providers, processors)
val compiler = new KtTreeCompiler(new KtCompiler(), settings.getPathFilters, parallel)
val detektion = filesOpt.fold {
val ktFiles = compiler.compile(path)
detektor.run(ktFiles)
} { files =>
val ktFiles = files.par.flatMap(file => compiler.compile(Paths.get(file.path))).to[List]
detektor.run(ktFiles)
}
detektion.values.flatten.toList
}
}