193 lines
5.8 KiB
Go
193 lines
5.8 KiB
Go
package config
|
|
|
|
import (
|
|
"sort"
|
|
"strings"
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/artifactpolicy"
|
|
configkit "go.maximumdirect.net/configkit"
|
|
)
|
|
|
|
// EffectiveArtifactRecord describes one concrete artifact after configuration
|
|
// resolution and family expansion.
|
|
type EffectiveArtifactRecord struct {
|
|
Key string
|
|
SourceID string
|
|
DefinitionSources []string
|
|
Enabled bool
|
|
OutputPath string
|
|
Dependencies []string
|
|
FamilyKey string
|
|
CharacterID string
|
|
FamilySources []string
|
|
PartySource string
|
|
}
|
|
|
|
// EffectiveArtifactCatalog returns the deterministic concrete artifact catalog
|
|
// consumed by runtime planning.
|
|
func EffectiveArtifactCatalog(cfg *PipelineConfig, party ResolvedParty) ([]EffectiveArtifactRecord, error) {
|
|
catalog, err := DeriveArtifactCatalog(cfg, party)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return catalog.Records, nil
|
|
}
|
|
|
|
func concreteArtifactRecords(catalog *ArtifactCatalog, party ResolvedParty, definitionSources func(configkit.Path) []string) []EffectiveArtifactRecord {
|
|
if len(catalog.Artifacts) == 0 {
|
|
return []EffectiveArtifactRecord{}
|
|
}
|
|
|
|
keys := make([]string, 0, len(catalog.Artifacts))
|
|
for key := range catalog.Artifacts {
|
|
keys = append(keys, key)
|
|
}
|
|
sort.Strings(keys)
|
|
records := make([]EffectiveArtifactRecord, 0, len(keys))
|
|
partySources := partyCharacterSourceLookup(party)
|
|
for _, key := range keys {
|
|
artifact := catalog.Artifacts[key]
|
|
record := EffectiveArtifactRecord{
|
|
Key: key,
|
|
SourceID: artifactpolicy.ConfiguredSourceID(key),
|
|
Enabled: artifact.Enabled,
|
|
OutputPath: artifact.OutputPath,
|
|
Dependencies: append([]string(nil), artifact.DependsOn...),
|
|
}
|
|
if member, ok := catalog.Families.Members[key]; ok {
|
|
record.DefinitionSources = append([]string(nil), catalog.Families.Families[member.Family].Sources...)
|
|
record.FamilyKey = member.Family
|
|
record.CharacterID = member.CharacterID
|
|
record.FamilySources = append([]string(nil), catalog.Families.Families[member.Family].Sources...)
|
|
record.PartySource = strings.Join(partySources(member.CharacterID), ",")
|
|
} else {
|
|
record.DefinitionSources = definitionSources(configkit.Keys("scriptorium", "artifacts", key))
|
|
}
|
|
record.Dependencies = nonEmptySortedStrings(record.Dependencies)
|
|
records = append(records, record)
|
|
}
|
|
return records
|
|
}
|
|
|
|
// partyCharacterSourceLookup owns a single inspection's detached native evidence.
|
|
func partyCharacterSourceLookup(party ResolvedParty) func(string) []string {
|
|
var metadata configkit.Metadata
|
|
sources := map[string]string{}
|
|
if party.Canonical != nil && party.Canonical.authoring != nil {
|
|
result := party.Canonical.authoring
|
|
metadata = result.Metadata()
|
|
for _, source := range result.Sources() {
|
|
name := source.Path
|
|
if name == "" {
|
|
name = source.Label
|
|
}
|
|
sources[source.ID] = name
|
|
}
|
|
}
|
|
cache := map[string][]string{}
|
|
return func(id string) []string {
|
|
if value, ok := cache[id]; ok {
|
|
return value
|
|
}
|
|
path := configkit.Keys("characters", id)
|
|
var ids []string
|
|
add := func(origin configkit.Origin) {
|
|
for _, location := range origin.Locations {
|
|
ids = append(ids, location.SourceID)
|
|
}
|
|
for _, step := range origin.Inheritance {
|
|
for _, location := range step.Locations {
|
|
ids = append(ids, location.SourceID)
|
|
}
|
|
}
|
|
}
|
|
if info, ok := metadata.Lookup(path); ok {
|
|
add(info.Origin)
|
|
}
|
|
for _, change := range metadata.History(path) {
|
|
if change.Origin.Layer == configkit.LayerMain || change.Origin.Layer == configkit.LayerOverlay {
|
|
add(change.Origin)
|
|
}
|
|
}
|
|
var paths []string
|
|
for _, id := range nonEmptySortedStrings(ids) {
|
|
if name := sources[id]; name != "" {
|
|
paths = append(paths, name)
|
|
}
|
|
}
|
|
if len(paths) == 0 {
|
|
paths = append(paths, party.Source.Path)
|
|
}
|
|
cache[id] = nonEmptySortedStrings(paths)
|
|
return cache[id]
|
|
}
|
|
}
|
|
|
|
// declarationSourceLookup shares one detached snapshot within a catalog derivation.
|
|
func declarationSourceLookup(cfg *PipelineConfig) func(configkit.Path) []string {
|
|
result := cfg.ConfigKitResult()
|
|
if result == nil {
|
|
return func(configkit.Path) []string { return nil }
|
|
}
|
|
metadata := result.Metadata()
|
|
pathsByID := pipelineSourcePaths(cfg)
|
|
return func(declaration configkit.Path) []string {
|
|
return declarationDefinitionSourcesWithIndex(metadata, declaration, pathsByID)
|
|
}
|
|
}
|
|
|
|
func declarationDefinitionSourcesWithIndex(metadata configkit.Metadata, declaration configkit.Path, pathsByID map[string]string) []string {
|
|
seen := map[string]struct{}{}
|
|
for _, change := range metadata.History(declaration) {
|
|
if change.Origin.Layer != configkit.LayerMain && change.Origin.Layer != configkit.LayerOverlay {
|
|
continue
|
|
}
|
|
for _, location := range change.Origin.Locations {
|
|
seen[location.SourceID] = struct{}{}
|
|
}
|
|
for _, inheritance := range change.Origin.Inheritance {
|
|
for _, location := range inheritance.Locations {
|
|
seen[location.SourceID] = struct{}{}
|
|
}
|
|
}
|
|
}
|
|
if len(seen) == 0 {
|
|
if info, ok := metadata.Lookup(declaration); ok {
|
|
for _, location := range info.Origin.Locations {
|
|
seen[location.SourceID] = struct{}{}
|
|
}
|
|
for _, inheritance := range info.Origin.Inheritance {
|
|
for _, location := range inheritance.Locations {
|
|
seen[location.SourceID] = struct{}{}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
result := make([]string, 0, len(seen))
|
|
for sourceID := range seen {
|
|
source := pathsByID[sourceID]
|
|
if source == "" {
|
|
source = sourceID
|
|
}
|
|
result = append(result, source)
|
|
}
|
|
sort.Strings(result)
|
|
return result
|
|
}
|
|
|
|
func nonEmptySortedStrings(values []string) []string {
|
|
seen := map[string]struct{}{}
|
|
for _, value := range values {
|
|
value = strings.TrimSpace(value)
|
|
if value != "" {
|
|
seen[value] = struct{}{}
|
|
}
|
|
}
|
|
result := make([]string, 0, len(seen))
|
|
for value := range seen {
|
|
result = append(result, value)
|
|
}
|
|
sort.Strings(result)
|
|
return result
|
|
}
|