Skip to content

Commit

Permalink
Move resource and serilisation code into main package
Browse files Browse the repository at this point in the history
  • Loading branch information
andydotxyz committed Jun 19, 2018
1 parent 1d744a8 commit 1292279
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 29 deletions.
10 changes: 5 additions & 5 deletions api/ui/theme/bundled.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions api/ui/theme/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import "path"
import "runtime"
import "strings"

import "github.com/fyne-io/fyne/resource"
import "github.com/fyne-io/fyne"

const filename = "bundled.go"

Expand Down Expand Up @@ -42,7 +42,7 @@ func main() {
return
}

_, err = f.WriteString("package theme\n\nimport \"github.com/fyne-io/fyne/resource\"\n\n")
_, err = f.WriteString("package theme\n\nimport \"github.com/fyne-io/fyne\"\n\n")
if err != nil {
fmt.Println("Unable to write file " + filename)
return
Expand Down
22 changes: 11 additions & 11 deletions resource/path.go → path.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package resource
package fyne

import "os"
import "path"
import "io/ioutil"
import "os/user"

var rootDir string
var cacheDirPath string

func lookupRoot() string {
func lookupCacheDir() string {
// TODO replace this with io.UserCacheDir once Go 11 is out
user, err := user.Current()
if err == nil {
Expand All @@ -18,19 +18,19 @@ func lookupRoot() string {
return root
}

func rootPath() string {
if rootDir == "" {
rootDir = lookupRoot()
if !pathExists(rootDir) {
os.Mkdir(rootDir, 0700)
func cacheDir() string {
if cacheDirPath == "" {
cacheDirPath = lookupCacheDir()
if !pathExists(cacheDirPath) {
os.Mkdir(cacheDirPath, 0700)
}
}

return rootDir
return cacheDirPath
}

func filePath(name string) string {
return path.Join(rootPath(), name)
func cachePath(name string) string {
return path.Join(cacheDir(), name)
}

func pathExists(path string) bool {
Expand Down
5 changes: 2 additions & 3 deletions resource/resource.go → resource.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Package resource manages bundled assets in Fyne and Fyne applications
package resource
package fyne

// Resource represents a single bundled resource.
// A resource has an identifying name and byte array content.
Expand All @@ -14,7 +13,7 @@ type Resource struct {
// If the resource has not previously been written to a cache this operation
// will block until the data is available at the returned location.
func (r *Resource) CachePath() string {
path := filePath(r.Name)
path := cachePath(r.Name)
if !pathExists(path) {
toFile(r)
}
Expand Down
2 changes: 1 addition & 1 deletion resource/resource_test.go → resource_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resource
package fyne

import "os"

Expand Down
8 changes: 4 additions & 4 deletions resource/serialise.go → serialise.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package resource
package fyne

import "bytes"
import "fmt"
import "io/ioutil"

func fromFile(name string) *Resource {
data, err := ioutil.ReadFile(filePath(name))
data, err := ioutil.ReadFile(cachePath(name))

if err != nil {
return nil
Expand All @@ -15,15 +15,15 @@ func fromFile(name string) *Resource {
}

func toFile(res *Resource) {
ioutil.WriteFile(filePath(res.Name), res.Content, 0644)
ioutil.WriteFile(cachePath(res.Name), res.Content, 0644)
}

// ToGo converts a Resource object to Go code.
// This is useful if serialising to a go file for compilation into a binary
func ToGo(res *Resource) string {
var buffer bytes.Buffer

buffer.WriteString("&resource.Resource{\n")
buffer.WriteString("&fyne.Resource{\n")
buffer.WriteString("\tName: \"" + res.Name + "\",\n")
buffer.WriteString("\tContent: []byte{")
for i, v := range res.Content {
Expand Down
6 changes: 3 additions & 3 deletions resource/serialise_test.go → serialise_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resource
package fyne

import "os"

Expand All @@ -7,10 +7,10 @@ import "github.com/stretchr/testify/assert"

var imgName = "dot.png"
var imgBytes = []byte{137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 1, 0, 0, 0, 1, 8, 2, 0, 0, 0, 144, 119, 83, 222, 0, 0, 0, 9, 112, 72, 89, 115, 0, 0, 11, 19, 0, 0, 11, 19, 1, 0, 154, 156, 24, 0, 0, 0, 7, 116, 73, 77, 69, 7, 226, 6, 5, 15, 49, 19, 102, 121, 20, 234, 0, 0, 0, 29, 105, 84, 88, 116, 67, 111, 109, 109, 101, 110, 116, 0, 0, 0, 0, 0, 67, 114, 101, 97, 116, 101, 100, 32, 119, 105, 116, 104, 32, 71, 73, 77, 80, 100, 46, 101, 7, 0, 0, 0, 12, 73, 68, 65, 84, 8, 215, 99, 248, 207, 192, 0, 0, 3, 1, 1, 0, 24, 221, 141, 176, 0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130}
var imgGo = "&resource.Resource{\n\tName: \"dot.png\",\n\tContent: []byte{137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 1, 0, 0, 0, 1, 8, 2, 0, 0, 0, 144, 119, 83, 222, 0, 0, 0, 9, 112, 72, 89, 115, 0, 0, 11, 19, 0, 0, 11, 19, 1, 0, 154, 156, 24, 0, 0, 0, 7, 116, 73, 77, 69, 7, 226, 6, 5, 15, 49, 19, 102, 121, 20, 234, 0, 0, 0, 29, 105, 84, 88, 116, 67, 111, 109, 109, 101, 110, 116, 0, 0, 0, 0, 0, 67, 114, 101, 97, 116, 101, 100, 32, 119, 105, 116, 104, 32, 71, 73, 77, 80, 100, 46, 101, 7, 0, 0, 0, 12, 73, 68, 65, 84, 8, 215, 99, 248, 207, 192, 0, 0, 3, 1, 1, 0, 24, 221, 141, 176, 0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130}}"
var imgGo = "&fyne.Resource{\n\tName: \"dot.png\",\n\tContent: []byte{137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 1, 0, 0, 0, 1, 8, 2, 0, 0, 0, 144, 119, 83, 222, 0, 0, 0, 9, 112, 72, 89, 115, 0, 0, 11, 19, 0, 0, 11, 19, 1, 0, 154, 156, 24, 0, 0, 0, 7, 116, 73, 77, 69, 7, 226, 6, 5, 15, 49, 19, 102, 121, 20, 234, 0, 0, 0, 29, 105, 84, 88, 116, 67, 111, 109, 109, 101, 110, 116, 0, 0, 0, 0, 0, 67, 114, 101, 97, 116, 101, 100, 32, 119, 105, 116, 104, 32, 71, 73, 77, 80, 100, 46, 101, 7, 0, 0, 0, 12, 73, 68, 65, 84, 8, 215, 99, 248, 207, 192, 0, 0, 3, 1, 1, 0, 24, 221, 141, 176, 0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130}}"

func TestToFromFile(t *testing.T) {
path := filePath(imgName)
path := cachePath(imgName)

res := NewResource(imgName, imgBytes)
toFile(res)
Expand Down

0 comments on commit 1292279

Please sign in to comment.