File: transforms.go

package info (click to toggle)
golang-github-azure-azure-sdk-for-go 68.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 556,256 kB
  • sloc: javascript: 196; sh: 96; makefile: 7
file content (240 lines) | stat: -rw-r--r-- 7,385 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// +build go1.9

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

// Package model holds the business logic for the operations made available by
// profileBuilder.
//
// This package is not governed by the SemVer associated with the rest of the
// Azure-SDK-for-Go.
package model

import (
	"bytes"
	"fmt"
	"go/ast"
	"go/parser"
	"go/printer"
	"go/token"
	"log"
	"os"
	"os/exec"
	"path"
	"path/filepath"
	"strings"
	"sync"

	"github.com/Azure/azure-sdk-for-go/eng/tools/internal/modinfo"

	"golang.org/x/tools/imports"
)

// ListDefinition represents a JSON file that contains a list of packages to include
type ListDefinition struct {
	Include      []string          `json:"include"`
	PathOverride map[string]string `json:"pathOverride"`
	IgnoredPaths []string          `json:"ignoredPaths"`
}

const (
	armPathModifier = "mgmt"
	aliasFileName   = "models.go"
)

// BuildProfile takes a list of packages and creates a profile
func BuildProfile(packageList ListDefinition, name, outputLocation string, outputLog, errLog *log.Logger, recursive, modules bool, semLimit int) {
	sem := make(chan struct{}, semLimit)
	wg := &sync.WaitGroup{}
	wg.Add(len(packageList.Include))
	for _, pkgDir := range packageList.Include {
		if !filepath.IsAbs(pkgDir) {
			abs, err := filepath.Abs(pkgDir)
			if err != nil {
				errLog.Fatalf("failed to convert to absolute path: %v", err)
			}
			pkgDir = abs
		}
		go func(pd string) {
			filepath.Walk(pd, func(path string, info os.FileInfo, err error) error {
				if !info.IsDir() {
					return nil
				}
				fs := token.NewFileSet()
				sem <- struct{}{}
				packages, err := parser.ParseDir(fs, path, func(f os.FileInfo) bool {
					// exclude test files
					return !strings.HasSuffix(f.Name(), "_test.go")
				}, 0)
				<-sem
				if err != nil {
					errLog.Fatalf("failed to parse '%s': %v", path, err)
				}
				if len(packages) < 1 {
					errLog.Fatalf("didn't find any packages in '%s'", path)
				}
				if len(packages) > 1 {
					errLog.Fatalf("found more than one package in '%s'", path)
				}
				for pn := range packages {
					p := packages[pn]
					// trim any non-exported nodes
					if exp := ast.PackageExports(p); !exp {
						errLog.Fatalf("package '%s' doesn't contain any exports", pn)
					}
					// construct the import path from the outputLocation
					// e.g. D:\work\src\github.com\Azure\azure-sdk-for-go\profiles\2017-03-09\compute\mgmt\compute
					// becomes github.com/Azure/azure-sdk-for-go/profiles/2017-03-09/compute/mgmt/compute
					i := strings.Index(path, "github.com")
					if i == -1 {
						errLog.Fatalf("didn't find 'github.com' in '%s'", path)
					}
					importPath := strings.Replace(path[i:], "\\", "/", -1)
					ap, err := NewAliasPackage(p, importPath)
					if err != nil {
						errLog.Fatalf("failed to create alias package: %v", err)
					}
					updateAliasPackageUserAgent(ap, name)
					// build the profile output directory, if there's an override path use that
					var aliasPath string
					var ok bool
					if aliasPath, ok = packageList.PathOverride[importPath]; !ok {
						var err error
						if modules && modinfo.HasVersionSuffix(path) {
							// strip off the major version dir so it's not included in the alias path
							path = filepath.Dir(path)
						}
						aliasPath, err = getAliasPath(path)
						if err != nil {
							errLog.Fatalf("failed to calculate alias directory: %v", err)
						}
					}
					aliasPath = filepath.Join(outputLocation, aliasPath)
					if _, err := os.Stat(aliasPath); os.IsNotExist(err) {
						err = os.MkdirAll(aliasPath, os.ModeDir|0755)
						if err != nil {
							errLog.Fatalf("failed to create alias directory: %v", err)
						}
					}
					writeAliasPackage(ap, aliasPath, outputLog, errLog)
				}
				if !recursive {
					return filepath.SkipDir
				}
				return nil
			})
			wg.Done()
		}(pkgDir)
	}
	wg.Wait()
	close(sem)
	outputLog.Print(len(packageList.Include), " packages generated.")
}

// getAliasPath takes an existing API Version path and converts the path to a path which uses the new profile layout.
func getAliasPath(packageDir string) (string, error) {
	// we want to transform this:
	//  .../services/compute/mgmt/2016-03-30/compute
	// into this:
	//  compute/mgmt/compute
	// i.e. remove everything to the left of /services along with the API version
	pi, err := DeconstructPath(packageDir)
	if err != nil {
		return "", err
	}

	output := []string{
		pi.Provider,
	}

	if pi.IsArm {
		output = append(output, armPathModifier)
	}
	output = append(output, pi.Group)
	if pi.APIPkg != "" {
		output = append(output, pi.APIPkg)
	}

	return filepath.Join(output...), nil
}

// updateAliasPackageUserAgent updates the "UserAgent" function in the generated profile, if it is present.
func updateAliasPackageUserAgent(ap *AliasPackage, profileName string) {
	var userAgent *ast.FuncDecl
	for _, decl := range ap.Files[aliasFileName].Decls {
		if fd, ok := decl.(*ast.FuncDecl); ok && fd.Name.Name == "UserAgent" {
			userAgent = fd
			break
		}
	}
	if userAgent == nil {
		return
	}

	// Grab the expression being returned.
	retResults := &userAgent.Body.List[0].(*ast.ReturnStmt).Results[0]

	// Append a string literal to the result
	updated := &ast.BinaryExpr{
		Op: token.ADD,
		X:  *retResults,
		Y: &ast.BasicLit{
			Value: fmt.Sprintf(`" profiles/%s"`, profileName),
		},
	}
	*retResults = updated
}

// writeAliasPackage adds the MSFT Copyright Header, then writes the alias package to disk.
func writeAliasPackage(ap *AliasPackage, outputPath string, outputLog, errLog *log.Logger) {
	files := token.NewFileSet()

	err := os.MkdirAll(path.Dir(outputPath), 0755|os.ModeDir)
	if err != nil {
		errLog.Fatalf("error creating directory: %v", err)
	}

	aliasFile := filepath.Join(outputPath, aliasFileName)
	outputFile, err := os.Create(aliasFile)
	if err != nil {
		errLog.Fatalf("error creating file: %v", err)
	}

	// TODO: This should really be added by the `goalias` package itself. Doing it here is a work around
	fmt.Fprintln(outputFile, "// +build go1.9")
	fmt.Fprintln(outputFile)

	generatorStampBuilder := new(bytes.Buffer)

	fmt.Fprintln(generatorStampBuilder, `// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.`)

	fmt.Fprintln(outputFile, generatorStampBuilder.String())

	generatorStampBuilder.Reset()

	fmt.Fprintln(generatorStampBuilder, "// This code was auto-generated by:")
	fmt.Fprintln(generatorStampBuilder, "// github.com/Azure/azure-sdk-for-go/eng/tools/profileBuilder")

	fmt.Fprintln(generatorStampBuilder)
	fmt.Fprint(outputFile, generatorStampBuilder.String())

	outputLog.Printf("Writing File: %s", aliasFile)

	file := ap.ModelFile()

	var b bytes.Buffer
	printer.Fprint(&b, files, file)
	res, err := imports.Process(aliasFile, b.Bytes(), nil)
	if err != nil {
		errLog.Fatalf("failed to process imports: %v", err)
	}
	fmt.Fprintf(outputFile, "%s", res)
	outputFile.Close()

	// be sure to specify the file for formatting not the directory; this is to
	// avoid race conditions when formatting parent/child directories (foo and foo/fooapi)
	if err := exec.Command("gofmt", "-w", aliasFile).Run(); err != nil {
		errLog.Fatalf("error formatting profile '%s': %v", aliasFile, err)
	}
}