agent/pkg/setup/detect.go

75 lines
1.6 KiB
Go
Raw Normal View History

2026-03-20 19:31:45 +00:00
// SPDX-License-Identifier: EUPL-1.2
// Package setup provides workspace setup and scaffolding using lib templates.
package setup
import (
core "dappco.re/go/core"
2026-03-20 19:31:45 +00:00
)
// ProjectType identifies what kind of project lives at a path.
type ProjectType string
const (
TypeGo ProjectType = "go"
TypePHP ProjectType = "php"
TypeNode ProjectType = "node"
TypeWails ProjectType = "wails"
TypeUnknown ProjectType = "unknown"
)
// fs provides unrestricted filesystem access for setup operations.
var fs = (&core.Fs{}).NewUnrestricted()
2026-03-20 19:31:45 +00:00
// Detect identifies the project type from files present at the given path.
//
// projType := setup.Detect("./repo")
2026-03-20 19:31:45 +00:00
func Detect(path string) ProjectType {
base := absolutePath(path)
2026-03-20 19:31:45 +00:00
checks := []struct {
file string
projType ProjectType
}{
{"wails.json", TypeWails},
{"go.mod", TypeGo},
{"composer.json", TypePHP},
{"package.json", TypeNode},
}
for _, c := range checks {
if fs.IsFile(core.JoinPath(base, c.file)) {
2026-03-20 19:31:45 +00:00
return c.projType
}
}
return TypeUnknown
}
// DetectAll returns all project types found at the path (polyglot repos).
//
// types := setup.DetectAll("./repo")
2026-03-20 19:31:45 +00:00
func DetectAll(path string) []ProjectType {
base := absolutePath(path)
2026-03-20 19:31:45 +00:00
var types []ProjectType
all := []struct {
file string
projType ProjectType
}{
{"go.mod", TypeGo},
{"composer.json", TypePHP},
{"package.json", TypeNode},
{"wails.json", TypeWails},
}
for _, c := range all {
if fs.IsFile(core.JoinPath(base, c.file)) {
2026-03-20 19:31:45 +00:00
types = append(types, c.projType)
}
}
return types
}
func absolutePath(path string) string {
if path == "" {
return core.Env("DIR_CWD")
}
return core.Path(path)
}