Skip to content

Commit

Permalink
Trim white spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
zli82016 committed Apr 18, 2024
1 parent 026dc98 commit b2bbc93
Show file tree
Hide file tree
Showing 77 changed files with 504 additions and 176 deletions.
2 changes: 2 additions & 0 deletions mmv1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ var version = flag.String("version", "", "optional version name. If specified, t
var product = flag.String("product", "", "optional product name. If specified, the resources under the specific product will be generated. Otherwise, resources under all products will be generated.")

func main() {
convertTemplates()

flag.Parse()
var generateCode = true
var generateDocs = true
Expand Down
326 changes: 326 additions & 0 deletions mmv1/template-converter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,326 @@
package main

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

"github.com/golang/glog"
)

func find(root, ext string) []string {
var a []string

files, err := ioutil.ReadDir(root)
if err != nil {
log.Fatal(err)
}

for _, file := range files {
if filepath.Ext(file.Name()) == ext {
a = append(a, file.Name())
}
}
return a
}

func convertTemplates() {
folders := []string{"examples", "constants", "custom_check_destroy", "custom_create", "custom_delete", "custom_import", "custom_update", "decoders", "encoders", "extra_schema_entry", "post_create", "post_create_failure", "post_delete", "post_import", "post_update", "pre_create", "pre_delete", "pre_read", "pre_update", "state_migrations", "update_encoder", "custom_expand", "custom_flatten"}
counts := 0
for _, folder := range folders {
counts += convertTemplate(folder)
}
log.Printf("%d template files in total", counts)
}

func convertTemplate(folder string) int {
rubyDir := fmt.Sprintf("templates/terraform/%s", folder)
goDir := fmt.Sprintf("templates/terraform/%s/go", folder)

// rubyDir := "templates/terraform"
// goDir := "templates/terraform"
if err := os.MkdirAll(goDir, os.ModePerm); err != nil {
glog.Error(fmt.Errorf("error creating directory %v: %v", goDir, err))
}

templates := find(rubyDir, ".erb")
// templates := []string{"expand_resource_ref.erb"}
log.Printf("%d template files in folder %s", len(templates), folder)

for _, file := range templates {
data, err := os.ReadFile(path.Join(rubyDir, file))
if err != nil {
log.Fatalf("Cannot open the file: %v", file)
}

// Replace {{}}
r, err := regexp.Compile(`{{(.*?)}}`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte(`{{"{{"}}$1{{"}}"}}`))

// Replace primary_resource_id
r, err = regexp.Compile(`<%=\s*ctx\[:primary_resource_id\]\s*-?%>`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte("{{$.PrimaryResourceId}}"))

// Replace vars
r, err = regexp.Compile(`<%=\s*ctx\[:vars\]\[('|")([a-zA-Z0-9_-]+)('|")\]\s*-?%>`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte(`{{index $.Vars "$2"}}`))

// Replace test_env_vars
r, err = regexp.Compile(`<%=\s*ctx\[:test_env_vars\]\[('|")([a-zA-Z0-9_-]+)('|")\]\s*-?%>`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte(`{{index $.TestEnvVars "$2"}}`))

// Replace <% unless compiler == "terraformgoogleconversion-codegen" -%>
r, err = regexp.Compile(`<% unless compiler == "terraformgoogleconversion-codegen" -%>`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte(`{{- if ne $.Compiler "terraformgoogleconversion-codegen" }}`))

// Replace <% unless version == 'ga' -%>
r, err = regexp.Compile(`<% unless version == ['|"]ga['|"] -%>`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte(`{{- if ne $.TargetVersionName "ga" }}`))

// Replace <% if version == 'ga' -%>
r, err = regexp.Compile(`<% if version == 'ga' -%>`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte(`{{- if eq $.TargetVersionName "ga" }}`))

// Replace <% else -%>
r, err = regexp.Compile(`<% else -%>`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte(`{{- else }}`))

// Replace <%= object.name -%>
r, err = regexp.Compile(`<%= object.name -%>`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte(`{{$.Name}}`))

// Replace <%= object.resource_name -%>
r, err = regexp.Compile(`<%= object.resource_name -%>`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte(`{{$.ResourceName}}`))

// Replace <%=object.self_link_uri-%>
r, err = regexp.Compile(`<%=object.self_link_uri-%>`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte(`{{$.SelfLinkUri}}`))

// Replace <%=object.create_uri-%>
r, err = regexp.Compile(`<%=object.create_uri-%>`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte(`{{$.CreateUri}}`))

// Replace <%=object.base_url-%>
r, err = regexp.Compile(`<%=object.base_url-%>`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte(`{{$.BaseUrl}}`))

// Replace <%=object.__product.name-%>
r, err = regexp.Compile(`<%=object.__product.name-%>`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte(`{{$.ProductMetadata.Name}}`))

// Replace <% if object.name == 'Disk' -%>
r, err = regexp.Compile(`<% if object.name == 'Disk' -%>`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte(`{{- if eq $.Name "Disk" }}`))

// Replace <% elsif object.name == 'RegionDisk' -%>
r, err = regexp.Compile(`<% elsif object.name == 'RegionDisk' -%>`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte(`{{- else if eq $.Name "RegionDisk" }}`))

// Replace <% if object.properties.any?{ |p| p.name == "labels" } -%>
r, err = regexp.Compile(`<% if object\.properties.any\?\{ \|p\| p\.name == "labels" \} -%>`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte(`{{- if $.HasLabelsField }}`))

// Replace <% if object.error_retry_predicates -%>
r, err = regexp.Compile(`<% if object.error_retry_predicates -%>`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte(`{{- if $.ErrorRetryPredicates }}`))

// Replace <% if object.error_abort_predicates -%>
r, err = regexp.Compile(`<% if object.error_abort_predicates -%>`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte(`{{- if $.ErrorAbortPredicates }}`))

// Replace <%= object.error_retry_predicates.join(',') -%>
r, err = regexp.Compile(`<%= object.error_retry_predicates.join\(','\) -%>`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte(` {{- join $.ErrorRetryPredicates "," -}} `))

// Replace <%= object.error_abort_predicates.join(',') -%>
r, err = regexp.Compile(`<%= object.error_abort_predicates.join\(','\) -%>`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte(` {{- join $.ErrorAbortPredicates "," -}} `))

// Replace <%= object.name.camelize(:lower) -%>
r, err = regexp.Compile(`<%= object.name.camelize\(:lower\) -?%>`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte(`{{camelize $.Name "lower"}}`))

// Replace <%= object.name.plural.camelize(:lower) -%>
r, err = regexp.Compile(`<%= object.name.plural.camelize\(:lower\) -%>`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte(`{{camelize (plural $.Name) "lower"}}`))

// Replace <%= id_format(object) -%>
r, err = regexp.Compile(`<%= id_format\(object\) -%>`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte(`{{$.GetIdFormat}}`))

// Replace <%= prefix -%>
r, err = regexp.Compile(`<%= prefix -%>`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte(`{{$.GetPrefix}}`))

// Replace <%= titlelize_property(property) -%>
r, err = regexp.Compile(`<%= titlelize_property\(property\) -%>`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte(`{{$.TitlelizeProperty}}`))

// Replace <%= build_expand_resource_ref('v.(string)', property, pwd) %>
r, err = regexp.Compile(`<%= build_expand_resource_ref\('v\.\(string\)', property, pwd\) %>`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte(`{{ template "expandResourceRef" dict "VarName" "v.(string)" "Property" $ }}`))

// Replace <%= build_expand_resource_ref('raw.(string)', property.item_type, pwd) %>
r, err = regexp.Compile(`<%= build_expand_resource_ref\('raw\.\(string\)', property\.item_type, pwd\) %>`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte(`{{ template "expandResourceRef" dict "VarName" "raw.(string)" "Property" $.ItemType }}`))

// Replace <%= property.name.underscore -%>
r, err = regexp.Compile(`<%= property.name.underscore -%>`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte(`{{underscore $.Name}}`))

// Replace <%= resource_type -%>
r, err = regexp.Compile(`<%= resource_type -%>`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte(`{{$.ResourceType}}`))

// Replace <% if property.is_set -%>
r, err = regexp.Compile(`<% if property.is_set -%>`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte(`{{- if $.IsSet }}`))

// Replace <% end -%>
r, err = regexp.Compile(`<%\s*end -%>`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte(`{{- end }}`))

copyRight := `{{- /*
The license inside this block applies to this file
Copyright 2024 Google Inc.
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.
*/}}`
// Replace copyright
r, err = regexp.Compile(`(?s)<%[-\s#]*[tT]he license inside this.*?limitations under the License..*?%>`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte(copyRight))

// Replace comments
r, err = regexp.Compile(`(?s)<%#-?\s?(.*?)\s?-?%>`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte(`{{- /* $1 */}}`))

// Replace .erb
r, err = regexp.Compile(`\.erb`)
if err != nil {
log.Fatalf("Cannot compile the regular expression: %v", err)
}
data = r.ReplaceAll(data, []byte(`.tmpl`))

goTemplate := strings.Replace(file, "erb", "tmpl", 1)
err = ioutil.WriteFile(path.Join(goDir, goTemplate), data, 0644)
if err != nil {
glog.Exit(err)
}
}

return len(templates)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{/*
{{- /*
The license inside this block applies to this file
Copyright 2024 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -9,7 +9,7 @@
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.
*/ -}}
*/}}
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{/*
{{- /*
The license inside this block applies to this file
Copyright 2024 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -9,7 +9,7 @@
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.
*/ -}}
*/}}
_, err := expandSecretManagerSecretVersionEnabled(d.Get("enabled"), d, config)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions mmv1/templates/terraform/decoders/go/backend_service.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{/*
{{- /*
The license inside this block applies to this file
Copyright 2024 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -9,7 +9,7 @@
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.
*/ -}}
*/}}
// We need to pretend IAP isn't there if it's disabled for Terraform to maintain
// BC behaviour with the handwritten resource.
v, ok := res["iap"]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{/*
{{- /*
The license inside this block applies to this file
Copyright 2024 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -9,7 +9,7 @@
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.
*/ -}}
*/}}
if paramMap, ok := res["params"]; ok {
params := paramMap.(map[string]interface{})
for _, sp := range sensitiveParams {
Expand Down
Loading

0 comments on commit b2bbc93

Please sign in to comment.