Skip to content

Commit

Permalink
Add a version field to buildscripts.
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchell-as committed Nov 29, 2023
1 parent 4aeec49 commit f00aff9
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 19 deletions.
12 changes: 9 additions & 3 deletions internal/runbits/buildscript/buildscript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ func TestDiff(t *testing.T) {
)
in:
runtime`))
runtime
version: 1`))
require.NoError(t, err)

// Make a copy of the original expression.
Expand Down Expand Up @@ -64,7 +66,9 @@ in:
)
in:
runtime`, result)
runtime
version: 1`, result)
}

// TestRealWorld tests a real-world case where:
Expand Down Expand Up @@ -130,5 +134,7 @@ func TestRealWorld(t *testing.T) {
)
in:
runtime`, result)
runtime
version: 1`, result)
}
4 changes: 3 additions & 1 deletion internal/runbits/buildscript/testdata/buildscript1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ let:
)

in:
runtime
runtime

version: 1
4 changes: 3 additions & 1 deletion internal/runbits/buildscript/testdata/buildscript2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@ let:
)

in:
runtime
runtime

version: 1
32 changes: 24 additions & 8 deletions pkg/platform/runtime/buildexpression/merge/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ func TestMergeAdd(t *testing.T) {
)
in:
runtime`))
runtime
version: 1`))
require.NoError(t, err)
bytes, err := json.Marshal(scriptA)
require.NoError(t, err)
Expand All @@ -59,7 +61,9 @@ in:
)
in:
runtime`))
runtime
version: 1`))
require.NoError(t, err)
bytes, err = json.Marshal(scriptB)
require.NoError(t, err)
Expand Down Expand Up @@ -104,7 +108,9 @@ in:
)
in:
runtime`, mergedScript.String())
runtime
version: 1`, mergedScript.String())
}

func TestMergeRemove(t *testing.T) {
Expand Down Expand Up @@ -132,7 +138,9 @@ func TestMergeRemove(t *testing.T) {
)
in:
runtime`))
runtime
version: 1`))
require.NoError(t, err)
bytes, err := json.Marshal(scriptA)
require.NoError(t, err)
Expand All @@ -159,7 +167,9 @@ in:
)
in:
runtime`))
runtime
version: 1`))
require.NoError(t, err)
bytes, err = json.Marshal(scriptB)
require.NoError(t, err)
Expand Down Expand Up @@ -200,7 +210,9 @@ in:
)
in:
runtime`, mergedScript.String())
runtime
version: 1`, mergedScript.String())
}

func TestMergeConflict(t *testing.T) {
Expand All @@ -220,7 +232,9 @@ func TestMergeConflict(t *testing.T) {
)
in:
runtime`))
runtime
version: 1`))
require.NoError(t, err)
bytes, err := json.Marshal(scriptA)
require.NoError(t, err)
Expand All @@ -246,7 +260,9 @@ in:
)
in:
runtime`))
runtime
version: 1`))
require.NoError(t, err)
bytes, err = json.Marshal(scriptB)
require.NoError(t, err)
Expand Down
27 changes: 23 additions & 4 deletions pkg/platform/runtime/buildscript/buildscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ import (
"github.com/alecthomas/participle/v2"
)

// Version is the current buildscript version.
const CurrentVersion int = 1

// Script's tagged fields will be initially filled in by Participle.
// Expr will be constructed later and is this script's buildexpression. We keep a copy of the build
// expression here with any changes that have been applied before either writing it to disk or
// submitting it to the build planner. It's easier to operate on build expressions directly than to
// modify or manually populate the Participle-produced fields and re-generate a build expression.
type Script struct {
Let *Let `parser:"'let' ':' @@"`
In *In `parser:"'in' ':' @@"`
Expr *buildexpression.BuildExpression
Let *Let `parser:"'let' ':' @@"`
In *In `parser:"'in' ':' @@"`
Expr *buildexpression.BuildExpression
Version int `parser:"'version' ':' @Int"`
}

type Let struct {
Expand Down Expand Up @@ -82,11 +86,23 @@ func NewScript(data []byte) (*Script, error) {
}
script.Expr = expr

// Migrate from older buildscript versions if necessary.
if script.Version < CurrentVersion {
err := migrateOldBuildScript(script)
if err != nil {
return nil, errs.Wrap(err, "Unable to migrate old build script")
}
}

return script, nil
}

func migrateOldBuildScript(script *Script) error {
return nil // Noop for now until we need to migrate
}

func NewScriptFromBuildExpression(expr *buildexpression.BuildExpression) (*Script, error) {
return &Script{Expr: expr}, nil
return &Script{Expr: expr, Version: CurrentVersion}, nil
}

func indent(s string) string {
Expand All @@ -108,6 +124,9 @@ func (s *Script) String() string {
case s.Expr.Let.In.Name != nil:
buf.WriteString(indent(*s.Expr.Let.In.Name))
}
buf.WriteString("\n\n")
buf.WriteString("version: ")
buf.WriteString(strconv.Itoa(s.Version))
return buf.String()
}

Expand Down
16 changes: 14 additions & 2 deletions pkg/platform/runtime/buildscript/buildscript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func TestBasic(t *testing.T) {
)
in:
runtime
version: 1
`))
require.NoError(t, err)

Expand Down Expand Up @@ -89,6 +90,7 @@ in:
},
&In{Name: ptr.To("runtime")},
expr,
1,
}, script)
}

Expand Down Expand Up @@ -117,6 +119,8 @@ in:
win_installer(win_runtime),
tar_installer(linux_runtime)
)
version: 1
`))
require.NoError(t, err)

Expand Down Expand Up @@ -165,6 +169,7 @@ in:
{FuncCall: &FuncCall{"tar_installer", []*Value{{Ident: ptr.To("linux_runtime")}}}},
}}},
expr,
1,
}, script)
}

Expand All @@ -191,7 +196,8 @@ const example = `let:
solver_version = 0
)
in:
runtime`
runtime
version: 1`

func TestExample(t *testing.T) {
script, err := NewScript([]byte(example))
Expand Down Expand Up @@ -241,6 +247,7 @@ func TestExample(t *testing.T) {
},
&In{Name: ptr.To("runtime")},
expr,
1,
}, script)
}

Expand All @@ -253,6 +260,7 @@ func TestString(t *testing.T) {
)
in:
runtime
version: 1
`))
require.NoError(t, err)

Expand All @@ -271,7 +279,9 @@ in:
)
in:
runtime`, script.String())
runtime
version: 1`, script.String())
}

func TestRoundTrip(t *testing.T) {
Expand Down Expand Up @@ -304,6 +314,7 @@ func TestJson(t *testing.T) {
)
in:
runtime
version: 1
`))
require.NoError(t, err)

Expand Down Expand Up @@ -386,6 +397,7 @@ func TestBuildExpression(t *testing.T) {
script, err := NewScriptFromBuildExpression(expr)
require.NoError(t, err)
require.NotNil(t, script)
assert.Equal(t, CurrentVersion, script.Version)
newExpr := script.Expr
exprBytes, err := json.Marshal(expr)
require.NoError(t, err)
Expand Down

0 comments on commit f00aff9

Please sign in to comment.