init
This commit is contained in:
commit
9298fc4e4c
12 changed files with 599 additions and 0 deletions
111
internal/state/builtins.go
Normal file
111
internal/state/builtins.go
Normal file
|
@ -0,0 +1,111 @@
|
|||
package state
|
||||
|
||||
import (
|
||||
"code.vulpine.solutions/sam/vlp/internal/build"
|
||||
"code.vulpine.solutions/sam/vlp/internal/packages"
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
func (s *State) depends(ls *lua.LState) int {
|
||||
t := ls.GetTop()
|
||||
if t == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
for i := 1; i <= t; i++ {
|
||||
table, ok := ls.Get(i).(*lua.LTable)
|
||||
if !ok {
|
||||
ls.RaiseError("Argument %v was not a table", i)
|
||||
return 0
|
||||
}
|
||||
|
||||
var (
|
||||
name string
|
||||
version = "*"
|
||||
)
|
||||
|
||||
switch table.Len() {
|
||||
default:
|
||||
fallthrough
|
||||
case 2:
|
||||
version = table.RawGetInt(2).String()
|
||||
fallthrough
|
||||
case 1:
|
||||
name = table.RawGetInt(1).String()
|
||||
case 0:
|
||||
ls.RaiseError("Table %v is empty", i)
|
||||
return 0
|
||||
}
|
||||
|
||||
s.Builder.Dependencies = append(s.Builder.Dependencies, packages.Dependency{Name: name, Version: version})
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
func (s *State) git(ls *lua.LState) int {
|
||||
t := ls.GetTop()
|
||||
if t == 0 {
|
||||
ls.RaiseError("`git` needs a repository URL")
|
||||
return 0
|
||||
}
|
||||
|
||||
var repo, ref string
|
||||
|
||||
lrepo, ok := ls.Get(1).(lua.LString)
|
||||
if !ok {
|
||||
ls.RaiseError("Argument 1 is not a string")
|
||||
return 0
|
||||
}
|
||||
repo = lrepo.String()
|
||||
|
||||
lref, ok := ls.Get(2).(lua.LString)
|
||||
if ok {
|
||||
ref = lref.String()
|
||||
}
|
||||
|
||||
s.Builder.SourceFetcher = build.GitFetcher{Repository: repo, Ref: ref}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (s *State) cmd(ls *lua.LState) int {
|
||||
t := ls.GetTop()
|
||||
if t == 0 {
|
||||
ls.RaiseError("`cmd` needs a command to run")
|
||||
return 0
|
||||
}
|
||||
|
||||
lcmd, ok := ls.Get(1).(lua.LString)
|
||||
if !ok {
|
||||
ls.RaiseError("Argument 1 is not a string")
|
||||
return 0
|
||||
}
|
||||
|
||||
var args []string
|
||||
for i := 2; i <= t; i++ {
|
||||
larg := ls.Get(i)
|
||||
if larg == lua.LNil {
|
||||
break
|
||||
}
|
||||
args = append(args, larg.String())
|
||||
}
|
||||
|
||||
s.Builder.Commands = append(s.Builder.Commands, build.BuildCommand{Command: lcmd.String(), Args: args})
|
||||
return 0
|
||||
}
|
||||
|
||||
func (s *State) bin(ls *lua.LState) int {
|
||||
if ls.GetTop() != 1 {
|
||||
ls.RaiseError("`bin` needs a single binary to install")
|
||||
return 0
|
||||
}
|
||||
|
||||
lbin, ok := ls.Get(1).(lua.LString)
|
||||
if !ok {
|
||||
ls.RaiseError("Argument 1 is not a string")
|
||||
return 0
|
||||
}
|
||||
|
||||
s.Builder.Binaries = append(s.Builder.Binaries, lbin.String())
|
||||
return 0
|
||||
}
|
121
internal/state/state.go
Normal file
121
internal/state/state.go
Normal file
|
@ -0,0 +1,121 @@
|
|||
package state
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"code.vulpine.solutions/sam/vlp/internal/build"
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
type State struct {
|
||||
Lua *lua.LState
|
||||
Builder *build.Builder
|
||||
}
|
||||
|
||||
func New(packageName string) *State {
|
||||
s := &State{
|
||||
Lua: lua.NewState(lua.Options{
|
||||
IncludeGoStackTrace: true,
|
||||
}),
|
||||
Builder: &build.Builder{Name: packageName},
|
||||
}
|
||||
|
||||
s.Lua.SetGlobal("depends", s.Lua.NewFunction(s.depends))
|
||||
s.Lua.SetGlobal("git", s.Lua.NewFunction(s.git))
|
||||
s.Lua.SetGlobal("cmd", s.Lua.NewFunction(s.cmd))
|
||||
s.Lua.SetGlobal("bin", s.Lua.NewFunction(s.bin))
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
// Eval evaluates a build script. It does not actually build the package yet.
|
||||
func (s *State) Eval(r io.Reader) error {
|
||||
script, err := io.ReadAll(r)
|
||||
if err != nil {
|
||||
return fmt.Errorf("reading script: %w", err)
|
||||
}
|
||||
|
||||
err = s.Lua.DoString(string(script))
|
||||
if err != nil {
|
||||
return fmt.Errorf("executing script: %w", err)
|
||||
}
|
||||
|
||||
var name, version string
|
||||
|
||||
lname := s.Lua.GetGlobal("name")
|
||||
if nameStr, ok := lname.(lua.LString); ok {
|
||||
name = nameStr.String()
|
||||
} else {
|
||||
name = s.Builder.Name
|
||||
}
|
||||
|
||||
lversion := s.Lua.GetGlobal("version")
|
||||
if versionStr, ok := lversion.(lua.LString); ok {
|
||||
version = versionStr.String()
|
||||
}
|
||||
|
||||
s.Builder, err = build.New(name, version)
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating builder: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *State) Build() error {
|
||||
fetchFn, ok := s.Lua.GetGlobal("fetch").(*lua.LFunction)
|
||||
if !ok {
|
||||
return fmt.Errorf("`fetch` was not defined")
|
||||
}
|
||||
|
||||
err := s.Lua.CallByParam(lua.P{
|
||||
Fn: fetchFn,
|
||||
NRet: 0,
|
||||
Protect: true,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("running fetch function: %w", err)
|
||||
}
|
||||
|
||||
err = s.Builder.SourceFetcher.Fetch(s.Builder.TempDir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("fetching source: %w", err)
|
||||
}
|
||||
|
||||
buildFn := s.Lua.GetGlobal("build")
|
||||
if buildFn != lua.LNil {
|
||||
err := s.Lua.CallByParam(lua.P{
|
||||
Fn: buildFn,
|
||||
NRet: 0,
|
||||
Protect: true,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("running build function: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
err = s.Builder.Build()
|
||||
if err != nil {
|
||||
return fmt.Errorf("building source: %w", err)
|
||||
}
|
||||
|
||||
installFn := s.Lua.GetGlobal("install")
|
||||
if installFn != lua.LNil {
|
||||
err := s.Lua.CallByParam(lua.P{
|
||||
Fn: installFn,
|
||||
NRet: 0,
|
||||
Protect: true,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("running install function: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
err = s.Builder.GenerateTarball()
|
||||
if err != nil {
|
||||
return fmt.Errorf("generating zip: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue