Skip to content

Commit

Permalink
Merge pull request #29 from tmck-code/glow-up
Browse files Browse the repository at this point in the history
Glow up
  • Loading branch information
tmck-code authored Apr 13, 2022
2 parents 37f71be + 4abd49b commit 64af60f
Show file tree
Hide file tree
Showing 14 changed files with 273 additions and 154 deletions.
15 changes: 5 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,24 +98,19 @@ In order to re/build the binaries from scratch, along with all the cowfile conve
Makefile tasks
```shell
cd build && make build/docker build/release
cd build && make build/docker build/assets build/release
```

This will produce 4 executable bin files inside the build/ directory
This will produce 4 executable bin files inside the `build/bin` directory, and a heap of binary asset files in `build/assets`.

### On your host OS

You'll have to install a golang version that matches the go.mod, and ensure that other package
dependencies are installed (see the dockerfile for the dependencies)

```
# Build the pokedex build tool
go build src/pokedex.go
# Extract the cowfiles into a directory
tar xzf build/cows.tar.gz -C build/
# Generate a encoding/gob data file from the cowfiles
./pokedex -from build/cows -to build/pokedex.gob
```shell
# Generate binary asset files from the cowfiles
./build/build_assets.sh

# Finally, build the pokesay tool (this builds and uses the build/pokedex.gob file automatically)
go build pokesay.go
Expand Down
14 changes: 4 additions & 10 deletions build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ RUN apt-get update \
RUN git clone --depth 1 https://github.com/denilsonsa/img2xterm \
&& (cd img2xterm && make && make install)

ENV DOCKER_BUILD_DIR /tmp/original
ENV DOCKER_OUTPUT_DIR /tmp/cows

RUN mkdir -p /tmp/original /tmp/cows
WORKDIR /tmp/original

RUN git clone --depth 1 https://github.com/msikma/pokesprite
Expand All @@ -23,13 +19,11 @@ ADD go.* /usr/local/src/
RUN go mod tidy \
&& go get -v github.com/mitchellh/go-wordwrap

ADD . .
# Convert all of the pokesprite .pngs -> cowfiles for the terminal
# build up a map of category -> pokemon, and write it as a "encoding/gob" file
RUN go run /usr/local/src/src/pokedex.go \
RUN go run /usr/local/src/src/bin/convert/png_convert.go \
-from /tmp/original/pokesprite/ \
-to /tmp/cows/ \
-skip '["resources/", "misc/", "icons/", "items/", "items-outline/"]' \
-toCategoryFpath "build/pokedex.gob"
-padding 4 \
-skip '["resources/", "misc/", "icons/", "items/", "items-outline/"]'

RUN build/build.sh
ADD . .
29 changes: 14 additions & 15 deletions build/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,30 @@ DOCKER_OUTPUT_DIR ?= /tmp/cows
all: build/docker build/cows build/release

build/docker:
docker build \
-f Dockerfile \
-t pokesay-go:latest ..
docker build -f Dockerfile -t pokesay-go:latest ..

build/cows:
@rm -rf cows.tar.gz cows/
docker create \
--name pokebuilder \
pokesay-go:latest
docker rm -f pokebuilder
docker create --name pokebuilder pokesay-go:latest
@docker cp pokebuilder:$(DOCKER_OUTPUT_DIR)/ cows/
@tar czf cows.tar.gz cows/
@rm -rf cows/
@docker rm -f pokebuilder
@du -sh cows.tar.gz

install: build/bin
cp -v pokesay-$(TARGET_GOOS)-$(TARGET_GOARCH) $(HOME)/bin/pokesay
# generate embedded bin files for category/metadata/the actual pokemon
build/assets:
docker run \
-v $(PWD)/assets:/usr/local/src/build/assets \
--rm --name pokesay pokesay-go:latest \
build/build_assets.sh

build/release:
@docker create --name pokesay pokesay-go:latest
docker cp pokesay:/usr/local/src/pokesay-linux-amd64 .
docker cp pokesay:/usr/local/src/pokesay-darwin-amd64 .
docker cp pokesay:/usr/local/src/pokesay-darwin-arm64 .
docker cp pokesay:/usr/local/src/pokesay-windows-amd64.exe .
docker cp pokesay:/usr/local/src/pokesay-android-arm64 .
@docker rm -f pokesay
docker run \
-v $(PWD)/bin:/usr/local/src/build/bin \
-v $(PWD)/assets:/usr/local/src/build/assets \
--rm --name pokesay pokesay-go:latest build/build.sh
tree bin/

.PHONY: all build/docker build/cows install build/release
11 changes: 6 additions & 5 deletions build/build.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#!/bin/bash

set -euo pipefail
set -euxo pipefail

OUTPUT_DIR="build/bin"
mkdir -p "$OUTPUT_DIR"

function build() {
echo "building $1 / $2"
GOOS=$1 GOARCH=$2 go build -o bin/pokesay-${1}-${2}${3:-} pokesay.go
GOOS=$1 GOARCH=$2 go build -o "${OUTPUT_DIR}/pokesay-${1}-${2}${3:-}" pokesay.go
}

mkdir bin/

build darwin amd64 &
build darwin arm64 &
build linux amd64 &
build windows amd64 .exe &
build android arm64 &
wait
wait
15 changes: 15 additions & 0 deletions build/build_assets.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

set -euo pipefail

tar xzf build/cows.tar.gz
go run ./src/bin/pokedex/pokedex.go \
-from ./cows/ \
-to ./build/assets/ \
-toCategoryFpath pokedex.gob \
-toDataSubDir cows/ \
-toMetadataSubDir metadata/ \
-toTotalFname total.txt

rm -rf cows
ls -alh build/assets
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ require (
)

require (
github.com/fatih/color v1.13.0 // indirect
github.com/mattn/go-colorable v0.1.9 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/rivo/uniseg v0.2.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw=
github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
Expand All @@ -21,6 +27,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 h1:HWj/xjIHfjYU5nVXpTM0s39J9CbLn7Cc5a7IC5rwsMQ=
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
29 changes: 21 additions & 8 deletions pokesay.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,26 @@ import (
"strings"
"time"

"github.com/mitchellh/go-wordwrap"
"github.com/fatih/color"
"github.com/tmck-code/pokesay-go/src/pokedex"

"github.com/mitchellh/go-wordwrap"
)

var (
//go:embed build/pokedex.gob
//go:embed build/assets/pokedex.gob
GOBCategory []byte
//go:embed build/total.txt
//go:embed build/assets/total.txt
GOBTotal []byte
//go:embed build/*cow
//go:embed build/assets/cows/*cow
GOBCowData embed.FS
//go:embed build/*metadata
//go:embed build/assets/metadata/*metadata
GOBCowNames embed.FS

Rand rand.Source = rand.NewSource(time.Now().UnixNano())

textStyleItalic *color.Color = color.New(color.Italic)
textStyleBold *color.Color = color.New(color.Bold)
)

func check(e error) {
Expand Down Expand Up @@ -76,8 +81,16 @@ func printSpeechBubble(scanner *bufio.Scanner, args Args) {
}

func printPokemon(index int, name string, categoryKeys []string) {
d, _ := GOBCowData.ReadFile(pokedex.EntryFpath(index))
fmt.Printf("%schoice: %s / categories: %s\n", pokedex.Decompress(d), name, categoryKeys)
d, _ := GOBCowData.ReadFile(pokedex.EntryFpath("build/assets/cows", index))

fmt.Printf(
"%s%s: %s | %s: %s\n",
pokedex.Decompress(d),
"choice",
textStyleBold.Sprint(name),
"categories",
textStyleItalic.Sprint(strings.Join(categoryKeys, ", ")),
)
}

func chooseRandomCategory(keys [][]string, categories pokedex.PokemonTrie) ([]string, []*pokedex.PokemonEntry) {
Expand Down Expand Up @@ -169,7 +182,7 @@ func runPrintByCategory(args Args, categories pokedex.PokemonTrie) {
func runPrintRandom(args Args) {
total, _ := strconv.Atoi(string(GOBTotal))
choice := randomInt(total)
m, err := GOBCowNames.ReadFile(pokedex.MetadataFpath(choice))
m, err := GOBCowNames.ReadFile(pokedex.MetadataFpath("build/assets/metadata", choice))
check(err)
metadata := pokedex.ReadMetadataFromBytes(m)

Expand Down
67 changes: 67 additions & 0 deletions src/bin/convert/png_convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"log"
"os"

"github.com/tmck-code/pokesay-go/src/bin"
"github.com/tmck-code/pokesay-go/src/pokedex"
)

func check(e error) {
if e != nil {
log.Fatal(e)
}
}

var (
DEBUG bool = false
)

type CowBuildArgs struct {
FromDir string
ToDir string
SkipDirs []string
Padding int
Debug bool
}

func parseArgs() CowBuildArgs {
fromDir := flag.String("from", ".", "from dir")
toDir := flag.String("to", ".", "to dir")
skipDirs := flag.String("skip", "'[\"resources\"]'", "JSON array of dir patterns to skip converting")
padding := flag.Int("padding", 2, "the number of spaces to pad from the left")
debug := flag.Bool("debug", DEBUG, "show debug logs")

flag.Parse()

DEBUG = *debug

args := CowBuildArgs{FromDir: *fromDir, ToDir: *toDir, Padding: *padding}
json.Unmarshal([]byte(*skipDirs), &args.SkipDirs)

if DEBUG {
fmt.Println("%+v", args)
}
return args
}

func main() {
args := parseArgs()

fpaths := pokedex.FindFiles(args.FromDir, ".png", args.SkipDirs)

// Ensure that the destination dir exists
os.MkdirAll(args.ToDir, 0755)

fmt.Println("Converting PNGs -> cowfiles")
pbar := bin.NewProgressBar(len(fpaths))
for _, f := range fpaths {
pokedex.ConvertPngToCow(args.FromDir, f, args.ToDir, args.Padding)
pbar.Add(1)
}
fmt.Println("Finished converting", len(fpaths), "pokesprite PNGs", "-> cowfiles")
}
90 changes: 90 additions & 0 deletions src/bin/pokedex/pokedex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package main

import (
"flag"
"fmt"
"log"
"os"
"path"
"strconv"

"github.com/tmck-code/pokesay-go/src/bin"
"github.com/tmck-code/pokesay-go/src/pokedex"
)

func check(e error) {
if e != nil {
log.Fatal(e)
}
}

type PokedexArgs struct {
FromDir string
ToDir string
Debug bool
ToCategoryFname string
ToDataSubDir string
ToMetadataSubDir string
ToTotalFname string
}

func parseArgs() PokedexArgs {
fromDir := flag.String("from", "/tmp/cows", "from dir")
toDir := flag.String("to", "build/assets/", "to dir")

toDataSubDir := flag.String("toDataSubDir", "cows/", "dir to write all binary (image) data to")
toMetadataSubDir := flag.String("toMetadataSubDir", "metadata/", "dir to write all binary (metadata) data to")
toCategoryFname := flag.String("toCategoryFpath", "pokedex.gob", "to fpath")
toTotalFname := flag.String("toTotalFname", "total.txt", "file to write the number of available entries to")
debug := flag.Bool("debug", false, "show debug logs")

flag.Parse()

args := PokedexArgs{
FromDir: pokedex.NormaliseRelativeDir(*fromDir),
ToDir: pokedex.NormaliseRelativeDir(*toDir),
ToCategoryFname: *toCategoryFname,
ToDataSubDir: pokedex.NormaliseRelativeDir(*toDataSubDir),
ToMetadataSubDir: pokedex.NormaliseRelativeDir(*toMetadataSubDir),
ToTotalFname: *toTotalFname,
Debug: *debug,
}
if args.Debug {
fmt.Printf("%+v\n", args)
}
return args
}

func main() {
args := parseArgs()

totalFpath := path.Join(args.ToDir, args.ToTotalFname)
categoryFpath := path.Join(args.ToDir, args.ToCategoryFname)

fpaths := pokedex.FindFiles(args.FromDir, ".cow", make([]string, 0))

err := os.MkdirAll(path.Join(args.ToDir, args.ToDataSubDir), 0755)
check(err)
err = os.MkdirAll(path.Join(args.ToDir, args.ToMetadataSubDir), 0755)
check(err)

// categories is a PokemonTrie struct that will be written to a file using encoding/gob
// metadata is a list of pokemon data and an index to use when writing them to a file
// - this index matches a corresponding one in the categories struct
// - these files are embedded into the build binary using go:embed and then loaded at runtime
categories, metadata := pokedex.CreateMetadata(args.FromDir, fpaths, args.Debug)

pokedex.WriteStructToFile(categories, categoryFpath)

fmt.Println("\nConverting cowfiles -> category & metadata GOB")
pbar := bin.NewProgressBar(len(fpaths))
for _, m := range metadata {
pokedex.WriteBytesToFile(m.Data, pokedex.EntryFpath(path.Join(args.ToDir, args.ToDataSubDir), m.Index), true)
pokedex.WriteStructToFile(m.Metadata, pokedex.MetadataFpath(path.Join(args.ToDir, args.ToMetadataSubDir), m.Index))
pbar.Add(1)
}
pokedex.WriteBytesToFile([]byte(strconv.Itoa(len(metadata))), totalFpath, false)

fmt.Println("Finished converting", len(fpaths), "pokesprite -> cowfiles")
fmt.Println("Wrote categories to", path.Join(args.ToDir, args.ToCategoryFname))
}
Loading

0 comments on commit 64af60f

Please sign in to comment.