Skip to content

Commit a51b46b

Browse files
looks like works ok
1 parent 86872df commit a51b46b

File tree

2 files changed

+36
-153
lines changed

2 files changed

+36
-153
lines changed

cmd/config.go

Lines changed: 17 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
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("\nCreating 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...")

config/detector.go

Lines changed: 19 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -93,101 +93,38 @@ func GetRecognizableExtensions(extCount map[string]int, toolLangMap map[string]d
9393
return result
9494
}
9595

96-
// DetectLanguages detects languages based on file extensions found in the path
97-
func DetectLanguages(rootPath string, toolLangMap map[string]domain.ToolLanguageInfo) (map[string]struct{}, error) {
98-
detectedLangs := make(map[string]struct{})
99-
extToLang := make(map[string][]string)
100-
101-
// Build extension to language mapping based on file extensions, not tool capabilities
102-
// Each extension should map to its actual language, not all languages that tools supporting it can handle
103-
extensionLanguageMap := map[string][]string{
104-
".java": {"Java"},
105-
".py": {"Python"},
106-
".js": {"JavaScript"},
107-
".jsx": {"JSX"},
108-
".ts": {"TypeScript"},
109-
".tsx": {"TSX"},
110-
".go": {"Go", "Golang"}, // Both Go and Golang are used in tool definitions
111-
".dart": {"Dart"},
112-
".c": {"C"},
113-
".cpp": {"CPP"},
114-
".cc": {"CPP"},
115-
".h": {"C", "CPP"},
116-
".hpp": {"CPP"},
117-
".cs": {"C#"},
118-
".rb": {"Ruby"},
119-
".php": {"PHP"},
120-
".scala": {"Scala"},
121-
".swift": {"Swift"},
122-
".kt": {"Kotlin"},
123-
".rs": {"Rust"},
124-
".lua": {"Lua"},
125-
".pl": {"Perl"},
126-
".f": {"Fortran"},
127-
".f90": {"Fortran"},
128-
".erl": {"Erlang"},
129-
".sol": {"Solidity"},
130-
".zig": {"Zig"},
131-
".m": {"Objective-C"},
132-
".vue": {"VueJS"},
133-
".ttcn": {"TTCN-3"},
134-
".gd": {"GDScript"},
135-
".json": {"JSON"},
136-
".xml": {"XML"},
137-
".jsp": {"JSP"},
138-
".vm": {"Velocity"},
139-
".cls": {"Apex"},
140-
".trigger": {"Apex"},
141-
".page": {"VisualForce"},
142-
".component": {"VisualForce"},
143-
".tf": {"Terraform"},
144-
".tfvars": {"Terraform"},
145-
}
146-
147-
// Use the precise extension mapping instead of tool-based mapping
148-
for ext, langs := range extensionLanguageMap {
149-
extToLang[ext] = langs
150-
}
151-
96+
// DetectRelevantTools detects tools based on file extensions found in the path
97+
func DetectRelevantTools(rootPath string, toolLangMap map[string]domain.ToolLanguageInfo) (map[string]struct{}, error) {
15298
// Get file extensions from the path
15399
extCount, err := DetectFileExtensions(rootPath)
154100
if err != nil {
155101
return nil, fmt.Errorf("failed to detect file extensions in path %s: %w", rootPath, err)
156102
}
157103

158-
// Map only found extensions to languages
159-
for ext := range extCount {
160-
if langs, ok := extToLang[ext]; ok {
161-
// Log which extensions map to which languages for debugging
162-
logger.Debug("Found files with extension", logrus.Fields{
163-
"extension": ext,
164-
"count": extCount[ext],
165-
"languages": langs,
166-
})
167-
for _, lang := range langs {
168-
detectedLangs[lang] = struct{}{}
104+
// Find tools that support these extensions
105+
relevantTools := make(map[string]struct{})
106+
for toolName, toolInfo := range toolLangMap {
107+
for _, ext := range toolInfo.Extensions {
108+
if _, found := extCount[ext]; found {
109+
logger.Debug("Found relevant tool for extension", logrus.Fields{
110+
"tool": toolName,
111+
"extension": ext,
112+
"count": extCount[ext],
113+
})
114+
relevantTools[toolName] = struct{}{}
115+
break
169116
}
170117
}
171118
}
172119

173-
// Log the final set of detected languages with their corresponding extensions
174-
if len(detectedLangs) > 0 {
175-
langToExts := make(map[string][]string)
176-
for ext, count := range extCount {
177-
if langs, ok := extToLang[ext]; ok {
178-
for _, lang := range langs {
179-
langToExts[lang] = append(langToExts[lang], fmt.Sprintf("%s (%d files)", ext, count))
180-
}
181-
}
182-
}
183-
184-
logger.Debug("Detected languages in path", logrus.Fields{
185-
"languages_with_files": langToExts,
186-
"path": rootPath,
120+
if len(relevantTools) > 0 {
121+
logger.Debug("Detected relevant tools for path", logrus.Fields{
122+
"tools": GetSortedKeys(relevantTools),
123+
"path": rootPath,
187124
})
188125
}
189126

190-
return detectedLangs, nil
127+
return relevantTools, nil
191128
}
192129

193130
// GetSortedKeys returns a sorted slice of strings from a string set

0 commit comments

Comments
 (0)