blob: 1c2e4f0afdfcf82cf5f4e7d4267dea653c957473 [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"
19 manifestSrc = "src/manifest.json"
20)
21
22var beginningOfIfStatement = regexp.MustCompile(`^\s*#if defined\(([^\(\)]*)\)\s*$`)
23var endOfIfStatement = regexp.MustCompile(`^\s*#endif\s*$`)
24
25var (
26 quietMode = flag.Bool("quiet", false, "Quiet mode")
27)
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
89 template, err := os.Open(manifestTemplate)
90 if err != nil {
91 log.Fatalf("Couldn't open file %v: %v", manifestTemplate, err)
92 }
93 defer template.Close()
94
95 dest, err := os.Create(manifestSrc)
96 if err != nil {
97 log.Fatalf("Couldn't create file %v: %v", manifestSrc, err)
98 }
99 defer dest.Close()
100
101 err = WriteManifest(template, dest, dependencies)
102 if err != nil {
103 log.Fatalf("%v", err)
104 } else if !*quietMode {
105 log.Println("Manifest has been generated successfully")
106 }
107}