| // Copyright 2019 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package loaders |
| |
| import ( |
| "fmt" |
| "path" |
| |
| generated "fuchsia.googlesource.com/infra/infra/fxicfg/starlark" |
| luci "go.chromium.org/luci/starlark/interpreter" |
| ) |
| |
| // EmbeddedLoader makes a map of loaders for embedded Starlark packages. |
| // |
| // Each directory directly under fuchsia.googlesource.com/infra/infra/fxicfg/stdlib/stdlib/... |
| // represents a corresponding starlark package. E.g. files in 'stdlib' directory are |
| // loadable via load("@stdlib//<path>", ...). |
| func EmbeddedLoader() map[string]luci.Loader { |
| perRoot := map[string]map[string]string{} |
| |
| for abs, data := range generated.Assets() { |
| root, rel := path.Split(abs) |
| if root == "" || rel == "" { |
| panic(fmt.Sprintf("forbidden *.star outside the package dir: %s", abs)) |
| } |
| root = path.Clean(root) |
| m := perRoot[root] |
| if m == nil { |
| m = make(map[string]string, 1) |
| perRoot[root] = m |
| } |
| m[rel] = data |
| } |
| |
| loaders := make(map[string]luci.Loader, len(perRoot)) |
| for pkg, files := range perRoot { |
| loaders[pkg] = luci.MemoryLoader(files) |
| } |
| return loaders |
| } |