diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c3adf6cc31..a93ebd6f26 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,3 +1,5 @@ +# This file is autogenerated by the 'modulegen' tool. +# Please update the 'ci.yml' template instead. name: Main pipeline on: diff --git a/modulegen/_template/ci.yml.tmpl b/modulegen/_template/ci.yml.tmpl index 388d891bfc..2038cdb0e4 100644 --- a/modulegen/_template/ci.yml.tmpl +++ b/modulegen/_template/ci.yml.tmpl @@ -1,3 +1,5 @@ +# This file is autogenerated by the 'modulegen' tool. +# Please update the 'ci.yml' template instead. name: Main pipeline on: @@ -70,7 +72,7 @@ jobs: strategy: matrix: go-version: [1.20.x, 1.x] - platform: [ubuntu-latest, macos-latest] + platform: [ubuntu-latest, macos-latest, windows-latest] uses: ./.github/workflows/ci-test-go.yml with: go-version: {{ "${{ matrix.go-version }}" }} diff --git a/modulegen/context_test.go b/modulegen/context_test.go index b0812a5308..2ec8acc2dd 100644 --- a/modulegen/context_test.go +++ b/modulegen/context_test.go @@ -30,7 +30,7 @@ func TestGetDependabotConfigFile(t *testing.T) { } func TestExamplesHasDependabotEntry(t *testing.T) { - ctx := getRootContext(t) + ctx := getTestRootContext(t) examples, err := ctx.GetExamples() require.NoError(t, err) dependabotUpdates, err := dependabot.GetUpdates(ctx.DependabotConfigFile()) @@ -64,7 +64,7 @@ func TestExamplesHasDependabotEntry(t *testing.T) { } func TestModulesHasDependabotEntry(t *testing.T) { - ctx := getRootContext(t) + ctx := getTestRootContext(t) modules, err := ctx.GetModules() require.NoError(t, err) dependabotUpdates, err := dependabot.GetUpdates(ctx.DependabotConfigFile()) @@ -95,3 +95,9 @@ func TestModulesHasDependabotEntry(t *testing.T) { assert.True(t, found, "module %s is not present in the dependabot updates", module) } } + +func getTestRootContext(t *testing.T) *Context { + current, err := os.Getwd() + require.NoError(t, err) + return NewContext(filepath.Dir(current)) +} diff --git a/modulegen/internal/template/main.go b/modulegen/internal/template/main.go new file mode 100644 index 0000000000..0198cb6465 --- /dev/null +++ b/modulegen/internal/template/main.go @@ -0,0 +1,22 @@ +package template + +import ( + "os" + "path/filepath" + "text/template" +) + +func Generate(t *template.Template, exampleFilePath string, name string, data any) error { + err := os.MkdirAll(filepath.Dir(exampleFilePath), 0o755) + if err != nil { + return err + } + exampleFile, _ := os.Create(exampleFilePath) + defer exampleFile.Close() + + err = t.ExecuteTemplate(exampleFile, name, data) + if err != nil { + return err + } + return nil +} diff --git a/modulegen/internal/workflow/main.go b/modulegen/internal/workflow/main.go new file mode 100644 index 0000000000..098fe2ab75 --- /dev/null +++ b/modulegen/internal/workflow/main.go @@ -0,0 +1,18 @@ +package workflow + +import ( + "path/filepath" + "text/template" + + internal_template "github.com/testcontainers/testcontainers-go/modulegen/internal/template" +) + +func Generate(githubWorkflowsDir string, examples []string, modules []string) error { + projectDirectories := newProjectDirectories(examples, modules) + name := "ci.yml.tmpl" + t, err := template.New(name).ParseFiles(filepath.Join("_template", name)) + if err != nil { + return err + } + return internal_template.Generate(t, filepath.Join(githubWorkflowsDir, "ci.yml"), name, projectDirectories) +} diff --git a/modulegen/internal/workflow/types.go b/modulegen/internal/workflow/types.go new file mode 100644 index 0000000000..77433613fb --- /dev/null +++ b/modulegen/internal/workflow/types.go @@ -0,0 +1,17 @@ +package workflow + +import ( + "strings" +) + +type ProjectDirectories struct { + Examples string + Modules string +} + +func newProjectDirectories(examples []string, modules []string) *ProjectDirectories { + return &ProjectDirectories{ + Examples: strings.Join(examples, ", "), + Modules: strings.Join(modules, ", "), + } +} diff --git a/modulegen/main.go b/modulegen/main.go index 28976b2e7f..65eb2762ad 100644 --- a/modulegen/main.go +++ b/modulegen/main.go @@ -7,7 +7,6 @@ import ( "os" "path/filepath" "regexp" - "sort" "strings" "unicode" "unicode/utf8" @@ -18,6 +17,7 @@ import ( "github.com/testcontainers/testcontainers-go/modulegen/internal/dependabot" "github.com/testcontainers/testcontainers-go/modulegen/internal/mkdocs" "github.com/testcontainers/testcontainers-go/modulegen/internal/tools" + "github.com/testcontainers/testcontainers-go/modulegen/internal/workflow" ) var ( @@ -28,7 +28,7 @@ var ( ) var templates = []string{ - "ci.yml", "docs_example.md", "example_test.go", "example.go", "go.mod", "Makefile", + "docs_example.md", "example_test.go", "example.go", "go.mod", "Makefile", } func init() { @@ -176,7 +176,6 @@ func generate(example Example, ctx *Context) error { return err } - githubWorkflowsDir := ctx.GithubWorkflowsDir() outputDir := filepath.Join(ctx.RootDir, example.ParentDir()) docsOuputDir := filepath.Join(ctx.DocsDir(), example.ParentDir()) @@ -219,30 +218,6 @@ func generate(example Example, ctx *Context) error { if strings.EqualFold(tmpl, "docs_example.md") { // docs example file will go into the docs directory exampleFilePath = filepath.Join(docsOuputDir, exampleLower+".md") - } else if strings.EqualFold(tmpl, "ci.yml") { - // GitHub workflow file will go into the .github/workflows directory - exampleFilePath = filepath.Join(githubWorkflowsDir, "ci.yml") - - type stringsList struct { - Examples string - Modules string - } - - syncDataFn = func() any { - modulesList, err := getModulesOrExamplesAsString(true) - if err != nil { - return "" - } - examplesList, err := getModulesOrExamplesAsString(false) - if err != nil { - return "" - } - - return stringsList{ - Examples: examplesList, - Modules: modulesList, - } - } } else { exampleFilePath = filepath.Join(outputDir, exampleLower, strings.ReplaceAll(tmpl, "example", exampleLower)) } @@ -262,19 +237,21 @@ func generate(example Example, ctx *Context) error { return err } } - + // update github ci workflow + err = generateWorkFlow(ctx) + if err != nil { + return err + } // update examples in mkdocs err = generateMkdocs(ctx, example) if err != nil { return err } - // update examples in dependabot err = generateDependabotUpdates(ctx, example) if err != nil { return err } - return nil } @@ -291,58 +268,26 @@ func generateMkdocs(ctx *Context, example Example) error { return mkdocs.UpdateConfig(ctx.MkdocsConfigFile(), example.IsModule, exampleMd, indexMd) } -func getModulesOrExamples(t bool) ([]os.DirEntry, error) { - baseDir := "examples" - if t { - baseDir = "modules" - } - - parent, err := getRootDir() +func generateWorkFlow(ctx *Context) error { + rootCtx, err := getRootContext() if err != nil { - return nil, err + return err } - - dir := filepath.Join(parent, baseDir) - - allFiles, err := os.ReadDir(dir) + examples, err := rootCtx.GetExamples() if err != nil { - return nil, err - } - - dirs := make([]os.DirEntry, 0) - - for _, f := range allFiles { - // only accept the directories and not the template - if f.IsDir() && f.Name() != "_template" { - dirs = append(dirs, f) - } + return err } - - return dirs, nil -} - -func getModulesOrExamplesAsString(t bool) (string, error) { - dirs, err := getModulesOrExamples(t) + modules, err := rootCtx.GetModules() if err != nil { - return "", err - } - - // sort the dir names by name - names := make([]string, len(dirs)) - for i, f := range dirs { - names[i] = f.Name() + return err } - - sort.Strings(names) - - return strings.Join(names, ", "), nil + return workflow.Generate(ctx.GithubWorkflowsDir(), examples, modules) } -func getRootDir() (string, error) { +func getRootContext() (*Context, error) { current, err := os.Getwd() if err != nil { - return "", err + return nil, err } - - return filepath.Dir(current), nil + return NewContext(filepath.Dir(current)), nil } diff --git a/modulegen/main_test.go b/modulegen/main_test.go index 9e9c104d43..883054890e 100644 --- a/modulegen/main_test.go +++ b/modulegen/main_test.go @@ -475,15 +475,15 @@ func assertExampleGithubWorkflowContent(t *testing.T, example Example, exampleWo assert.Nil(t, err) data := sanitiseContent(content) - ctx := getRootContext(t) + ctx := getTestRootContext(t) modulesList, err := ctx.GetModules() assert.Nil(t, err) - assert.Equal(t, " module: ["+strings.Join(modulesList, ", ")+"]", data[88]) + assert.Equal(t, " module: ["+strings.Join(modulesList, ", ")+"]", data[90]) examplesList, err := ctx.GetExamples() assert.Nil(t, err) - assert.Equal(t, " module: ["+strings.Join(examplesList, ", ")+"]", data[104]) + assert.Equal(t, " module: ["+strings.Join(examplesList, ", ")+"]", data[106]) } // assert content go.mod @@ -548,7 +548,6 @@ func sanitiseContent(bytes []byte) []string { } func copyInitialDependabotConfig(t *testing.T, tmpCtx *Context) error { - ctx := getRootContext(t) - + ctx := getTestRootContext(t) return dependabot.CopyConfig(ctx.DependabotConfigFile(), tmpCtx.DependabotConfigFile()) } diff --git a/modulegen/mkdocs_test.go b/modulegen/mkdocs_test.go index b8314a12a0..badce9e442 100644 --- a/modulegen/mkdocs_test.go +++ b/modulegen/mkdocs_test.go @@ -56,7 +56,7 @@ func TestReadMkDocsConfig(t *testing.T) { } func TestExamples(t *testing.T) { - ctx := getRootContext(t) + ctx := getTestRootContext(t) examples, err := ctx.GetExamples() require.NoError(t, err) examplesDocs, err := ctx.GetExamplesDocs() @@ -81,13 +81,6 @@ func TestExamples(t *testing.T) { } func copyInitialMkdocsConfig(t *testing.T, tmpCtx *Context) error { - ctx := getRootContext(t) - + ctx := getTestRootContext(t) return mkdocs.CopyConfig(ctx.MkdocsConfigFile(), tmpCtx.MkdocsConfigFile()) } - -func getRootContext(t *testing.T) *Context { - current, err := os.Getwd() - require.NoError(t, err) - return NewContext(filepath.Dir(current)) -}