Skip to content

Commit

Permalink
Merge pull request #59 from tmck-code/assorted-cleanup
Browse files Browse the repository at this point in the history
Assorted cleanup
  • Loading branch information
tmck-code authored Mar 19, 2024
2 parents f1aba52 + 372f136 commit c461547
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 52 deletions.
14 changes: 1 addition & 13 deletions pokesay.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,6 @@ func parseFlags() pokesay.Args {
return args
}

func EntryFpath(idx int) string {
return pokedex.EntryFpath(CowDataRoot, idx)
}

func MetadataFpath(idx int) string {
return pokedex.MetadataFpath(MetadataRoot, idx)
}

func CategoryFpath(category string, fname string) string {
return pokedex.CategoryFpath(CategoryRoot, category, fname)
}

func runListCategories() {
categories := pokedex.ReadStructFromBytes[[]string](GOBCategoryKeys)
fmt.Printf("%s\n%d %s\n", strings.Join(categories, " "), len(categories), "total categories")
Expand Down Expand Up @@ -170,7 +158,7 @@ func runPrintRandom(args pokesay.Args) {
t := timer.NewTimer("runPrintRandom", true)
choice := pokesay.RandomInt(pokedex.ReadIntFromBytes(GOBTotal))
t.Mark("choose index")
metadata := pokedex.ReadMetadataFromEmbedded(GOBCowNames, MetadataFpath(choice))
metadata := pokedex.ReadMetadataFromEmbedded(GOBCowNames, pokedex.MetadataFpath(MetadataRoot, choice))
t.Mark("read metadata")

final := metadata.Entries[pokesay.RandomInt(len(metadata.Entries))]
Expand Down
7 changes: 0 additions & 7 deletions src/bin/convert/png_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,12 @@ import (
"encoding/json"
"flag"
"fmt"
"log"
"os"

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

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

var (
DEBUG bool = false
)
Expand Down
42 changes: 23 additions & 19 deletions src/bin/pokedex/pokedex.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"flag"
"fmt"
"log"
"os"
"path"
"strings"
Expand All @@ -12,12 +11,6 @@ import (
"github.com/tmck-code/pokesay/src/pokedex"
)

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

// Strips the leading "./" from a path e.g. "./cows/ -> cows/"
func normaliseRelativeDir(dirPath string) string {
return strings.TrimPrefix(dirPath, "./")
Expand All @@ -33,6 +26,20 @@ type PokedexArgs struct {
ToTotalFname string
}

type PokedexPaths struct {
EntryDirPath string
MetadataDirPath string
TotalFpath string
}

func NewPokedexPaths(args PokedexArgs) PokedexPaths {
return PokedexPaths{
EntryDirPath: path.Join(args.ToDir, args.ToDataSubDir),
MetadataDirPath: path.Join(args.ToDir, args.ToMetadataSubDir),
TotalFpath: path.Join(args.ToDir, args.ToTotalFname),
}
}

func parseArgs() PokedexArgs {
fromDir := flag.String("from", "/tmp/cows", "from dir")
fromMetadataFname := flag.String("fromMetadata", "/tmp/cows/pokemon.json", "metadata file")
Expand Down Expand Up @@ -63,7 +70,7 @@ func parseArgs() PokedexArgs {
func mkDirs(dirPaths []string) {
for _, dirPath := range dirPaths {
err := os.MkdirAll(dirPath, 0755)
check(err)
pokedex.Check(err)
}
}

Expand All @@ -81,13 +88,10 @@ func mkDirs(dirPaths []string) {
// - contains the total number of pokemon files, used for random selection
func main() {
args := parseArgs()

totalFpath := path.Join(args.ToDir, args.ToTotalFname)
entryDirPath := path.Join(args.ToDir, args.ToDataSubDir)
metadataDirPath := path.Join(args.ToDir, args.ToMetadataSubDir)
paths := NewPokedexPaths(args)

// ensure that the destination directories exist
mkDirs([]string{entryDirPath, metadataDirPath})
mkDirs([]string{paths.EntryDirPath, paths.MetadataDirPath})

// Find all the cowfiles
cowfileFpaths := pokedex.FindFiles(args.FromDir, ".cow", make([]string, 0))
Expand All @@ -102,7 +106,7 @@ func main() {
data, err := os.ReadFile(fpath)
pokedex.Check(err)

pokedex.WriteBytesToFile(data, pokedex.EntryFpath(entryDirPath, i), true)
pokedex.WriteBytesToFile(data, pokedex.EntryFpath(paths.EntryDirPath, i), true)
pbar.Add(1)
}

Expand All @@ -115,7 +119,7 @@ func main() {
pbar = bin.NewProgressBar(len(pokemonNames))
for key, name := range pokemonNames {
metadata := pokedex.CreateNameMetadata(i, key, name, args.FromDir, cowfileFpaths)
pokedex.WriteStructToFile(metadata, pokedex.MetadataFpath(metadataDirPath, i))
pokedex.WriteStructToFile(metadata, pokedex.MetadataFpath(paths.MetadataDirPath, i))
pokemonMetadata = append(pokemonMetadata, *metadata)
uniqueNames[name.Slug] = append(uniqueNames[name.Slug], i)
i++
Expand All @@ -130,12 +134,12 @@ func main() {
pokedex.WriteStructToFile(categories, "build/assets/category_keys.txt")

fmt.Println("- Writing total metadata to file")
pokedex.WriteIntToFile(len(pokemonMetadata), totalFpath)
pokedex.WriteIntToFile(len(pokemonMetadata), paths.TotalFpath)

fmt.Println("✓ Complete! Indexed", len(cowfileFpaths), "total cowfiles")
fmt.Println("wrote", i, "names to", "build/assets/names.txt")

fmt.Println("✓ Wrote gzipped metadata to", metadataDirPath)
fmt.Println("✓ Wrote gzipped cowfiles to", entryDirPath)
fmt.Println("✓ Wrote 'total' metadata to", totalFpath, len(pokemonMetadata))
fmt.Println("✓ Wrote gzipped metadata to", paths.MetadataDirPath)
fmt.Println("✓ Wrote gzipped cowfiles to", paths.EntryDirPath)
fmt.Println("✓ Wrote 'total' metadata to", paths.TotalFpath, len(pokemonMetadata))
}
4 changes: 2 additions & 2 deletions src/pokesay/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func ChooseByCategory(category string, categoryDir []fs.DirEntry, categoryFiles
categoryMetadata, err := categoryFiles.ReadFile(
pokedex.CategoryFpath(categoryRootDir, category, choice.Name()),
)
Check(err)
pokedex.Check(err)

parts := strings.Split(string(categoryMetadata), "/")

Expand All @@ -55,7 +55,7 @@ func ChooseByCategory(category string, categoryDir []fs.DirEntry, categoryFiles
)

entryIndex, err := strconv.Atoi(string(parts[1]))
Check(err)
pokedex.Check(err)

return metadata, metadata.Entries[entryIndex]
}
Expand Down
9 changes: 0 additions & 9 deletions src/pokesay/utils.go

This file was deleted.

3 changes: 1 addition & 2 deletions test/pokedex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"testing"

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

var (
Expand All @@ -32,7 +31,7 @@ func TestReadEntry(test *testing.T) {
result := pokedex.ReadPokemonCow(GOBCowData, "data/cows/1.cow")

expected, err := os.ReadFile("data/cows/egg.cow")
pokesay.Check(err)
pokedex.Check(err)

Assert(expected, result, test)
}
Expand Down

0 comments on commit c461547

Please sign in to comment.