55 "log"
66 "os"
77 "path/filepath"
8+ "strings"
89
910 "codacy/cli-v2/plugins"
1011 "codacy/cli-v2/utils"
@@ -78,6 +79,59 @@ func (c *ConfigType) AddRuntimes(configs []plugins.RuntimeConfig) error {
7879 // Store the runtime information in the config
7980 for name , info := range runtimeInfoMap {
8081 c .runtimes [name ] = info
82+
83+ // Update codacy.yaml with the new runtime
84+ if err := addRuntimeToCodacyYaml (name , info .Version ); err != nil {
85+ return fmt .Errorf ("failed to update codacy.yaml with runtime %s: %w" , name , err )
86+ }
87+ }
88+
89+ return nil
90+ }
91+
92+ // addRuntimeToCodacyYaml adds or updates a runtime entry in .codacy/codacy.yaml as a YAML list
93+ func addRuntimeToCodacyYaml (name string , version string ) error {
94+ codacyPath := ".codacy/codacy.yaml"
95+
96+ type CodacyConfig struct {
97+ Runtimes []string `yaml:"runtimes"`
98+ Tools []string `yaml:"tools"`
99+ }
100+
101+ // Read existing config
102+ var config CodacyConfig
103+ if data , err := os .ReadFile (codacyPath ); err == nil {
104+ if err := yaml .Unmarshal (data , & config ); err != nil {
105+ return fmt .Errorf ("failed to parse %s: %w" , codacyPath , err )
106+ }
107+ }
108+
109+ // Prepare the new runtime string
110+ runtimeEntry := name + "@" + version
111+ found := false
112+ for i , entry := range config .Runtimes {
113+ if strings .HasPrefix (entry , name + "@" ) {
114+ config .Runtimes [i ] = runtimeEntry
115+ found = true
116+ break
117+ }
118+ }
119+ if ! found {
120+ config .Runtimes = append (config .Runtimes , runtimeEntry )
121+ }
122+
123+ // Create .codacy directory if it doesn't exist
124+ if err := os .MkdirAll (".codacy" , 0755 ); err != nil {
125+ return fmt .Errorf ("failed to create .codacy directory: %w" , err )
126+ }
127+
128+ // Write back to .codacy/codacy.yaml
129+ yamlData , err := yaml .Marshal (config )
130+ if err != nil {
131+ return fmt .Errorf ("failed to marshal config to YAML: %w" , err )
132+ }
133+ if err := os .WriteFile (codacyPath , yamlData , 0644 ); err != nil {
134+ return fmt .Errorf ("failed to write config to %s: %w" , codacyPath , err )
81135 }
82136
83137 return nil
@@ -102,14 +156,22 @@ func (c *ConfigType) AddTools(configs []plugins.ToolConfig) error {
102156 if pluginConfig .Runtime != "" {
103157 runtimeInfo := c .runtimes [pluginConfig .Runtime ]
104158 if runtimeInfo == nil {
105- // Try to install the missing runtime
106- if err := InstallRuntimeStrict (pluginConfig .Runtime , nil ); err != nil {
107- return fmt .Errorf ("failed to install required runtime %s: %w" , pluginConfig .Runtime , err )
159+ // Get the default version for the runtime
160+ defaultVersions := plugins .GetRuntimeVersions ()
161+ version , ok := defaultVersions [pluginConfig .Runtime ]
162+ if ! ok {
163+ return fmt .Errorf ("no default version found for runtime %s" , pluginConfig .Runtime )
108164 }
165+
166+ // Add the runtime to the config
167+ if err := c .AddRuntimes ([]plugins.RuntimeConfig {{Name : pluginConfig .Runtime , Version : version }}); err != nil {
168+ return fmt .Errorf ("failed to add runtime %s: %w" , pluginConfig .Runtime , err )
169+ }
170+
109171 // Fetch the runtimeInfo again
110172 runtimeInfo = c .runtimes [pluginConfig .Runtime ]
111173 if runtimeInfo == nil {
112- return fmt .Errorf ("runtime %s still missing after install " , pluginConfig .Runtime )
174+ return fmt .Errorf ("runtime %s still missing after adding to config " , pluginConfig .Runtime )
113175 }
114176 }
115177 }
0 commit comments