Skip to content

Commit ef328c6

Browse files
authored
Optimize go generate speed (#161)
1 parent d771a46 commit ef328c6

File tree

3 files changed

+29
-20
lines changed

3 files changed

+29
-20
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,13 @@ Additional documentation, including available resources and their arguments/attr
4343

4444
If you wish to work on the provider, you'll first need [Go](http://www.golang.org) installed on your machine (see [Requirements](#requirements) above).
4545

46-
To compile the provider, run `go install`. This will build the provider and put the provider binary in the `$GOPATH/bin` directory.
46+
If any file in `gen` directory has been modified, run all the generators:
4747

48-
To generate or update documentation, run `go generate`.
48+
```shell
49+
go generate -x
50+
```
51+
52+
To compile the provider, run `go install`. This will build the provider and put the provider binary in the `$GOPATH/bin` directory.
4953

5054
In order to run the full suite of Acceptance tests, run `make testacc`. Make sure the respective environment variables are set (e.g., `IOSXE_USERNAME`, `IOSXE_PASSWORD`, `IOSXE_URL`).
5155

gen/generator.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"bufio"
2424
"bytes"
2525
"fmt"
26-
"io/ioutil"
2726
"log"
2827
"math"
2928
"os"
@@ -438,7 +437,7 @@ func parseAttribute(e *yang.Entry, attr *YamlConfigAttribute) {
438437
}
439438
}
440439

441-
func augmentConfig(config *YamlConfig, modelPaths []string) {
440+
func augmentConfig(config *YamlConfig, yangModules *yang.Modules) {
442441
path := ""
443442
if config.AugmentPath != "" {
444443
path = config.AugmentPath
@@ -447,7 +446,7 @@ func augmentConfig(config *YamlConfig, modelPaths []string) {
447446
}
448447

449448
module := strings.Split(path, ":")[0]
450-
e, errors := yang.GetModule(module, modelPaths...)
449+
e, errors := yangModules.GetModule(module)
451450
if len(errors) > 0 {
452451
fmt.Printf("YANG parser error(s): %+v\n\n", errors)
453452
return
@@ -530,12 +529,12 @@ func renderTemplate(templatePath, outputPath string, config interface{}) {
530529
}
531530

532531
func main() {
533-
items, _ := ioutil.ReadDir(definitionsPath)
532+
items, _ := os.ReadDir(definitionsPath)
534533
configs := make([]YamlConfig, len(items))
535534

536535
// Load configs
537536
for i, filename := range items {
538-
yamlFile, err := ioutil.ReadFile(filepath.Join(definitionsPath, filename.Name()))
537+
yamlFile, err := os.ReadFile(filepath.Join(definitionsPath, filename.Name()))
539538
if err != nil {
540539
log.Fatalf("Error reading file: %v", err)
541540
}
@@ -548,22 +547,30 @@ func main() {
548547
configs[i] = config
549548
}
550549

551-
items, _ = ioutil.ReadDir(modelsPath)
552-
modelPaths := make([]string, 0)
550+
items, _ = os.ReadDir(modelsPath)
551+
552+
yangModules := yang.NewModules()
553553

554554
// Iterate over yang models
555555
for _, item := range items {
556-
if filepath.Ext(item.Name()) == ".yang" {
557-
modelPaths = append(modelPaths, filepath.Join(modelsPath, item.Name()))
556+
if filepath.Ext(item.Name()) != ".yang" {
557+
continue
558+
}
559+
560+
fn := filepath.Join(modelsPath, item.Name())
561+
if err := yangModules.Read(fn); err != nil {
562+
log.Fatalf("yang parser: %v", err)
558563
}
559564
}
560565

561566
for i := range configs {
562567
// Augment config by yang models
563568
if !configs[i].NoAugmentConfig {
564-
augmentConfig(&configs[i], modelPaths)
569+
augmentConfig(&configs[i], yangModules)
565570
}
566571

572+
fmt.Printf("Augumented %d/%d: %v\n", i+1, len(configs), configs[i].Name)
573+
567574
// Iterate over templates and render files
568575
for _, t := range templates {
569576
renderTemplate(t.path, t.prefix+SnakeCase(configs[i].Name)+t.suffix, configs[i])
@@ -573,7 +580,7 @@ func main() {
573580
// render provider.go
574581
renderTemplate(providerTemplate, providerLocation, configs)
575582

576-
changelog, err := ioutil.ReadFile(changelogOriginal)
583+
changelog, err := os.ReadFile(changelogOriginal)
577584
if err != nil {
578585
log.Fatalf("Error reading changelog: %v", err)
579586
}

main.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,19 @@ import (
2525
"github.com/hashicorp/terraform-plugin-framework/providerserver"
2626
)
2727

28-
// Run "go generate" to format example terraform files and generate the docs for the registry/website
29-
30-
// Donwload YANG models.
28+
// Download the YANG models.
3129
//go:generate go run gen/load_models.go
3230

33-
// Run the resource and datasource generation tool.
31+
// Having YANG models as input, generate code (Go and Terraform).
3432
//go:generate go run gen/generator.go
3533

36-
// Format code and cleanup imports
34+
// Format Go code and cleanup imports.
3735
//go:generate go run golang.org/x/tools/cmd/goimports -w internal/provider/
3836

39-
// If you do not have terraform installed, you can remove the formatting command, but its suggested to
40-
// ensure the documentation is formatted properly.
37+
// Format Terraform code.
4138
//go:generate terraform fmt -recursive ./examples/
4239

40+
// Having Terraform code as input, generate documentation.
4341
// Run the docs generation tool, check its repository for more information on how it works and how docs
4442
// can be customized.
4543
//go:generate go run github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs

0 commit comments

Comments
 (0)