Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modulegen: create internal/workflow #1520

Merged
merged 1 commit into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# This file is autogenerated by the 'modulegen' tool.
# Please update the 'ci.yml' template instead.
name: Main pipeline

on:
Expand Down
4 changes: 3 additions & 1 deletion modulegen/_template/ci.yml.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# This file is autogenerated by the 'modulegen' tool.
# Please update the 'ci.yml' template instead.
name: Main pipeline

on:
Expand Down Expand Up @@ -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 }}" }}
Expand Down
10 changes: 8 additions & 2 deletions modulegen/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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 {
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved
current, err := os.Getwd()
require.NoError(t, err)
return NewContext(filepath.Dir(current))
}
22 changes: 22 additions & 0 deletions modulegen/internal/template/main.go
Original file line number Diff line number Diff line change
@@ -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
}
18 changes: 18 additions & 0 deletions modulegen/internal/workflow/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
17 changes: 17 additions & 0 deletions modulegen/internal/workflow/types.go
Original file line number Diff line number Diff line change
@@ -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, ", "),
}
}
91 changes: 18 additions & 73 deletions modulegen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"unicode"
"unicode/utf8"
Expand All @@ -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 (
Expand All @@ -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() {
Expand Down Expand Up @@ -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())

Expand Down Expand Up @@ -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))
}
Expand All @@ -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
}

Expand All @@ -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
}
9 changes: 4 additions & 5 deletions modulegen/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
}
11 changes: 2 additions & 9 deletions modulegen/mkdocs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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))
}