go-build/pkg/sdk/detect.go
Snider bb64608120 refactor(module): migrate module path to dappco.re/go/core/build
Update go.mod module declaration, all require lines, and .go import
paths from forge.lthn.ai to dappco.re. Dependencies updated: core
v0.5.0, log v0.1.0, io v0.2.0. Replace directives added for local
module resolution. forge.lthn.ai/core/cli and go-inference retained
at old paths (not yet migrated).

Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-22 01:53:16 +00:00

78 lines
2.1 KiB
Go

package sdk
import (
"path/filepath"
"strings"
coreio "dappco.re/go/core/io"
coreerr "dappco.re/go/core/log"
)
// commonSpecPaths are checked in order when no spec is configured.
var commonSpecPaths = []string{
"api/openapi.yaml",
"api/openapi.json",
"openapi.yaml",
"openapi.json",
"docs/api.yaml",
"docs/api.json",
"swagger.yaml",
"swagger.json",
}
// DetectSpec finds the OpenAPI spec file.
// Priority: config path -> common paths -> Laravel Scramble.
func (s *SDK) DetectSpec() (string, error) {
// 1. Check configured path
if s.config.Spec != "" {
specPath := filepath.Join(s.projectDir, s.config.Spec)
if coreio.Local.IsFile(specPath) {
return specPath, nil
}
return "", coreerr.E("sdk.DetectSpec", "configured spec not found: "+s.config.Spec, nil)
}
// 2. Check common paths
for _, p := range commonSpecPaths {
specPath := filepath.Join(s.projectDir, p)
if coreio.Local.IsFile(specPath) {
return specPath, nil
}
}
// 3. Try Laravel Scramble detection
specPath, err := s.detectScramble()
if err == nil {
return specPath, nil
}
return "", coreerr.E("sdk.DetectSpec", "no OpenAPI spec found (checked config, common paths, Scramble)", nil)
}
// detectScramble checks for Laravel Scramble and exports the spec.
func (s *SDK) detectScramble() (string, error) {
composerPath := filepath.Join(s.projectDir, "composer.json")
if !coreio.Local.IsFile(composerPath) {
return "", coreerr.E("sdk.detectScramble", "no composer.json", nil)
}
// Check for scramble in composer.json
data, err := coreio.Local.Read(composerPath)
if err != nil {
return "", err
}
// Simple check for scramble package
if !containsScramble(data) {
return "", coreerr.E("sdk.detectScramble", "scramble not found in composer.json", nil)
}
// TODO: Run php artisan scramble:export
return "", coreerr.E("sdk.detectScramble", "scramble export not implemented", nil)
}
// containsScramble checks if composer.json includes scramble.
func containsScramble(content string) bool {
return strings.Contains(content, "dedoc/scramble") ||
strings.Contains(content, "\"scramble\"")
}