- Host discovers/builds modules under modules/ (bbc, rte, tg4) - module-publish.yaml tracked; local module.yaml gitignored - drm --modules-dir / DRM_MODULES_DIR - drm proxy reset; Magisk reinject reliability - modules-v* Forgejo release workflow + build scripts - BBC full Explore channel map including S4C - README quickstart based on release binaries
140 lines
3.1 KiB
Go
140 lines
3.1 KiB
Go
package modhost
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
)
|
|
|
|
// Module is one on-disk provider under modules/<name>/.
|
|
type Module struct {
|
|
Name string // directory base name, e.g. "bbc"
|
|
Dir string // absolute path to modules/<name>
|
|
}
|
|
|
|
// ModulesDir returns the modules directory (honours --modules-dir / DRM_MODULES_DIR).
|
|
func ModulesDir(root string) string {
|
|
return EffectiveModulesDir(root)
|
|
}
|
|
|
|
// Discover lists modules/<name>/ directories that look like providers
|
|
// (have module.yaml / module-publish.yaml and/or Go source and/or a prebuilt binary).
|
|
func Discover(root string) ([]Module, error) {
|
|
dir := ModulesDir(root)
|
|
ents, err := os.ReadDir(dir)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
var out []Module
|
|
for _, e := range ents {
|
|
if !e.IsDir() {
|
|
continue
|
|
}
|
|
name := e.Name()
|
|
if strings.HasPrefix(name, ".") {
|
|
continue
|
|
}
|
|
modDir := filepath.Join(dir, name)
|
|
if !looksLikeModule(modDir, name) {
|
|
continue
|
|
}
|
|
out = append(out, Module{Name: name, Dir: modDir})
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func looksLikeModule(dir, name string) bool {
|
|
if fileExists(filepath.Join(dir, "module.yaml")) {
|
|
return true
|
|
}
|
|
if fileExists(filepath.Join(dir, "module-publish.yaml")) {
|
|
return true
|
|
}
|
|
if fileExists(filepath.Join(dir, "go.mod")) {
|
|
return true
|
|
}
|
|
if fileExists(BinPath(dir, name)) {
|
|
return true
|
|
}
|
|
// Any .go file in the root of the module dir.
|
|
ents, err := os.ReadDir(dir)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
for _, e := range ents {
|
|
if !e.IsDir() && strings.HasSuffix(e.Name(), ".go") {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// Find returns the module named name, or an error listing what exists.
|
|
func Find(root, name string) (Module, error) {
|
|
mods, err := Discover(root)
|
|
if err != nil {
|
|
return Module{}, err
|
|
}
|
|
for _, m := range mods {
|
|
if m.Name == name {
|
|
return m, nil
|
|
}
|
|
}
|
|
names := make([]string, 0, len(mods))
|
|
for _, m := range mods {
|
|
names = append(names, m.Name)
|
|
}
|
|
if len(names) == 0 {
|
|
return Module{}, errNoModules(root)
|
|
}
|
|
return Module{}, &NotFoundError{Name: name, Available: names}
|
|
}
|
|
|
|
// BinPath is modules/<name>/.bin/<name>[.exe].
|
|
func BinPath(modDir, name string) string {
|
|
bin := filepath.Join(modDir, ".bin", name)
|
|
if runtime.GOOS == "windows" {
|
|
bin += ".exe"
|
|
}
|
|
return bin
|
|
}
|
|
|
|
func (m Module) BinPath() string {
|
|
return BinPath(m.Dir, m.Name)
|
|
}
|
|
|
|
func fileExists(p string) bool {
|
|
st, err := os.Stat(p)
|
|
return err == nil && !st.IsDir()
|
|
}
|
|
|
|
func errNoModules(root string) error {
|
|
return &NotFoundError{
|
|
Name: "",
|
|
Available: nil,
|
|
Hint: "create modules/<name>/ with Go source + module.yaml (see docs/modules.md)",
|
|
Root: ModulesDir(root),
|
|
}
|
|
}
|
|
|
|
// NotFoundError is returned when --app does not match a modules/ directory.
|
|
type NotFoundError struct {
|
|
Name string
|
|
Available []string
|
|
Hint string
|
|
Root string
|
|
}
|
|
|
|
func (e *NotFoundError) Error() string {
|
|
if e.Name == "" {
|
|
return "no modules found under " + e.Root + " — " + e.Hint
|
|
}
|
|
if len(e.Available) == 0 {
|
|
return "unknown module " + e.Name + " (modules dir empty) — " + e.Hint
|
|
}
|
|
return "unknown module " + e.Name + " — available: " + strings.Join(e.Available, ", ")
|
|
}
|