55 "log"
66 "os"
77 "path/filepath"
8- "slices"
98 "sort"
109 "strings"
1110
@@ -186,17 +185,17 @@ var configDiscoverCmd = &cobra.Command{
186185 }
187186 }
188187
189- detectedLanguages , err := config .DetectLanguages (discoverPath , defaultToolLangMap )
188+ detectedTools , err := config .DetectRelevantTools (discoverPath , defaultToolLangMap )
190189 if err != nil {
191- log .Fatalf ("Error detecting languages : %v" , err )
190+ log .Fatalf ("Error detecting relevant tools : %v" , err )
192191 }
193- if len (detectedLanguages ) == 0 {
194- fmt .Println ("No known languages detected in the provided path." )
192+ if len (detectedTools ) == 0 {
193+ fmt .Println ("No relevant tools found for the file extensions in the provided path." )
195194 return
196195 }
197196
198197 toolsConfigDir := config .Config .ToolsConfigsDirectory ()
199- if err := updateLanguagesConfig ( detectedLanguages , toolsConfigDir , defaultToolLangMap ); err != nil {
198+ if err := updateLanguagesConfigForTools ( detectedTools , toolsConfigDir , defaultToolLangMap ); err != nil {
200199 log .Fatalf ("Error updating .codacy/tools-configs/languages-config.yaml: %v" , err )
201200 }
202201 fmt .Println ("Updated .codacy/tools-configs/languages-config.yaml" )
@@ -210,7 +209,7 @@ var configDiscoverCmd = &cobra.Command{
210209
211210 codacyYAMLPath := config .Config .ProjectConfigFile ()
212211
213- if err := updateCodacyYAML ( detectedLanguages , codacyYAMLPath , defaultToolLangMap , configResetInitFlags , currentCliMode ); err != nil {
212+ if err := updateCodacyYAMLForTools ( detectedTools , codacyYAMLPath , configResetInitFlags , currentCliMode ); err != nil {
214213 if strings .Contains (err .Error (), "❌ Fatal:" ) {
215214 fmt .Println (err )
216215 os .Exit (1 )
@@ -219,21 +218,10 @@ var configDiscoverCmd = &cobra.Command{
219218 }
220219 fmt .Printf ("Updated %s with relevant tools.\n " , filepath .Base (codacyYAMLPath ))
221220
222- // Determine which tools are relevant for discovered languages and create their configurations
223- discoveredToolNames := make (map [string ]struct {})
224- for toolName , toolInfo := range defaultToolLangMap {
225- for _ , toolLang := range toolInfo .Languages {
226- if _ , detected := detectedLanguages [toolLang ]; detected {
227- discoveredToolNames [toolName ] = struct {}{}
228- break
229- }
230- }
231- }
232-
233221 // Create tool configuration files for discovered tools
234- if len (discoveredToolNames ) > 0 {
222+ if len (detectedTools ) > 0 {
235223 fmt .Printf ("\n Creating tool configurations for discovered tools...\n " )
236- if err := configsetup .CreateConfigurationFilesForDiscoveredTools (discoveredToolNames , toolsConfigDir , configResetInitFlags ); err != nil {
224+ if err := configsetup .CreateConfigurationFilesForDiscoveredTools (detectedTools , toolsConfigDir , configResetInitFlags ); err != nil {
237225 log .Printf ("Warning: Failed to create some tool configurations: %v" , err )
238226 }
239227 }
@@ -243,49 +231,15 @@ var configDiscoverCmd = &cobra.Command{
243231 },
244232}
245233
246- // updateLanguagesConfig updates the .codacy/tools-configs/languages-config.yaml file.
247- func updateLanguagesConfig ( detectedLanguages map [string ]struct {}, toolsConfigDir string , defaultToolLangMap map [string ]domain.ToolLanguageInfo ) error {
234+ // updateLanguagesConfigForTools updates the .codacy/tools-configs/languages-config.yaml file based on detected tools .
235+ func updateLanguagesConfigForTools ( detectedTools map [string ]struct {}, toolsConfigDir string , defaultToolLangMap map [string ]domain.ToolLanguageInfo ) error {
248236 langConfigPath := filepath .Join (toolsConfigDir , "languages-config.yaml" )
249237
250- // Create a fresh languages config based only on detected languages
251- // This ensures we only include tools relevant to the currently detected languages
238+ // Build language configuration for detected tools
252239 var langConf domain.LanguagesConfig
253-
254- for toolName , toolInfoFromDefaults := range defaultToolLangMap {
255- isRelevantTool := false
256- relevantLangsForThisTool := []string {}
257- relevantExtsForThisToolMap := make (map [string ]struct {})
258-
259- // Only include languages that are both supported by the tool AND actually detected
260- for _ , langDefault := range toolInfoFromDefaults .Languages {
261- if _ , isDetected := detectedLanguages [langDefault ]; isDetected {
262- isRelevantTool = true
263- if ! slices .Contains (relevantLangsForThisTool , langDefault ) {
264- relevantLangsForThisTool = append (relevantLangsForThisTool , langDefault )
265- }
266- }
267- }
268-
269- // Only add extensions for the languages that were actually detected
270- if isRelevantTool {
271- // Add only extensions that correspond to detected languages
272- // For now, we'll include all extensions of a relevant tool, but this could be refined
273- for _ , defaultExt := range toolInfoFromDefaults .Extensions {
274- relevantExtsForThisToolMap [defaultExt ] = struct {}{}
275- }
276- }
277-
278- if isRelevantTool {
279- relevantExtsForThisTool := config .GetSortedKeys (relevantExtsForThisToolMap )
280- sort .Strings (relevantLangsForThisTool )
281-
282- // Create a new entry for each relevant tool
283- newEntry := domain.ToolLanguageInfo {
284- Name : toolName ,
285- Languages : relevantLangsForThisTool ,
286- Extensions : relevantExtsForThisTool ,
287- }
288- langConf .Tools = append (langConf .Tools , newEntry )
240+ for toolName := range detectedTools {
241+ if toolInfo , exists := defaultToolLangMap [toolName ]; exists {
242+ langConf .Tools = append (langConf .Tools , toolInfo )
289243 }
290244 }
291245
@@ -304,8 +258,8 @@ func updateLanguagesConfig(detectedLanguages map[string]struct{}, toolsConfigDir
304258 return os .WriteFile (langConfigPath , data , utils .DefaultFilePerms )
305259}
306260
307- // updateCodacyYAML updates the codacy.yaml file with newly relevant tools.
308- func updateCodacyYAML ( detectedLanguages map [string ]struct {}, codacyYAMLPath string , defaultToolLangMap map [ string ]domain. ToolLanguageInfo , initFlags domain.InitFlags , cliMode string ) error {
261+ // updateCodacyYAMLForTools updates the codacy.yaml file with detected tools.
262+ func updateCodacyYAMLForTools ( detectedTools map [string ]struct {}, codacyYAMLPath string , initFlags domain.InitFlags , cliMode string ) error {
309263
310264 var configData map [string ]interface {}
311265
@@ -335,15 +289,7 @@ func updateCodacyYAML(detectedLanguages map[string]struct{}, codacyYAMLPath stri
335289 }
336290 }
337291
338- candidateToolsToAdd := make (map [string ]struct {}) // tool names like "eslint"
339- for toolName , toolInfo := range defaultToolLangMap {
340- for _ , lang := range toolInfo .Languages {
341- if _ , detected := detectedLanguages [lang ]; detected {
342- candidateToolsToAdd [toolName ] = struct {}{}
343- break
344- }
345- }
346- }
292+ candidateToolsToAdd := detectedTools
347293
348294 if cliMode == "remote" && initFlags .ApiToken != "" {
349295 fmt .Println ("Cloud mode: Verifying tools against repository settings..." )
0 commit comments