Skip to content

Commit

Permalink
Merge branch 'master' into mckinstry/#8613-reduce-types-output
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbieMcKinstry committed Nov 9, 2022
2 parents 909c185 + d3422d5 commit 0b66bfb
Show file tree
Hide file tree
Showing 14 changed files with 296 additions and 105 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -32,13 +32,13 @@ For example, create three web servers:
```typescript
let aws = require("@pulumi/aws");
let sg = new aws.ec2.SecurityGroup("web-sg", {
ingress: [{ protocol: "tcp", fromPort: 80, toPort: 80, cidrBlocks: ["0.0.0.0/0"]}],
ingress: [{ protocol: "tcp", fromPort: 80, toPort: 80, cidrBlocks: ["0.0.0.0/0"] }],
});
for (let i = 0; i < 3; i++) {
new aws.ec2.Instance(`web-${i}`, {
ami: "ami-7172b611",
instanceType: "t2.micro",
securityGroups: [ sg.name ],
vpcSecurityGroupIds: [sg.id],
userData: `#!/bin/bash
echo "Hello, World!" > index.html
nohup python -m SimpleHTTPServer 80 &`,
Expand Down
17 changes: 4 additions & 13 deletions pkg/codegen/docs/gen.go
Expand Up @@ -43,7 +43,6 @@ import (
"github.com/pulumi/pulumi/pkg/v3/codegen/nodejs"
"github.com/pulumi/pulumi/pkg/v3/codegen/python"
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
)

//go:embed templates/*.tmpl
Expand Down Expand Up @@ -1690,14 +1689,6 @@ func (mod *modContext) getTypes(member interface{}, types nestedTypeUsageInfo) {
}
}

type fs map[string][]byte

func (fs fs) add(path string, contents []byte) {
_, has := fs[path]
contract.Assertf(!has, "duplicate file: %s", path)
fs[path] = contents
}

// getModuleFileName returns the file name to use for a module.
func (mod *modContext) getModuleFileName() string {
dctx := mod.docGenContext
Expand All @@ -1713,13 +1704,13 @@ func (mod *modContext) getModuleFileName() string {
return mod.mod
}

func (mod *modContext) gen(fs fs) error {
func (mod *modContext) gen(fs codegen.Fs) error {
dctx := mod.docGenContext
modName := mod.getModuleFileName()

addFile := func(name, contents string) {
p := path.Join(modName, name, "_index.md")
fs.add(p, []byte(contents))
fs.Add(p, []byte(contents))
}

// Resources
Expand Down Expand Up @@ -1758,7 +1749,7 @@ func (mod *modContext) gen(fs fs) error {
return err
}

fs.add(path.Join(modName, "_index.md"), buffer.Bytes())
fs.Add(path.Join(modName, "_index.md"), buffer.Bytes())
return nil
}

Expand Down Expand Up @@ -2090,7 +2081,7 @@ func (dctx *docGenContext) generatePackage(tool string, pkg *schema.Package) (ma
defer glog.Flush()

glog.V(3).Infoln("generating package docs now...")
files := fs{}
files := codegen.Fs{}
modules := []string{}
modMap := dctx.modules()
for k := range modMap {
Expand Down
30 changes: 11 additions & 19 deletions pkg/codegen/dotnet/gen.go
Expand Up @@ -1961,14 +1961,6 @@ func (mod *modContext) genConfig(variables []*schema.Property) (string, error) {
return w.String(), nil
}

type fs map[string][]byte

func (fs fs) add(path string, contents []byte) {
_, has := fs[path]
contract.Assertf(!has, "duplicate file: %s", path)
fs[path] = contents
}

func (mod *modContext) genUtilities() (string, error) {
// Strip any 'v' off of the version.
w := &bytes.Buffer{}
Expand All @@ -1986,7 +1978,7 @@ func (mod *modContext) genUtilities() (string, error) {
return w.String(), nil
}

func (mod *modContext) gen(fs fs) error {
func (mod *modContext) gen(fs codegen.Fs) error {
nsComponents := strings.Split(mod.namespaceName, ".")
if len(nsComponents) > 0 {
// Trim off "Pulumi.Pkg"
Expand All @@ -2012,15 +2004,15 @@ func (mod *modContext) gen(fs fs) error {
addFile := func(name, contents string) {
p := path.Join(dir, name)
files = append(files, p)
fs.add(p, []byte(contents))
fs.Add(p, []byte(contents))
}

// Ensure that the target module directory contains a README.md file.
readme := mod.pkg.Description
if readme != "" && readme[len(readme)-1] != '\n' {
readme += "\n"
}
fs.add(filepath.Join(dir, "README.md"), []byte(readme))
fs.Add(filepath.Join(dir, "README.md"), []byte(readme))

// Utilities, config
switch mod.mod {
Expand All @@ -2029,7 +2021,7 @@ func (mod *modContext) gen(fs fs) error {
if err != nil {
return err
}
fs.add("Utilities.cs", []byte(utilities))
fs.Add("Utilities.cs", []byte(utilities))
case "config":
if len(mod.pkg.Config) > 0 {
config, err := mod.genConfig(mod.pkg.Config)
Expand Down Expand Up @@ -2158,7 +2150,7 @@ func genPackageMetadata(pkg *schema.Package,
assemblyName string,
packageReferences map[string]string,
projectReferences []string,
files fs) error {
files codegen.Fs) error {

projectFile, err := genProjectFile(pkg, assemblyName, packageReferences, projectReferences)
if err != nil {
Expand All @@ -2177,7 +2169,7 @@ func genPackageMetadata(pkg *schema.Package,

lang, ok := pkg.Language["csharp"].(CSharpPackageInfo)
if pkg.Version != nil && ok && lang.RespectSchemaVersion {
files.add("version.txt", []byte(pkg.Version.String()))
files.Add("version.txt", []byte(pkg.Version.String()))
pulumiPlugin.Version = pkg.Version.String()
}

Expand All @@ -2186,9 +2178,9 @@ func genPackageMetadata(pkg *schema.Package,
return err
}

files.add(assemblyName+".csproj", projectFile)
files.add("logo.png", logo)
files.add("pulumi-plugin.json", plugin)
files.Add(assemblyName+".csproj", projectFile)
files.Add("logo.png", logo)
files.Add("pulumi-plugin.json", plugin)
return nil
}

Expand Down Expand Up @@ -2472,9 +2464,9 @@ func GeneratePackage(tool string, pkg *schema.Package, extraFiles map[string][]b
assemblyName := info.GetRootNamespace() + "." + namespaceName(info.Namespaces, pkg.Name)

// Generate each module.
files := fs{}
files := codegen.Fs{}
for p, f := range extraFiles {
files.add(p, f)
files.Add(p, f)

}
for _, mod := range modules {
Expand Down
9 changes: 3 additions & 6 deletions pkg/codegen/go/gen.go
Expand Up @@ -3560,7 +3560,7 @@ func GeneratePackage(tool string, pkg *schema.Package) (map[string][]byte, error
name := packageName(pkg)
pathPrefix := packageRoot(pkg)

files := map[string][]byte{}
files := codegen.Fs{}

// Generate pulumi-plugin.json
pulumiPlugin := &plugin.PulumiPluginJSON{
Expand All @@ -3575,13 +3575,10 @@ func GeneratePackage(tool string, pkg *schema.Package) (map[string][]byte, error
if err != nil {
return nil, fmt.Errorf("Failed to format pulumi-plugin.json: %w", err)
}
files[path.Join(pathPrefix, "pulumi-plugin.json")] = pulumiPluginJSON
files.Add(path.Join(pathPrefix, "pulumi-plugin.json"), pulumiPluginJSON)

setFile := func(relPath, contents string) {
relPath = path.Join(pathPrefix, relPath)
if _, ok := files[relPath]; ok {
panic(fmt.Errorf("duplicate file: %s", relPath))
}

// Run Go formatter on the code before saving to disk
formattedSource, err := format.Source([]byte(contents))
Expand All @@ -3590,7 +3587,7 @@ func GeneratePackage(tool string, pkg *schema.Package) (map[string][]byte, error
panic(fmt.Errorf("invalid Go source code:\n\n%s\n: %w", relPath, err))
}

files[relPath] = formattedSource
files.Add(relPath, formattedSource)
}

for _, mod := range pkgMods {
Expand Down
40 changes: 16 additions & 24 deletions pkg/codegen/nodejs/gen.go
Expand Up @@ -2024,14 +2024,6 @@ func (mod *modContext) genEnum(w io.Writer, enum *schema.EnumType) error {
return nil
}

type fs map[string][]byte

func (fs fs) add(path string, contents []byte) {
_, has := fs[path]
contract.Assertf(!has, "duplicate file: %s", path)
fs[path] = contents
}

func (mod *modContext) isReservedSourceFileName(name string) bool {
switch name {
case "index.ts":
Expand All @@ -2047,7 +2039,7 @@ func (mod *modContext) isReservedSourceFileName(name string) bool {
}
}

func (mod *modContext) gen(fs fs) error {
func (mod *modContext) gen(fs codegen.Fs) error {
var files []fileInfo
for _, path := range mod.extraSourceFiles {
files = append(files, fileInfo{
Expand All @@ -2064,7 +2056,7 @@ func (mod *modContext) gen(fs fs) error {
fileType: fileType,
pathToNodeModule: p,
})
fs.add(p, []byte(contents))
fs.Add(p, []byte(contents))
}

addResourceFile := func(resourceFileInfo resourceFileInfo, name, contents string) {
Expand All @@ -2074,7 +2066,7 @@ func (mod *modContext) gen(fs fs) error {
resourceFileInfo: resourceFileInfo,
pathToNodeModule: p,
})
fs.add(p, []byte(contents))
fs.Add(p, []byte(contents))
}

addFunctionFile := func(info functionFileInfo, name, contents string) {
Expand All @@ -2084,7 +2076,7 @@ func (mod *modContext) gen(fs fs) error {
functionFileInfo: info,
pathToNodeModule: p,
})
fs.add(p, []byte(contents))
fs.Add(p, []byte(contents))
}

// Utilities, config, readme
Expand All @@ -2093,7 +2085,7 @@ func (mod *modContext) gen(fs fs) error {
buffer := &bytes.Buffer{}
mod.genHeader(buffer, nil, nil, nil)
mod.genUtilitiesFile(buffer)
fs.add(path.Join(modDir, "utilities.ts"), buffer.Bytes())
fs.Add(path.Join(modDir, "utilities.ts"), buffer.Bytes())

// Ensure that the top-level (provider) module directory contains a README.md file.
readme := mod.pkg.Language["nodejs"].(NodePackageInfo).Readme
Expand All @@ -2112,7 +2104,7 @@ func (mod *modContext) gen(fs fs) error {
if readme != "" && readme[len(readme)-1] != '\n' {
readme += "\n"
}
fs.add(path.Join(modDir, "README.md"), []byte(readme))
fs.Add(path.Join(modDir, "README.md"), []byte(readme))
case "config":
if len(mod.pkg.Config) > 0 {
buffer := &bytes.Buffer{}
Expand Down Expand Up @@ -2186,7 +2178,7 @@ func (mod *modContext) gen(fs fs) error {
fileName = path.Join(modDir, "index.ts")
}
fileName = path.Join("types", "enums", fileName)
fs.add(fileName, buffer.Bytes())
fs.Add(fileName, buffer.Bytes())
}

// Nested types
Expand All @@ -2197,12 +2189,12 @@ func (mod *modContext) gen(fs fs) error {
return err
}
for _, file := range files {
fs.add(file.name(), file.contents())
fs.Add(file.name(), file.contents())
}
}

// Index
fs.add(path.Join(modDir, "index.ts"), []byte(mod.genIndex(files)))
fs.Add(path.Join(modDir, "index.ts"), []byte(mod.genIndex(files)))
return nil
}

Expand Down Expand Up @@ -2449,14 +2441,14 @@ func (mod *modContext) genEnums(buffer *bytes.Buffer, enums []*schema.EnumType)
}

// genPackageMetadata generates all the non-code metadata required by a Pulumi package.
func genPackageMetadata(pkg *schema.Package, info NodePackageInfo, files fs) error {
func genPackageMetadata(pkg *schema.Package, info NodePackageInfo, fs codegen.Fs) error {
// The generator already emitted Pulumi.yaml, so that leaves three more files to write out:
// 1) package.json: minimal NPM package metadata
// 2) tsconfig.json: instructions for TypeScript compilation
// 3) install-pulumi-plugin.js: plugin install script
files.add("package.json", []byte(genNPMPackageMetadata(pkg, info)))
files.add("tsconfig.json", []byte(genTypeScriptProjectFile(info, files)))
files.add("scripts/install-pulumi-plugin.js", []byte(genInstallScript(pkg.PluginDownloadURL)))
fs.Add("package.json", []byte(genNPMPackageMetadata(pkg, info)))
fs.Add("tsconfig.json", []byte(genTypeScriptProjectFile(info, fs)))
fs.Add("scripts/install-pulumi-plugin.js", []byte(genInstallScript(pkg.PluginDownloadURL)))
return nil
}

Expand Down Expand Up @@ -2577,7 +2569,7 @@ func genNPMPackageMetadata(pkg *schema.Package, info NodePackageInfo) string {
return string(npmjson) + "\n"
}

func genTypeScriptProjectFile(info NodePackageInfo, files fs) string {
func genTypeScriptProjectFile(info NodePackageInfo, files codegen.Fs) string {
w := &bytes.Buffer{}

fmt.Fprintf(w, `{
Expand Down Expand Up @@ -2845,9 +2837,9 @@ func GeneratePackage(tool string, pkg *schema.Package, extraFiles map[string][]b
}
pkg.Language["nodejs"] = info

files := fs{}
files := codegen.Fs{}
for p, f := range extraFiles {
files.add(p, f)
files.Add(p, f)
}
for _, mod := range modules {
if err := mod.gen(files); err != nil {
Expand Down

0 comments on commit 0b66bfb

Please sign in to comment.