190 lines
5.7 KiB
Go
190 lines
5.7 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"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) {
|
|
if cfg == nil {
|
|
return nil, fmt.Errorf("pipeline config is required")
|
|
}
|
|
if cfg.Scriptorium == nil || len(cfg.Scriptorium.Artifacts) == 0 {
|
|
return []EffectiveArtifactRecord{}, nil
|
|
}
|
|
|
|
keys := make([]string, 0, len(cfg.Scriptorium.Artifacts))
|
|
for key := range cfg.Scriptorium.Artifacts {
|
|
keys = append(keys, key)
|
|
}
|
|
sort.Strings(keys)
|
|
records := make([]EffectiveArtifactRecord, 0, len(keys))
|
|
pathsByID := pipelineSourcePaths(cfg)
|
|
partySources := partyCharacterSourceLookup(party)
|
|
for _, key := range keys {
|
|
artifact := cfg.Scriptorium.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 := cfg.familyCatalog.Members[key]; ok {
|
|
record.DefinitionSources = append([]string(nil), cfg.familyCatalog.Families[member.Family].Sources...)
|
|
record.FamilyKey = member.Family
|
|
record.CharacterID = member.CharacterID
|
|
record.FamilySources = append([]string(nil), cfg.familyCatalog.Families[member.Family].Sources...)
|
|
record.PartySource = strings.Join(partySources(member.CharacterID), ",")
|
|
} else {
|
|
record.DefinitionSources = declarationDefinitionSourcesWithIndex(cfg, configkit.Keys("scriptorium", "artifacts", key), pathsByID)
|
|
}
|
|
record.Dependencies = nonEmptySortedStrings(record.Dependencies)
|
|
records = append(records, record)
|
|
}
|
|
return records, nil
|
|
}
|
|
|
|
// 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]
|
|
}
|
|
}
|
|
|
|
func artifactFamilyDefinitionSources(cfg *PipelineConfig, key string) []string {
|
|
return declarationDefinitionSources(cfg,
|
|
configkit.Keys("scriptorium", "artifact_families", key),
|
|
)
|
|
}
|
|
|
|
func declarationDefinitionSources(cfg *PipelineConfig, declaration configkit.Path) []string {
|
|
return declarationDefinitionSourcesWithIndex(cfg, declaration, pipelineSourcePaths(cfg))
|
|
}
|
|
|
|
func declarationDefinitionSourcesWithIndex(cfg *PipelineConfig, declaration configkit.Path, pathsByID map[string]string) []string {
|
|
if cfg == nil || cfg.resolution == nil {
|
|
return nil
|
|
}
|
|
seen := map[string]struct{}{}
|
|
for _, change := range cfg.resolution.configkitMetadata.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 := cfg.resolution.configkitMetadata.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
|
|
}
|