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: generate code-workspace with json marshal #1551

Merged
merged 1 commit into from
Aug 30, 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
117 changes: 117 additions & 0 deletions .vscode/.testcontainers-go.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// This file is autogenerated by the 'modulegen' tool.
{
"folders": [
{
"name": "testcontainers-go",
"path": "../"
},
{
"name": "example / bigtable",
"path": "../examples/bigtable"
},
{
"name": "example / cockroachdb",
"path": "../examples/cockroachdb"
},
{
"name": "example / consul",
"path": "../examples/consul"
},
{
"name": "example / datastore",
"path": "../examples/datastore"
},
{
"name": "example / firestore",
"path": "../examples/firestore"
},
{
"name": "example / nats",
"path": "../examples/nats"
},
{
"name": "example / nginx",
"path": "../examples/nginx"
},
{
"name": "example / pubsub",
"path": "../examples/pubsub"
},
{
"name": "example / questdb",
"path": "../examples/questdb"
},
{
"name": "example / spanner",
"path": "../examples/spanner"
},
{
"name": "example / toxiproxy",
"path": "../examples/toxiproxy"
},
{
"name": "module / artemis",
"path": "../modules/artemis"
},
{
"name": "module / clickhouse",
"path": "../modules/clickhouse"
},
{
"name": "module / compose",
"path": "../modules/compose"
},
{
"name": "module / couchbase",
"path": "../modules/couchbase"
},
{
"name": "module / k3s",
"path": "../modules/k3s"
},
{
"name": "module / localstack",
"path": "../modules/localstack"
},
{
"name": "module / mariadb",
"path": "../modules/mariadb"
},
{
"name": "module / mongodb",
"path": "../modules/mongodb"
},
{
"name": "module / mysql",
"path": "../modules/mysql"
},
{
"name": "module / neo4j",
"path": "../modules/neo4j"
},
{
"name": "module / postgres",
"path": "../modules/postgres"
},
{
"name": "module / pulsar",
"path": "../modules/pulsar"
},
{
"name": "module / redis",
"path": "../modules/redis"
},
{
"name": "module / redpanda",
"path": "../modules/redpanda"
},
{
"name": "module / vault",
"path": "../modules/vault"
},
{
"name": "modulegen",
"path": "../modulegen"
}
]
}
1 change: 1 addition & 0 deletions docs/modules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ We have provided a command line tool to generate the scaffolding for the code of
- a new Nav entry for the module in the docs site, adding it to the `mkdocs.yml` file located at the root directory of the project.
- a GitHub workflow file in the .github/workflows directory to run the tests for the example.
- an entry in Dependabot's configuration file, in order to receive dependency updates.
- an entry in the VSCode workspace file, in order to include the new module in the project's workspace.

### Command line flags

Expand Down
20 changes: 0 additions & 20 deletions modulegen/_template/testcontainers-go.code-workspace.tmpl

This file was deleted.

12 changes: 2 additions & 10 deletions modulegen/internal/vscode/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,12 @@ package vscode

import (
"path/filepath"
"text/template"

internal_template "github.com/testcontainers/testcontainers-go/modulegen/internal/template"
)

func Generate(rootDir string, examples []string, modules []string) error {
projectDirectories := newProjectDirectories(rootDir, examples, modules)
name := "testcontainers-go.code-workspace.tmpl"
t, err := template.New(name).ParseFiles(filepath.Join("_template", name))
if err != nil {
return err
}
config := newConfig(examples, modules)

exampleFilePath := filepath.Join(rootDir, ".vscode", ".testcontainers-go.code-workspace")

return internal_template.GenerateFile(t, exampleFilePath, name, projectDirectories)
return writeConfig(exampleFilePath, config)
}
56 changes: 32 additions & 24 deletions modulegen/internal/vscode/types.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,42 @@
package vscode

import "path/filepath"
import (
"sort"
)

type ProjectDirectories struct {
RootDir string
ModuleGeneratorDir string
Examples []string
Modules []string
type Config struct {
Folders []Folder `json:"folders"`
}

func newProjectDirectories(rootDir string, examples []string, modules []string) *ProjectDirectories {
rootDirAbs, err := filepath.Abs(rootDir)
if err != nil {
rootDirAbs = rootDir
}

moduleGeneratorDirAbs := filepath.Join(rootDirAbs, "modulegen")
type Folder struct {
Name string `json:"name"`
Path string `json:"path"`
}

for i, example := range examples {
examples[i] = filepath.Join(rootDirAbs, "examples", example)
func newConfig(examples []string, modules []string) *Config {
config := Config{
Folders: []Folder{
{
Path: "../modulegen",
Name: "modulegen",
},
},
}

for i, module := range modules {
modules[i] = filepath.Join(rootDirAbs, "modules", module)
for _, example := range examples {
config.Folders = append(config.Folders, Folder{
Path: "../examples/" + example,
Name: "example / " + example,
})
}

return &ProjectDirectories{
RootDir: rootDirAbs,
ModuleGeneratorDir: moduleGeneratorDirAbs,
Examples: examples,
Modules: modules,
for _, module := range modules {
config.Folders = append(config.Folders, Folder{
Path: "../modules/" + module,
Name: "module / " + module,
})
}
sort.Slice(config.Folders, func(i, j int) bool { return config.Folders[i].Name < config.Folders[j].Name })
config.Folders = append([]Folder{
{Path: "../", Name: "testcontainers-go"},
}, config.Folders...)
return &config
}
21 changes: 21 additions & 0 deletions modulegen/internal/vscode/writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package vscode

import (
"encoding/json"
"os"
"path/filepath"
)

func writeConfig(configFile string, config *Config) error {
err := os.MkdirAll(filepath.Dir(configFile), 0o755)
if err != nil {
return err
}
data, err := json.MarshalIndent(config, "", " ")
if err != nil {
return err
}
header := "// This file is autogenerated by the 'modulegen' tool.\n"
data = append([]byte(header), data...)
return os.WriteFile(configFile, data, 0o644)
}