-
Notifications
You must be signed in to change notification settings - Fork 0
/
ranbundler.go
320 lines (299 loc) · 8.31 KB
/
ranbundler.go
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package main
import (
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"github.com/pelletier/go-toml"
)
/**
* Check if the directive type matches
* @param {string} directiveType - The directive type
* @param {string} line - The line of the file
* @return {bool} - If the directive type matches
*/
func isMatchingDirectiveType(
directiveType string,
component string,
) bool {
if strings.Contains(component, "OF") {
return strings.Contains(component, directiveType) || strings.Contains(component, "*")
}
return true
}
/**
* Extract the exclusive component, and parent component string
* @param {string} fileContentString - The file content string
* @return {string} - The exclusive component string
* @return {string} - The parent component string prefix
* @return {string} - The parent component string suffix
*/
func removeInvalidExclusiveComponent(
fileContentString string,
directiveType string,
) string {
newFileContentString := fileContentString
var exclusiveComponentRegex = regexp.MustCompile(`(?s)<EXCLUSIVE(.*?)<\/EXCLUSIVE>`)
var exclusiveComponentStrings = exclusiveComponentRegex.FindAllStringIndex(fileContentString, -1)
for _, exclusiveComponent := range exclusiveComponentStrings {
exclusiveComponent := fileContentString[exclusiveComponent[0]:exclusiveComponent[1]]
if !isMatchingDirectiveType(directiveType, exclusiveComponent) {
newFileContentString = strings.ReplaceAll(newFileContentString, exclusiveComponent, "")
}
}
return newFileContentString
}
/**
* Parse the javascript file
* @param {string} path - The path of the file
* @param {string} directiveType - The directive type
* @return {string} - The file content
*/
func parseJavascriptFile(path string, directiveType string) string {
fileContent, err := os.ReadFile(path)
if err != nil {
fmt.Println(err)
return err.Error()
}
fileContentString := string(fileContent)
if !strings.Contains(fileContentString, "<EXCLUSIVE") {
return string(fileContent)
}
fileContent = []byte(removeInvalidExclusiveComponent(fileContentString, directiveType))
return string(fileContent)
}
/**
* Check if the file is a javascript file
* @param {string} path - The path of the file
* @return {bool} - If the file is a javascript file
*/
func fileContainsUiComponents(path string) bool {
fileExtensions := []string{".jsx", ".tsx", ".astro", ".svelte", ".vue"}
ext := filepath.Ext(path)
for _, fileExt := range fileExtensions {
if ext == fileExt {
return true
}
}
return false
}
/**
* Walk through the file path and create the build path in the output directory
* @param {string} path - The path of the file
* @param {string} directiveType - The directive type
*/
func walkFilePath(srcDir string, directiveBuildPath string, directiveType string, ignoredPaths []string) {
err := filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
newPath := srcDir + strings.ReplaceAll(path, srcDir, directiveBuildPath)
if pathIsIgnored(path, ignoredPaths) {
return filepath.SkipDir
}
if info.IsDir() {
error := os.MkdirAll(newPath, 0777)
if error != nil {
fmt.Println(error)
return error
}
} else if strings.Contains(path, "platform.ts") {
newData, err := modifyPlatformConfigurationFile(path, directiveType)
if err != nil {
fmt.Println(err)
return err
}
file, err := os.Create(newPath)
if err != nil {
fmt.Println(err)
return err
}
defer file.Close()
_, err = file.WriteString(newData)
if err != nil {
fmt.Println(err)
return err
}
} else {
if fileContainsUiComponents(path) {
var fileContent = parseJavascriptFile(path, directiveType)
if fileContent == "" {
return nil
}
file, err := os.Create(newPath)
if err != nil {
fmt.Println(err)
return err
}
defer file.Close()
_, err = file.WriteString(fileContent)
if err != nil {
fmt.Println(err)
return err
}
} else {
srcFile, err := os.Open(path)
if err != nil {
fmt.Println(err)
return err
}
defer srcFile.Close()
dstFile, err := os.Create(newPath)
if err != nil {
fmt.Println(err)
return err
}
defer dstFile.Close()
_, err = io.Copy(dstFile, srcFile)
if err != nil {
fmt.Println(err)
return err
}
}
}
if err != nil {
fmt.Println(err)
return nil
}
return nil
})
if err != nil {
fmt.Println(err)
}
}
/**
* Check if the path is ignored
* @param {string} path - The path
* @param {[]string} ignoredPaths - The ignored paths
* @return {bool} - If the path is ignored
*/
func pathIsIgnored(path string, ignoredPaths []string) bool {
var currentDirArray []string = strings.Split(path, "/")
if len(currentDirArray) == 0 {
return false
}
var currentDir string = currentDirArray[len(currentDirArray)-1]
for _, ignoredPath := range ignoredPaths {
if currentDir == ignoredPath {
return true
}
}
return false
}
/**
* Modify the platform configuration file
* @param {string} path - The path of the file
* @param {string} deviceType - The device type
* @return {string} - The modified data
* @return {error} - The error
*/
func modifyPlatformConfigurationFile(path string, deviceType string) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", err
}
defer file.Close()
// Read the entire file
data, err := os.ReadFile(path)
if err != nil {
return "", err
}
var fileString string = string(data)
platformStringIndex := strings.Index(fileString, "PLATFORM")
platformStringTerminationIndex := strings.Index(fileString[platformStringIndex:], "}")
if platformStringIndex == -1 &&
platformStringTerminationIndex == -1 &&
platformStringTerminationIndex < platformStringIndex {
return "", fmt.Errorf("PLATFORM not found in the file")
}
newPlatformString := fmt.Sprintf(
`PLATFORM: { MODE: "%s", NAME: "%s", ID: "%s" }`, deviceType, deviceType, deviceType,
)
newData := strings.ReplaceAll(fileString, fileString[platformStringIndex:platformStringIndex+platformStringTerminationIndex+1], newPlatformString)
return newData, nil
}
type Config struct {
InputPath string `toml:"input_path" json:"input_path"`
OutputDir string `toml:"output_dir" json:"output_dir"`
IgnoredPaths []string `toml:"ignored_paths" json:"ignored_paths"`
DeviceTypes []string `toml:"device_types" json:"device_types"`
}
/**
* Parse the TOML configuration file
* @return {Config} - The configuration
* @return {error} - The error
*/
func parseTomlConfigurationFile() (Config, error) {
file, err := os.Open("config.toml")
if err != nil {
panic(err)
}
defer file.Close()
var config Config
b, err := io.ReadAll(file)
if err != nil {
panic(err)
}
err = toml.Unmarshal(b, &config)
if err != nil {
panic(err)
}
return config, nil
}
func parseJsonConfigurationFile(jsonFilePath string) (Config, error) {
jsonFile, err := os.Open(jsonFilePath)
if err != nil {
fmt.Printf("Error opening JSON file: %v\n", err)
return Config{}, err
}
defer jsonFile.Close()
byteValue, err := io.ReadAll(jsonFile)
if err != nil {
fmt.Printf("Error reading JSON file: %v\n", err)
return Config{}, err
}
var config Config
err = json.Unmarshal(byteValue, &config)
if err != nil {
fmt.Printf("Error unmarshalling JSON file: %v\n", err)
return Config{}, err
}
return config, nil
}
/**
* Get the Configuration from the configuration file
* @param {string} configurationFile - The configuration file
* @return {Config} - The configuration
* @return {error} - The error
*/
func getConfigurationFile(configurationFile string) (Config, error) {
if strings.HasSuffix(configurationFile, ".toml") {
return parseTomlConfigurationFile()
} else if strings.HasSuffix(configurationFile, ".json") {
return parseJsonConfigurationFile(configurationFile)
}
return Config{}, fmt.Errorf("invalid configuration file")
}
func main() {
if len(os.Args) < 2 {
fmt.Println("Please provide the configuration file path")
return
}
config, err := getConfigurationFile(os.Args[1])
if err != nil {
fmt.Println(err)
}
path := config.InputPath
outputDir := config.OutputDir
var wg sync.WaitGroup
for _, deviceType := range config.DeviceTypes {
wg.Add(1)
go func(deviceType string) {
var deviceBuildPath = outputDir + "/" + deviceType + "/"
defer wg.Done()
walkFilePath(path, deviceBuildPath, deviceType, config.IgnoredPaths)
}(deviceType)
}
wg.Wait()
}