From 4ee8fd52d1f8137e42ea2c44d453ae36f46ab5db Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Mon, 9 Aug 2021 13:44:50 +0200 Subject: [PATCH] Replaced go/embed with rice again. --- .gitignore | 1 + README.md | 26 ++++++++++++++++++++++++++ cmd/tins2021/appcontext.go | 10 +++++----- cmd/tins2021/credits.go | 2 ++ cmd/tins2021/extract.go | 15 ++++++++------- cmd/tins2021/tins2021.go | 15 ++++++++------- 6 files changed, 50 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index 9f1521f..4b59098 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .vscode/launch.json scripts/build +cmd/tins2021/rice-box.go diff --git a/README.md b/README.md index 34573f7..77c0158 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ * [go-colurful](###go-colurful) * [resize](###resize) * [testify](###testify) + * [go.rice](###rice) ## Introduction @@ -387,3 +388,28 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +### rice + +Copyright (c) 2013, Geert-Johan Riemer +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/cmd/tins2021/appcontext.go b/cmd/tins2021/appcontext.go index 26689cf..f2bf637 100644 --- a/cmd/tins2021/appcontext.go +++ b/cmd/tins2021/appcontext.go @@ -16,10 +16,10 @@ type appContext struct { } func newAppContext(ctx ui.Context, setView func(ui.Control)) *appContext { - newAnimatedTexture(ctx, "star", "resources/images/star.png", func() image.Image { + newAnimatedTexture(ctx, "star", "images/star.png", func() image.Image { return tins2021.AnimatePolygon(tins2021.CreateStar(5), tins2021.Yellow, tins2021.NewRotateAnimation(defaultAnimationFrames)) }) - newAnimatedTexture(ctx, "heart", "resources/images/heart.png", func() image.Image { + newAnimatedTexture(ctx, "heart", "images/heart.png", func() image.Image { return tins2021.AnimatePolygon(tins2021.CreateHeart(), tins2021.Red, tins2021.NewRotateAnimation(defaultAnimationFrames)) }) @@ -31,13 +31,13 @@ func newAppContext(ctx ui.Context, setView func(ui.Control)) *appContext { tins2021.MonsterTypeChaser: "chasing-monster", }, } - newAnimatedTexture(ctx, app.MonsterTextureNames[tins2021.MonsterTypeStraight], "resources/images/monster-straight.png", func() image.Image { + newAnimatedTexture(ctx, app.MonsterTextureNames[tins2021.MonsterTypeStraight], "images/monster-straight.png", func() image.Image { return tins2021.AnimatePolygon(tins2021.CreateHexagon(), tins2021.Green, tins2021.NewWobbleAnimation(defaultAnimationFrames, 30)) }) - newAnimatedTexture(ctx, app.MonsterTextureNames[tins2021.MonsterTypeRandom], "resources/images/monster-random.png", func() image.Image { + newAnimatedTexture(ctx, app.MonsterTextureNames[tins2021.MonsterTypeRandom], "images/monster-random.png", func() image.Image { return tins2021.AnimatePolygon(tins2021.CreateHexagon(), tins2021.Blue, tins2021.NewWobbleAnimation(defaultAnimationFrames, 30)) }) - newAnimatedTexture(ctx, app.MonsterTextureNames[tins2021.MonsterTypeChaser], "resources/images/monster-chaser.png", func() image.Image { + newAnimatedTexture(ctx, app.MonsterTextureNames[tins2021.MonsterTypeChaser], "images/monster-chaser.png", func() image.Image { return tins2021.AnimatePolygon(tins2021.CreateHexagon(), tins2021.Purple, tins2021.NewWobbleAnimation(defaultAnimationFrames, 30)) }) return app diff --git a/cmd/tins2021/credits.go b/cmd/tins2021/credits.go index b171743..620b38b 100644 --- a/cmd/tins2021/credits.go +++ b/cmd/tins2021/credits.go @@ -58,6 +58,8 @@ func newCredits(app *appContext, ctx ui.Context) *credits { " - https://github.com/nfnt/resize", "", "testify: a testing library for Go", " - https://github.com/stretchr/testify", "", + "rice: a library for embedding files in Go", + " - https://github.com/GeertJohan/go.rice", "", "", "# THANKS", "", diff --git a/cmd/tins2021/extract.go b/cmd/tins2021/extract.go index 66dc907..eca51be 100644 --- a/cmd/tins2021/extract.go +++ b/cmd/tins2021/extract.go @@ -1,11 +1,12 @@ package main import ( - "embed" "io" "io/fs" "os" "path/filepath" + + rice "github.com/GeertJohan/go.rice" ) func copyFile(path string, file fs.File) error { @@ -20,19 +21,19 @@ func copyFile(path string, file fs.File) error { return err } -func copyBoxToDisk(resources embed.FS) error { - return fs.WalkDir(resources, "", func(path string, entry fs.DirEntry, err error) error { +func copyBoxToDisk() error { + box := rice.MustFindBox(`resources`) + return box.Walk("", func(path string, info os.FileInfo, err error) error { if err != nil { return err } - if entry.IsDir() { + if info.IsDir() { return nil } - src, err := resources.Open(path) + src, err := box.Open(path) if err != nil { return err } - defer src.Close() - return copyFile(filepath.Join(`resources`, path), src) + return copyFile(filepath.Join(box.Name(), path), src) }) } diff --git a/cmd/tins2021/tins2021.go b/cmd/tins2021/tins2021.go index 2378dae..8deeb19 100644 --- a/cmd/tins2021/tins2021.go +++ b/cmd/tins2021/tins2021.go @@ -1,20 +1,20 @@ package main import ( - "embed" "flag" "image/color" "log" + rice "github.com/GeertJohan/go.rice" "opslag.de/schobers/geom" "opslag.de/schobers/tins2021" "opslag.de/schobers/zntg" - "opslag.de/schobers/zntg/addons/embedres" + "opslag.de/schobers/zntg/addons/riceres" "opslag.de/schobers/zntg/ui" ) -//go:embed resources/* -var resources embed.FS +//go:generate go get -u github.com/GeertJohan/go.rice/rice +//go:generate rice embed func main() { err := run() @@ -24,8 +24,9 @@ func main() { } func openResources() ui.Resources { - embedded := embedres.New(resources) - return ui.NewFallbackResources(ui.NewPathResources(nil, `resources`), embedded) + box := rice.MustFindBox(`resources`) + embedded := riceres.New(box) + return ui.NewFallbackResources(ui.NewPathResources(nil, box.Name()), embedded) } func run() error { @@ -34,7 +35,7 @@ func run() error { flag.Parse() if extract { - return copyBoxToDisk(resources) + return copyBoxToDisk() } res := openResources()