diff --git a/cmd/fyne/bundle.go b/cmd/fyne/bundle.go new file mode 100644 index 0000000000..248a95d8a2 --- /dev/null +++ b/cmd/fyne/bundle.go @@ -0,0 +1,72 @@ +// Run a command line helper for various Fyne tools. +package main + +import ( + "flag" + "fmt" + "io/ioutil" + "path" + "strings" + + "github.com/fyne-io/fyne" +) + +func writeResource(file, name string) { + bytes, err := ioutil.ReadFile(file) + if err != nil { + fmt.Println("Unable to load file " + file) + 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 \"github.com/fyne-io/fyne\"") +} + +func sanitiseName(file string) string { + name := strings.Replace(file, ".", "-", -1) + name = strings.Replace(name, " ", "", -1) + + return strings.ToLower(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 Bundle(name, pkg string, noheader bool, filepath string) { + if !noheader { + writeHeader(pkg) + } + fmt.Println() + + if name == "" { + name = sanitiseName(path.Base(filepath)) + } + writeResource(filepath, name) +} + +var name, pkg string +var noheader bool + +func bundleFlags() { + flag.StringVar(&name, "name", "", "The variable name to assign the serialised resource") + flag.StringVar(&pkg, "package", "main", "The package to output in headers (if not appending)") + flag.BoolVar(&noheader, "append", false, "Append an existing go file (don't output headers)") +} + +func bundleRun(args []string) { + if len(args) != 1 { + fmt.Println("Missing required file parameter after flags") + return + } + + Bundle(name, pkg, noheader, args[0]) +} diff --git a/cmd/fyne/main.go b/cmd/fyne/main.go index 888fcbeb13..d4adc27261 100644 --- a/cmd/fyne/main.go +++ b/cmd/fyne/main.go @@ -4,80 +4,34 @@ package main import ( "flag" "fmt" - "io/ioutil" "os" - "path" - "strings" - - "github.com/fyne-io/fyne" ) -func writeResource(file, name string) { - bytes, err := ioutil.ReadFile(file) - if err != nil { - fmt.Println("Unable to load file " + file) - 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 \"github.com/fyne-io/fyne\"") -} - -func sanitiseName(file string) string { - name := strings.Replace(file, ".", "-", -1) - name = strings.Replace(name, " ", "", -1) - - return strings.ToLower(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 Bundle(name, pkg string, noheader bool, filepath string) { - if !noheader { - writeHeader(pkg) - } - fmt.Println() - - if name == "" { - name = sanitiseName(path.Base(filepath)) - } - writeResource(filepath, name) -} - func main() { - var name, pkg string - var noheader bool - flag.StringVar(&name, "name", "", "The variable name to assign the serialised resource") - flag.StringVar(&pkg, "package", "main", "The package to output in headers (if not appending)") - flag.BoolVar(&noheader, "append", false, "Append an existing go file (don't output headers)") - // first let's extract the first "command" args := os.Args[1:] if len(args) < 1 { - fmt.Println("The fyne command requires a command parameter, one of \"bundle\"") - return - } else if args[0] != "bundle" { - fmt.Println("Currently the fyne tool only supports the \"bundle\" command") + fmt.Println("The fyne command requires a command parameter, one of \"bundle\" or \"package\"") return } - // then parse the remaining args - flag.CommandLine.Parse(args[1:]) - args = flag.Args() - if len(args) != 1 { - fmt.Println("Missing required file parameter after flags") + command := args[0] + if command == "bundle" { + bundleFlags() + + // then parse the remaining args + flag.CommandLine.Parse(args[1:]) + bundleRun(flag.Args()) + return + } else if command == "package" { + packageFlags() + + // then parse the remaining args + flag.CommandLine.Parse(args[1:]) + packageRun(flag.Args()) return } - Bundle(name, pkg, noheader, args[0]) + fmt.Println("Unsupported command", command) + return } diff --git a/cmd/fyne/package.go b/cmd/fyne/package.go new file mode 100644 index 0000000000..ece008ed39 --- /dev/null +++ b/cmd/fyne/package.go @@ -0,0 +1,11 @@ +package main + +import "fmt" + +func packageFlags() { + +} + +func packageRun(_ []string) { + fmt.Println("TODO implement packager") +}