From f00aff9de16e22895f2cbc7b8320f894ccfb0a83 Mon Sep 17 00:00:00 2001 From: mitchell Date: Wed, 29 Nov 2023 10:34:24 -0500 Subject: [PATCH] Add a version field to buildscripts. --- .../runbits/buildscript/buildscript_test.go | 12 +++++-- .../buildscript/testdata/buildscript1.yaml | 4 ++- .../buildscript/testdata/buildscript2.yaml | 4 ++- .../buildexpression/merge/merge_test.go | 32 ++++++++++++++----- .../runtime/buildscript/buildscript.go | 27 +++++++++++++--- .../runtime/buildscript/buildscript_test.go | 16 ++++++++-- 6 files changed, 76 insertions(+), 19 deletions(-) diff --git a/internal/runbits/buildscript/buildscript_test.go b/internal/runbits/buildscript/buildscript_test.go index ceb19353b2..e3a3f57051 100644 --- a/internal/runbits/buildscript/buildscript_test.go +++ b/internal/runbits/buildscript/buildscript_test.go @@ -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. @@ -64,7 +66,9 @@ in: ) in: - runtime`, result) + runtime + +version: 1`, result) } // TestRealWorld tests a real-world case where: @@ -130,5 +134,7 @@ func TestRealWorld(t *testing.T) { ) in: - runtime`, result) + runtime + +version: 1`, result) } diff --git a/internal/runbits/buildscript/testdata/buildscript1.yaml b/internal/runbits/buildscript/testdata/buildscript1.yaml index 7ae010f237..33451569f9 100644 --- a/internal/runbits/buildscript/testdata/buildscript1.yaml +++ b/internal/runbits/buildscript/testdata/buildscript1.yaml @@ -33,4 +33,6 @@ let: ) in: - runtime \ No newline at end of file + runtime + +version: 1 \ No newline at end of file diff --git a/internal/runbits/buildscript/testdata/buildscript2.yaml b/internal/runbits/buildscript/testdata/buildscript2.yaml index 0196b1d734..01f89cb7aa 100644 --- a/internal/runbits/buildscript/testdata/buildscript2.yaml +++ b/internal/runbits/buildscript/testdata/buildscript2.yaml @@ -39,4 +39,6 @@ let: ) in: - runtime \ No newline at end of file + runtime + +version: 1 \ No newline at end of file diff --git a/pkg/platform/runtime/buildexpression/merge/merge_test.go b/pkg/platform/runtime/buildexpression/merge/merge_test.go index bbf3d142e9..887a4ec263 100644 --- a/pkg/platform/runtime/buildexpression/merge/merge_test.go +++ b/pkg/platform/runtime/buildexpression/merge/merge_test.go @@ -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) @@ -59,7 +61,9 @@ in: ) in: - runtime`)) + runtime + +version: 1`)) require.NoError(t, err) bytes, err = json.Marshal(scriptB) require.NoError(t, err) @@ -104,7 +108,9 @@ in: ) in: - runtime`, mergedScript.String()) + runtime + +version: 1`, mergedScript.String()) } func TestMergeRemove(t *testing.T) { @@ -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) @@ -159,7 +167,9 @@ in: ) in: - runtime`)) + runtime + +version: 1`)) require.NoError(t, err) bytes, err = json.Marshal(scriptB) require.NoError(t, err) @@ -200,7 +210,9 @@ in: ) in: - runtime`, mergedScript.String()) + runtime + +version: 1`, mergedScript.String()) } func TestMergeConflict(t *testing.T) { @@ -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) @@ -246,7 +260,9 @@ in: ) in: - runtime`)) + runtime + +version: 1`)) require.NoError(t, err) bytes, err = json.Marshal(scriptB) require.NoError(t, err) diff --git a/pkg/platform/runtime/buildscript/buildscript.go b/pkg/platform/runtime/buildscript/buildscript.go index de4b765fcd..304d6628ca 100644 --- a/pkg/platform/runtime/buildscript/buildscript.go +++ b/pkg/platform/runtime/buildscript/buildscript.go @@ -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 { @@ -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 { @@ -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() } diff --git a/pkg/platform/runtime/buildscript/buildscript_test.go b/pkg/platform/runtime/buildscript/buildscript_test.go index b705392934..13db02d125 100644 --- a/pkg/platform/runtime/buildscript/buildscript_test.go +++ b/pkg/platform/runtime/buildscript/buildscript_test.go @@ -48,6 +48,7 @@ func TestBasic(t *testing.T) { ) in: runtime +version: 1 `)) require.NoError(t, err) @@ -89,6 +90,7 @@ in: }, &In{Name: ptr.To("runtime")}, expr, + 1, }, script) } @@ -117,6 +119,8 @@ in: win_installer(win_runtime), tar_installer(linux_runtime) ) + +version: 1 `)) require.NoError(t, err) @@ -165,6 +169,7 @@ in: {FuncCall: &FuncCall{"tar_installer", []*Value{{Ident: ptr.To("linux_runtime")}}}}, }}}, expr, + 1, }, script) } @@ -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)) @@ -241,6 +247,7 @@ func TestExample(t *testing.T) { }, &In{Name: ptr.To("runtime")}, expr, + 1, }, script) } @@ -253,6 +260,7 @@ func TestString(t *testing.T) { ) in: runtime +version: 1 `)) require.NoError(t, err) @@ -271,7 +279,9 @@ in: ) in: - runtime`, script.String()) + runtime + +version: 1`, script.String()) } func TestRoundTrip(t *testing.T) { @@ -304,6 +314,7 @@ func TestJson(t *testing.T) { ) in: runtime +version: 1 `)) require.NoError(t, err) @@ -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)