-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
dxg
committed
Jan 7, 2025
1 parent
68951a2
commit 9f39d83
Showing
6 changed files
with
182 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package cmd | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"github.com/goplus/igop" | ||
"io" | ||
"os" | ||
) | ||
|
||
func IgopExec(options CmdOptions, args []string) (int, error) { | ||
var content []byte | ||
|
||
if options.ScriptIsSet { | ||
content = []byte(options.Script) | ||
} else { | ||
stat, err := os.Stdin.Stat() | ||
if err != nil || (stat.Mode()&os.ModeCharDevice) != 0 { | ||
return -1, fmt.Errorf("must input a valid file or content, \"igop exec < 1.txt\"") | ||
} | ||
content, err = io.ReadAll(bufio.NewReader(os.Stdin)) | ||
if err != nil { | ||
return -2, err | ||
} | ||
} | ||
|
||
var mode = igop.EnablePrintAny | ||
if options.Debug { | ||
mode |= igop.EnableTracing | igop.EnableDumpImports | igop.EnableDumpInstr | ||
} | ||
|
||
ctx := igop.NewContext(mode) | ||
|
||
return ctx.RunFile("main.gop", content, args) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package cmd | ||
|
||
type CmdOptions struct { | ||
Debug bool | ||
Script string | ||
ScriptIsSet bool | ||
|
||
Path string | ||
VendorPath string | ||
ImportPaths map[string]string | ||
PluginPaths []string | ||
|
||
realPath string | ||
isDir bool | ||
isArchive bool | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/c4milo/unpackit" | ||
"go.uber.org/multierr" | ||
"gopkg.in/go-mixed/gos.v1/mod" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
func initialPath(options CmdOptions) (CmdOptions, error) { | ||
var err error | ||
var stat os.FileInfo | ||
if stat, err = os.Stat(options.Path); err != nil { | ||
return options, err | ||
} | ||
|
||
options.isDir = stat.IsDir() | ||
// 获取绝对路径 | ||
options.Path, _ = filepath.Abs(options.Path) | ||
|
||
// 是tar.gz压缩文件 | ||
if !options.isDir && (strings.HasSuffix(options.Path, ".tar.gz") || | ||
strings.HasSuffix(options.Path, ".tar.bzip2") || | ||
strings.HasSuffix(options.Path, ".tar.xz") || | ||
strings.HasSuffix(options.Path, ".zip") || | ||
strings.HasSuffix(options.Path, ".tar")) { | ||
var f *os.File | ||
if f, err = os.Open(options.Path); err != nil { | ||
return options, err | ||
} | ||
defer f.Close() | ||
options.realPath = filepath.Join(filepath.Dir(options.Path), "__"+filepath.Base(options.Path)+"__") | ||
if err = unpackit.Unpack(f, options.realPath); err != nil { | ||
return options, err | ||
} | ||
options.isArchive = true | ||
} else { | ||
options.realPath = options.Path | ||
} | ||
|
||
// 配置了vendor,检查是否有效 | ||
if options.VendorPath != "" { | ||
// 转为绝对路径 | ||
if options.VendorPath, err = filepath.Abs(options.VendorPath); err != nil { | ||
return options, fmt.Errorf("[Vendor]%w", err) | ||
} | ||
|
||
if stat, err = os.Stat(filepath.Join(options.VendorPath, "modules.txt")); err != nil || stat.IsDir() { // 不存在vendor/modules.txt | ||
return options, fmt.Errorf("[Vendor]%w", multierr.Append(err, fmt.Errorf("%s/modules.txt is not a regular file", options.VendorPath))) | ||
} | ||
} | ||
return options, nil | ||
} | ||
|
||
func IgopRun(options CmdOptions, args []string) (int, error) { | ||
var err error | ||
if options, err = initialPath(options); err != nil { | ||
return -11, err | ||
} | ||
|
||
// 删除解压的文件夹 | ||
defer func() { | ||
if options.isArchive { | ||
os.RemoveAll(options.realPath) | ||
} | ||
}() | ||
|
||
ctx := mod.NewContext(options.realPath, options.Debug) | ||
|
||
// 加载importPaths | ||
for k, v := range options.ImportPaths { | ||
if err = ctx.AddImport(k, v); err != nil { | ||
return -12, err | ||
} | ||
if options.Debug { | ||
fmt.Printf("# imported package [%s]%s\n", k, v) | ||
} | ||
} | ||
// 加载plugins | ||
if err = ctx.LoadPlugins(options.PluginPaths); err != nil { | ||
return -13, err | ||
} | ||
// 加载vendor | ||
if options.isArchive || options.isDir || options.VendorPath != "" { | ||
if err = ctx.LoadVendor(options.VendorPath); err != nil { | ||
return -14, err | ||
} | ||
} | ||
|
||
return ctx.RunMain(args) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.