forked from mh-cbon/go-msi
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.go
70 lines (64 loc) · 1.48 KB
/
index.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
package templates
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/bmatcuk/doublestar"
"github.com/mat007/go-msi/manifest"
)
var funcMap = template.FuncMap{
"dec": func(i int) int {
return i - 1
},
"inc": func(i int) int {
return i + 1
},
"cat": func(filename string) string {
out, err := ioutil.ReadFile(filename)
if err != nil {
panic(fmt.Errorf("failed to read file %q", filename))
}
return string(out)
},
"download": func(url string) string {
response, err := http.Get(url)
if err != nil {
panic(fmt.Errorf("failed to download url %q", url))
}
defer response.Body.Close()
var b bytes.Buffer
if _, err := io.Copy(&b, response.Body); err != nil {
panic(fmt.Errorf("failed to download url %q", url))
}
return b.String()
},
"upper": strings.ToUpper,
}
// Find all wxs files in given directory
func Find(srcDir string, pattern string) ([]string, error) {
glob := filepath.Join(srcDir, pattern)
return doublestar.Glob(glob)
}
// GenerateTemplate generates given src template to out file using given manifest
func GenerateTemplate(wixFile *manifest.WixManifest, src string, out string) error {
tpl, err := template.New("").Funcs(funcMap).ParseFiles(src)
if err != nil {
return err
}
fileWriter, err := os.Create(out)
if err != nil {
return err
}
defer fileWriter.Close()
err = tpl.ExecuteTemplate(fileWriter, filepath.Base(src), wixFile)
if err != nil {
return err
}
return nil
}