Detect checks if the directory contains any of the specified files
(dirFS FS)
| 25 | |
| 26 | // Detect checks if the directory contains any of the specified files |
| 27 | func (fd FileDetector) Detect(dirFS FS) (Match, error) { |
| 28 | var matchedPaths []string |
| 29 | |
| 30 | for _, path := range fd.Paths { |
| 31 | if _, err := dirFS.Stat(path); err == nil { |
| 32 | matchedPaths = append(matchedPaths, path) |
| 33 | } else if !os.IsNotExist(err) { |
| 34 | return Match{}, errors.Join(err, errors.New("project file detection failure")) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // Calculate the match confidence based on the percentage of matched paths |
| 39 | percentage := float64(len(matchedPaths)) / float64(len(fd.Paths)) |
| 40 | var confidence Confidence |
| 41 | |
| 42 | // Assume a 25% window for each confidence level, anything less than 30% is None |
| 43 | // Completely arbitrary, but it's a start. |
| 44 | switch { |
| 45 | case percentage >= 0.80: |
| 46 | confidence = High |
| 47 | case percentage >= 0.55: |
| 48 | confidence = Medium |
| 49 | case percentage >= 0.30: |
| 50 | confidence = Low |
| 51 | default: |
| 52 | confidence = None |
| 53 | } |
| 54 | |
| 55 | var missingRequiredFiles []string |
| 56 | if len(matchedPaths) > 0 && fd.RequiredFiles != nil && len(fd.RequiredFiles) > 0 { |
| 57 | for _, reqPath := range fd.RequiredFiles { |
| 58 | if !slices.Contains(matchedPaths, reqPath) { |
| 59 | missingRequiredFiles = append(missingRequiredFiles, reqPath) |
| 60 | // Only lower confidence |
| 61 | if confidence != None && confidence != Low { |
| 62 | confidence = Low // Force confidence to low when required files are missing |
| 63 | } |
| 64 | continue |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | match := Match{ |
| 70 | Detector: fd, |
| 71 | Detected: len(matchedPaths) > 0, |
| 72 | Confidence: confidence, |
| 73 | FollowUpDetectors: fd.FollowUpDetectors, |
| 74 | MissingRequiredFiles: missingRequiredFiles, |
| 75 | } |
| 76 | |
| 77 | if fd.AnchorCategory != nil { |
| 78 | match.AnchorCategory = fd.AnchorCategory |
| 79 | } else { |
| 80 | // Default to Custom Category if not specified by the detector |
| 81 | match.AnchorCategory = anchorcli.CategoryCustom |
| 82 | } |
| 83 | |
| 84 | // Return a Match with the calculated confidence, follow-ups and category |