Skip to content

Commit

Permalink
mod: Optimize the PostInstall and PreInstall structure of the plugin
Browse files Browse the repository at this point in the history
* It is clear that the PreInstall function is responsible for providing the meta-information of the current version (including version number, download address, attached file address), and vfox helps download and verify the file.
  • Loading branch information
aooohan committed Feb 2, 2024
1 parent 2a4fc77 commit b41c2e6
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 55 deletions.
4 changes: 2 additions & 2 deletions cmd/commands/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ func searchCmd(ctx *cli.Context) error {
} else {
value = fmt.Sprintf("v%s", p.Main.Version)
}
if len(p.Additional) != 0 {
if len(p.Additions) != 0 {
var additional []string
for _, a := range p.Additional {
for _, a := range p.Additions {
additional = append(additional, fmt.Sprintf("%s v%s", a.Name, a.Version))
}
value = fmt.Sprintf("%s [%s]", value, strings.Join(additional, ","))
Expand Down
7 changes: 5 additions & 2 deletions internal/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ package internal
import "path/filepath"

type Package struct {
Main *Info
Additional []*Info
Main *Info
Additions []*Info
}

type Info struct {
Expand All @@ -36,5 +36,8 @@ func (i *Info) label() string {
}

func (i *Info) storagePath(parentDir string) string {
if i.Version == "" {
return filepath.Join(parentDir, i.Name)
}
return filepath.Join(parentDir, i.Name+"-"+string(i.Version))
}
43 changes: 28 additions & 15 deletions internal/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,17 @@ func (l *LuaPlugin) Available() ([]*Package, error) {
if err != nil {
return
}
if item.Name == "" {
err = fmt.Errorf("additional file no name provided")
return
}
additionalArr = append(additionalArr, item)
})
}

result = append(result, &Package{
Main: mainSdk,
Additional: additionalArr,
Main: mainSdk,
Additions: additionalArr,
})

})
Expand Down Expand Up @@ -174,10 +178,10 @@ func (l *LuaPlugin) PreInstall(version Version) (*Package, error) {
}
mainSdk.Name = l.Name
var additionalArr []*Info
additional := table.RawGetString("addition")
if tb, ok := additional.(*lua.LTable); ok && tb.Len() != 0 {
additions := table.RawGetString("addition")
if tb, ok := additions.(*lua.LTable); ok && tb.Len() != 0 {
var err error
additional.(*lua.LTable).ForEach(func(key lua.LValue, value lua.LValue) {
additions.(*lua.LTable).ForEach(func(key lua.LValue, value lua.LValue) {
kvTable, ok := value.(*lua.LTable)
if !ok {
err = fmt.Errorf("the return value is not a table")
Expand All @@ -187,6 +191,10 @@ func (l *LuaPlugin) PreInstall(version Version) (*Package, error) {
if err != nil {
return
}
if info.Name == "" {
err = fmt.Errorf("additional file no name provided")
return
}
additionalArr = append(additionalArr, info)
})
if err != nil {
Expand All @@ -195,8 +203,8 @@ func (l *LuaPlugin) PreInstall(version Version) (*Package, error) {
}

return &Package{
Main: mainSdk,
Additional: additionalArr,
Main: mainSdk,
Additions: additionalArr,
}, nil
}

Expand Down Expand Up @@ -240,6 +248,7 @@ func (l *LuaPlugin) PostInstall(rootPath string, sdks []*Info) error {
L.SetField(sdkTable, "name", lua.LString(v.Name))
L.SetField(sdkTable, "version", lua.LString(v.Version))
L.SetField(sdkTable, "path", lua.LString(v.Path))
L.SetField(sdkTable, "note", lua.LString(v.Note))
L.SetField(sdkArr, v.Name, sdkTable)
}
ctxTable := L.NewTable()
Expand All @@ -263,15 +272,19 @@ func (l *LuaPlugin) PostInstall(rootPath string, sdks []*Info) error {

func (l *LuaPlugin) EnvKeys(sdkPackage *Package) (env.Envs, error) {
L := l.state
ctxTable := L.NewTable()
L.SetField(ctxTable, "path", lua.LString(sdkPackage.Main.Path))
if len(sdkPackage.Additional) != 0 {
additionalTable := L.NewTable()
for _, v := range sdkPackage.Additional {
L.SetField(additionalTable, v.Name, lua.LString(v.Path))
}
L.SetField(ctxTable, "additional_path", additionalTable)
mainInfo := sdkPackage.Main
sdkArr := L.NewTable()
for _, v := range sdkPackage.Additions {
sdkTable := L.NewTable()
L.SetField(sdkTable, "name", lua.LString(v.Name))
L.SetField(sdkTable, "version", lua.LString(v.Version))
L.SetField(sdkTable, "path", lua.LString(v.Path))
L.SetField(sdkArr, v.Name, sdkTable)
}
ctxTable := L.NewTable()
L.SetField(ctxTable, "sdkInfo", sdkArr)
// TODO Will be deprecated in future versions
L.SetField(ctxTable, "path", lua.LString(mainInfo.Path))
if err := L.CallByParam(lua.P{
Fn: l.pluginObj.RawGetString("EnvKeys"),
NRet: 1,
Expand Down
76 changes: 41 additions & 35 deletions internal/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,12 @@ func (b *Sdk) Install(version Version) error {
installedSdkInfos = append(installedSdkInfos, &Info{
Name: mainSdk.Name,
Version: mainSdk.Version,
Note: mainSdk.Note,
Path: path,
})
if len(installInfo.Additional) > 0 {
pterm.Printf("There are %d additional items that need to be installed...\n", len(installInfo.Additional))
for _, oSdk := range installInfo.Additional {
if len(installInfo.Additions) > 0 {
pterm.Printf("There are %d additional files that need to be downloaded...\n", len(installInfo.Additions))
for _, oSdk := range installInfo.Additions {
path, err = b.preInstallSdk(oSdk, newDirPath)
if err != nil {
return err
Expand All @@ -107,26 +108,23 @@ func (b *Sdk) Install(version Version) error {
return nil
}

func (b *Sdk) moveLocalFile(info *Info, sdkDestPath string) (string, error) {
path := info.storagePath(sdkDestPath)
pterm.Printf("Moving %s to %s...\n", info.Path, path)
err := os.Rename(info.Path, path)
if err != nil {
return "", fmt.Errorf("failed to move file, err:%w", err)
func (b *Sdk) moveLocalFile(info *Info, targetPath string) error {
pterm.Printf("Moving %s to %s...\n", info.Path, targetPath)
if err := os.Rename(info.Path, targetPath); err != nil {
return fmt.Errorf("failed to move file, err:%w", err)
}
return path, nil
return nil
}

func (b *Sdk) moveRemoteFile(info *Info, sdkDestPath string) (string, error) {
func (b *Sdk) moveRemoteFile(info *Info, targetPath string) error {
u, err := url.Parse(info.Path)
label := info.label()
if err != nil {
return "", err
return err
}
filePath, err := b.Download(u)
if err != nil {
fmt.Printf("Failed to download %s file, err:%s", label, err.Error())
return "", err
return fmt.Errorf("failed to download %s file, err:%w", label, err)
}
defer func() {
// del cache file
Expand All @@ -136,32 +134,40 @@ func (b *Sdk) moveRemoteFile(info *Info, sdkDestPath string) (string, error) {
checksum := info.Checksum.verify(filePath)
if !checksum {
fmt.Printf("Checksum error, file: %s\n", filePath)
return "", errors.New("checksum error")
return errors.New("checksum error")
}

decompressor := util.NewDecompressor(filePath)
if decompressor == nil {
fmt.Printf("Unable to process current file type, file: %s\n", filePath)
return "", fmt.Errorf("unknown file type")
// If it is not a compressed file, move file to the corresponding sdk directory,
// and the rest be handled by the PostInstall function.
if err = os.Rename(filePath, targetPath); err != nil {
return fmt.Errorf("failed to move file, err:%w", err)
}
return nil
}
pterm.Printf("Unpacking %s...\n", filePath)
path := info.storagePath(sdkDestPath)
err = decompressor.Decompress(path)
err = decompressor.Decompress(targetPath)
if err != nil {
fmt.Printf("Unpack failed, err:%s", err.Error())
return "", err
return fmt.Errorf("unpack failed, err:%w", err)
}
return path, nil
return nil
}
func (b *Sdk) preInstallSdk(info *Info, sdkDestPath string) (string, error) {
pterm.Printf("Preinstalling %s...\n", info.label())
path := info.storagePath(sdkDestPath)
if info.Path == "" {
return info.storagePath(sdkDestPath), nil
return path, nil
}
if strings.HasPrefix(info.Path, "https://") || strings.HasPrefix(info.Path, "http://") {
return b.moveRemoteFile(info, sdkDestPath)
if err := b.moveRemoteFile(info, sdkDestPath); err != nil {
return "", err
}
return path, nil
} else {
return b.moveLocalFile(info, sdkDestPath)
if err := b.moveLocalFile(info, sdkDestPath); err != nil {
return "", err
}
return path, nil
}
}

Expand Down Expand Up @@ -305,24 +311,24 @@ func (b *Sdk) getLocalSdkPackage(version Version) (*Package, error) {
Name: b.Plugin.Name,
Version: version,
}
var additional []*Info
var additions []*Info
dir, err := os.ReadDir(versionPath)
if err != nil {
return nil, err
}
for _, d := range dir {
if d.IsDir() {
split := strings.SplitN(d.Name(), "-", 2)
if len(split) != 2 {
continue
}
name := split[0]
v := split[1]
if name == b.Plugin.Name {
mainSdk.Path = filepath.Join(versionPath, d.Name())
continue
}
additional = append(additional, &Info{
if len(split) != 2 {
continue
}
v := split[1]
additions = append(additions, &Info{
Name: name,
Version: Version(v),
Path: filepath.Join(versionPath, d.Name()),
Expand All @@ -338,8 +344,8 @@ func (b *Sdk) getLocalSdkPackage(version Version) (*Package, error) {

}
return &Package{
Main: mainSdk,
Additional: additional,
Main: mainSdk,
Additions: additions,
}, nil
}

Expand All @@ -356,7 +362,7 @@ func (b *Sdk) Download(u *url.URL) (string, error) {
if err != nil {
return "", err
}
resp, err := http.DefaultClient.Do(req)
resp, err := b.sdkManager.httpClient().Do(req)
if err != nil {
var urlErr *url.Error
if errors.As(err, &urlErr) {
Expand Down
24 changes: 23 additions & 1 deletion template.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ function PLUGIN:PreInstall(ctx)
sha1 = "xxx",
--- sha512 checksum [optional]
sha512 = "xx",
--- additional need files [optional]
addition = {
{
--- additional file name !
name = "xxx",
--- remote URL or local file path [optional]
url = "xxx",
--- SHA256 checksum [optional]
sha256 = "xxx",
--- md5 checksum [optional]
md5= "xxx",
--- sha1 checksum [optional]
sha1 = "xxx",
--- sha512 checksum [optional]
sha512 = "xx",
}
}
}
end

Expand Down Expand Up @@ -81,9 +98,14 @@ end
--- This allows plugins to define custom environment variables (including PATH settings)
--- Note: Be sure to distinguish between environment variable settings for different platforms!
--- @param ctx table Context information
--- @field ctx.version_path string SDK installation directory
--- @field ctx.path string SDK installation directory
function PLUGIN:EnvKeys(ctx)
--- this variable is same as ctx.sdkInfo['plugin-name'].path
local mainPath = ctx.path
local sdkInfo = ctx.sdkInfo['sdk-name']
local path = sdkInfo.path
local version = sdkInfo.version
local name = sdkInfo.name
return {
{
key = "JAVA_HOME",
Expand Down

0 comments on commit b41c2e6

Please sign in to comment.