core/generate/command_step_builder.go
core/generate/command_step_builder.goBrowse 1970 files
805 tokens
2,972 bytes
Token encoding: o200k_base
Snapshot 21a254f
← Back to SKILL.md
1package generate2 3import (4 "maps"5 6 "github.com/charmbracelet/log"7 a "github.com/railwayapp/railpack/core/app"8 "github.com/railwayapp/railpack/core/plan"9)10 11type CommandStepBuilder struct {12 DisplayName string13 Commands []plan.Command14 Inputs []plan.Layer15 Assets map[string]string16 Variables map[string]string17 Caches []string18 Secrets []string19 app *a.App20 env *a.Environment21}22 23func (c *GenerateContext) NewCommandStep(name string) *CommandStepBuilder {24 step := &CommandStepBuilder{25 DisplayName: c.GetStepName(name),26 Inputs: []plan.Layer{},27 Commands: []plan.Command{},28 Assets: map[string]string{},29 Variables: map[string]string{},30 Caches: []string{},31 Secrets: []string{"*"},32 app: c.App,33 env: c.Env,34 }35 36 // Remove any existing step with the same name37 for i, existingStep := range c.Steps {38 if existingStep.Name() == step.Name() {39 c.Steps = append(c.Steps[:i], c.Steps[i+1:]...)40 break41 }42 }43 44 c.Steps = append(c.Steps, step)45 46 return step47}48 49func (b *CommandStepBuilder) AddInput(input plan.Layer) {50 b.Inputs = append(b.Inputs, input)51}52 53func (b *CommandStepBuilder) AddInputs(inputs []plan.Layer) {54 b.Inputs = append(b.Inputs, inputs...)55}56 57func (b *CommandStepBuilder) AddVariables(variables map[string]string) {58 maps.Copy(b.Variables, variables)59}60 61func (b *CommandStepBuilder) AddCache(name string) {62 if name == "" {63 log.Error("Cache name is empty")64 return65 }66 67 b.Caches = append(b.Caches, name)68}69 70func (b *CommandStepBuilder) AddCommand(command plan.Command) {71 b.AddCommands([]plan.Command{command})72}73 74func (b *CommandStepBuilder) AddCommands(commands []plan.Command) {75 if b.Commands == nil {76 b.Commands = []plan.Command{}77 }78 b.Commands = append(b.Commands, commands...)79}80 81func (b *CommandStepBuilder) AddEnvVars(envVars map[string]string) {82 maps.Copy(b.Variables, envVars)83}84 85func (b *CommandStepBuilder) AddPaths(paths []string) {86 commands := []plan.Command{}87 for _, path := range paths {88 commands = append(commands, plan.NewPathCommand(path))89 }90 b.AddCommands(commands)91}92 93func (b *CommandStepBuilder) UseSecretsWithPrefixes(prefixes []string) {94 for _, prefix := range prefixes {95 b.UseSecretsWithPrefix(prefix)96 }97}98 99func (b *CommandStepBuilder) UseSecretsWithPrefix(prefix string) {100 secrets := b.env.GetSecretsWithPrefix(prefix)101 b.Secrets = append(b.Secrets, secrets...)102}103 104func (b *CommandStepBuilder) UseSecrets(secrets []string) {105 if b.env.GetVariable("CI") != "" {106 b.Secrets = append(b.Secrets, secrets...)107 }108}109 110func (b *CommandStepBuilder) Name() string {111 return b.DisplayName112}113 114func (b *CommandStepBuilder) Build(p *plan.BuildPlan, options *BuildStepOptions) error {115 step := plan.NewStep(b.DisplayName)116 117 step.Inputs = b.Inputs118 step.Commands = b.Commands119 step.Assets = b.Assets120 step.Caches = b.Caches121 step.Variables = b.Variables122 step.Secrets = b.Secrets123 124 p.Steps = append(p.Steps, *step)125 126 return nil127}128