Skip to content

Commit

Permalink
[tools/generator] add the template command (Azure#15206)
Browse files Browse the repository at this point in the history
* adds the template command

* fix package name of readDir

* add a new comment

* resolve linter issues

* add autorest.md template

* update the template accordingly
  • Loading branch information
ArcturusZhang authored and vindicatesociety committed Sep 18, 2021
1 parent 3a682e7 commit 3e86b48
Show file tree
Hide file tree
Showing 17 changed files with 323 additions and 22 deletions.
2 changes: 0 additions & 2 deletions tools/generator/autorest/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ type ChangelogContext interface {
// ChangelogProcessor processes the metadata and output changelog with the desired format
type ChangelogProcessor struct {
ctx ChangelogContext
metadataLocation string
readme string
}

// NewChangelogProcessorFromContext returns a new ChangelogProcessor
Expand Down
8 changes: 2 additions & 6 deletions tools/generator/autorest/generation.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func validate(readme string, metadataMap map[string]model.Metadata, validators [
for tag, metadata := range metadataMap {
errors := ValidateMetadata(validators, tag, metadata)
if len(errors) != 0 {
builder.addMultiple(errors)
builder.add(errors...)
}
}

Expand All @@ -145,14 +145,10 @@ type validationErrorBuilder struct {
errors []error
}

func (b *validationErrorBuilder) addMultiple(errors []error) {
func (b *validationErrorBuilder) add(errors ...error) {
b.errors = append(b.errors, errors...)
}

func (b *validationErrorBuilder) add(err error) {
b.errors = append(b.errors, err)
}

func (b *validationErrorBuilder) build() error {
if len(b.errors) == 0 {
return nil
Expand Down
2 changes: 1 addition & 1 deletion tools/generator/autorest/model/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func getNewContents(c *delta.Content) []string {
returns := ""
if v.Returns != nil {
returns = *v.Returns
if strings.Index(returns, ",") > -1 {
if strings.Contains(returns, ",") {
returns = fmt.Sprintf("(%s)", returns)
}
}
Expand Down
7 changes: 0 additions & 7 deletions tools/generator/cmd/automation/automationCmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,6 @@ func execute(inputPath, outputPath string, flags Flags) error {
return nil
}

func tempDir() string {
if dir := os.Getenv("TMP_DIR"); dir != "" {
return dir
}
return os.TempDir()
}

type automationContext struct {
sdkRoot string
specRoot string
Expand Down
2 changes: 1 addition & 1 deletion tools/generator/cmd/automation/pipeline/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

// GenerateInput ...
type GenerateInput struct {
DryRun bool `json:"dryRun,omitempty,omitempty"`
DryRun bool `json:"dryRun,omitempty"`
SpecFolder string `json:"specFolder,omitempty"`
HeadSha string `json:"headSha,omitempty"`
HeadRef string `json:"headRef,omitempty"`
Expand Down
4 changes: 0 additions & 4 deletions tools/generator/cmd/issue/issueCmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,6 @@ func isGoReleaseRequest(issue *github.Issue) bool {
return issueHasLabel(issue, "Go")
}

func isTrack2ReleaseRequest(issue *github.Issue) bool {
return issueHasLabel(issue, "Track2")
}

func (c *commandContext) parseIssues(issues []*github.Issue) ([]request.Request, error) {
var requests []request.Request
var errResult error
Expand Down
2 changes: 1 addition & 1 deletion tools/generator/cmd/issue/link/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Readme string

// IsMgmt returns true when the readme belongs to a mgmt plane package
func (r Readme) IsMgmt() bool {
return strings.Index(string(r), mgmtSegment) >= 0
return strings.Contains(string(r), mgmtSegment)
}

// GetReadmePathFromChangedFiles ...
Expand Down
2 changes: 2 additions & 0 deletions tools/generator/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/Azure/azure-sdk-for-go/tools/generator/cmd/automation"
"github.com/Azure/azure-sdk-for-go/tools/generator/cmd/issue"
"github.com/Azure/azure-sdk-for-go/tools/generator/cmd/template"
"github.com/spf13/cobra"
)

Expand All @@ -29,6 +30,7 @@ func Command() *cobra.Command {
rootCmd.AddCommand(
automation.Command(),
issue.Command(),
template.Command(),
)

return rootCmd
Expand Down
169 changes: 169 additions & 0 deletions tools/generator/cmd/template/templateCmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

package template

import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"

"github.com/Azure/azure-sdk-for-go/tools/generator/flags"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

// Command returns the template command
func Command() *cobra.Command {
templateCmd := &cobra.Command{
Use: "template (<rpName> <packageName>) | <packagePath>",
Short: "Onboards new RP with the template",
Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) error {
var rpName, packageName string
if len(args) == 1 {
segments := strings.Split(args[0], "/")
if len(segments) != 2 {
return fmt.Errorf("%s is not a valid package path. Please assign the package path using `rpName/packageName` format", args[0])
}
rpName = segments[0]
packageName = segments[1]
} else {
rpName = args[0]
packageName = args[1]
}

return GeneratePackageByTemplate(rpName, packageName, ParseFlags(cmd.Flags()))
},
}

BindFlags(templateCmd.Flags())
if err := templateCmd.MarkFlagRequired("package-title"); err != nil {
log.Fatal(err)
}
if err := templateCmd.MarkFlagRequired("commit"); err != nil {
log.Fatal(err)
}

return templateCmd
}

// BindFlags binds the flags to this command
func BindFlags(flagSet *pflag.FlagSet) {
flagSet.String("go-sdk-folder", ".", "Specifies the path of root of azure-sdk-for-go")
flagSet.String("template-path", "tools/generator/template/rpName/packageName", "Specifies the path of the template")
flagSet.String("package-title", "", "Specifies the title of this package")
flagSet.String("commit", "", "Specifies the commit hash of azure-rest-api-specs")
}

// ParseFlags parses the flags to a Flags struct
func ParseFlags(flagSet *pflag.FlagSet) Flags {
return Flags{
SDKRoot: flags.GetString(flagSet, "go-sdk-folder"),
TemplatePath: flags.GetString(flagSet, "template-path"),
PackageTitle: flags.GetString(flagSet, "package-title"),
Commit: flags.GetString(flagSet, "commit"),
}
}

// Flags ...
type Flags struct {
SDKRoot string
TemplatePath string
PackageTitle string
Commit string
}

// GeneratePackageByTemplate creates a new set of files based on the things in template directory
func GeneratePackageByTemplate(rpName, packageName string, flags Flags) error {
root, err := filepath.Abs(flags.SDKRoot)
if err != nil {
return fmt.Errorf("cannot get the root of azure-sdk-for-go from '%s': %+v", flags.SDKRoot, err)
}
var absTemplateDir string
if filepath.IsAbs(flags.TemplatePath) {
absTemplateDir = flags.TemplatePath
} else {
absTemplateDir = filepath.Join(root, flags.TemplatePath)
}
fileList, err := ioutil.ReadDir(absTemplateDir)
if err != nil {
return fmt.Errorf("cannot read the directory '%s': %+v", absTemplateDir, err)
}

// build the replaceMap
buildReplaceMap(rpName, packageName, flags.PackageTitle, flags.Commit)

// copy everything to destination directory
for _, file := range fileList {
path := filepath.Join(absTemplateDir, file.Name())
content, err := readAndReplace(path)
if err != nil {
return err
}

dirPath := filepath.Join(root, "sdk", rpName, packageName)
if err := os.MkdirAll(dirPath, os.ModePerm); err != nil {
return fmt.Errorf("cannot create directory '%s': %+v", dirPath, err)
}

newFilePath := filepath.Join(dirPath, strings.TrimSuffix(file.Name(), FilenameSuffix))
if err := createNewFile(newFilePath, content); err != nil {
return err
}
}

return nil
}

func buildReplaceMap(rpName, packageName, packageTitle, commitID string) {
replaceMap = make(map[string]string)

replaceMap[RPNameKey] = rpName
replaceMap[PackageNameKey] = packageName
replaceMap[PackageTitleKey] = packageTitle
replaceMap[CommitIDKey] = commitID
}

func readAndReplace(path string) (string, error) {
b, err := ioutil.ReadFile(path)
if err != nil {
return "", fmt.Errorf("cannot read from file '%s': %+v", path, err)
}

content := string(b)
for k, v := range replaceMap {
content = strings.ReplaceAll(content, k, v)
}

return content, nil
}

func createNewFile(path, content string) error {
file, err := os.Create(path)
if err != nil {
return fmt.Errorf("cannot create file '%s': %+v", path, err)
}
defer file.Close()

if _, err := file.WriteString(content); err != nil {
return fmt.Errorf("cannot write to file '%s': %+v", path, err)
}

return nil
}

var (
replaceMap map[string]string
)

const (
RPNameKey = "{{rpName}}"
PackageNameKey = "{{packageName}}"
PackageTitleKey = "{{PackageTitle}}"
CommitIDKey = "{{commitID}}"
FilenameSuffix = ".tpl"
)
3 changes: 3 additions & 0 deletions tools/generator/template/rpName/packageName/CHANGELOG.md.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Release History

## v0.1.0 (released)
21 changes: 21 additions & 0 deletions tools/generator/template/rpName/packageName/LICENSE.txt.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2021 Microsoft

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
76 changes: 76 additions & 0 deletions tools/generator/template/rpName/packageName/README.md.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Azure {{PackageTitle}} Module for Go

[![PkgGoDev](https://pkg.go.dev/badge/github.com/Azure/azure-sdk-for-go/sdk/{{rpName}}/{{packageName}})](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/{{rpName}}/{{packageName}})

The `{{packageName}}` module provides operations for working with Azure {{PackageTitle}}.

[Source code](https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/{{rpName}}/{{packageName}})

# Getting started

## Prerequisites

- an [Azure subscription](https://azure.microsoft.com/free/)
- Go 1.13 or above

## Install the package

This project uses [Go modules](https://github.com/golang/go/wiki/Modules) for versioning and dependency management.

Install the Azure AgFood module:

```sh
go get github.com/Azure/azure-sdk-for-go/sdk/{{rpName}}/{{packageName}}
```

## Authorization

When creating a client, you will need to provide a credential for authenticating with Azure {{PackageTitle}}. The `azidentity` module provides facilities for various ways of authenticating with Azure including client/secret, certificate, managed identity, and more.

```go
cred, err := azidentity.NewDefaultAzureCredential(nil)
```

For more information on authentication, please see the documentation for `azidentity` at [pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity).

## Connecting to Azure {{PackageTitle}}

Once you have a credential, create a connection to the desired ARM endpoint. The `armcore` module provides facilities for connecting with ARM endpoints including public and sovereign clouds as well as Azure Stack.

```go
con := armcore.NewDefaultConnection(cred, nil)
```

For more information on ARM connections, please see the documentation for `armcore` at [pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/armcore](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/armcore).

## Clients

Azure {{PackageTitle}} modules consist of one or more clients. A client groups a set of related APIs, providing access to its functionality within the specified subscription. Create one or more clients to access the APIs you require using your `armcore.Connection`.

```go
client := {{packageName}}.{{NewClientMethod}}(con, "<subscription ID>")
```

## Provide Feedback

If you encounter bugs or have suggestions, please
[open an issue](https://github.com/Azure/azure-sdk-for-go/issues) and assign the `{{PackageTitle}}` label.

# Contributing

This project welcomes contributions and suggestions. Most contributions require
you to agree to a Contributor License Agreement (CLA) declaring that you have
the right to, and actually do, grant us the rights to use your contribution.
For details, visit [https://cla.microsoft.com](https://cla.microsoft.com).

When you submit a pull request, a CLA-bot will automatically determine whether
you need to provide a CLA and decorate the PR appropriately (e.g., label,
comment). Simply follow the instructions provided by the bot. You will only
need to do this once across all repos using our CLA.

This project has adopted the
[Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
For more information, see the
[Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/)
or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any
additional questions or comments.
10 changes: 10 additions & 0 deletions tools/generator/template/rpName/packageName/autorest.md.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
### AutoRest Configuration

> see https://aka.ms/autorest

``` yaml
require:
- https://github.com/Azure/azure-rest-api-specs/blob/{{commitID}}/specification/{{rpName}}/resource-manager/readme.md
- https://github.com/Azure/azure-rest-api-specs/blob/{{commitID}}/specification/{{rpName}}/resource-manager/readme.go.md
module-version: 0.1.0
```
7 changes: 7 additions & 0 deletions tools/generator/template/rpName/packageName/build.go.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

// This file enables 'go generate' to regenerate this specific SDK
//go:generate pwsh.exe ../../../eng/scripts/build.ps1 -skipBuild -format -tidy -generate {{packageName}}

package {{packageName}}
Loading

0 comments on commit 3e86b48

Please sign in to comment.