Skip to content

Commit 67ef295

Browse files
detect project language auto
1 parent 385ae99 commit 67ef295

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

main.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func cmdGenerate(args []string) {
7171
watchFlag bool
7272
)
7373

74-
fs.StringVar(&appLanguage, "language", "go", "Application language (supported: go)")
74+
fs.StringVar(&appLanguage, "language", "auto", "Application language (supported: go)")
7575
fs.StringVar(&outputPath, "out", "", "Output path for generated code (default: <app-path>/app)")
7676
fs.BoolVar(&watchFlag, "watch", false, "Watch <app-path>/services and regenerate on changes")
7777
fs.Usage = func() {
@@ -93,6 +93,14 @@ func cmdGenerate(args []string) {
9393
}
9494
appPath := fs.Arg(0)
9595

96+
if appLanguage == "" || appLanguage == "auto" {
97+
appLanguage = detectLanguage(appPath)
98+
if appLanguage == "" {
99+
log.Fatalf("unable to detect language for %s — please specify with -language", appPath)
100+
}
101+
fmt.Println("Detected language:", appLanguage)
102+
}
103+
96104
// Defaults
97105
if outputPath == "" {
98106
outputPath = filepath.Join(appPath, "app")
@@ -443,6 +451,35 @@ func errorsIsClosed(err error) bool {
443451
return strings.Contains(msg, "closed network connection") || strings.Contains(msg, "server closed")
444452
}
445453

454+
func detectLanguage(appPath string) string {
455+
files, _ := os.ReadDir(appPath)
456+
hasGo, hasJava, hasPython := false, false, false
457+
458+
for _, f := range files {
459+
name := f.Name()
460+
switch {
461+
case name == "go.mod" || strings.HasSuffix(name, ".go"):
462+
hasGo = true
463+
case name == "pom.xml" || strings.HasPrefix(name, "build.gradle"):
464+
hasJava = true
465+
case name == "pyproject.toml" || name == "setup.py" || strings.HasSuffix(name, ".py"):
466+
hasPython = true
467+
}
468+
}
469+
470+
// Priority order if multiple found (tune to your project needs)
471+
switch {
472+
case hasGo:
473+
return "go"
474+
case hasJava:
475+
return "java"
476+
case hasPython:
477+
return "python"
478+
default:
479+
return ""
480+
}
481+
}
482+
446483
func printRootUsage() {
447484
fmt.Println(`polycode — code generator & extractor
448485

0 commit comments

Comments
 (0)