2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-12 14:17:57 +00:00
restic/vendor/github.com/Azure/azure-sdk-for-go/Gododir/gen.go

528 lines
13 KiB
Go
Raw Normal View History

2017-08-05 18:30:20 +00:00
package main
// Copyright 2017 Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2017-08-05 18:30:20 +00:00
// To run this package...
// go run gen.go -- --sdk 3.14.16
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"time"
2017-08-05 18:30:20 +00:00
do "gopkg.in/godo.v2"
)
type service struct {
2017-09-13 12:09:48 +00:00
Name string
Fullname string
Namespace string
TaskName string
Tag string
Input string
Output string
2017-08-05 18:30:20 +00:00
}
const (
testsSubDir = "tests"
)
type mapping struct {
2017-09-13 12:09:48 +00:00
PlaneInput string
PlaneOutput string
2017-08-05 18:30:20 +00:00
Services []service
}
type failList []string
type failLocker struct {
sync.Mutex
failList
}
2017-08-05 18:30:20 +00:00
var (
start time.Time
2017-08-05 18:30:20 +00:00
gopath = os.Getenv("GOPATH")
sdkVersion string
autorestDir string
swaggersDir string
testGen bool
deps do.P
2017-08-05 18:30:20 +00:00
services = []*service{}
servicesMapping = []mapping{
{
2017-09-13 12:09:48 +00:00
PlaneOutput: "arm",
PlaneInput: "resource-manager",
2017-08-05 18:30:20 +00:00
Services: []service{
2017-09-13 12:09:48 +00:00
{Name: "advisor"},
{Name: "analysisservices"},
2017-08-05 18:30:20 +00:00
// {
// Autorest Bug, duplicate files
// Name: "apimanagement",
2017-08-05 18:30:20 +00:00
// },
2017-09-13 12:09:48 +00:00
{Name: "appinsights"},
{Name: "authorization"},
{Name: "automation"},
// {
// Name: "commerce",
// Input: "azsadmin/resource-manager/commerce",
// Output: "azsadmin/commerce",
// },
// {
// Name: "fabric",
// Input: "azsadmin/resource-manager/fabric",
// Output: "azsadmin/fabric",
// },
// {
// Name: "infrastructureinsights",
// Input: "azsadmin/resource-manager/InfrastructureInsights",
// Output: "azsadmin/infrastructureinsights",
// },
2017-09-13 12:09:48 +00:00
{Name: "batch"},
{Name: "billing"},
{Name: "cdn"},
{Name: "cognitiveservices"},
2017-09-13 12:09:48 +00:00
{Name: "commerce"},
{Name: "compute"},
{
Name: "containerservice",
Input: "compute/resource-manager",
2017-09-13 12:09:48 +00:00
Tag: "package-container-service-2017-01",
},
{Name: "consumption"},
{Name: "containerinstance"},
{Name: "containerregistry"},
{Name: "cosmos-db"},
{Name: "customer-insights"},
{
Name: "account",
Input: "datalake-analytics/resource-manager",
2017-09-13 12:09:48 +00:00
Output: "datalake-analytics/account",
},
{
Name: "account",
Input: "datalake-store/resource-manager",
2017-09-13 12:09:48 +00:00
Output: "datalake-store/account",
},
{Name: "devtestlabs"},
{Name: "dns"},
{Name: "eventgrid"},
{Name: "eventhub"},
{Name: "hdinsight"},
{Name: "intune"},
{Name: "iothub"},
{Name: "keyvault"},
{Name: "logic"},
{
Name: "commitmentplans",
Input: "machinelearning/resource-manager",
Output: "machinelearning/commitmentplans",
2017-09-13 12:09:48 +00:00
Tag: "package-commitmentPlans-2016-05-preview",
},
{
Name: "webservices",
Input: "machinelearning/resource-manager",
2017-09-13 12:09:48 +00:00
Output: "machinelearning/webservices",
Tag: "package-webservices-2017-01",
},
{Name: "marketplaceordering"},
2017-09-13 12:09:48 +00:00
{Name: "mediaservices"},
{Name: "mobileengagement"},
{Name: "monitor"},
{Name: "mysql"},
{Name: "network"},
{Name: "notificationhubs"},
{Name: "operationalinsights"},
{Name: "operationsmanagement"},
2017-09-13 12:09:48 +00:00
{Name: "postgresql"},
{Name: "powerbiembedded"},
{Name: "recoveryservices"},
{Name: "recoveryservicesbackup"},
{Name: "recoveryservicessiterecovery"},
{
Name: "redis",
Tag: "package-2016-04",
},
2017-09-13 12:09:48 +00:00
{Name: "relay"},
{Name: "resourcehealth"},
{
Name: "features",
Input: "resources/resource-manager",
2017-09-13 12:09:48 +00:00
Output: "resources/features",
Tag: "package-features-2015-12",
},
{
Name: "links",
Input: "resources/resource-manager",
2017-09-13 12:09:48 +00:00
Output: "resources/links",
Tag: "package-links-2016-09",
},
{
Name: "locks",
Input: "resources/resource-manager",
2017-09-13 12:09:48 +00:00
Output: "resources/locks",
Tag: "package-locks-2016-09",
},
{
Name: "managedapplications",
Input: "resources/resource-manager",
2017-09-13 12:09:48 +00:00
Output: "resources/managedapplications",
Tag: "package-managedapplications-2016-09",
},
{
Name: "policy",
Input: "resources/resource-manager",
2017-09-13 12:09:48 +00:00
Output: "resources/policy",
Tag: "package-policy-2016-12",
},
{
Name: "resources",
Input: "resources/resource-manager",
2017-09-13 12:09:48 +00:00
Output: "resources/resources",
Tag: "package-resources-2017-05",
},
{
Name: "subscriptions",
Input: "resources/resource-manager",
2017-09-13 12:09:48 +00:00
Output: "resources/subscriptions",
Tag: "package-subscriptions-2016-06",
},
{Name: "scheduler"},
{Name: "search"},
{Name: "servermanagement"},
{Name: "service-map"},
{Name: "servicebus"},
{Name: "servicefabric"},
{Name: "sql"},
{Name: "storage"},
{Name: "storageimportexport"},
{Name: "storsimple8000series"},
{Name: "streamanalytics"},
2017-08-05 18:30:20 +00:00
// {
// error in the modeler
// https://github.com/Azure/autorest/issues/2579
// Name: "timeseriesinsights",
2017-08-05 18:30:20 +00:00
// },
2017-09-13 12:09:48 +00:00
{Name: "trafficmanager"},
{Name: "visualstudio"},
{Name: "web"},
},
},
{
PlaneOutput: "dataplane",
PlaneInput: "data-plane",
Services: []service{
{Name: "keyvault"},
2017-08-05 18:30:20 +00:00
{
Name: "face",
Input: "cognitiveservices/data-plane/Face",
Output: "cognitiveservices/face",
},
{
Name: "textanalytics",
Input: "cognitiveservices/data-plane/TextAnalytics",
Output: "cognitiveservices/textanalytics",
2017-08-05 18:30:20 +00:00
},
},
},
{
2017-09-13 12:09:48 +00:00
PlaneInput: "data-plane",
2017-08-05 18:30:20 +00:00
Services: []service{
{
2017-09-13 12:09:48 +00:00
Name: "filesystem",
Input: "datalake-store/data-plane",
2017-09-13 12:09:48 +00:00
Output: "datalake-store/filesystem",
2017-08-05 18:30:20 +00:00
},
},
},
{
2017-09-13 12:09:48 +00:00
PlaneOutput: "arm",
PlaneInput: "data-plane",
2017-08-05 18:30:20 +00:00
Services: []service{
{Name: "graphrbac"},
2017-08-05 18:30:20 +00:00
},
},
}
fails = failLocker{}
2017-08-05 18:30:20 +00:00
)
func init() {
start = time.Now()
2017-08-05 18:30:20 +00:00
for _, swaggerGroup := range servicesMapping {
swg := swaggerGroup
for _, service := range swg.Services {
s := service
2017-09-13 12:09:48 +00:00
initAndAddService(&s, swg.PlaneInput, swg.PlaneOutput)
2017-08-05 18:30:20 +00:00
}
}
}
func main() {
2017-08-05 18:30:20 +00:00
do.Godo(tasks)
}
2017-09-13 12:09:48 +00:00
func initAndAddService(service *service, planeInput, planeOutput string) {
if service.Input == "" {
service.Input = service.Name
2017-08-05 18:30:20 +00:00
}
path := []string{service.Input}
if service.Input == service.Name {
path = append(path, planeInput)
}
path = append(path, "readme.md")
service.Input = filepath.Join(path...)
2017-09-13 12:09:48 +00:00
if service.Output == "" {
service.Output = service.Name
2017-08-05 18:30:20 +00:00
}
2017-09-13 12:09:48 +00:00
service.TaskName = fmt.Sprintf("%s>%s", planeOutput, strings.Join(strings.Split(service.Output, "/"), ">"))
service.Fullname = filepath.Join(planeOutput, service.Output)
2017-08-05 18:30:20 +00:00
service.Namespace = filepath.Join("github.com", "Azure", "azure-sdk-for-go", service.Fullname)
service.Output = filepath.Join(gopath, "src", service.Namespace)
2017-09-13 12:09:48 +00:00
services = append(services, service)
deps = append(deps, service.TaskName)
2017-08-05 18:30:20 +00:00
}
func tasks(p *do.Project) {
p.Task("default", do.S{"setvars", "generate:all", "management", "report"}, nil)
2017-08-05 18:30:20 +00:00
p.Task("setvars", nil, setVars)
p.Use("generate", generateTasks)
p.Use("gofmt", formatTasks)
p.Use("gobuild", buildTasks)
p.Use("golint", lintTasks)
p.Use("govet", vetTasks)
p.Task("management", do.S{"setvars"}, managementVersion)
p.Task("addVersion", nil, addVersion)
p.Task("report", nil, report)
2017-08-05 18:30:20 +00:00
}
func setVars(c *do.Context) {
if gopath == "" {
panic("Gopath not set\n")
}
sdkVersion = c.Args.MustString("s", "sdk", "version")
autorestDir = c.Args.MayString("", "a", "ar", "autorest")
2017-09-13 12:09:48 +00:00
swaggersDir = c.Args.MayString("", "w", "sw", "swagger")
2017-08-05 18:30:20 +00:00
testGen = c.Args.MayBool(false, "t", "testgen")
}
func generateTasks(p *do.Project) {
addTasks(generate, p)
}
func generate(service *service) {
2017-09-13 12:09:48 +00:00
codegen := "--go"
2017-08-05 18:30:20 +00:00
if testGen {
2017-09-13 12:09:48 +00:00
codegen = "--go.testgen"
2017-08-05 18:30:20 +00:00
service.Fullname = strings.Join([]string{service.Fullname, testsSubDir}, string(os.PathSeparator))
service.Output = filepath.Join(service.Output, testsSubDir)
}
fmt.Printf("Generating %s...\n\n", service.Fullname)
2017-09-13 12:09:48 +00:00
fullInput := ""
if swaggersDir == "" {
fullInput = fmt.Sprintf("https://raw.githubusercontent.com/Azure/azure-rest-api-specs/current/specification/%s", service.Input)
} else {
fullInput = filepath.Join(swaggersDir, "azure-rest-api-specs", "specification", service.Input)
}
2017-08-05 18:30:20 +00:00
execCommand := "autorest"
commandArgs := []string{
2017-09-13 12:09:48 +00:00
fullInput,
codegen,
"--license-header=MICROSOFT_APACHE_NO_VERSION",
2017-09-13 12:09:48 +00:00
fmt.Sprintf("--namespace=%s", service.Name),
fmt.Sprintf("--output-folder=%s", service.Output),
fmt.Sprintf("--package-version=%s", sdkVersion),
"--clear-output-folder",
"--can-clear-output-folder",
}
if service.Tag != "" {
commandArgs = append(commandArgs, fmt.Sprintf("--tag=%s", service.Tag))
2017-08-05 18:30:20 +00:00
}
if testGen {
commandArgs = append([]string{"-LEGACY"}, commandArgs...)
}
if autorestDir != "" {
// if an AutoRest directory was specified then assume
// the caller wants to use a locally-built version.
2017-09-13 12:09:48 +00:00
commandArgs = append(commandArgs, fmt.Sprintf("--use=%s", autorestDir))
2017-08-05 18:30:20 +00:00
}
autorest := exec.Command(execCommand, commandArgs...)
2017-09-13 12:09:48 +00:00
fmt.Println(commandArgs)
2017-08-05 18:30:20 +00:00
if _, stderr, err := runner(autorest); err != nil {
fails.Add(fmt.Sprintf("%s: autorest error: %s: %s", service.Fullname, err, stderr))
2017-08-05 18:30:20 +00:00
}
format(service)
build(service)
lint(service)
vet(service)
}
func formatTasks(p *do.Project) {
addTasks(format, p)
}
func format(service *service) {
fmt.Printf("Formatting %s...\n\n", service.Fullname)
gofmt := exec.Command("gofmt", "-w", service.Output)
_, stderr, err := runner(gofmt)
2017-08-05 18:30:20 +00:00
if err != nil {
fails.Add(fmt.Sprintf("%s: gofmt error:%s: %s", service.Fullname, err, stderr))
2017-08-05 18:30:20 +00:00
}
}
func buildTasks(p *do.Project) {
addTasks(build, p)
}
func build(service *service) {
fmt.Printf("Building %s...\n\n", service.Fullname)
gobuild := exec.Command("go", "build", service.Namespace)
_, stderr, err := runner(gobuild)
2017-08-05 18:30:20 +00:00
if err != nil {
fails.Add(fmt.Sprintf("%s: build error: %s: %s", service.Fullname, err, stderr))
2017-08-05 18:30:20 +00:00
}
}
func lintTasks(p *do.Project) {
addTasks(lint, p)
}
func lint(service *service) {
fmt.Printf("Linting %s...\n\n", service.Fullname)
golint := exec.Command(filepath.Join(gopath, "bin", "golint"), service.Namespace)
_, stderr, err := runner(golint)
2017-08-05 18:30:20 +00:00
if err != nil {
fails.Add(fmt.Sprintf("%s: golint error: %s: %s", service.Fullname, err, stderr))
2017-08-05 18:30:20 +00:00
}
}
func vetTasks(p *do.Project) {
addTasks(vet, p)
}
func vet(service *service) {
fmt.Printf("Vetting %s...\n\n", service.Fullname)
govet := exec.Command("go", "vet", service.Namespace)
_, stderr, err := runner(govet)
2017-08-05 18:30:20 +00:00
if err != nil {
fails.Add(fmt.Sprintf("%s: go vet error: %s: %s", service.Fullname, err, stderr))
2017-08-05 18:30:20 +00:00
}
}
func addVersion(c *do.Context) {
gitStatus := exec.Command("git", "status", "-s")
out, _, err := runner(gitStatus)
2017-08-05 18:30:20 +00:00
if err != nil {
panic(fmt.Errorf("Git error: %s", err))
}
files := strings.Split(out, "\n")
for _, f := range files {
if strings.HasPrefix(f, " M ") && strings.HasSuffix(f, "version.go") {
gitAdd := exec.Command("git", "add", f[3:])
_, _, err := runner(gitAdd)
2017-08-05 18:30:20 +00:00
if err != nil {
panic(fmt.Errorf("Git error: %s", err))
}
}
}
}
func managementVersion(c *do.Context) {
version("management")
}
func version(packageName string) {
versionFile := filepath.Join(packageName, "version.go")
os.Remove(versionFile)
template := `// +build go1.7
2017-09-13 12:09:48 +00:00
package %s
2017-08-05 18:30:20 +00:00
// Copyright 2017 Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2017-08-05 18:30:20 +00:00
var (
sdkVersion = "%s"
)
`
data := []byte(fmt.Sprintf(template, packageName, sdkVersion))
ioutil.WriteFile(versionFile, data, 0644)
}
func addTasks(fn func(*service), p *do.Project) {
for _, service := range services {
s := service
p.Task(s.TaskName, nil, func(c *do.Context) {
fn(s)
})
}
p.Task("all", deps, nil)
}
func runner(cmd *exec.Cmd) (string, string, error) {
2017-08-05 18:30:20 +00:00
var stdout, stderr bytes.Buffer
cmd.Stdout, cmd.Stderr = &stdout, &stderr
err := cmd.Run()
if stdout.Len() > 0 {
fmt.Println(stdout.String())
}
if stderr.Len() > 0 {
fmt.Println(stderr.String())
}
return stdout.String(), stderr.String(), err
}
func (fl *failLocker) Add(fail string) {
fl.Lock()
defer fl.Unlock()
fl.failList = append(fl.failList, fail)
}
func report(c *do.Context) {
fmt.Printf("Script ran for %s\n", time.Since(start))
for _, f := range fails.failList {
fmt.Println(f)
fmt.Println("==========")
}
2017-08-05 18:30:20 +00:00
}