Skip to content

Commit

Permalink
Move capitalization logic into getPackageDisplayName (#2625)
Browse files Browse the repository at this point in the history
This pull request clarifies the purpose and use of a package name for
display reasons.

Prior to this change, when generating front matter, a provider's display
name would be put through a capitalization logic that would capitalize
each word in the name, even if the word was in all caps already. E.g.:

`Palo Alto Networks Cloud NGFW For AWS Provider` -> `Palo Alto Networks
Cloud Ngfw For Aws Provider`

This change ensures that whatever is passed into `info.DisplayName` in
the provider remains unaltered. It also unblocks the setting up of
automatic index docs for https://github.com/pulumi/pulumi-cloudngfwaws.

I've also updated the function name to clarify it is specifically about
finding or generating a display name.
  • Loading branch information
guineveresaenger authored Nov 15, 2024
2 parents fde5671 + 9d051bd commit d4b1550
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
23 changes: 13 additions & 10 deletions pkg/tfgen/installation_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ func plainDocsParser(docFile *DocFile, g *Generator) ([]byte, error) {
// Get file content without front matter
content := trimFrontMatter(contentBytes)

providerName := getProviderName(g)
providerDisplayName := getProviderDisplayName(g)

// Add pulumi-specific front matter
// Generate pulumi-specific front matter
frontMatter := writeFrontMatter(providerName)
frontMatter := writeFrontMatter(providerDisplayName)

// Remove the title. A title gets populated from Hugo frontmatter; we do not want two.
content, err = removeTitle(content)
Expand All @@ -51,7 +51,7 @@ func plainDocsParser(docFile *DocFile, g *Generator) ([]byte, error) {
// Generate pulumi-specific installation instructions
installationInstructions := writeInstallationInstructions(
g.info.Golang.ImportBasePath,
providerName,
providerDisplayName,
g.pkg.Name().String(),
)

Expand Down Expand Up @@ -83,10 +83,7 @@ func plainDocsParser(docFile *DocFile, g *Generator) ([]byte, error) {
return []byte(contentStr), nil
}

func writeFrontMatter(providerName string) string {
// Capitalize the package name
capitalize := cases.Title(language.English)
title := capitalize.String(providerName)
func writeFrontMatter(providerDisplayName string) string {
return fmt.Sprintf(delimiter+
"# *** WARNING: This file was auto-generated. "+
"Do not edit by hand unless you're certain you know what you are doing! ***\n"+
Expand All @@ -95,7 +92,7 @@ func writeFrontMatter(providerName string) string {
"layout: package\n"+
delimiter+
"\n",
title)
providerDisplayName)
}

// writeInstallationInstructions renders the following for any provider:
Expand Down Expand Up @@ -437,10 +434,16 @@ func removeTfVersionMentions() tfbridge.DocsEdit {
}
}

func getProviderName(g *Generator) string {
func getProviderDisplayName(g *Generator) string {
providerName := g.info.DisplayName
if providerName != "" {
return providerName
}
return g.pkg.Name().String()
// If the provider hasn't set an explicit Display Name, we infer from the package name.
providerName = g.pkg.Name().String()
// For display purposes, we'll capitalize the name.
// This won't always work well - "aws" --> "Aws" isn't necessarily what we want
// but it's a reasonable fallback option when info.DisplayName isn't set.
capitalize := cases.Title(language.English)
return capitalize.String(providerName)
}
15 changes: 13 additions & 2 deletions pkg/tfgen/installation_docs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,17 @@ func TestDisplayNameFallback(t *testing.T) {
pkgName: "Horse",
expected: "Horse",
},
{
name: "Capitalizes pkgName if lower case",
pkgName: "shetlandpony",
expected: "Shetlandpony",
},
{
name: "Does not alter Display Name",
displayName: "Palo Alto Networks Cloud NGFW For AWS Provider",
pkgName: "cloudngfwaws",
expected: "Palo Alto Networks Cloud NGFW For AWS Provider",
},
}
for _, tt := range tests {
tt := tt
Expand All @@ -145,7 +156,7 @@ func TestDisplayNameFallback(t *testing.T) {
},
pkg: tokens.NewPackageToken(tokens.PackageName(tt.pkgName)),
}
actual := getProviderName(g)
actual := getProviderDisplayName(g)

assert.Equal(t, tt.expected, actual)
})
Expand Down Expand Up @@ -340,7 +351,7 @@ func TestWriteFrontMatter(t *testing.T) {

tc := testCase{
name: "Generates Front Matter for installation-configuration.md",
providerName: "test",
providerName: "Test",
expected: delimiter +
"# *** WARNING: This file was auto-generated. " +
"Do not edit by hand unless you're certain you know what you are doing! ***\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ layout: package
---
## Installation

The libvirt provider is available as a package in all Pulumi languages:
The Libvirt provider is available as a package in all Pulumi languages:

* JavaScript/TypeScript: [`@pulumi/libvirt`](https://www.npmjs.com/package/@pulumi/libvirt)
* Python: [`pulumi-libvirt`](https://pypi.org/project/pulumi-libvirt/)
Expand Down
2 changes: 1 addition & 1 deletion pkg/tfgen/test_data/convert-index-file/expected.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ layout: package
---
## Installation

The libvirt provider is available as a package in all Pulumi languages:
The Libvirt provider is available as a package in all Pulumi languages:

* JavaScript/TypeScript: [`@pulumi/libvirt`](https://www.npmjs.com/package/@pulumi/libvirt)
* Python: [`pulumi-libvirt`](https://pypi.org/project/pulumi-libvirt/)
Expand Down

0 comments on commit d4b1550

Please sign in to comment.