Skip to content

Commit 0bc5b1d

Browse files
committed
feat: Verify the minimum runtime version supported by the plug-in
In order to better upgrade the plug-in system in the future, plugins are now supported to specify the minimum runtime version that is compatible with running. If not configured, it means that the full version of the plug-in is available
1 parent 167de5d commit 0bc5b1d

File tree

7 files changed

+66
-35
lines changed

7 files changed

+66
-35
lines changed

cmd/cmd.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ import (
2020
"fmt"
2121
"github.com/urfave/cli/v2"
2222
"github.com/version-fox/vfox/cmd/commands"
23+
"github.com/version-fox/vfox/internal"
2324
"os"
2425
)
2526

26-
func Execute(version string, args []string) {
27-
newCmd(version).Execute(args)
27+
func Execute(args []string) {
28+
newCmd().Execute(args)
2829
}
2930

3031
type cmd struct {
@@ -39,8 +40,8 @@ func (c *cmd) Execute(args []string) {
3940
}
4041
}
4142

42-
func newCmd(version string) *cmd {
43-
43+
func newCmd() *cmd {
44+
version := internal.RuntimeVersion
4445
cli.VersionFlag = &cli.BoolFlag{
4546
Name: "version",
4647
Aliases: []string{"v", "V"},

internal/manager.go

+5
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@ func (m *Manager) Add(pluginName, url, alias string) error {
235235
}
236236
defer source.Close()
237237

238+
// Check if the plugin is compatible with the current runtime
239+
if source.MinRuntimeVersion != "" && util.CompareVersion(source.MinRuntimeVersion, RuntimeVersion) > 0 {
240+
return fmt.Errorf("check failed: this plugin is not compatible with current vfox (>= %s), please upgrade vfox version to latest", source.MinRuntimeVersion)
241+
}
242+
238243
pname := source.Name
239244
if len(alias) > 0 {
240245
pname = alias

internal/package.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2024 Han Li and contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package internal
18+
19+
import "path/filepath"
20+
21+
type Package struct {
22+
Main *Info
23+
Additional []*Info
24+
}
25+
26+
type Info struct {
27+
Name string
28+
Version Version
29+
Path string
30+
Note string
31+
Checksum *Checksum
32+
}
33+
34+
func (i *Info) label() string {
35+
return i.Name + "@" + string(i.Version)
36+
}
37+
38+
func (i *Info) storagePath(parentDir string) string {
39+
return filepath.Join(parentDir, i.Name+"-"+string(i.Version))
40+
}

internal/plugin.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@ type LuaPlugin struct {
3535
state *lua.LState
3636
pluginObj *lua.LTable
3737
// plugin source path
38-
SourcePath string
39-
Name string
40-
Author string
41-
Version string
42-
Description string
43-
UpdateUrl string
38+
SourcePath string
39+
Name string
40+
Author string
41+
Version string
42+
Description string
43+
UpdateUrl string
44+
MinRuntimeVersion string
4445
}
4546

4647
func (l *LuaPlugin) checkValid() error {
@@ -365,6 +366,9 @@ func NewLuaPlugin(content, path string, manager *Manager) (*LuaPlugin, error) {
365366
if author := PLUGIN.RawGetString("author"); author.Type() != lua.LTNil {
366367
source.Author = author.String()
367368
}
369+
if minRuntimeVersion := PLUGIN.RawGetString("minRuntimeVersion"); minRuntimeVersion.Type() != lua.LTNil {
370+
source.MinRuntimeVersion = minRuntimeVersion.String()
371+
}
368372
return source, nil
369373
}
370374

internal/version.go

+1-22
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,4 @@
1616

1717
package internal
1818

19-
import "path/filepath"
20-
21-
type Package struct {
22-
Main *Info
23-
Additional []*Info
24-
}
25-
26-
type Info struct {
27-
Name string
28-
Version Version
29-
Path string
30-
Note string
31-
Checksum *Checksum
32-
}
33-
34-
func (i *Info) label() string {
35-
return i.Name + "@" + string(i.Version)
36-
}
37-
38-
func (i *Info) storagePath(parentDir string) string {
39-
return filepath.Join(parentDir, i.Name+"-"+string(i.Version))
40-
}
19+
const RuntimeVersion = "0.2.3"

main.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ import (
2121
"os"
2222
)
2323

24-
const Version = "0.2.2"
25-
2624
func main() {
27-
cmd.Execute(Version, os.Args)
25+
cmd.Execute(os.Args)
2826
}

template.lua

+4
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ PLUGIN = {
1717
author = "Lihan",
1818
--- Plugin version
1919
version = "0.0.1",
20+
--- Plugin description
21+
description = "xxx",
2022
-- Update URL
2123
updateUrl = "{URL}/sdk.lua",
24+
-- minimum compatible vfox version
25+
minRuntimeVersion = "0.2.2",
2226
}
2327

2428
--- Returns some pre-installed information, such as version number, download address, local files, etc.

0 commit comments

Comments
 (0)