Skip to content

Commit

Permalink
util -> cmd.
Browse files Browse the repository at this point in the history
  • Loading branch information
schwarzlichtbezirk committed Nov 27, 2024
1 parent eaed1b2 commit abed437
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 58 deletions.
11 changes: 6 additions & 5 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Checkout
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: '1.20'

- name: Build utilities
run: |
go build -v ./util/build
go build -v ./util/extract
go build -v ./util/pack
go build -v ./cmd/build
go build -v ./cmd/extract
go build -v ./cmd/pack
- name: Test wpk & luawpk
run: go test -v . ./luawpk
25 changes: 23 additions & 2 deletions .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,31 @@
-- see: https://github.com/lunarmodules/luacheck

globals = {
"wpk", "path",
"log", "checkfile", "bin2hex", "hex2bin", "milli2time", "time2milli",
-- tables
"wpk",
}

read_globals = {
-- variables
"buildvers", "buildtime", "bindir", "scrdir", "tmpdir",
-- tables
"path",
-- functions
"log", "checkfile", "bin2hex", "hex2bin", "milli2time", "time2milli",
}

std = { -- Lua 5.1 & GopherLua
read_globals = {
-- basic functions
"assert", "collectgarbage", "dofile", "error", "_G", "getfenv",
"getmetatable", "ipairs", "load", "loadfile", "loadstring",
"next", "pairs", "pcall", "print",
"rawequal", "rawget", "rawset", "select", "setfenv", "setmetatable",
"tonumber", "tostring", "type", "unpack", "_VERSION", "xpcall",
"module", "require",
"goto", -- GopherLua
-- basic libraries
"coroutine", "debug", "io", "math", "os", "package", "string", "table",
"channel" -- GopherLua
}
}
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ Package writer with package building process scripting using [Lua 5.1]([https://
* **wpk/testdata**
Contains some Lua-scripts to test **`wpk/luawpk`** module and learn scripting API opportunities.

* **wpk/util/pack**
* **wpk/cmd/pack**
Small simple utility designed to pack a directory, or a list of directories into an package.

* **wpk/util/extract**
* **wpk/cmd/extract**
Small simple utility designed to extract all packed files from package, or list of packages to given directory.

* **wpk/util/build**
* **wpk/cmd/build**
Utility for the packages programmable building, based on **`wpk/luawpk`** module.

Compiled binaries of utilities can be downloaded in [Releases](https://github.com/schwarzlichtbezirk/wpk/releases) section.
Expand All @@ -69,23 +69,23 @@ go get github.com/schwarzlichtbezirk/wpk
Then you can make simple package with files at [testdata/media](https://github.com/schwarzlichtbezirk/wpk/tree/master/testdata/media) directory by command:

```batch
go run github.com/schwarzlichtbezirk/wpk/util/pack --src=${GOPATH}/src/github.com/schwarzlichtbezirk/wpk/testdata/media --dst=${GOPATH}/bin/media.wpk
go run github.com/schwarzlichtbezirk/wpk/cmd/pack --src=${GOPATH}/src/github.com/schwarzlichtbezirk/wpk/testdata/media --dst=${GOPATH}/bin/media.wpk
```

It's runs utility that receives source directory full path and destination package full path. `${GOPATH}` at command line directory path replaced by `GOPATH` environment variable value. To place any other environment variable `VAR` you can by `${VAR}`. In this sample package placed into `bin` directory with other compiled golang binary files.

To extract files from this `media.wpk` package run command:

```batch
go run github.com/schwarzlichtbezirk/wpk/util/extract --md --src=${GOPATH}/bin/media.wpk --dst=${GOPATH}/bin/media
go run github.com/schwarzlichtbezirk/wpk/cmd/extract --md --src=${GOPATH}/bin/media.wpk --dst=${GOPATH}/bin/media
```

and see files in directory `${GOPATH}/bin/media`.

To build package at development workflow you can by **`build`** utility. It can put files into package from any different paths with given names, and bind addition tags to each file, such as MIME types, keywords, CRC, MD5, SHA256 and others. Run this command to see how its work:

```batch
go run github.com/schwarzlichtbezirk/wpk/util/build ${GOPATH}/src/github.com/schwarzlichtbezirk/wpk/testdata/build.lua
go run github.com/schwarzlichtbezirk/wpk/cmd/build ${GOPATH}/src/github.com/schwarzlichtbezirk/wpk/testdata/build.lua
```

and see `build.wpk` file in binary directory near compiled output.
Expand All @@ -111,13 +111,13 @@ Package can be splitted in two files: 1) file with header and tags table, `.wpt`
[step1.lua](https://github.com/schwarzlichtbezirk/wpk/blob/master/testdata/step1.lua) and [step2.lua](https://github.com/schwarzlichtbezirk/wpk/blob/master/testdata/step2.lua) scripts shows sample how to create new package at *step1*:

```batch
go run github.com/schwarzlichtbezirk/wpk/util/build ${GOPATH}/src/github.com/schwarzlichtbezirk/wpk/testdata/step1.lua
go run github.com/schwarzlichtbezirk/wpk/cmd/build ${GOPATH}/src/github.com/schwarzlichtbezirk/wpk/testdata/step1.lua
```

and later append to exising package new files at *step2* call:

```batch
go run github.com/schwarzlichtbezirk/wpk/util/build ${GOPATH}/src/github.com/schwarzlichtbezirk/wpk/testdata/step2.lua
go run github.com/schwarzlichtbezirk/wpk/cmd/build ${GOPATH}/src/github.com/schwarzlichtbezirk/wpk/testdata/step2.lua
```

[packdir.lua](https://github.com/schwarzlichtbezirk/wpk/blob/master/testdata/packdir.lua) script has function that can be used to put to package directory with original tree hierarchy.
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 2 additions & 4 deletions luawpk/luapath.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ import (
lua "github.com/yuin/gopher-lua"
)

// OpenPath registers "path" namespace into Lua virtual machine.
func OpenPath(ls *lua.LState) int {
// RegPath registers "path" namespace into Lua virtual machine.
func RegPath(ls *lua.LState) {
var mod = ls.RegisterModule("path", pathfuncs).(*lua.LTable)
mod.RawSetString("sep", lua.LString("/"))
ls.Push(mod)
return 1
}

var pathfuncs = map[string]lua.LGFunction{
Expand Down
30 changes: 21 additions & 9 deletions luawpk/runlvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,24 @@ func luatime2milli(ls *lua.LState) int {
return 1
}

// RunLuaVM runs specified Lua-script with Lua WPK API.
func RunLuaVM(fpath string) (err error) {
var ls = lua.NewState()
defer ls.Close()

OpenPath(ls)
// InitLuaVM performs initial registrations.
func InitLuaVM(ls *lua.LState) {
// set modules
RegPath(ls)
RegPack(ls)

var bindir = path.Dir(wpk.ToSlash(os.Args[0]))
var scrdir = path.Dir(wpk.ToSlash(fpath))
var bindir = func() string {
if str, err := os.Executable(); err == nil {
return path.Dir(wpk.ToSlash(str))
} else {
return path.Dir(wpk.ToSlash(os.Args[0]))
}
}()

// global variables
ls.SetGlobal("buildvers", lua.LString(BuildVers))
ls.SetGlobal("buildtime", lua.LString(BuildTime))
ls.SetGlobal("bindir", lua.LString(bindir))
ls.SetGlobal("scrdir", lua.LString(scrdir))
ls.SetGlobal("tmpdir", lua.LString(wpk.ToSlash(os.TempDir())))
// global functions
ls.SetGlobal("log", ls.NewFunction(lualog))
Expand All @@ -122,6 +124,16 @@ func RunLuaVM(fpath string) (err error) {
ls.SetGlobal("hex2bin", ls.NewFunction(luahex2bin))
ls.SetGlobal("milli2time", ls.NewFunction(luamilli2time))
ls.SetGlobal("time2milli", ls.NewFunction(luatime2milli))
}

// RunLuaVM runs specified Lua-script with Lua WPK API.
func RunLuaVM(fpath string) (err error) {
var ls = lua.NewState()
defer ls.Close()
InitLuaVM(ls)

var scrdir = path.Dir(wpk.ToSlash(fpath))
ls.SetGlobal("scrdir", lua.LString(scrdir))

if err = ls.DoFile(fpath); err != nil {
return
Expand Down
2 changes: 1 addition & 1 deletion strutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func B2S(b []byte) string {
}

// S2B converts string to bytes slice without memory allocation.
func S2B(s string) (b []byte) {
func S2B(s string) []byte {
return unsafe.Slice(unsafe.StringData(s), len(s))
}

Expand Down
10 changes: 5 additions & 5 deletions strutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import (
func TestS2B(t *testing.T) {
var s = "some string"
var ps = unsafe.Pointer(unsafe.StringData(s))
var b = []byte(s)
/*var b = []byte(s)
var pb = unsafe.Pointer(unsafe.SliceData(b))
if ps == pb {
t.Error("string pointer is equal to pointer on new allocated bytes slice")
}
b = wpk.S2B(s)
pb = unsafe.Pointer(unsafe.SliceData(b))
}*/
var b = wpk.S2B(s)
var pb = unsafe.Pointer(unsafe.SliceData(b))
if ps != pb {
t.Error("string pointer is not equal to pointer on same bytes slice")
}
Expand Down Expand Up @@ -81,7 +81,7 @@ func ExampleJoinFilePath() {
// dir/base.ext
}

func ExampleBaseName() {
func ExamplePathName() {
fmt.Println(wpk.PathName("C:\\Windows\\system.ini"))
fmt.Println(wpk.PathName("/go/bin/wpkbuild_win_x64.exe"))
fmt.Println(wpk.PathName("wpkbuild_win_x64.exe"))
Expand Down
9 changes: 5 additions & 4 deletions task/build.cmd
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
@echo off

for /F "tokens=*" %%g in ('git describe --tags') do (set buildvers=%%g)
for /F "tokens=*" %%g in ('go run %~dp0/timenow.go') do (set buildtime=%%g)
for /f "tokens=2 delims==" %%g in ('wmic os get localdatetime /value') do set dt=%%g
set buildtime=%dt:~0,4%-%dt:~4,2%-%dt:~6,2%T%dt:~8,2%:%dt:~10,2%:%dt:~12,2%.%dt:~15,3%Z

set wd=%~dp0..
go build -o %GOPATH%/bin/wpkbuild.exe -v -ldflags="-X 'github.com/schwarzlichtbezirk/wpk/luawpk.BuildVers=%buildvers%' -X 'github.com/schwarzlichtbezirk/wpk/luawpk.BuildTime=%buildtime%'" %wd%/util/build
go build -o %GOPATH%/bin/wpkextract.exe -v %wd%/util/extract
go build -o %GOPATH%/bin/wpkpack.exe -v %wd%/util/pack
go build -o %GOPATH%/bin/wpkbuild.exe -v -ldflags="-X 'github.com/schwarzlichtbezirk/wpk/luawpk.BuildVers=%buildvers%' -X 'github.com/schwarzlichtbezirk/wpk/luawpk.BuildTime=%buildtime%'" %wd%/cmd/build
go build -o %GOPATH%/bin/wpkextract.exe -v %wd%/cmd/extract
go build -o %GOPATH%/bin/wpkpack.exe -v %wd%/cmd/pack
13 changes: 9 additions & 4 deletions task/build.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
#!/bin/bash

buildvers=$(git describe --tags)
buildtime=$(go run "$(dirname "$0")/timenow.go") # $(date -u +'%FT%TZ')
# See https://tc39.es/ecma262/#sec-date-time-string-format
# time format acceptable for Date constructors.
buildtime=$(date +'%FT%T.%3NZ')

wd=$(realpath -s "$(dirname "$0")/..")
go build -o $GOPATH/bin/wpkbuild.exe -v -ldflags="-X 'github.com/schwarzlichtbezirk/wpk/luawpk.BuildVers=$buildvers' -X 'github.com/schwarzlichtbezirk/wpk/luawpk.BuildTime=$buildtime'" $wd/util/build
go build -o $GOPATH/bin/wpkextract.exe -v $wd/util/extract
go build -o $GOPATH/bin/wpkpack.exe -v $wd/util/pack
go build -o $GOPATH/bin/wpkbuild.exe -v -ldflags="\
-X 'github.com/schwarzlichtbezirk/wpk/luawpk.BuildVers=$buildvers'\
-X 'github.com/schwarzlichtbezirk/wpk/luawpk.BuildTime=$buildtime'"\
$wd/cmd/build
go build -o $GOPATH/bin/wpkextract.exe -v $wd/cmd/extract
go build -o $GOPATH/bin/wpkpack.exe -v $wd/cmd/pack
15 changes: 0 additions & 15 deletions task/timenow.go

This file was deleted.

2 changes: 1 addition & 1 deletion testdata/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ function wpk.create(fpath)-- additional wpk-constructor
pkg.autofid = true -- put auto generated file ID for each file
pkg.automime = true -- put MIME type for each file if it is not given explicit
pkg.crc32 = true -- generate CRC32 Castagnoli code for each file
pkg:begin(fpath) -- open wpk-file for write
pkg:begin(path.toslash(fpath)) -- open wpk-file for write
log("starts: "..pkg.pkgpath)
return pkg
end
Expand Down

0 comments on commit abed437

Please sign in to comment.