Skip to content

Commit

Permalink
Refactoring to insert the package sub-command
Browse files Browse the repository at this point in the history
  • Loading branch information
andydotxyz committed Jan 2, 2019
1 parent 077a661 commit f1a5417
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 63 deletions.
72 changes: 72 additions & 0 deletions cmd/fyne/bundle.go
Original file line number Diff line number Diff line change
@@ -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])
}
80 changes: 17 additions & 63 deletions cmd/fyne/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
11 changes: 11 additions & 0 deletions cmd/fyne/package.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

import "fmt"

func packageFlags() {

}

func packageRun(_ []string) {
fmt.Println("TODO implement packager")
}

0 comments on commit f1a5417

Please sign in to comment.