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