Skip to content

Commit

Permalink
feat: slim down builtin types
Browse files Browse the repository at this point in the history
  • Loading branch information
axtloss committed May 21, 2024
1 parent e1831a6 commit c23ebf9
Show file tree
Hide file tree
Showing 17 changed files with 246 additions and 210 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ jobs:
with:
go-version: 1.21

- name: Build
- name: Build vib
run: |
go get ./...
go build -v -trimpath ./...
make build
- name: Build plugins
run: |
go get ./...
make build-plugins
12 changes: 10 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,18 @@ jobs:
- name: Build
run: |
go get ./...
go build -o vib -trimpath
make build
- name: Build plugins
run: |
go get ./...
make build-plugins
tar cvf plugins.tar.xz build/plugins

- name: Upload a Release Asset
if: github.repository == 'Vanilla-OS/Vib'
uses: softprops/action-gh-release@v2
with:
files: vib
files: |
build/vib
plugins.tar.xz
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
example/downloads/*
example/sources/*
test/*
vib
build
docs/website/dist/*
docs/website/node_modules/*
go.work
*~
28 changes: 28 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.POSIX:

PREFIX=/usr/
DESTDIR=/
BINARY_NAME=vib

all: build # plugins

build:
mkdir -p build
sed 's|$$INSTALLPREFIX$$|${PREFIX}|g' core/plugins.in > core/plugins.go
go build -a -o build/${BINARY_NAME}

plugins: FORCE
mkdir -p build/plugins
$(MAKE) -C plugins/

install:
install -Dm755 -t ${DESTDIR}/${PREFIX}/bin/ ./${BINARY_NAME}

install-plugins:
install -Dm644 -t ${DESTDIR}/${PREFIX}/share/vib/plugins/ ./build/plugins/*.so

clean:
rm -r build
rm core/plugins.go

FORCE:
10 changes: 1 addition & 9 deletions core/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,16 +285,8 @@ func BuildModule(recipe *api.Recipe, moduleInterface interface{}) (string, error
}

moduleBuilders := map[string]func(interface{}, *api.Recipe) (string, error){
"apt": BuildAptModule,
"dnf": BuildDnfModule,
"cmake": BuildCMakeModule,
"dpkg": BuildDpkgModule,
"dpkg-buildpackage": BuildDpkgBuildPkgModule,
"go": BuildGoModule,
"make": BuildMakeModule,
"meson": BuildMesonModule,
"shell": BuildShellModule,
"includes": func(interface{}, *api.Recipe) (string, error) { return "", nil },
"includes": func(interface{}, *api.Recipe) (string, error) { return "", nil },
}

if moduleBuilder, ok := moduleBuilders[module.Type]; ok {
Expand Down
71 changes: 0 additions & 71 deletions core/dnf.go

This file was deleted.

38 changes: 0 additions & 38 deletions core/dpkg.go

This file was deleted.

31 changes: 0 additions & 31 deletions core/make.go

This file was deleted.

22 changes: 19 additions & 3 deletions core/plugins.go → core/plugins.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/ebitengine/purego"
"github.com/vanilla-os/vib/api"
)
import "os"

var openedPlugins map[string]Plugin

Expand All @@ -23,10 +24,25 @@ func LoadPlugin(name string, module interface{}, recipe *api.Recipe) (string, er
fmt.Println("Loading new plugin")
buildModule = Plugin{Name: name}

loadedPlugin, err := purego.Dlopen(fmt.Sprintf("%s/%s.so", recipe.PluginPath, name), purego.RTLD_NOW|purego.RTLD_GLOBAL)
if err != nil {
panic(err)
localPluginPath := fmt.Sprintf("%s/%s.so", recipe.PluginPath, name)

globalPluginPath := fmt.Sprintf("$INSTALLPREFIX$/share/vib/plugins/%s.so", name)

// Prefer local plugins before global ones
var loadedPlugin uintptr
_, err := os.Stat(localPluginPath)
if os.IsNotExist(err) {
loadedPlugin, err = purego.Dlopen(globalPluginPath, purego.RTLD_NOW|purego.RTLD_GLOBAL)
if err != nil {
panic(err) // yayyy panics <3
}
} else {
loadedPlugin, err = purego.Dlopen(localPluginPath, purego.RTLD_NOW|purego.RTLD_GLOBAL)
if err != nil {
panic(err)
}
}

var buildFunction func(*C.char, *C.char) string
purego.RegisterLibFunc(&buildFunction, loadedPlugin, "BuildModule")
buildModule.BuildFunc = buildFunction
Expand Down
3 changes: 2 additions & 1 deletion core/shell.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package core
package main

import (
"errors"
"strings"
"fmt"

"github.com/mitchellh/mapstructure"
"github.com/vanilla-os/vib/api"
Expand Down
10 changes: 10 additions & 0 deletions plugins/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

.PHONY: all

PLUGS := $(wildcard *.go)
OBJS := $(PLUGS:go=so)

all: $(OBJS)

$(OBJS): %.so: %.go
go build -buildmode=c-shared -a -o ../build/plugins/$@ $<
45 changes: 31 additions & 14 deletions core/apt.go → plugins/apt.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package core
package main

import (
"C"
"bufio"
"errors"
"fmt"
"os"
"path/filepath"

"github.com/mitchellh/mapstructure"

"github.com/vanilla-os/vib/api"
)
import "encoding/json"

type AptModule struct {
Name string `json:"name"`
Expand All @@ -28,11 +27,20 @@ type AptOptions struct {

// BuildAptModule builds a module that installs packages
// using the apt package manager
func BuildAptModule(moduleInterface interface{}, recipe *api.Recipe) (string, error) {
var module AptModule
err := mapstructure.Decode(moduleInterface, &module)
//
//export BuildModule
func BuildModule(moduleInterface *C.char, recipeInterface *C.char) *C.char {
var module *AptModule
var recipe *api.Recipe

err := json.Unmarshal([]byte(C.GoString(moduleInterface)), &module)
if err != nil {
return C.CString(fmt.Sprintf("ERROR: %s", err.Error()))
}

err = json.Unmarshal([]byte(C.GoString(recipeInterface)), &recipe)
if err != nil {
return "", err
return C.CString(fmt.Sprintf("ERROR: %s", err.Error()))
}

args := ""
Expand All @@ -55,7 +63,11 @@ func BuildAptModule(moduleInterface interface{}, recipe *api.Recipe) (string, er
packages += pkg + " "
}

<<<<<<< HEAD:core/apt.go
return fmt.Sprintf("apt-get install -y %s %s && apt-get clean", args, packages), nil
=======
return C.CString(fmt.Sprintf("apt install -y %s %s && apt clean", args, packages))
>>>>>>> ad41c74 (feat: slim down builtin types):plugins/apt.go
}

if len(module.Source.Paths) > 0 {
Expand All @@ -66,7 +78,7 @@ func BuildAptModule(moduleInterface interface{}, recipe *api.Recipe) (string, er
packages := ""
file, err := os.Open(instPath)
if err != nil {
return "", err
return C.CString(fmt.Sprintf("ERROR: %s", err.Error()))
}
defer file.Close()

Expand All @@ -76,20 +88,25 @@ func BuildAptModule(moduleInterface interface{}, recipe *api.Recipe) (string, er
}

if err := scanner.Err(); err != nil {
return "", err
return C.CString(fmt.Sprintf("ERROR: %s", err.Error()))
}

cmd += fmt.Sprintf("apt install -y %s %s", args, packages)
cmd += fmt.Sprintf("apt-get install -y %s %s", args, packages)

if i != len(module.Source.Paths)-1 {
cmd += "&& "
} else {
cmd += "&& apt clean"
cmd += "&& apt-get clean"
}
}

return cmd, nil
return C.CString(cmd)
}

return "", errors.New("no packages or paths specified")
return C.CString("ERROR: no packages or paths specified")
}
<<<<<<< HEAD:core/apt.go
=======

func main() {}
>>>>>>> ad41c74 (feat: slim down builtin types):plugins/apt.go
Loading

0 comments on commit c23ebf9

Please sign in to comment.