mirror of https://gitee.com/answerdev/answer.git
feat(plugin): Support for specifying different plugin version
This commit is contained in:
parent
2fe00d4630
commit
3fbecefdf3
2
go.mod
2
go.mod
|
@ -4,6 +4,7 @@ go 1.18
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/Chain-Zhang/pinyin v0.1.3
|
github.com/Chain-Zhang/pinyin v0.1.3
|
||||||
|
github.com/Masterminds/semver/v3 v3.1.1
|
||||||
github.com/anargu/gin-brotli v0.0.0-20220116052358-12bf532d5267
|
github.com/anargu/gin-brotli v0.0.0-20220116052358-12bf532d5267
|
||||||
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d
|
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d
|
||||||
github.com/bwmarrin/snowflake v0.3.0
|
github.com/bwmarrin/snowflake v0.3.0
|
||||||
|
@ -141,4 +142,5 @@ require (
|
||||||
|
|
||||||
// github action runner Sometimes it will time out.
|
// github action runner Sometimes it will time out.
|
||||||
replace gitee.com/travelliu/dm v1.8.11192 => github.com/aichy126/dm v1.8.11192
|
replace gitee.com/travelliu/dm v1.8.11192 => github.com/aichy126/dm v1.8.11192
|
||||||
|
|
||||||
replace modernc.org/z v1.2.19 => github.com/aichy126/modernc.org_z v1.2.19
|
replace modernc.org/z v1.2.19 => github.com/aichy126/modernc.org_z v1.2.19
|
||||||
|
|
5
go.sum
5
go.sum
|
@ -51,6 +51,7 @@ github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc
|
||||||
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
|
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
|
||||||
github.com/LinkinStars/go-i18n/v2 v2.2.2 h1:ZfjpzbW13dv6btv3RALKZkpN9A+7K1JA//2QcNeWaxU=
|
github.com/LinkinStars/go-i18n/v2 v2.2.2 h1:ZfjpzbW13dv6btv3RALKZkpN9A+7K1JA//2QcNeWaxU=
|
||||||
github.com/LinkinStars/go-i18n/v2 v2.2.2/go.mod h1:hLglSJ4/3M0Y7ZVcoEJI+OwqkglHCA32DdjuJJR2LbM=
|
github.com/LinkinStars/go-i18n/v2 v2.2.2/go.mod h1:hLglSJ4/3M0Y7ZVcoEJI+OwqkglHCA32DdjuJJR2LbM=
|
||||||
|
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
|
||||||
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
|
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
|
||||||
github.com/Microsoft/go-winio v0.5.2 h1:a9IhgEQBCUEk6QCdml9CiJGhAws+YwffDHEMp1VMrpA=
|
github.com/Microsoft/go-winio v0.5.2 h1:a9IhgEQBCUEk6QCdml9CiJGhAws+YwffDHEMp1VMrpA=
|
||||||
github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY=
|
github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY=
|
||||||
|
@ -64,6 +65,8 @@ github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/
|
||||||
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c=
|
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c=
|
||||||
github.com/agiledragon/gomonkey/v2 v2.3.1/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoaWLA6jbbgxfB4w2iY=
|
github.com/agiledragon/gomonkey/v2 v2.3.1/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoaWLA6jbbgxfB4w2iY=
|
||||||
github.com/aichy126/dm v1.8.11192/go.mod h1:DHTzyhCrM843x9VdKVbZ+GKXGRbKM2sJ4LxihRxShkE=
|
github.com/aichy126/dm v1.8.11192/go.mod h1:DHTzyhCrM843x9VdKVbZ+GKXGRbKM2sJ4LxihRxShkE=
|
||||||
|
github.com/aichy126/modernc.org_z v1.2.19 h1:g4KvQojkFWBJk47OGvKzuudTr5mRF7jORSwobYyc2Sc=
|
||||||
|
github.com/aichy126/modernc.org_z v1.2.19/go.mod h1:+ZpP0pc4zz97eukOzW3xagV/lS82IpPN9NGG5pNF9vY=
|
||||||
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
|
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
|
||||||
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
|
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
|
||||||
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
|
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
|
||||||
|
@ -1290,8 +1293,6 @@ modernc.org/tcl v1.8.13 h1:V0sTNBw0Re86PvXZxuCub3oO9WrSTqALgrwNZNvLFGw=
|
||||||
modernc.org/tcl v1.8.13/go.mod h1:V+q/Ef0IJaNUSECieLU4o+8IScapxnMyFV6i/7uQlAY=
|
modernc.org/tcl v1.8.13/go.mod h1:V+q/Ef0IJaNUSECieLU4o+8IScapxnMyFV6i/7uQlAY=
|
||||||
modernc.org/token v1.0.0 h1:a0jaWiNMDhDUtqOj09wvjWWAqd3q7WpBulmL9H2egsk=
|
modernc.org/token v1.0.0 h1:a0jaWiNMDhDUtqOj09wvjWWAqd3q7WpBulmL9H2egsk=
|
||||||
modernc.org/token v1.0.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
|
modernc.org/token v1.0.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
|
||||||
modernc.org/z v1.2.19 h1:BGyRFWhDVn5LFS5OcX4Yd/MlpRTOc7hOPTdcIpCiUao=
|
|
||||||
modernc.org/z v1.2.19/go.mod h1:+ZpP0pc4zz97eukOzW3xagV/lS82IpPN9NGG5pNF9vY=
|
|
||||||
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
|
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
|
||||||
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
|
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
|
||||||
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
|
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
|
||||||
|
|
|
@ -6,22 +6,52 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
|
"github.com/Masterminds/semver/v3"
|
||||||
"github.com/answerdev/answer/pkg/dir"
|
"github.com/answerdev/answer/pkg/dir"
|
||||||
"github.com/answerdev/answer/pkg/writer"
|
"github.com/answerdev/answer/pkg/writer"
|
||||||
"github.com/answerdev/answer/ui"
|
"github.com/answerdev/answer/ui"
|
||||||
cp "github.com/otiai10/copy"
|
cp "github.com/otiai10/copy"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
mainGoTpl = `package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
answercmd "github.com/answerdev/answer/cmd"
|
||||||
|
|
||||||
|
// remote plugins
|
||||||
|
{{- range .remote_plugins}}
|
||||||
|
_ "{{.}}"
|
||||||
|
{{- end}}
|
||||||
|
|
||||||
|
// local plugins
|
||||||
|
{{- range .local_plugins}}
|
||||||
|
_ "answer/{{.}}"
|
||||||
|
{{- end}}
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
answercmd.Main()
|
||||||
|
}
|
||||||
|
`
|
||||||
|
goModTpl = `module answer
|
||||||
|
|
||||||
|
go 1.19
|
||||||
|
`
|
||||||
|
)
|
||||||
|
|
||||||
type answerBuilder struct {
|
type answerBuilder struct {
|
||||||
buildingMaterial *buildingMaterial
|
buildingMaterial *buildingMaterial
|
||||||
BuildError error
|
BuildError error
|
||||||
}
|
}
|
||||||
|
|
||||||
type buildingMaterial struct {
|
type buildingMaterial struct {
|
||||||
|
answerModuleReplacement string
|
||||||
plugins []*pluginInfo
|
plugins []*pluginInfo
|
||||||
outputPath string
|
outputPath string
|
||||||
tmpDir string
|
tmpDir string
|
||||||
|
@ -44,17 +74,15 @@ type pluginInfo struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func newAnswerBuilder(outputPath string, plugins []string, originalAnswerInfo OriginalAnswerInfo) *answerBuilder {
|
func newAnswerBuilder(outputPath string, plugins []string, originalAnswerInfo OriginalAnswerInfo) *answerBuilder {
|
||||||
|
material := &buildingMaterial{originalAnswerInfo: originalAnswerInfo}
|
||||||
parentDir, _ := filepath.Abs(".")
|
parentDir, _ := filepath.Abs(".")
|
||||||
tmpDir, _ := os.MkdirTemp(parentDir, "answer_build")
|
material.tmpDir, _ = os.MkdirTemp(parentDir, "answer_build")
|
||||||
if len(outputPath) == 0 {
|
if len(outputPath) == 0 {
|
||||||
outputPath = filepath.Join(parentDir, "new_answer")
|
outputPath = filepath.Join(parentDir, "new_answer")
|
||||||
}
|
}
|
||||||
material := &buildingMaterial{
|
material.outputPath = outputPath
|
||||||
plugins: formatPlugins(plugins),
|
material.plugins = formatPlugins(plugins)
|
||||||
outputPath: outputPath,
|
material.answerModuleReplacement = os.Getenv("ANSWER_MODULE")
|
||||||
tmpDir: tmpDir,
|
|
||||||
originalAnswerInfo: originalAnswerInfo,
|
|
||||||
}
|
|
||||||
return &answerBuilder{
|
return &answerBuilder{
|
||||||
buildingMaterial: material,
|
buildingMaterial: material,
|
||||||
}
|
}
|
||||||
|
@ -103,7 +131,7 @@ func createMainGoFile(b *buildingMaterial) (err error) {
|
||||||
)
|
)
|
||||||
for _, p := range b.plugins {
|
for _, p := range b.plugins {
|
||||||
if len(p.Path) == 0 {
|
if len(p.Path) == 0 {
|
||||||
remotePlugins = append(remotePlugins, p.Name)
|
remotePlugins = append(remotePlugins, versionedModulePath(p.Name, p.Version))
|
||||||
} else {
|
} else {
|
||||||
localPluginDir := filepath.Base(p.Path)
|
localPluginDir := filepath.Base(p.Path)
|
||||||
localPlugins = append(localPlugins, localPluginDir)
|
localPlugins = append(localPlugins, localPluginDir)
|
||||||
|
@ -139,6 +167,15 @@ func createMainGoFile(b *buildingMaterial) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func downloadGoModFile(b *buildingMaterial) (err error) {
|
func downloadGoModFile(b *buildingMaterial) (err error) {
|
||||||
|
// If user specify a module replacement, use it. Otherwise, use the latest version.
|
||||||
|
if len(b.answerModuleReplacement) > 0 {
|
||||||
|
replacement := fmt.Sprintf("%s=%s", "github.com/answerdev/answer@latest", b.answerModuleReplacement)
|
||||||
|
err = b.newExecCmd("go", "mod", "edit", "-replace", replacement).Run()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
err = b.newExecCmd("go", "mod", "tidy").Run()
|
err = b.newExecCmd("go", "mod", "tidy").Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -215,31 +252,17 @@ func (b *buildingMaterial) newExecCmd(command string, args ...string) *exec.Cmd
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
func versionedModulePath(modulePath, moduleVersion string) string {
|
||||||
mainGoTpl = `package main
|
if moduleVersion == "" {
|
||||||
|
return modulePath
|
||||||
import (
|
}
|
||||||
answercmd "github.com/answerdev/answer/cmd"
|
ver, err := semver.StrictNewVersion(strings.TrimPrefix(moduleVersion, "v"))
|
||||||
|
if err != nil {
|
||||||
// remote plugins
|
return modulePath
|
||||||
{{- range .remote_plugins}}
|
}
|
||||||
_ "{{.}}"
|
major := ver.Major()
|
||||||
{{- end}}
|
if major > 1 {
|
||||||
|
modulePath += fmt.Sprintf("/v%d", major)
|
||||||
// local plugins
|
}
|
||||||
{{- range .local_plugins}}
|
return path.Clean(modulePath)
|
||||||
_ "answer/{{.}}"
|
|
||||||
{{- end}}
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
answercmd.Main()
|
|
||||||
}
|
}
|
||||||
`
|
|
||||||
goModTpl = `module answer
|
|
||||||
|
|
||||||
go 1.19
|
|
||||||
|
|
||||||
replace github.com/answerdev/answer latest => github.com/answerdev/answer feature-plugin
|
|
||||||
`
|
|
||||||
)
|
|
||||||
|
|
Loading…
Reference in New Issue