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

Add Example documentation for Image #479

Merged
merged 13 commits into from
Feb 3, 2023
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ cleanup:
rm -f provider/cmd/$(PROVIDER)/schema.go

examples:
guineveresaenger marked this conversation as resolved.
Show resolved Hide resolved
cd provider/pkg/gen/examples/ && go run generate.go ./yaml ./
cd provider/pkg/docs-gen/examples/ && go run generate.go ./yaml ./

help:
@grep '^[^.#]\+:\s\+.*#' Makefile | \
Expand All @@ -105,7 +105,7 @@ provider: tfgen install_plugins
test:
cd examples && go test -v -tags=all -parallel $(TESTPARALLELISM) -timeout 2h

tfgen: install_plugins
tfgen: examples install_plugins
(cd provider && go build -p 1 -o $(WORKING_DIR)/bin/$(TFGEN) -ldflags "-X $(PROJECT)/$(VERSION_PATH)=$(VERSION)" $(PROJECT)/$(PROVIDER_PATH)/cmd/$(TFGEN))
$(WORKING_DIR)/bin/$(TFGEN) schema --out provider/cmd/$(PROVIDER)
(cd provider && VERSION=$(VERSION) go generate cmd/$(PROVIDER)/main.go)
Expand Down
108 changes: 33 additions & 75 deletions provider/pkg/docs-gen/examples/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,32 @@ func markdownExample(description string,
description, typescript, python, csharp, golang, yaml, java)
}

func convert(language, tempDir, programFile string) (string, error) {
exampleDir := filepath.Join(tempDir, "example"+language)
cmd := exec.Command(
"pulumi",
"convert",
"--language",
language,
"--out",
filepath.Join(tempDir, exampleDir),
"--generate-only",
)

cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Dir = tempDir
if err := cmd.Run(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "convert %s failed, ignoring: %+v", language, err)
}
content, err := os.ReadFile(filepath.Join(tempDir, exampleDir, programFile))
if err != nil {
return "", err
}

return string(content), nil
}

func processYaml(path string, mdDir string) error {
yamlFile, err := os.Open(path)
if err != nil {
Expand Down Expand Up @@ -119,85 +145,17 @@ func processYaml(path string, mdDir string) error {
}
contract.AssertNoError(src.Close())

cmd := exec.Command("pulumi", "convert", "--language", "typescript", "--out",
filepath.Join(dir, "example-nodejs"), "--generate-only")
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
fmt.Println(dir)
cmd.Dir = dir
if err = cmd.Run(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "convert nodejs failed, ignoring: %+v", err)
}
content, err := os.ReadFile(filepath.Join(dir, "example-nodejs", "index.ts"))
if err != nil {
return err
}
typescript := string(content)

cmd = exec.Command("pulumi", "convert", "--language", "python", "--out",
filepath.Join(dir, "example-py"), "--generate-only")
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Dir = dir
if err := cmd.Run(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "convert python failed, ignoring: %+v", err)
}
content, err = os.ReadFile(filepath.Join(dir, "example-py", "__main__.py"))
if err != nil {
return err
}
python := string(content)

cmd = exec.Command("pulumi", "convert", "--language", "csharp", "--out",
filepath.Join(dir, "example-dotnet"), "--generate-only")
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Dir = dir
if err = cmd.Run(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "convert go failed, ignoring: %+v", err)
}
content, err = os.ReadFile(filepath.Join(dir, "example-dotnet", "Program.cs"))
if err != nil {
return err
}
csharp := string(content)

cmd = exec.Command("pulumi", "convert", "--language", "go", "--out",
filepath.Join(dir, "example-go"), "--generate-only")
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Dir = dir
if err = cmd.Run(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "convert go failed, ignoring: %+v", err)
}
content, err = os.ReadFile(filepath.Join(dir, "example-go", "main.go"))
if err != nil {
return err
}
golang := string(content)

// TODO add java when convert supports it.
cmd = exec.Command("pulumi", "convert", "--language", "java", "--out",
filepath.Join(dir, "example-java"), "--generate-only")
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Dir = dir
fmt.Println(dir)
if err = cmd.Run(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "convert java failed, ignoring: %+v", err)
}
content, err = os.ReadFile(filepath.Join(dir, "example-java", "src/main/java/generated_program/App.java"))

if err != nil {
return fmt.Errorf("java err %s", err)
}
java := string(content)
typescript, err := convert("typescript", dir, "index.ts")
guineveresaenger marked this conversation as resolved.
Show resolved Hide resolved
python, err := convert("python", dir, "__main__.py")
csharp, err := convert("csharp", dir, "Program.cs")
golang, err := convert("go", dir, "main.go")
java, err := convert("java", dir, "src/main/java/generated_program/App.java")

content, err = os.ReadFile(filepath.Join(dir, "Pulumi.yaml"))
yamlContent, err := os.ReadFile(filepath.Join(dir, "Pulumi.yaml"))
if err != nil {
return err
}
yaml := string(content)
yaml := string(yamlContent)

exampleStrings = append(exampleStrings, markdownExample(description, typescript, python, csharp, golang, yaml, java))
}
Expand Down