|
| 1 | +package configsetup |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "sort" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "codacy/cli-v2/domain" |
| 10 | + "codacy/cli-v2/plugins" |
| 11 | +) |
| 12 | + |
| 13 | +// RuntimePluginConfig holds the structure of the runtime plugin.yaml file |
| 14 | +type RuntimePluginConfig struct { |
| 15 | + Name string `yaml:"name"` |
| 16 | + Description string `yaml:"description"` |
| 17 | + DefaultVersion string `yaml:"default_version"` |
| 18 | +} |
| 19 | + |
| 20 | +func ConfigFileTemplate(tools []domain.Tool) string { |
| 21 | + toolsMap := make(map[string]bool) |
| 22 | + toolVersions := make(map[string]string) |
| 23 | + neededRuntimes := make(map[string]bool) |
| 24 | + |
| 25 | + toolsWithLatestVersion, _, _ := KeepToolsWithLatestVersion(tools) |
| 26 | + |
| 27 | + // Get versions and runtime dependencies |
| 28 | + defaultVersions := plugins.GetToolVersions() |
| 29 | + runtimeVersions := plugins.GetRuntimeVersions() |
| 30 | + runtimeDependencies := plugins.GetToolRuntimeDependencies() |
| 31 | + |
| 32 | + // Process enabled tools |
| 33 | + for _, tool := range toolsWithLatestVersion { |
| 34 | + toolsMap[tool.Uuid] = true |
| 35 | + toolVersions[tool.Uuid] = getToolVersion(tool, defaultVersions) |
| 36 | + addRequiredRuntime(tool.Uuid, neededRuntimes, runtimeDependencies) |
| 37 | + } |
| 38 | + |
| 39 | + var sb strings.Builder |
| 40 | + |
| 41 | + // Build runtimes section |
| 42 | + buildRuntimesSection(&sb, tools, neededRuntimes, runtimeVersions, runtimeDependencies) |
| 43 | + |
| 44 | + // Build tools section |
| 45 | + buildToolsSection(&sb, tools, toolsMap, toolVersions, defaultVersions) |
| 46 | + |
| 47 | + return sb.String() |
| 48 | +} |
| 49 | + |
| 50 | +// getToolVersion returns the version for a tool, preferring tool.Version over default |
| 51 | +func getToolVersion(tool domain.Tool, defaultVersions map[string]string) string { |
| 52 | + if tool.Version != "" { |
| 53 | + return tool.Version |
| 54 | + } |
| 55 | + if meta, ok := domain.SupportedToolsMetadata[tool.Uuid]; ok { |
| 56 | + if defaultVersion, ok := defaultVersions[meta.Name]; ok { |
| 57 | + return defaultVersion |
| 58 | + } |
| 59 | + } |
| 60 | + return "" |
| 61 | +} |
| 62 | + |
| 63 | +// addRequiredRuntime adds the runtime requirement for a tool |
| 64 | +func addRequiredRuntime(toolUuid string, neededRuntimes map[string]bool, runtimeDependencies map[string]string) { |
| 65 | + if meta, ok := domain.SupportedToolsMetadata[toolUuid]; ok { |
| 66 | + if runtime, ok := runtimeDependencies[meta.Name]; ok { |
| 67 | + if meta.Name == "dartanalyzer" { |
| 68 | + // For dartanalyzer, default to dart runtime |
| 69 | + neededRuntimes["dart"] = true |
| 70 | + } else { |
| 71 | + neededRuntimes[runtime] = true |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// buildRuntimesSection builds the runtimes section of the configuration |
| 78 | +func buildRuntimesSection(sb *strings.Builder, tools []domain.Tool, neededRuntimes map[string]bool, runtimeVersions map[string]string, runtimeDependencies map[string]string) { |
| 79 | + sb.WriteString("runtimes:\n") |
| 80 | + |
| 81 | + if len(tools) == 0 { |
| 82 | + // In local mode with no tools specified, include all necessary runtimes |
| 83 | + addAllSupportedRuntimes(neededRuntimes, runtimeDependencies) |
| 84 | + } |
| 85 | + |
| 86 | + writeRuntimesList(sb, neededRuntimes, runtimeVersions) |
| 87 | +} |
| 88 | + |
| 89 | +// addAllSupportedRuntimes adds all runtimes needed by supported tools |
| 90 | +func addAllSupportedRuntimes(neededRuntimes map[string]bool, runtimeDependencies map[string]string) { |
| 91 | + supportedTools, err := plugins.GetSupportedTools() |
| 92 | + if err != nil { |
| 93 | + log.Printf("Warning: failed to get supported tools: %v", err) |
| 94 | + return |
| 95 | + } |
| 96 | + |
| 97 | + for toolName := range supportedTools { |
| 98 | + if runtime, ok := runtimeDependencies[toolName]; ok { |
| 99 | + if toolName == "dartanalyzer" { |
| 100 | + neededRuntimes["dart"] = true |
| 101 | + } else { |
| 102 | + neededRuntimes[runtime] = true |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// writeRuntimesList writes the sorted runtimes list to the string builder |
| 109 | +func writeRuntimesList(sb *strings.Builder, neededRuntimes map[string]bool, runtimeVersions map[string]string) { |
| 110 | + var sortedRuntimes []string |
| 111 | + for runtime := range neededRuntimes { |
| 112 | + sortedRuntimes = append(sortedRuntimes, runtime) |
| 113 | + } |
| 114 | + sort.Strings(sortedRuntimes) |
| 115 | + |
| 116 | + for _, runtime := range sortedRuntimes { |
| 117 | + sb.WriteString(fmt.Sprintf(" - %s@%s\n", runtime, runtimeVersions[runtime])) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +// buildToolsSection builds the tools section of the configuration |
| 122 | +func buildToolsSection(sb *strings.Builder, tools []domain.Tool, toolsMap map[string]bool, toolVersions map[string]string, defaultVersions map[string]string) { |
| 123 | + sb.WriteString("tools:\n") |
| 124 | + |
| 125 | + if len(tools) > 0 { |
| 126 | + writeEnabledTools(sb, toolsMap, toolVersions) |
| 127 | + } else { |
| 128 | + writeAllSupportedTools(sb, defaultVersions) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +// writeEnabledTools writes the enabled tools to the string builder |
| 133 | +func writeEnabledTools(sb *strings.Builder, toolsMap map[string]bool, toolVersions map[string]string) { |
| 134 | + var sortedTools []string |
| 135 | + for uuid, meta := range domain.SupportedToolsMetadata { |
| 136 | + if toolsMap[uuid] { |
| 137 | + sortedTools = append(sortedTools, meta.Name) |
| 138 | + } |
| 139 | + } |
| 140 | + sort.Strings(sortedTools) |
| 141 | + |
| 142 | + for _, name := range sortedTools { |
| 143 | + for uuid, meta := range domain.SupportedToolsMetadata { |
| 144 | + if meta.Name == name && toolsMap[uuid] { |
| 145 | + version := toolVersions[uuid] |
| 146 | + sb.WriteString(fmt.Sprintf(" - %s@%s\n", name, version)) |
| 147 | + break |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +// writeAllSupportedTools writes all supported tools to the string builder |
| 154 | +func writeAllSupportedTools(sb *strings.Builder, defaultVersions map[string]string) { |
| 155 | + supportedTools, err := plugins.GetSupportedTools() |
| 156 | + if err != nil { |
| 157 | + log.Printf("Warning: failed to get supported tools: %v", err) |
| 158 | + return |
| 159 | + } |
| 160 | + |
| 161 | + var sortedTools []string |
| 162 | + for toolName := range supportedTools { |
| 163 | + if version, ok := defaultVersions[toolName]; ok && version != "" { |
| 164 | + sortedTools = append(sortedTools, toolName) |
| 165 | + } |
| 166 | + } |
| 167 | + sort.Strings(sortedTools) |
| 168 | + |
| 169 | + for _, toolName := range sortedTools { |
| 170 | + if version, ok := defaultVersions[toolName]; ok { |
| 171 | + sb.WriteString(fmt.Sprintf(" - %s@%s\n", toolName, version)) |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments