@@ -22,13 +22,287 @@ func CopyAllDescriptions(tempMode bool) {
22
22
"attributes:" ,
23
23
}
24
24
25
- for i , id := range identifiers {
26
- CopyText (id , len ( identifiers ) - 1 == i , tempMode )
25
+ for _ , id := range identifiers {
26
+ CopyText (id , tempMode )
27
27
}
28
+
29
+ copyComments (tempMode )
30
+ }
31
+
32
+ // TODO rewrite: ServicePerimeters.yaml is an exeption and needs manually fixing the comments over after switchover
33
+ // Used to copy/paste comments from Ruby -> Go YAML files
34
+ func copyComments (tempMode bool ) {
35
+ log .Printf ("Starting to copy comments from Ruby yaml files to Go yaml files" )
36
+
37
+ renamedFields := map [string ]string {
38
+ "skip_sweeper" : "exclude_sweeper" ,
39
+ "skip_delete" : "exclude_delete" ,
40
+ "skip_test" : "exclude_test" ,
41
+ "skip_import_test" : "exclude_import_test" ,
42
+ "skip_docs" : "exclude_docs" ,
43
+ "skip_attribution_label" : "exclude_attribution_label" ,
44
+ "skip_read" : "exclude_read" ,
45
+ "skip_default_cdiff" : "exclude_default_cdiff" ,
46
+ "skip_docs_values" : "skip_docs_values" ,
47
+ "values" : "enum_values" ,
48
+ }
49
+ var allProductFiles []string = make ([]string , 0 )
50
+ glob := "products/**/go_product.yaml"
51
+ if tempMode {
52
+ glob = "products/**/*.temp"
53
+ }
54
+ files , err := filepath .Glob (glob )
55
+ if err != nil {
56
+ return
57
+ }
58
+ for _ , filePath := range files {
59
+ dir := filepath .Dir (filePath )
60
+ allProductFiles = append (allProductFiles , fmt .Sprintf ("products/%s" , filepath .Base (dir )))
61
+ }
62
+
63
+ for _ , productPath := range allProductFiles {
64
+ // Gather go and ruby file pairs
65
+ yamlMap := make (map [string ][]string )
66
+ yamlPaths , err := filepath .Glob (fmt .Sprintf ("%s/*" , productPath ))
67
+ if err != nil {
68
+ log .Fatalf ("Cannot get yaml files: %v" , err )
69
+ }
70
+ for _ , yamlPath := range yamlPaths {
71
+ if strings .HasSuffix (yamlPath , "_new" ) {
72
+ continue
73
+ }
74
+
75
+ if tempMode {
76
+ cutName , found := strings .CutSuffix (yamlPath , ".temp" )
77
+ if ! found {
78
+ continue
79
+ }
80
+
81
+ baseName := filepath .Base (yamlPath )
82
+ yamlMap [baseName ] = make ([]string , 2 )
83
+ yamlMap [baseName ][1 ] = yamlPath
84
+ yamlMap [baseName ][0 ] = cutName
85
+ continue
86
+ }
87
+
88
+ fileName := filepath .Base (yamlPath )
89
+ baseName , found := strings .CutPrefix (fileName , "go_" )
90
+ if yamlMap [baseName ] == nil {
91
+ yamlMap [baseName ] = make ([]string , 2 )
92
+ }
93
+ if found {
94
+ yamlMap [baseName ][1 ] = yamlPath
95
+ } else {
96
+ yamlMap [baseName ][0 ] = yamlPath
97
+ }
98
+ }
99
+
100
+ for _ , files := range yamlMap {
101
+ rubyPath := files [0 ]
102
+ goPath := files [1 ]
103
+
104
+ recordingComments := false
105
+ comments := ""
106
+ commentsMap := make (map [string ]string , 0 )
107
+ nestedNameLine := ""
108
+ previousNameLine := ""
109
+ trimmedPreviousLine := ""
110
+
111
+ // Ready Ruby yaml
112
+ wholeLineComment , err := regexp .Compile (`^\s*#.*?` )
113
+ if err != nil {
114
+ log .Fatalf ("Cannot compile the regular expression: %v" , err )
115
+ }
116
+
117
+ if err != nil {
118
+ log .Fatalf ("Cannot compile the regular expression: %v" , err )
119
+ }
120
+
121
+ file , _ := os .Open (rubyPath )
122
+ defer file .Close ()
123
+ scanner := bufio .NewScanner (file )
124
+ for scanner .Scan () {
125
+ line := scanner .Text ()
126
+ if line == "" {
127
+ if recordingComments {
128
+ comments = fmt .Sprintf ("%s\n %s" , comments , line )
129
+ }
130
+ continue
131
+ }
132
+
133
+ if wholeLineComment .MatchString (line ) {
134
+ if ! recordingComments {
135
+ recordingComments = true
136
+ comments = line
137
+ } else {
138
+ comments = fmt .Sprintf ("%s\n %s" , comments , line )
139
+ }
140
+ } else {
141
+ normalizedLine := line
142
+
143
+ indexOfComment := strings .Index (normalizedLine , " # " )
144
+ if indexOfComment > 0 { // The comments are in the same line with the code
145
+ comments = normalizedLine [indexOfComment :]
146
+ recordingComments = true
147
+ normalizedLine = strings .TrimRight (normalizedLine [:indexOfComment ], " " )
148
+ }
149
+
150
+ normalizedLine = strings .ReplaceAll (normalizedLine , "'" , "" )
151
+ normalizedLine = strings .ReplaceAll (normalizedLine , `"` , "" )
152
+ normalizedLine = strings .ReplaceAll (normalizedLine , `\` , "" )
153
+ normalizedLine = strings .ReplaceAll (normalizedLine , ": :" , ": " )
154
+ normalizedLine = strings .ReplaceAll (normalizedLine , "- :" , "- " )
155
+ trimmed := strings .TrimSpace (normalizedLine )
156
+ index := strings .Index (normalizedLine , trimmed )
157
+
158
+ if index == 0 {
159
+ previousNameLine = ""
160
+ nestedNameLine = ""
161
+ } else if index >= 2 && (strings .HasPrefix (trimmedPreviousLine , "- !ruby/object" ) || strings .HasPrefix (trimmedPreviousLine , "--- !ruby/object" )) {
162
+ normalizedLine = fmt .Sprintf ("%s- %s" , normalizedLine [:index - 2 ], normalizedLine [index :])
163
+
164
+ if strings .HasPrefix (trimmed , "name:" ) {
165
+ if nestedNameLine != "" {
166
+ previousNameLine = nestedNameLine
167
+ }
168
+ nestedNameLine = normalizedLine
169
+ }
170
+ }
171
+
172
+ trimmedPreviousLine = trimmed
173
+
174
+ if recordingComments {
175
+ if ! strings .HasPrefix (comments , "# Copyright" ) {
176
+ // The line is a type, for example - !ruby/object:Api::Type::Array.
177
+ // The lines of types are not present in Go yaml files
178
+ if strings .HasPrefix (trimmed , "- !ruby/object" ) || strings .HasPrefix (trimmed , "--- !ruby/object" ) {
179
+ continue
180
+ }
181
+
182
+ // Remove suffix " !ruby/object" as the types are not present in Go yaml files
183
+ indexOfRuby := strings .Index (normalizedLine , ": !ruby/object" )
184
+ if indexOfRuby >= 0 {
185
+ normalizedLine = normalizedLine [:indexOfRuby + 1 ]
186
+ }
187
+ // Remove suffix Api::Type::
188
+ indexOfRuby = strings .Index (normalizedLine , " Api::Type::" )
189
+ if indexOfRuby >= 0 {
190
+ normalizedLine = normalizedLine [:indexOfRuby ]
191
+ }
192
+
193
+ // Some fields are renamed during yaml file conversion
194
+ field := strings .Split (normalizedLine , ":" )[0 ]
195
+ if shouldUseFieldName (normalizedLine ) {
196
+ normalizedLine = field
197
+ }
198
+
199
+ field = strings .TrimSpace (field )
200
+ if goName , ok := renamedFields [field ]; ok {
201
+ normalizedLine = strings .Replace (normalizedLine , field , goName , 1 )
202
+ }
203
+
204
+ key := fmt .Sprintf ("%s$%s$%s" , previousNameLine , nestedNameLine , normalizedLine )
205
+ commentsMap [key ] = comments
206
+ }
207
+ recordingComments = false
208
+ comments = ""
209
+ }
210
+ }
211
+ }
212
+
213
+ // Read Go yaml while writing to a temp file
214
+ firstLine := true
215
+ nestedNameLine = ""
216
+ previousNameLine = ""
217
+ newFilePath := fmt .Sprintf ("%s_new" , goPath )
218
+ fo , _ := os .Create (newFilePath )
219
+ w := bufio .NewWriter (fo )
220
+ file , _ = os .Open (goPath )
221
+ defer file .Close ()
222
+ scanner = bufio .NewScanner (file )
223
+ for scanner .Scan () {
224
+ line := scanner .Text ()
225
+
226
+ if firstLine {
227
+ if strings .Contains (line , "NOT CONVERTED - RUN YAML MODE" ) {
228
+ firstLine = false
229
+ continue
230
+ } else {
231
+ break
232
+ }
233
+ }
234
+
235
+ if len (commentsMap ) > 0 && ! wholeLineComment .MatchString (line ) && line != "" { // This line is not a comment
236
+ normalizedLine := strings .ReplaceAll (line , "'" , "" )
237
+ normalizedLine = strings .ReplaceAll (normalizedLine , `"` , "" )
238
+ trimmed := strings .TrimSpace (normalizedLine )
239
+ index := strings .Index (normalizedLine , trimmed )
240
+
241
+ if index == 0 {
242
+ previousNameLine = ""
243
+ nestedNameLine = ""
244
+ } else if index >= 2 && strings .HasPrefix (trimmed , "- name:" ) {
245
+ if nestedNameLine != "" {
246
+ previousNameLine = nestedNameLine
247
+ }
248
+ nestedNameLine = normalizedLine
249
+ }
250
+
251
+ field := strings .Split (normalizedLine , ":" )[0 ]
252
+ if shouldUseFieldName (normalizedLine ) {
253
+ normalizedLine = field
254
+ }
255
+
256
+ key := fmt .Sprintf ("%s$%s$%s" , previousNameLine , nestedNameLine , normalizedLine )
257
+ if comments , ok := commentsMap [key ]; ok {
258
+ delete (commentsMap , key )
259
+ line = fmt .Sprintf ("%s\n %s" , comments , line )
260
+ }
261
+ }
262
+ _ , err := w .WriteString (fmt .Sprintf ("%s\n " , line ))
263
+ if err != nil {
264
+ log .Fatalf ("Error when writing the line %s: %#v" , line , err )
265
+ }
266
+ }
267
+
268
+ if ! firstLine {
269
+ // Flush writes any buffered data to the underlying io.Writer.
270
+ if err = w .Flush (); err != nil {
271
+ panic (err )
272
+ }
273
+
274
+ if len (commentsMap ) > 0 {
275
+ log .Printf ("Some comments in rubyPath %s are not copied over: %#v" , rubyPath , commentsMap )
276
+ }
277
+ // Overwrite original file with temp
278
+ os .Rename (newFilePath , goPath )
279
+ } else {
280
+ os .Remove (newFilePath )
281
+ }
282
+ }
283
+ }
284
+ log .Printf ("Finished to copy comments from Ruby yaml files to Go yaml files" )
285
+ }
286
+
287
+ // custom template files in Go yaml files have different names
288
+ // The format of primary_resource_name for enum is different in Go yaml files
289
+ func shouldUseFieldName (line string ) bool {
290
+ filedNames := []string {
291
+ "templates/terraform/" ,
292
+ "primary_resource_name:" ,
293
+ "default_value:" ,
294
+ "deprecation_message:" ,
295
+ }
296
+ for _ , fieldName := range filedNames {
297
+ if strings .Contains (line , fieldName ) {
298
+ return true
299
+ }
300
+ }
301
+ return false
28
302
}
29
303
30
304
// Used to copy/paste text from Ruby -> Go YAML files
31
- func CopyText (identifier string , last , tempMode bool ) {
305
+ func CopyText (identifier string , tempMode bool ) {
32
306
var allProductFiles []string = make ([]string , 0 )
33
307
glob := "products/**/go_product.yaml"
34
308
if tempMode {
@@ -135,14 +409,13 @@ func CopyText(identifier string, last, tempMode bool) {
135
409
if firstLine {
136
410
if strings .Contains (line , "NOT CONVERTED - RUN YAML MODE" ) {
137
411
firstLine = false
138
- if ! last {
139
- w .WriteString (fmt .Sprintf ("NOT CONVERTED - RUN YAML MODE\n " ))
140
- }
412
+ w .WriteString (fmt .Sprintf ("NOT CONVERTED - RUN YAML MODE\n " ))
141
413
continue
142
414
} else {
143
415
break
144
416
}
145
417
}
418
+
146
419
if strings .Contains (line , identifier ) {
147
420
if index >= len (text ) {
148
421
log .Printf ("did not replace %s correctly! Is the file named correctly?" , goPath )
0 commit comments