|
| 1 | +// Copyright (C) 2020 Matthew "strager" Glazar |
| 2 | +// See end of file for extended copyright information. |
| 3 | + |
| 4 | +package main |
| 5 | + |
| 6 | +import "crypto/sha256" |
| 7 | +import "flag" |
| 8 | +import "fmt" |
| 9 | +import "io" |
| 10 | +import "io/ioutil" |
| 11 | +import "log" |
| 12 | +import "os" |
| 13 | +import "path/filepath" |
| 14 | +import "regexp" |
| 15 | +import "runtime" |
| 16 | +import "text/template" |
| 17 | + |
| 18 | +var BaseURI string |
| 19 | +var MSIXPath string |
| 20 | +var OutDirPath string |
| 21 | + |
| 22 | +var WingetSourcePath string |
| 23 | + |
| 24 | +var BaseURIRegexp *regexp.Regexp = regexp.MustCompile(`^https?://.*/$`) |
| 25 | + |
| 26 | +type TemplateVariables struct { |
| 27 | + BaseURI string |
| 28 | + MSIXSHA256 string |
| 29 | +} |
| 30 | + |
| 31 | +func main() { |
| 32 | + flag.StringVar(&BaseURI, "BaseURI", "", "") |
| 33 | + flag.StringVar(&MSIXPath, "MSIX", "", "") |
| 34 | + flag.StringVar(&OutDirPath, "OutDir", "", "") |
| 35 | + flag.Parse() |
| 36 | + if BaseURI == "" { |
| 37 | + fmt.Fprintf(os.Stderr, "error: missing -BaseURI\n") |
| 38 | + os.Exit(2) |
| 39 | + } |
| 40 | + if !BaseURIRegexp.MatchString(BaseURI) { |
| 41 | + fmt.Fprintf(os.Stderr, "error: invalid -BaseURI; must match regular expression: %v\n", BaseURIRegexp.String()) |
| 42 | + os.Exit(2) |
| 43 | + } |
| 44 | + if MSIXPath == "" { |
| 45 | + fmt.Fprintf(os.Stderr, "error: missing -MSIX\n") |
| 46 | + os.Exit(2) |
| 47 | + } |
| 48 | + if OutDirPath == "" { |
| 49 | + fmt.Fprintf(os.Stderr, "error: missing -OutDir\n") |
| 50 | + os.Exit(2) |
| 51 | + } |
| 52 | + |
| 53 | + _, scriptPath, _, ok := runtime.Caller(0) |
| 54 | + if !ok { |
| 55 | + panic("could not determine path of .go file") |
| 56 | + } |
| 57 | + WingetSourcePath = filepath.Dir(scriptPath) |
| 58 | + |
| 59 | + if err := Main(); err != nil { |
| 60 | + log.Fatal(err) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func Main() error { |
| 65 | + if err := os.MkdirAll(OutDirPath, 0755); err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + |
| 69 | + msixSHA256, err := SHA256File(MSIXPath) |
| 70 | + if err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + variables := TemplateVariables{ |
| 74 | + BaseURI: BaseURI, |
| 75 | + MSIXSHA256: msixSHA256, |
| 76 | + } |
| 77 | + |
| 78 | + filesToTransform := map[string]string{ |
| 79 | + "quick-lint.quick-lint-js.installer.template.yaml": "quick-lint.quick-lint-js.installer.yaml", |
| 80 | + "quick-lint.quick-lint-js.locale.en-US.template.yaml": "quick-lint.quick-lint-js.locale.en-US.yaml", |
| 81 | + "quick-lint.quick-lint-js.template.yaml": "quick-lint.quick-lint-js.yaml", |
| 82 | + } |
| 83 | + for templatePath, outputPath := range filesToTransform { |
| 84 | + if err := TransformTemplateFile( |
| 85 | + filepath.Join(WingetSourcePath, templatePath), |
| 86 | + filepath.Join(OutDirPath, outputPath), |
| 87 | + variables, |
| 88 | + ); err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return nil |
| 94 | +} |
| 95 | + |
| 96 | +func TransformTemplateFile(templatePath string, outputPath string, variables TemplateVariables) error { |
| 97 | + templateSource, err := ioutil.ReadFile(templatePath) |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + tmpl, err := template.New(templatePath).Parse(string(templateSource)) |
| 102 | + if err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + |
| 106 | + outputFile, err := os.Create(outputPath) |
| 107 | + if err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + defer outputFile.Close() |
| 111 | + if err := tmpl.Execute(outputFile, variables); err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + |
| 115 | + return nil |
| 116 | +} |
| 117 | + |
| 118 | +func SHA256File(path string) (string, error) { |
| 119 | + file, err := os.Open(path) |
| 120 | + if err != nil { |
| 121 | + return "", err |
| 122 | + } |
| 123 | + defer file.Close() |
| 124 | + hasher := sha256.New() |
| 125 | + if _, err := io.Copy(hasher, file); err != nil { |
| 126 | + return "", err |
| 127 | + } |
| 128 | + return fmt.Sprintf("%x", hasher.Sum(nil)), nil |
| 129 | +} |
| 130 | + |
| 131 | +// quick-lint-js finds bugs in JavaScript programs. |
| 132 | +// Copyright (C) 2020 Matthew "strager" Glazar |
| 133 | +// |
| 134 | +// This file is part of quick-lint-js. |
| 135 | +// |
| 136 | +// quick-lint-js is free software: you can redistribute it and/or modify |
| 137 | +// it under the terms of the GNU General Public License as published by |
| 138 | +// the Free Software Foundation, either version 3 of the License, or |
| 139 | +// (at your option) any later version. |
| 140 | +// |
| 141 | +// quick-lint-js is distributed in the hope that it will be useful, |
| 142 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 143 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 144 | +// GNU General Public License for more details. |
| 145 | +// |
| 146 | +// You should have received a copy of the GNU General Public License |
| 147 | +// along with quick-lint-js. If not, see <https://www.gnu.org/licenses/>. |
0 commit comments