Skip to content

Commit 9247e50

Browse files
committed
fix: refactor setup and fix Lizard remote init
1 parent a07e0f3 commit 9247e50

File tree

7 files changed

+829
-780
lines changed

7 files changed

+829
-780
lines changed

cmd/configsetup/codacy_yaml.go

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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+
}

cmd/configsetup/config_creators.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package configsetup
2+
3+
import (
4+
"fmt"
5+
"path/filepath"
6+
7+
"codacy/cli-v2/config"
8+
"codacy/cli-v2/constants"
9+
"codacy/cli-v2/domain"
10+
"codacy/cli-v2/tools"
11+
12+
"gopkg.in/yaml.v3"
13+
)
14+
15+
func CreateLanguagesConfigFileLocal(toolsConfigDir string) error {
16+
// Build tool language configurations from API
17+
configTools, err := tools.BuildLanguagesConfigFromAPI()
18+
if err != nil {
19+
return fmt.Errorf("failed to build languages config from API: %w", err)
20+
}
21+
22+
// Create the config structure
23+
config := domain.LanguagesConfig{
24+
Tools: configTools,
25+
}
26+
27+
// Marshal to YAML
28+
data, err := yaml.Marshal(config)
29+
if err != nil {
30+
return fmt.Errorf("failed to marshal languages config to YAML: %w", err)
31+
}
32+
33+
return writeConfigFile(filepath.Join(toolsConfigDir, constants.LanguagesConfigFileName), data)
34+
}
35+
36+
func CreateGitIgnoreFile() error {
37+
gitIgnorePath := filepath.Join(config.Config.LocalCodacyDirectory(), constants.GitIgnoreFileName)
38+
content := "# Codacy CLI\ntools-configs/\n.gitignore\ncli-config.yaml\nlogs/\n"
39+
return writeConfigFile(gitIgnorePath, []byte(content))
40+
}
41+
42+
func CreateConfigurationFiles(tools []domain.Tool, cliLocalMode bool, flags domain.InitFlags) error {
43+
// Create project config file
44+
configContent := ConfigFileTemplate(tools)
45+
if err := writeConfigFile(config.Config.ProjectConfigFile(), []byte(configContent)); err != nil {
46+
return fmt.Errorf("failed to write project config file: %w", err)
47+
}
48+
49+
// Create CLI config file
50+
cliConfigContent := buildCliConfigContent(cliLocalMode, flags)
51+
if err := writeConfigFile(config.Config.CliConfigFile(), []byte(cliConfigContent)); err != nil {
52+
return fmt.Errorf("failed to write CLI config file: %w", err)
53+
}
54+
55+
return nil
56+
}
57+
58+
// buildCliConfigContent creates the CLI configuration content
59+
func buildCliConfigContent(cliLocalMode bool, initFlags domain.InitFlags) string {
60+
if cliLocalMode {
61+
return fmt.Sprintf("mode: local")
62+
} else {
63+
return fmt.Sprintf("mode: remote\nprovider: %s\norganization: %s\nrepository: %s", initFlags.Provider, initFlags.Organization, initFlags.Repository)
64+
}
65+
}

0 commit comments

Comments
 (0)