From 59ab72c0661441a4823f4d43fa6d3cb952a61a5d Mon Sep 17 00:00:00 2001 From: Guinevere Saenger Date: Fri, 19 Jul 2024 16:12:38 -0700 Subject: [PATCH] Apply default docs edit rules to installation docs (#2211) Adds default doc edit rules plus a few more installation-doc specific edit rules to the plain docs parser functionality. Fixes #2208 --- pkg/tfgen/installation_docs.go | 39 +++++++++++++++++++++-- pkg/tfgen/installation_docs_test.go | 48 +++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 3 deletions(-) diff --git a/pkg/tfgen/installation_docs.go b/pkg/tfgen/installation_docs.go index e62ec794d..e9128c70f 100644 --- a/pkg/tfgen/installation_docs.go +++ b/pkg/tfgen/installation_docs.go @@ -23,11 +23,15 @@ func plainDocsParser(docFile *DocFile, g *Generator) ([]byte, error) { //TODO: See https://github.com/pulumi/pulumi-terraform-bridge/issues/2078 // - translate code blocks with code choosers - // - apply default edit rules // - reformat TF names - // - Translation for certain headers such as "Arguments Reference" or "Configuration block" // - Ability to omit irrelevant sections - return []byte(contentStr), nil + + // Apply edit rules to transform the doc for Pulumi-ready presentation + contentBytes, err := applyEditRules([]byte(contentStr), docFile) + if err != nil { + return nil, err + } + return contentBytes, nil } func writeFrontMatter(title string) string { @@ -91,3 +95,32 @@ func writeInstallationInstructions(goImportBasePath, providerName string) string goImportBasePath, ) } + +func applyEditRules(contentBytes []byte, docFile *DocFile) ([]byte, error) { + // Obtain default edit rules for documentation files + edits := defaultEditRules() + + // Additional edit rules for installation files + edits = append(edits, + // Replace all "T/terraform" with "P/pulumi" + reReplace(`Terraform`, `Pulumi`), + reReplace(`terraform`, `pulumi`), + // Replace all "H/hashicorp" strings + reReplace(`Hashicorp`, `Pulumi`), + reReplace(`hashicorp`, `pulumi`), + // Reformat certain headers + reReplace(`The following arguments are supported`, + `The following configuration inputs are supported`), + reReplace(`Argument Reference`, + `Configuration Reference`), + reReplace(`block contains the following arguments`, + `input has the following nested fields`)) + var err error + for _, rule := range edits { + contentBytes, err = rule.Edit(docFile.FileName, contentBytes) + if err != nil { + return nil, err + } + } + return contentBytes, nil +} diff --git a/pkg/tfgen/installation_docs_test.go b/pkg/tfgen/installation_docs_test.go index 05fc9fef7..c313db57c 100644 --- a/pkg/tfgen/installation_docs_test.go +++ b/pkg/tfgen/installation_docs_test.go @@ -136,3 +136,51 @@ func TestWriteIndexFrontMatter(t *testing.T) { require.Equal(t, tc.expected, actual) }) } + +func TestApplyEditRules(t *testing.T) { + t.Parallel() + + type testCase struct { + // The name of the test case. + name string + docFile DocFile + expected []byte + } + + tests := []testCase{ + { + name: "Replaces h/Hashicorp With p/Pulumi", + docFile: DocFile{ + Content: []byte("Any mention of Hashicorp or hashicorp will be Pulumi or pulumi"), + }, + expected: []byte("Any mention of Pulumi or pulumi will be Pulumi or pulumi"), + }, + { + name: "Replaces t/Terraform With p/Pulumi", + docFile: DocFile{ + Content: []byte("Any mention of Terraform or terraform will be Pulumi or pulumi"), + }, + expected: []byte("Any mention of Pulumi or pulumi will be Pulumi or pulumi"), + }, + { + name: "Replaces argument headers with input headers", + docFile: DocFile{ + Content: []byte("# Argument Reference\n" + + "The following arguments are supported:\n* `some_argument`\n" + + "block contains the following arguments"), + }, + expected: []byte("# Configuration Reference\n" + + "The following configuration inputs are supported:\n* `some_argument`\n" + + "input has the following nested fields"), + }, + } + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + actual, err := applyEditRules(tt.docFile.Content, &tt.docFile) + require.NoError(t, err) + require.Equal(t, string(tt.expected), string(actual)) + }) + } +}