Skip to content

Commit

Permalink
mod: Adjust the plug-in structure to make it more flexible
Browse files Browse the repository at this point in the history
The PreInstall function only handles pre-installation information for the sdk, such as version number, download address, etc., except for the version number, which is not required.
The PostInstall function as an extension, such as compiling source code, etc.
  • Loading branch information
aooohan committed Feb 1, 2024
1 parent 8888f41 commit 167de5d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
18 changes: 14 additions & 4 deletions internal/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ type Sdk struct {
}

func (b *Sdk) Install(version Version) error {
label := b.label(version)
if b.checkExists(version) {
return fmt.Errorf("%s is already installed", label)
}
installInfo, err := b.Plugin.PreInstall(version)
if err != nil {
return fmt.Errorf("plugin [PreInstall] method error: %w", err)
Expand All @@ -63,7 +67,9 @@ func (b *Sdk) Install(version Version) error {
_ = os.RemoveAll(newDirPath)
}
}()
label := b.label(mainSdk.Version)
// A second check is required because the plug-in may change the version number,
// for example, latest is resolved to a specific version number.
label = b.label(mainSdk.Version)
if b.checkExists(mainSdk.Version) {
return fmt.Errorf("%s is already installed", label)
}
Expand Down Expand Up @@ -91,25 +97,26 @@ func (b *Sdk) Install(version Version) error {
})
}
}
success = true
err = b.Plugin.PostInstall(newDirPath, installedSdkInfos)
if err != nil {
return fmt.Errorf("plugin [PostInstall] method error: %w", err)
}
success = true
pterm.Printf("Install %s success! \n", pterm.LightGreen(label))
pterm.Printf("Please use %s to use it.\n", pterm.LightBlue(fmt.Sprintf("vfox use %s", label)))
return nil
}

func (b *Sdk) moveLocalFile(info *Info, sdkDestPath string) (string, error) {
path := filepath.Join(sdkDestPath, info.Name+"-"+string(info.Version))
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)
}
return path, nil
}

func (b *Sdk) moveRemoteFile(info *Info, sdkDestPath string) (string, error) {
u, err := url.Parse(info.Path)
label := info.label()
Expand Down Expand Up @@ -138,7 +145,7 @@ func (b *Sdk) moveRemoteFile(info *Info, sdkDestPath string) (string, error) {
return "", fmt.Errorf("unknown file type")
}
pterm.Printf("Unpacking %s...\n", filePath)
path := filepath.Join(sdkDestPath, info.Name+"-"+string(info.Version))
path := info.storagePath(sdkDestPath)
err = decompressor.Decompress(path)
if err != nil {
fmt.Printf("Unpack failed, err:%s", err.Error())
Expand All @@ -148,6 +155,9 @@ func (b *Sdk) moveRemoteFile(info *Info, sdkDestPath string) (string, error) {
}
func (b *Sdk) preInstallSdk(info *Info, sdkDestPath string) (string, error) {
pterm.Printf("Preinstalling %s...\n", info.label())
if info.Path == "" {
return info.storagePath(sdkDestPath), nil
}
if strings.HasPrefix(info.Path, "https://") || strings.HasPrefix(info.Path, "http://") {
return b.moveRemoteFile(info, sdkDestPath)
} else {
Expand Down
6 changes: 6 additions & 0 deletions internal/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package internal

import "path/filepath"

type Package struct {
Main *Info
Additional []*Info
Expand All @@ -32,3 +34,7 @@ type Info struct {
func (i *Info) label() string {
return i.Name + "@" + string(i.Version)
}

func (i *Info) storagePath(parentDir string) string {
return filepath.Join(parentDir, i.Name+"-"+string(i.Version))
}
13 changes: 9 additions & 4 deletions template.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,30 @@ PLUGIN = {
updateUrl = "{URL}/sdk.lua",
}

--- Return information about the specified version based on ctx.version, including version, download URL, etc.
--- Returns some pre-installed information, such as version number, download address, local files, etc.
--- If checksum is provided, vfox will automatically check it for you.
--- @param ctx table
--- @field ctx.version string User-input version
--- @return table Version information
function PLUGIN:PreInstall(ctx)
return {
--- Version number
version = "xxx",
--- Download URL
--- remote URL or local file path [optional]
url = "xxx",
--- SHA256 checksum
--- SHA256 checksum [optional]
sha256 = "xxx",
--- md5 checksum [optional]
md5= "xxx",
--- sha1 checksum [optional]
sha1 = "xxx",
--- sha512 checksum [optional]
sha512 = "xx",
}
end

--- Extension point, called after PreInstall, can perform additional operations,
--- such as file operations for the SDK installation directory
--- such as file operations for the SDK installation directory or compile source code
--- Currently can be left unimplemented!
function PLUGIN:PostInstall(ctx)
--- ctx.rootPath SDK installation directory
Expand Down

0 comments on commit 167de5d

Please sign in to comment.