-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcodacy_yaml.go
More file actions
178 lines (151 loc) · 5.53 KB
/
codacy_yaml.go
File metadata and controls
178 lines (151 loc) · 5.53 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Package configsetup provides helpers to create Codacy configuration
// files (codacy.yaml and related tool/runtime sections).
package configsetup
import (
"fmt"
"log"
"sort"
"strings"
"codacy/cli-v2/domain"
"codacy/cli-v2/plugins"
)
// RuntimePluginConfig holds the structure of the runtime plugin.yaml file
type RuntimePluginConfig struct {
Name string `yaml:"name"`
Description string `yaml:"description"`
DefaultVersion string `yaml:"default_version"`
}
// ConfigFileTemplate returns the content of codacy.yaml given the enabled
// tools. It lists required runtimes and tools with their versions.
func ConfigFileTemplate(tools []domain.Tool) string {
toolsMap := make(map[string]bool)
toolVersions := make(map[string]string)
neededRuntimes := make(map[string]bool)
toolsWithLatestVersion, _, _ := KeepToolsWithLatestVersion(tools)
// Get versions and runtime dependencies
defaultVersions := plugins.GetToolVersions()
runtimeVersions := plugins.GetRuntimeVersions()
runtimeDependencies := plugins.GetToolRuntimeDependencies()
// Process enabled tools
for _, tool := range toolsWithLatestVersion {
toolsMap[tool.Uuid] = true
toolVersions[tool.Uuid] = getToolVersion(tool, defaultVersions)
addRequiredRuntime(tool.Uuid, neededRuntimes, runtimeDependencies)
}
var sb strings.Builder
// Build runtimes section
buildRuntimesSection(&sb, tools, neededRuntimes, runtimeVersions, runtimeDependencies)
// Build tools section
buildToolsSection(&sb, tools, toolsMap, toolVersions, defaultVersions)
return sb.String()
}
// getToolVersion returns the version for a tool, preferring tool.Version over default.
func getToolVersion(tool domain.Tool, defaultVersions map[string]string) string {
if tool.Version != "" {
return tool.Version
}
if meta, ok := domain.SupportedToolsMetadata[tool.Uuid]; ok {
if defaultVersion, ok := defaultVersions[meta.Name]; ok {
return defaultVersion
}
}
return ""
}
// addRequiredRuntime adds the runtime requirement for a tool.
func addRequiredRuntime(toolUUID string, neededRuntimes map[string]bool, runtimeDependencies map[string]string) {
if meta, ok := domain.SupportedToolsMetadata[toolUUID]; ok {
if runtime, ok := runtimeDependencies[meta.Name]; ok {
if meta.Name == "dartanalyzer" {
// For dartanalyzer, default to dart runtime
neededRuntimes["dart"] = true
} else {
neededRuntimes[runtime] = true
}
}
}
}
// buildRuntimesSection builds the runtimes section of the configuration.
func buildRuntimesSection(sb *strings.Builder, tools []domain.Tool, neededRuntimes map[string]bool, runtimeVersions map[string]string, runtimeDependencies map[string]string) {
sb.WriteString("runtimes:\n")
if len(tools) == 0 {
// In local mode with no tools specified, include all necessary runtimes
addAllSupportedRuntimes(neededRuntimes, runtimeDependencies)
}
writeRuntimesList(sb, neededRuntimes, runtimeVersions)
}
// addAllSupportedRuntimes adds all runtimes needed by supported tools.
func addAllSupportedRuntimes(neededRuntimes map[string]bool, runtimeDependencies map[string]string) {
supportedTools, err := plugins.GetSupportedTools()
if err != nil {
log.Printf("Warning: failed to get supported tools: %v", err)
return
}
for toolName := range supportedTools {
if runtime, ok := runtimeDependencies[toolName]; ok {
if toolName == "dartanalyzer" {
neededRuntimes["dart"] = true
} else {
neededRuntimes[runtime] = true
}
}
}
}
// writeRuntimesList writes the sorted runtimes list to the string builder.
func writeRuntimesList(sb *strings.Builder, neededRuntimes map[string]bool, runtimeVersions map[string]string) {
var sortedRuntimes []string
for runtime := range neededRuntimes {
sortedRuntimes = append(sortedRuntimes, runtime)
}
sort.Strings(sortedRuntimes)
for _, runtime := range sortedRuntimes {
sb.WriteString(fmt.Sprintf(" - %s@%s\n", runtime, runtimeVersions[runtime]))
}
}
// buildToolsSection builds the tools section of the configuration.
func buildToolsSection(sb *strings.Builder, tools []domain.Tool, toolsMap map[string]bool, toolVersions map[string]string, defaultVersions map[string]string) {
sb.WriteString("tools:\n")
if len(tools) > 0 {
writeEnabledTools(sb, toolsMap, toolVersions)
} else {
writeAllSupportedTools(sb, defaultVersions)
}
}
// writeEnabledTools writes the enabled tools to the string builder.
func writeEnabledTools(sb *strings.Builder, toolsMap map[string]bool, toolVersions map[string]string) {
var sortedTools []string
for uuid, meta := range domain.SupportedToolsMetadata {
if toolsMap[uuid] {
sortedTools = append(sortedTools, meta.Name)
}
}
sort.Strings(sortedTools)
for _, name := range sortedTools {
for uuid, meta := range domain.SupportedToolsMetadata {
if meta.Name == name && toolsMap[uuid] {
version := toolVersions[uuid]
sb.WriteString(fmt.Sprintf(" - %s@%s\n", name, version))
break
}
}
}
}
// writeAllSupportedTools writes all supported tools to the string builder.
func writeAllSupportedTools(sb *strings.Builder, defaultVersions map[string]string) {
supportedTools, err := plugins.GetSupportedTools()
if err != nil {
log.Printf("Warning: failed to get supported tools: %v", err)
return
}
var sortedTools []string
for toolName := range supportedTools {
if version, ok := defaultVersions[toolName]; ok && version != "" {
sortedTools = append(sortedTools, toolName)
}
}
sort.Strings(sortedTools)
for _, toolName := range sortedTools {
if version, ok := defaultVersions[toolName]; ok {
sb.WriteString(fmt.Sprintf(" - %s@%s\n", toolName, version))
}
}
}