analyzer.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package analyzer
  2. import (
  3. "github.com/sirupsen/logrus"
  4. )
  5. // Analyzer is responsible for analyzing codebases and guessing the technology stack.
  6. var stacks = make(map[string]Stack)
  7. // RegisterStack registers a new technology stack for analysis.
  8. func RegisterStack(stack Stack) {
  9. logrus.Debugf("Registering stack: %s", stack.Name())
  10. if stack == nil {
  11. panic("stack cannot be nil")
  12. }
  13. name := stack.Name()
  14. if _, exists := stacks[name]; exists {
  15. panic("stack already registered: " + name)
  16. }
  17. stacks[name] = stack
  18. }
  19. type Stack interface {
  20. // Name returns the name of the technology stack.
  21. Name() string
  22. // Analyze analyzes the codebase and returns a guessed technology stack.
  23. Analyze(codebasePath string) (bool, error)
  24. // GenerateDockerfile generates a Dockerfile for the technology stack.
  25. GenerateDockerfile(codebasePath string) (string, error)
  26. // Close cleans up any resources used by the stack.
  27. }
  28. // AnalyzeCode analyzes the provided codebase and returns a guessed technology stack.
  29. func AnalyzeCode(codebasePath string) (Stack, error) {
  30. logrus.Debugf("Analyzing codebase at path: %s", codebasePath)
  31. logrus.Debugf("Registered stacks: %v", stacks)
  32. // Iterate through registered stacks and analyze the codebase
  33. for _, stack := range stacks {
  34. logrus.Debugf("Analyzing with stack: %s", stack.Name())
  35. if stack == nil {
  36. continue // Skip nil stacks
  37. }
  38. // Perform analysis
  39. guessed, err := stack.Analyze(codebasePath)
  40. if err != nil {
  41. return nil, err // Return error if analysis fails
  42. }
  43. if guessed {
  44. logrus.Infof("Guessed technology stack: %s", stack.Name())
  45. return stack, nil // Return the stack if analysis is successful
  46. }
  47. }
  48. // Placeholder implementation
  49. return nil, nil // Replace with actual analysis logic
  50. }
  51. // Close cleans up any resources used by the Analyzer.
  52. func Close() {
  53. // Implement any cleanup logic if necessary
  54. // For example, closing connections, stopping background tasks, etc.
  55. }