Skip to content

feat: add basic bash support for windows #415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions pkg/engine/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"sort"
Expand Down Expand Up @@ -172,10 +173,9 @@ var ignoreENV = map[string]struct{}{
}

func appendEnv(envs []string, k, v string) []string {
for _, k := range []string{k, env.ToEnvLike(k)} {
if _, ignore := ignoreENV[k]; !ignore {
envs = append(envs, k+"="+v)
}
k = env.ToEnvLike(k)
if _, ignore := ignoreENV[k]; !ignore {
envs = append(envs, k+"="+v)
}
return envs
}
Expand Down Expand Up @@ -238,6 +238,10 @@ func (e *Engine) newCommand(ctx context.Context, extraEnv []string, tool types.T
})
}

if runtime.GOOS == "windows" && (args[0] == "/bin/bash" || args[0] == "/bin/sh") {
args[0] = path.Base(args[0])
}

if runtime.GOOS == "windows" && (args[0] == "/usr/bin/env" || args[0] == "/bin/env") {
args = args[1:]
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/repos/download/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strings"
"time"

"github.com/mholt/archiver/v4"
Expand Down Expand Up @@ -60,6 +62,18 @@ func Extract(ctx context.Context, downloadURL, digest, targetDir string) error {
return err
}

bin := path.Base(parsedURL.Path)
if strings.HasSuffix(bin, ".exe") {
dst, err := os.Create(filepath.Join(targetDir, bin))
if err != nil {
return err
}
defer dst.Close()

_, err = io.Copy(dst, tmpFile)
return err
}

format, input, err := archiver.Identify(filepath.Base(parsedURL.Path), tmpFile)
if err != nil {
return err
Expand Down
47 changes: 40 additions & 7 deletions pkg/repos/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"github.com/BurntSushi/locker"
"github.com/gptscript-ai/gptscript/pkg/config"
"github.com/gptscript-ai/gptscript/pkg/credentials"
"github.com/gptscript-ai/gptscript/pkg/hash"
"github.com/gptscript-ai/gptscript/pkg/loader/github"
"github.com/gptscript-ai/gptscript/pkg/repos/git"
"github.com/gptscript-ai/gptscript/pkg/repos/runtimes/golang"
Expand Down Expand Up @@ -51,13 +52,22 @@
credHelperDirs credentials.CredentialHelperDirs
runtimes []Runtime
credHelperConfig *credHelperConfig
supportLocal bool
}

type credHelperConfig struct {
lock sync.Mutex
initialized bool
cliCfg *config.CLIConfig
env []string
storageDir string

Check failure on line 63 in pkg/repos/get.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04)

field `storageDir` is unused (unused)
gitDir string

Check failure on line 64 in pkg/repos/get.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04)

field `gitDir` is unused (unused)
runtimeDir string

Check failure on line 65 in pkg/repos/get.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04)

field `runtimeDir` is unused (unused)
runtimes []Runtime

Check failure on line 66 in pkg/repos/get.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04)

field `runtimes` is unused (unused)
}

func (m *Manager) SetSupportLocal() {
m.supportLocal = true
}

func New(cacheDir string, runtimes ...Runtime) *Manager {
Expand Down Expand Up @@ -200,8 +210,14 @@
_ = os.RemoveAll(doneFile)
_ = os.RemoveAll(target)

if err := git.Checkout(ctx, m.gitDir, tool.Source.Repo.Root, tool.Source.Repo.Revision, target); err != nil {
return "", nil, err
if tool.Source.Repo.VCS == "git" {
if err := git.Checkout(ctx, m.gitDir, tool.Source.Repo.Root, tool.Source.Repo.Revision, target); err != nil {
return "", nil, err
}
} else {
if err := os.MkdirAll(target, 0755); err != nil {
return "", nil, err
}
}

newEnv, err := runtime.Setup(ctx, m.runtimeDir, targetFinal, env)
Expand All @@ -227,12 +243,25 @@
}

func (m *Manager) GetContext(ctx context.Context, tool types.Tool, cmd, env []string) (string, []string, error) {
if tool.Source.Repo == nil {
return tool.WorkingDir, env, nil
}
var isLocal bool
if !m.supportLocal {
if tool.Source.Repo == nil {
return tool.WorkingDir, env, nil
}

if tool.Source.Repo.VCS != "git" {
return "", nil, fmt.Errorf("only git is supported, found VCS %s for %s", tool.Source.Repo.VCS, tool.ID)
if tool.Source.Repo.VCS != "git" {
return "", nil, fmt.Errorf("only git is supported, found VCS %s for %s", tool.Source.Repo.VCS, tool.ID)
}
} else if tool.Source.Repo == nil {
isLocal = true
id := hash.Digest(tool)[:12]
tool.Source.Repo = &types.Repo{
VCS: "<local>",
Root: id,
Path: "/",
Name: id,
Revision: id,
}
}

for _, runtime := range m.runtimes {
Expand All @@ -242,5 +271,9 @@
}
}

if isLocal {
return tool.WorkingDir, env, nil
}

return m.setup(ctx, &noopRuntime{}, tool, env)
}
1 change: 1 addition & 0 deletions pkg/repos/runtimes/busybox/SHASUMS256.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6d2dfd1c1412c3550a89071a1b36a6f6073844320e687343d1dfc72719ecb8d9 FRP-5301-gda71f7c57/busybox-w64-FRP-5301-gda71f7c57.exe
107 changes: 107 additions & 0 deletions pkg/repos/runtimes/busybox/busybox.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package busybox

import (
"bufio"
"bytes"
"context"
_ "embed"
"errors"
"fmt"
"io/fs"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"

runtimeEnv "github.com/gptscript-ai/gptscript/pkg/env"
"github.com/gptscript-ai/gptscript/pkg/hash"
"github.com/gptscript-ai/gptscript/pkg/repos/download"
)

//go:embed SHASUMS256.txt
var releasesData []byte

const downloadURL = "https://github.com/gptscript-ai/busybox-w32/releases/download/%s"

type Runtime struct {
}

func (r *Runtime) ID() string {
return "busybox"
}

func (r *Runtime) Supports(cmd []string) bool {
if runtime.GOOS != "windows" {
return false
}
for _, bin := range []string{"bash", "sh", "/bin/sh", "/bin/bash"} {
if runtimeEnv.Matches(cmd, bin) {
return true
}
}
return false
}

func (r *Runtime) Setup(ctx context.Context, dataRoot, _ string, env []string) ([]string, error) {
binPath, err := r.getRuntime(ctx, dataRoot)
if err != nil {
return nil, err
}

newEnv := runtimeEnv.AppendPath(env, binPath)
return newEnv, nil
}

func (r *Runtime) getReleaseAndDigest() (string, string, error) {
scanner := bufio.NewScanner(bytes.NewReader(releasesData))
for scanner.Scan() {
line := scanner.Text()
fields := strings.Fields(line)
return fmt.Sprintf(downloadURL, fields[1]), fields[0], nil
}

return "", "", fmt.Errorf("failed to find %s release", r.ID())
}

func (r *Runtime) getRuntime(ctx context.Context, cwd string) (string, error) {
url, sha, err := r.getReleaseAndDigest()
if err != nil {
return "", err
}

target := filepath.Join(cwd, "busybox", hash.ID(url, sha))
if _, err := os.Stat(target); err == nil {
return target, nil
} else if !errors.Is(err, fs.ErrNotExist) {
return "", err
}

log.Infof("Downloading Busybox")
tmp := target + ".download"
defer os.RemoveAll(tmp)

if err := os.MkdirAll(tmp, 0755); err != nil {
return "", err
}

if err := download.Extract(ctx, url, sha, tmp); err != nil {
return "", err
}

bbExe := filepath.Join(tmp, path.Base(url))

cmd := exec.Command(bbExe, "--install", ".")
cmd.Dir = filepath.Dir(bbExe)

if err := cmd.Run(); err != nil {
return "", err
}

if err := os.Rename(tmp, target); err != nil {
return "", err
}

return target, nil
}
41 changes: 41 additions & 0 deletions pkg/repos/runtimes/busybox/busybox_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package busybox

import (
"context"
"errors"
"io/fs"
"os"
"path/filepath"
"runtime"
"strings"
"testing"

"github.com/adrg/xdg"
"github.com/samber/lo"
"github.com/stretchr/testify/require"
)

var (
testCacheHome = lo.Must(xdg.CacheFile("gptscript-test-cache/runtime"))
)

func firstPath(s []string) string {
_, p, _ := strings.Cut(s[0], "=")
return strings.Split(p, string(os.PathListSeparator))[0]
}

func TestRuntime(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip()
}

r := Runtime{}

s, err := r.Setup(context.Background(), testCacheHome, "testdata", os.Environ())
require.NoError(t, err)
_, err = os.Stat(filepath.Join(firstPath(s), "busybox.exe"))
if errors.Is(err, fs.ErrNotExist) {
_, err = os.Stat(filepath.Join(firstPath(s), "busybox"))
}
require.NoError(t, err)
}
5 changes: 5 additions & 0 deletions pkg/repos/runtimes/busybox/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package busybox

import "github.com/gptscript-ai/gptscript/pkg/mvl"

var log = mvl.Package()
2 changes: 2 additions & 0 deletions pkg/repos/runtimes/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package runtimes
import (
"github.com/gptscript-ai/gptscript/pkg/engine"
"github.com/gptscript-ai/gptscript/pkg/repos"
"github.com/gptscript-ai/gptscript/pkg/repos/runtimes/busybox"
"github.com/gptscript-ai/gptscript/pkg/repos/runtimes/golang"
"github.com/gptscript-ai/gptscript/pkg/repos/runtimes/node"
"github.com/gptscript-ai/gptscript/pkg/repos/runtimes/python"
)

var Runtimes = []repos.Runtime{
&busybox.Runtime{},
&python.Runtime{
Version: "3.12",
Default: true,
Expand Down
3 changes: 0 additions & 3 deletions pkg/tests/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -748,9 +748,6 @@ func TestGlobalErr(t *testing.T) {
}

func TestContextArg(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip()
}
runner := tester.NewRunner(t)
x, err := runner.Run("", `{
"file": "foo.db"
Expand Down
4 changes: 2 additions & 2 deletions pkg/tests/testdata/TestContextArg/other.gpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ name: fromcontext
args: first: an arg
args: second: an arg

#!/bin/bash
echo this is from other context ${first} and then ${second}
#!/usr/bin/env bash
echo this is from other context ${FIRST} and then ${SECOND}
2 changes: 1 addition & 1 deletion pkg/tests/testdata/TestContextArg/test.gpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ name: fromcontext
args: first: an arg

#!/bin/bash
echo this is from context -- ${first}
echo this is from context -- ${FIRST}
12 changes: 11 additions & 1 deletion pkg/tests/tester/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import (
"path/filepath"
"testing"

"github.com/adrg/xdg"
"github.com/gptscript-ai/gptscript/pkg/credentials"
"github.com/gptscript-ai/gptscript/pkg/loader"
"github.com/gptscript-ai/gptscript/pkg/repos"
"github.com/gptscript-ai/gptscript/pkg/repos/runtimes"
"github.com/gptscript-ai/gptscript/pkg/runner"
"github.com/gptscript-ai/gptscript/pkg/types"
"github.com/hexops/autogold/v2"
Expand Down Expand Up @@ -171,8 +174,15 @@ func NewRunner(t *testing.T) *Runner {
t: t,
}

cacheDir, err := xdg.CacheFile("gptscript-test-cache/runtime")
require.NoError(t, err)

rm := runtimes.Default(cacheDir)
rm.(*repos.Manager).SetSupportLocal()

run, err := runner.New(c, credentials.NoopStore{}, runner.Options{
Sequential: true,
Sequential: true,
RuntimeManager: rm,
})
require.NoError(t, err)

Expand Down
Loading