forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundle.go
115 lines (96 loc) · 2.97 KB
/
bundle.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Run a command line helper for various Fyne tools.
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path"
"regexp"
"strings"
"fyne.io/fyne"
)
type bundler struct {
name, pkg string
prefix string
noheader bool
}
func writeResource(file, name string) {
bytes, err := ioutil.ReadFile(file)
if err != nil {
fyne.LogError("Unable to load file "+file, err)
return
}
res := fyne.NewStaticResource(path.Base(file), bytes)
fmt.Printf("var %s = %#v\n", name, res)
}
func writeHeader(pkg string) {
fmt.Println("package", pkg)
fmt.Println()
fmt.Println("import \"fyne.io/fyne\"")
}
func sanitiseName(file, prefix string) string {
titled := strings.Title(file)
reg, _ := regexp.Compile("[^a-zA-Z0-9]+")
name := reg.ReplaceAllString(titled, "")
return prefix + name
}
// Bundle takes a file (at filepath) and serialises it into Go to be output into
// a generated bundle file. The go file will be part of the specified package
// (pkg) and the data will be assigned to variable named "name". If you are
// appending an existing resource file then pass true to noheader as the headers
// should only be output once per file.
func doBundle(name, pkg, prefix string, noheader bool, filepath string) {
if !noheader {
writeHeader(pkg)
}
fmt.Println()
if name == "" {
name = sanitiseName(path.Base(filepath), prefix)
}
writeResource(filepath, name)
}
func dirBundle(pkg, prefix string, noheader bool, dirpath string) {
files, err := ioutil.ReadDir(dirpath)
if err != nil {
fyne.LogError("Error reading specified directory", err)
return
}
omitHeader := noheader
for _, file := range files {
filename := file.Name()
if path.Ext(filename) == ".go" {
continue
}
doBundle("", pkg, prefix, omitHeader, path.Join(dirpath, filename))
omitHeader = true // only output header once at most
}
}
func (b *bundler) addFlags() {
flag.StringVar(&b.name, "name", "", "The variable name to assign the resource (file mode only)")
flag.StringVar(&b.pkg, "package", "main", "The package to output in headers (if not appending)")
flag.StringVar(&b.prefix, "prefix", "resource", "A prefix for variables (ignored if name is set)")
flag.BoolVar(&b.noheader, "append", false, "Append an existing go file (don't output headers)")
}
func (b *bundler) printHelp(indent string) {
fmt.Println(indent, "The bundle command embeds static content into your go application.")
fmt.Println(indent, "Each resource will have a generated filename unless specified")
fmt.Println(indent, "Command usage: fyne bundle [parameters] file|directory")
}
func (b *bundler) run(args []string) {
if len(args) != 1 {
fyne.LogError("Missing required file or directory parameter after flags", nil)
return
}
stat, err := os.Stat(args[0])
if os.IsNotExist(err) {
fyne.LogError("Specified file could not be found", err)
} else if stat.IsDir() {
dirBundle(b.pkg, b.prefix, b.noheader, args[0])
} else {
if b.name != "" {
b.prefix = ""
}
doBundle(b.name, b.pkg, b.prefix, b.noheader, args[0])
}
}