avm99963 | e13bace | 2020-12-25 01:35:25 +0100 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 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 | |
| 17 | const ( |
| 18 | manifestTemplate = "templates/manifest.gjson" |
avm99963 | e13bace | 2020-12-25 01:35:25 +0100 | [diff] [blame] | 19 | ) |
| 20 | |
| 21 | var beginningOfIfStatement = regexp.MustCompile(`^\s*#if defined\(([^\(\)]*)\)\s*$`) |
| 22 | var endOfIfStatement = regexp.MustCompile(`^\s*#endif\s*$`) |
| 23 | |
| 24 | var ( |
| 25 | quietMode = flag.Bool("quiet", false, "Quiet mode") |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 26 | destFile = flag.String("dest", "", "Destination file") |
avm99963 | e13bace | 2020-12-25 01:35:25 +0100 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | func 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 | |
| 38 | func 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 | |
| 78 | func 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ínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 89 | if *destFile == "" { |
| 90 | log.Fatalf("Pass the destination file name via the -dest flag.") |
| 91 | } |
| 92 | |
avm99963 | e13bace | 2020-12-25 01:35:25 +0100 | [diff] [blame] | 93 | 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ínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 99 | dest, err := os.Create(*destFile) |
avm99963 | e13bace | 2020-12-25 01:35:25 +0100 | [diff] [blame] | 100 | if err != nil { |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 101 | log.Fatalf("Couldn't create file %v: %v", *destFile, err) |
avm99963 | e13bace | 2020-12-25 01:35:25 +0100 | [diff] [blame] | 102 | } |
| 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 | } |