From b41c2e662e8ac28b9c6f456e8fce8c12e3d21fff Mon Sep 17 00:00:00 2001 From: lihan Date: Fri, 2 Feb 2024 11:20:25 +0800 Subject: [PATCH] mod: Optimize the PostInstall and PreInstall structure of the plugin * 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. --- cmd/commands/search.go | 4 +-- internal/package.go | 7 ++-- internal/plugin.go | 43 +++++++++++++++--------- internal/sdk.go | 76 +++++++++++++++++++++++------------------- template.lua | 24 ++++++++++++- 5 files changed, 99 insertions(+), 55 deletions(-) diff --git a/cmd/commands/search.go b/cmd/commands/search.go index 68d3ad7f..c035ce25 100644 --- a/cmd/commands/search.go +++ b/cmd/commands/search.go @@ -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, ",")) diff --git a/internal/package.go b/internal/package.go index 0b85b0c9..a8519604 100644 --- a/internal/package.go +++ b/internal/package.go @@ -19,8 +19,8 @@ package internal import "path/filepath" type Package struct { - Main *Info - Additional []*Info + Main *Info + Additions []*Info } type Info struct { @@ -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)) } diff --git a/internal/plugin.go b/internal/plugin.go index 2e2b2d82..313b5121 100644 --- a/internal/plugin.go +++ b/internal/plugin.go @@ -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, }) }) @@ -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") @@ -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 { @@ -195,8 +203,8 @@ func (l *LuaPlugin) PreInstall(version Version) (*Package, error) { } return &Package{ - Main: mainSdk, - Additional: additionalArr, + Main: mainSdk, + Additions: additionalArr, }, nil } @@ -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() @@ -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, diff --git a/internal/sdk.go b/internal/sdk.go index a1b38238..be382057 100644 --- a/internal/sdk.go +++ b/internal/sdk.go @@ -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 @@ -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 @@ -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 } } @@ -305,7 +311,7 @@ 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 @@ -313,16 +319,16 @@ func (b *Sdk) getLocalSdkPackage(version Version) (*Package, error) { 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()), @@ -338,8 +344,8 @@ func (b *Sdk) getLocalSdkPackage(version Version) (*Package, error) { } return &Package{ - Main: mainSdk, - Additional: additional, + Main: mainSdk, + Additions: additions, }, nil } @@ -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) { diff --git a/template.lua b/template.lua index c709fbe1..ae12f17c 100644 --- a/template.lua +++ b/template.lua @@ -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 @@ -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",