blob: 185f1c363ea1ad444c629ba04b6e935efd80ac3c [file] [log] [blame]
avm99963e13bace2020-12-25 01:35:25 +01001package main
2
3import (
4 "bufio"
5 "flag"
6 "fmt"
7 "io"
8 "log"
9 "os"
10 "regexp"
11 "strings"
12)
13
14// Generates the manifest.json file according to the dependencies passed
15// via CLI arguments
16
17const (
18 manifestTemplate = "templates/manifest.gjson"
avm99963e13bace2020-12-25 01:35:25 +010019)
20
21var beginningOfIfStatement = regexp.MustCompile(`^\s*#if defined\(([^\(\)]*)\)\s*$`)
22var endOfIfStatement = regexp.MustCompile(`^\s*#endif\s*$`)
23
24var (
25 quietMode = flag.Bool("quiet", false, "Quiet mode")
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020026 destFile = flag.String("dest", "", "Destination file")
avm99963e13bace2020-12-25 01:35:25 +010027)
28
29func FindWithCaseFolding(slice []string, val string) bool {
30 for _, item := range slice {
31 if strings.EqualFold(item, val) {
32 return true
33 }
34 }
35 return false
36}
37
38func WriteManifest(template io.Reader, dest io.Writer, dependencies []string) error {
39 level := 0
40 activeLevel := 0
41 scanner := bufio.NewScanner(template)
42 for scanner.Scan() {
43 line := scanner.Text()
44 considerLine := false
45 if m := beginningOfIfStatement.FindStringSubmatch(line); m != nil {
46 if level == activeLevel {
47 statementDeps := m[1]
48 deps := strings.Split(statementDeps, "||")
49 for _, dep := range deps {
50 dep = strings.TrimSpace(dep)
51 if FindWithCaseFolding(dependencies, dep) {
52 activeLevel++
53 break
54 }
55 }
56 }
57 level++
58 } else if m := endOfIfStatement.MatchString(line); m {
59 if activeLevel == level {
60 activeLevel--
61 }
62 level--
63 } else {
64 considerLine = level == activeLevel
65 }
66
67 if considerLine {
68 _, err := io.WriteString(dest, line + "\n")
69 if err != nil {
70 return fmt.Errorf("Can't write manifest: %v", err)
71 }
72 }
73 }
74
75 return nil
76}
77
78func main() {
79 log.SetPrefix("generateManifest: ")
80 log.SetFlags(0)
81
82 flag.Parse()
83 dependencies := flag.Args()
84
85 if len(dependencies) == 0 {
86 log.Fatalf("Pass the dependencies as arguments (for instance, run `go run generateManifest.go CHROMIUM`).")
87 }
88
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020089 if *destFile == "" {
90 log.Fatalf("Pass the destination file name via the -dest flag.")
91 }
92
avm99963e13bace2020-12-25 01:35:25 +010093 template, err := os.Open(manifestTemplate)
94 if err != nil {
95 log.Fatalf("Couldn't open file %v: %v", manifestTemplate, err)
96 }
97 defer template.Close()
98
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020099 dest, err := os.Create(*destFile)
avm99963e13bace2020-12-25 01:35:25 +0100100 if err != nil {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200101 log.Fatalf("Couldn't create file %v: %v", *destFile, err)
avm99963e13bace2020-12-25 01:35:25 +0100102 }
103 defer dest.Close()
104
105 err = WriteManifest(template, dest, dependencies)
106 if err != nil {
107 log.Fatalf("%v", err)
108 } else if !*quietMode {
109 log.Println("Manifest has been generated successfully")
110 }
111}