package trunk import ( "fmt" "strings" ) // A mesh package is a Go module of a repository of the mesh's forge (ForgeModulePrefix): mesh-sdk's Go module, and an // owner module's package (novox/hq ADR 0593 §1). The indexer alone reads which packages a module imports, their content // digests and their latest published versions, and writes them in its state (PublishedPackages, ADR 0593 §2); a build // writes the version it is asked into its own go.mod and go.sum (StepAsk.Packages). // ForgeModulePrefix is the module path prefix of the Go modules the mesh's forge serves: a Go module whose path starts // with it is a mesh package, git.novox.be/novox/[/]. const ForgeModulePrefix = "git.novox.be/novox/" // PackageRepository is the repository (/) a mesh package is published from and the package's directory in // it ("" for its root); false for a path that is not a forge module's. func PackageRepository(pkg string) (repository, dir string, ok bool) { rest, found := strings.CutPrefix(pkg, ForgeModulePrefix) if !found || rest == "" { return "", "", false } name, dir, _ := strings.Cut(rest, "/") if name == "" { return "", "", false } return "novox/" + name, dir, true } // DefaultNpmScope is the npm scope of the mesh's npm packages when a build module's setting names none (design 47 H2 // H-A-534): a TypeScript import set names each mesh npm package by its name in it, such as @novox/mesh-sdk. const DefaultNpmScope = "@novox" // CheckImports refuses an import set (the imports of a module's kept inputs, of a build's answer and of a build log entry) // that holds a path that is no mesh package's, or is not sorted and each once. func CheckImports(imports []string) error { return CheckGoImports(imports, ForgeModulePrefix) } // CheckGoImports refuses a Go import set holding a path that is no Go module path under prefix, the forge's module prefix // a build module's setting names (design 47 H2 H-A-534), or that is not sorted and each once. func CheckGoImports(imports []string, prefix string) error { if prefix == "" { prefix = ForgeModulePrefix } return checkSorted(imports, func(pkg string) error { rest, found := strings.CutPrefix(pkg, prefix) if name, _, _ := strings.Cut(rest, "/"); !found || name == "" || strings.ContainsAny(pkg, " \t\n@") { return fmt.Errorf("trunk: the import %q is no mesh package's path (it starts with %s)", pkg, prefix) } return nil }) } // CheckNpmImports refuses a TypeScript import set holding an entry that is no npm package name in scope (such as // @novox/mesh-sdk, never a sub-path of it: one package, one digest, H-A-532 DQ-6), or that is not sorted and each once // (design 47 H2 H-A-534). func CheckNpmImports(imports []string, scope string) error { if scope == "" { scope = DefaultNpmScope } return checkSorted(imports, func(pkg string) error { name, found := strings.CutPrefix(pkg, scope+"/") if !found || name == "" || strings.ContainsAny(name, "/@ \t\n") { return fmt.Errorf("trunk: the import %q is no mesh npm package's name (%s/)", pkg, scope) } return nil }) } // checkSorted refuses imports holding an entry one refuses, or not sorted and each once. func checkSorted(imports []string, one func(string) error) error { for i, pkg := range imports { if err := one(pkg); err != nil { return err } if i > 0 && imports[i-1] >= pkg { return fmt.Errorf("trunk: the imports are not sorted and each once (%s after %s)", pkg, imports[i-1]) } } return nil }