Skip to content

Commit 86872df

Browse files
wip: init not working ok
1 parent 579b70a commit 86872df

File tree

2 files changed

+92
-66
lines changed

2 files changed

+92
-66
lines changed

cmd/config.go

Lines changed: 26 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ var configDiscoverCmd = &cobra.Command{
209209
}
210210

211211
codacyYAMLPath := config.Config.ProjectConfigFile()
212+
212213
if err := updateCodacyYAML(detectedLanguages, codacyYAMLPath, defaultToolLangMap, configResetInitFlags, currentCliMode); err != nil {
213214
if strings.Contains(err.Error(), "❌ Fatal:") {
214215
fmt.Println(err)
@@ -245,81 +246,46 @@ var configDiscoverCmd = &cobra.Command{
245246
// updateLanguagesConfig updates the .codacy/tools-configs/languages-config.yaml file.
246247
func updateLanguagesConfig(detectedLanguages map[string]struct{}, toolsConfigDir string, defaultToolLangMap map[string]domain.ToolLanguageInfo) error {
247248
langConfigPath := filepath.Join(toolsConfigDir, "languages-config.yaml")
248-
var langConf domain.LanguagesConfig
249-
250-
if _, err := os.Stat(langConfigPath); err == nil {
251-
data, err := os.ReadFile(langConfigPath)
252-
if err != nil {
253-
return fmt.Errorf("failed to read existing languages-config.yaml: %w", err)
254-
}
255-
if err := yaml.Unmarshal(data, &langConf); err != nil {
256-
return fmt.Errorf("failed to parse existing languages-config.yaml: %w", err)
257-
}
258-
} else if !os.IsNotExist(err) {
259-
return fmt.Errorf("failed to stat languages-config.yaml: %w", err)
260-
}
261249

262-
// Create a map of existing tools for easier update
263-
existingToolsMap := make(map[string]*domain.ToolLanguageInfo)
264-
for i := range langConf.Tools {
265-
existingToolsMap[langConf.Tools[i].Name] = &langConf.Tools[i]
266-
}
250+
// Create a fresh languages config based only on detected languages
251+
// This ensures we only include tools relevant to the currently detected languages
252+
var langConf domain.LanguagesConfig
267253

268254
for toolName, toolInfoFromDefaults := range defaultToolLangMap {
269255
isRelevantTool := false
270256
relevantLangsForThisTool := []string{}
271257
relevantExtsForThisToolMap := make(map[string]struct{})
272258

259+
// Only include languages that are both supported by the tool AND actually detected
273260
for _, langDefault := range toolInfoFromDefaults.Languages {
274261
if _, isDetected := detectedLanguages[langDefault]; isDetected {
275262
isRelevantTool = true
276263
if !slices.Contains(relevantLangsForThisTool, langDefault) {
277264
relevantLangsForThisTool = append(relevantLangsForThisTool, langDefault)
278265
}
279-
// Add extensions associated with this detected language for this tool
280-
for _, defaultExt := range toolInfoFromDefaults.Extensions {
281-
// A simple heuristic: if a tool supports a language, and that language is detected,
282-
// all default extensions of that tool for that language group are considered relevant.
283-
// This assumes toolInfoFromDefaults.Extensions are relevant for all toolInfoFromDefaults.Languages.
284-
// A more precise mapping might be needed if a tool's extensions vary significantly per language it supports.
285-
relevantExtsForThisToolMap[defaultExt] = struct{}{}
286-
}
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{}{}
287275
}
288276
}
289277

290278
if isRelevantTool {
291279
relevantExtsForThisTool := config.GetSortedKeys(relevantExtsForThisToolMap)
292280
sort.Strings(relevantLangsForThisTool)
293281

294-
if existingEntry, ok := existingToolsMap[toolName]; ok {
295-
// Merge languages and extensions, keeping them unique and sorted
296-
existingLangsSet := make(map[string]struct{})
297-
for _, lang := range existingEntry.Languages {
298-
existingLangsSet[lang] = struct{}{}
299-
}
300-
for _, lang := range relevantLangsForThisTool {
301-
existingLangsSet[lang] = struct{}{}
302-
}
303-
existingEntry.Languages = config.GetSortedKeys(existingLangsSet)
304-
305-
existingExtsSet := make(map[string]struct{})
306-
for _, ext := range existingEntry.Extensions {
307-
existingExtsSet[ext] = struct{}{}
308-
}
309-
for _, ext := range relevantExtsForThisTool {
310-
existingExtsSet[ext] = struct{}{}
311-
}
312-
existingEntry.Extensions = config.GetSortedKeys(existingExtsSet)
313-
314-
} else {
315-
newEntry := domain.ToolLanguageInfo{
316-
Name: toolName,
317-
Languages: relevantLangsForThisTool,
318-
Extensions: relevantExtsForThisTool,
319-
}
320-
langConf.Tools = append(langConf.Tools, newEntry)
321-
existingToolsMap[toolName] = &langConf.Tools[len(langConf.Tools)-1] // update map with pointer to new entry
282+
// Create a new entry for each relevant tool
283+
newEntry := domain.ToolLanguageInfo{
284+
Name: toolName,
285+
Languages: relevantLangsForThisTool,
286+
Extensions: relevantExtsForThisTool,
322287
}
288+
langConf.Tools = append(langConf.Tools, newEntry)
323289
}
324290
}
325291

@@ -340,6 +306,7 @@ func updateLanguagesConfig(detectedLanguages map[string]struct{}, toolsConfigDir
340306

341307
// updateCodacyYAML updates the codacy.yaml file with newly relevant tools.
342308
func updateCodacyYAML(detectedLanguages map[string]struct{}, codacyYAMLPath string, defaultToolLangMap map[string]domain.ToolLanguageInfo, initFlags domain.InitFlags, cliMode string) error {
309+
343310
var configData map[string]interface{}
344311

345312
// Read and parse codacy.yaml (validation is done globally in PersistentPreRun)
@@ -411,9 +378,12 @@ func updateCodacyYAML(detectedLanguages map[string]struct{}, codacyYAMLPath stri
411378
}
412379

413380
defaultToolVersions := plugins.GetToolVersions()
414-
finalToolsList := currentToolsList // Start with existing tools
415381

382+
// Discover mode: preserve existing tools and add only new discovered ones
383+
finalToolsList := currentToolsList // Start with existing tools
416384
addedNewTool := false
385+
386+
// Add only newly discovered tools that aren't already configured
417387
for toolNameToAdd := range candidateToolsToAdd {
418388
if _, alreadyConfigured := currentToolSetByName[toolNameToAdd]; !alreadyConfigured {
419389
version, ok := defaultToolVersions[toolNameToAdd]
@@ -502,6 +472,7 @@ func updateCodacyYAML(detectedLanguages map[string]struct{}, codacyYAMLPath stri
502472
if err := os.MkdirAll(filepath.Dir(codacyYAMLPath), utils.DefaultDirPerms); err != nil {
503473
return fmt.Errorf("error creating .codacy directory: %w", err)
504474
}
475+
505476
return os.WriteFile(codacyYAMLPath, yamlData, utils.DefaultFilePerms)
506477
}
507478

config/detector.go

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,23 @@ import (
1717
func DetectFileExtensions(rootPath string) (map[string]int, error) {
1818
extCount := make(map[string]int)
1919

20-
err := filepath.Walk(rootPath, func(path string, info os.FileInfo, err error) error {
20+
// Check if rootPath is a file or directory
21+
info, err := os.Stat(rootPath)
22+
if err != nil {
23+
return nil, fmt.Errorf("failed to stat path %s: %w", rootPath, err)
24+
}
25+
26+
if !info.IsDir() {
27+
// If it's a single file, only process that file
28+
ext := strings.ToLower(filepath.Ext(rootPath))
29+
if ext != "" {
30+
extCount[ext] = 1
31+
}
32+
return extCount, nil
33+
}
34+
35+
// If it's a directory, walk through it
36+
err = filepath.Walk(rootPath, func(path string, info os.FileInfo, err error) error {
2137
if err != nil {
2238
return err
2339
}
@@ -82,16 +98,55 @@ func DetectLanguages(rootPath string, toolLangMap map[string]domain.ToolLanguage
8298
detectedLangs := make(map[string]struct{})
8399
extToLang := make(map[string][]string)
84100

85-
// Build extension to language mapping
86-
for _, toolInfo := range toolLangMap {
87-
for _, lang := range toolInfo.Languages {
88-
if lang == "Multiple" || lang == "Generic" { // Skip generic language types for direct detection
89-
continue
90-
}
91-
for _, ext := range toolInfo.Extensions {
92-
extToLang[ext] = append(extToLang[ext], lang)
93-
}
94-
}
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
95150
}
96151

97152
// Get file extensions from the path

0 commit comments

Comments
 (0)