From 79f17ade93426a04ae90c1a182a9f1cae6658115 Mon Sep 17 00:00:00 2001 From: Hu Yueh-Wei Date: Thu, 28 Nov 2024 14:58:16 +0800 Subject: [PATCH] feat: load go extensions based on go app's manifest --- .../binding/go/tools/build/main.go | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/core/src/ten_runtime/binding/go/tools/build/main.go b/core/src/ten_runtime/binding/go/tools/build/main.go index 77233bc0f..cccaeceb2 100644 --- a/core/src/ten_runtime/binding/go/tools/build/main.go +++ b/core/src/ten_runtime/binding/go/tools/build/main.go @@ -1061,6 +1061,20 @@ func (ab *AppBuilder) autoDetectExtensions() error { return nil } + // Load the app's `manifest.json` to get the list of extension dependencies. + manifest, err := LoadManifest(ab.options.AppDir) + if err != nil { + return fmt.Errorf("Failed to load manifest.json: %v", err) + } + + // Collect names of extensions from dependencies. + extensionNames := make(map[string]bool) + for _, dep := range manifest.Dependencies { + if dep.Type == "extension" { + extensionNames[dep.Name] = true + } + } + entries, err := os.ReadDir(extBaseDir) if err != nil { return err @@ -1075,6 +1089,18 @@ func (ab *AppBuilder) autoDetectExtensions() error { continue } + // Check if the extension is in the list of extension dependencies. + extensionName := entry.Name() + if _, ok := extensionNames[extensionName]; !ok { + if ab.options.Verbose { + log.Printf( + "Skipping extension [%s], not in manifest dependencies.\n", + extensionName, + ) + } + continue + } + extDir := path.Join(extBaseDir, entry.Name()) ext, err := LoadExtensionModule(extDir, ab.options.Verbose) if err != nil { @@ -1113,8 +1139,17 @@ func (ab *AppBuilder) autoDetectExtensions() error { // -------------- manifest --------------- +type Dependency struct { + Type string `json:"type"` + Name string `json:"name"` + Version string `json:"version"` +} + type TenPackageManifest struct { - Type string `json:"type"` + Type string `json:"type"` + Name string `json:"name"` + Version string `json:"version"` + Dependencies []Dependency `json:"dependencies"` } func LoadManifest(pkgDir string) (*TenPackageManifest, error) {