-
-
Notifications
You must be signed in to change notification settings - Fork 523
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
modulegen: create internal/module and internal/modfile (#1539)
Signed-off-by: Matthieu MOREL <[email protected]>
- Loading branch information
Showing
12 changed files
with
173 additions
and
73 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package modfile | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
"golang.org/x/mod/modfile" | ||
) | ||
|
||
func GenerateModFile(exampleDir string, rootGoModFilePath string, directory string, tcVersion string) error { | ||
rootGoMod, err := readModFile(rootGoModFilePath) | ||
if err != nil { | ||
return err | ||
} | ||
moduleStmt := rootGoMod.Module.Mod.Path + directory | ||
goStmt := rootGoMod.Go.Version | ||
tcPath := rootGoMod.Module.Mod.Path | ||
file, err := newModFile(moduleStmt, goStmt, tcPath, tcVersion) | ||
if err != nil { | ||
return err | ||
} | ||
return writeModFile(filepath.Join(exampleDir, "go.mod"), file) | ||
} | ||
|
||
func newModFile(moduleStmt string, goStmt string, tcPath string, tcVersion string) (*modfile.File, error) { | ||
file := &modfile.File{} | ||
err := file.AddModuleStmt(moduleStmt) | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = file.AddGoStmt(goStmt) | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = file.AddRequire(tcPath, tcVersion) | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = file.AddReplace(tcPath, "", "../..", "") | ||
if err != nil { | ||
return nil, err | ||
} | ||
return file, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package modfile | ||
|
||
import ( | ||
"os" | ||
|
||
"golang.org/x/mod/modfile" | ||
) | ||
|
||
func readModFile(modFilePath string) (*modfile.File, error) { | ||
file, err := os.ReadFile(modFilePath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return modfile.Parse(modFilePath, file, nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package modfile | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
|
||
"golang.org/x/mod/modfile" | ||
) | ||
|
||
func writeModFile(modFilePath string, file *modfile.File) error { | ||
err := os.MkdirAll(filepath.Dir(modFilePath), 0o755) | ||
if err != nil { | ||
return err | ||
} | ||
data, err := file.Format() | ||
if err != nil { | ||
return err | ||
} | ||
return os.WriteFile(modFilePath, data, 0o644) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package module | ||
|
||
import ( | ||
"path/filepath" | ||
"strings" | ||
"text/template" | ||
|
||
internal_template "github.com/testcontainers/testcontainers-go/modulegen/internal/template" | ||
) | ||
|
||
func GenerateFiles(exampleDir string, exampleName string, funcMap template.FuncMap, example any) error { | ||
for _, tmpl := range []string{"example_test.go", "example.go"} { | ||
name := tmpl + ".tmpl" | ||
t, err := template.New(name).Funcs(funcMap).ParseFiles(filepath.Join("_template", name)) | ||
if err != nil { | ||
return err | ||
} | ||
exampleFilePath := filepath.Join(exampleDir, strings.ReplaceAll(tmpl, "example", exampleName)) | ||
|
||
err = internal_template.Generate(t, exampleFilePath, name, example) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"text/template" | ||
|
||
"github.com/testcontainers/testcontainers-go/modulegen/internal/mkdocs" | ||
"github.com/testcontainers/testcontainers-go/modulegen/internal/modfile" | ||
"github.com/testcontainers/testcontainers-go/modulegen/internal/module" | ||
) | ||
|
||
func generateGoModule(ctx *Context, example Example) error { | ||
exampleDir := filepath.Join(ctx.RootDir, example.ParentDir(), example.Lower()) | ||
err := generateGoFiles(exampleDir, example) | ||
if err != nil { | ||
return err | ||
} | ||
return generateGoModFile(exampleDir, example) | ||
} | ||
|
||
func generateGoFiles(exampleDir string, example Example) error { | ||
funcMap := template.FuncMap{ | ||
"Entrypoint": func() string { return example.Entrypoint() }, | ||
"ContainerName": func() string { return example.ContainerName() }, | ||
"ParentDir": func() string { return example.ParentDir() }, | ||
"ToLower": func() string { return example.Lower() }, | ||
"Title": func() string { return example.Title() }, | ||
} | ||
return module.GenerateFiles(exampleDir, example.Lower(), funcMap, example) | ||
} | ||
|
||
func generateGoModFile(exampleDir string, example Example) error { | ||
rootCtx, err := getRootContext() | ||
if err != nil { | ||
return err | ||
} | ||
mkdocsConfig, err := mkdocs.ReadConfig(rootCtx.MkdocsConfigFile()) | ||
if err != nil { | ||
fmt.Printf(">> could not read MkDocs config: %v\n", err) | ||
return err | ||
} | ||
rootGoModFile := rootCtx.GoModFile() | ||
directory := "/" + example.ParentDir() + "/" + example.Lower() | ||
tcVersion := mkdocsConfig.Extra.LatestVersion | ||
return modfile.GenerateModFile(exampleDir, rootGoModFile, directory, tcVersion) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters