Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: plugin system hooks updated #3038

Merged
merged 24 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
03fa041
migration to newb branch after updating develop
Nov 1, 2022
11291ec
update to changelog
Nov 2, 2022
2ba4dfd
moving command prefix to const
Nov 2, 2022
e1f8479
Merge branch 'develop' into feat/plugin-system-hooks-updated
Nov 2, 2022
0c33027
update to commit hash for replace
Nov 2, 2022
0eaa37b
Merge branch 'feat/plugin-system-hooks-updated' of github.com:joshLon…
Nov 2, 2022
d549ab6
fixes to comment structure
Nov 2, 2022
10642df
reinforce TestLinkPluginHooks
tbruyelle Nov 2, 2022
531bfe5
chore: use latest hash for scaffolded plugin
tbruyelle Nov 2, 2022
e7d920c
pr comments
Nov 3, 2022
4268155
fix to error formatting
Nov 3, 2022
d281260
addition of error field in test struct
joshLong145 Nov 3, 2022
e046a6e
addition of arg testing in cmd plugin tests
joshLong145 Nov 3, 2022
3b4bae7
registering of hook struct on init of plugin
joshLong145 Nov 3, 2022
2527937
Merge remote-tracking branch 'upstream/develop' into feat/plugin-syst…
joshLong145 Nov 3, 2022
156982e
change of flag property to args
joshLong145 Nov 4, 2022
a9d290f
Merge branch 'develop' of github.com:ignite/cli into feat/plugin-syst…
joshLong145 Nov 4, 2022
9c6b3aa
Merge branch 'develop' into feat/plugin-system-hooks-updated
Nov 4, 2022
4f7c672
Merge branch 'develop' into feat/plugin-system-hooks-updated
Nov 4, 2022
d0a33f4
Merge branch 'develop' into feat/plugin-system-hooks-updated
Nov 4, 2022
abe9849
Merge branch 'develop' into feat/plugin-system-hooks-updated
Nov 6, 2022
facacb9
Merge branch 'develop' into feat/plugin-system-hooks-updated
tbruyelle Nov 7, 2022
0cec0d3
fix cl
tbruyelle Nov 7, 2022
22af92b
Merge branch 'develop' into feat/plugin-system-hooks-updated
tbruyelle Nov 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Features

- [#3038](https://github.com/ignite/cli/pull/3038) Addition of Plugin Hooks in Plugin System
- [#3056](https://github.com/ignite/cli/pull/3056) Add `--genesis-config` flag option to `ignite network chain publish`
- [#2892](https://github.com/ignite/cli/pull/2982/) Add `ignite scaffold react` command.
- [#2892](https://github.com/ignite/cli/pull/2982/) Add `ignite generate composables` command.
Expand Down
88 changes: 87 additions & 1 deletion ignite/cmd/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import (
// A global variable is used so the list is accessible to the plugin commands.
var plugins []*plugin.Plugin

const (
igniteCmdPrefix = "ignite "
)

// LoadPlugins tries to load all the plugins found in configuration.
// If no configuration found, it returns w/o error.
func LoadPlugins(ctx context.Context, rootCmd *cobra.Command) error {
Expand All @@ -35,6 +39,10 @@ func LoadPlugins(ctx context.Context, rootCmd *cobra.Command) error {
// Link plugins to related commands
var loadErrors []string
for _, p := range plugins {
linkPluginHooks(rootCmd, p)
if p.Error != nil {
loadErrors = append(loadErrors, p.Path)
}
linkPluginCmds(rootCmd, p)
if p.Error != nil {
loadErrors = append(loadErrors, p.Path)
Expand All @@ -57,6 +65,84 @@ func UnloadPlugins() {
}
}

func linkPluginHooks(rootCmd *cobra.Command, p *plugin.Plugin) {
if p.Error != nil {
return
}

for _, hook := range p.Interface.Hooks() {
linkPluginHook(rootCmd, p, hook)
}
}

func linkPluginHook(rootCmd *cobra.Command, p *plugin.Plugin, hook plugin.Hook) {
cmdPath := hook.PlaceHookOn

if !strings.HasPrefix(cmdPath, "ignite") {
// cmdPath must start with `ignite ` before comparison with
// cmd.CommandPath()
cmdPath = igniteCmdPrefix + cmdPath
}

cmdPath = strings.TrimSpace(cmdPath)

cmd := findCommandByPath(rootCmd, cmdPath)

if cmd == nil {
p.Error = errors.Errorf("unable to find commandPath %q for plugin hook %q", cmdPath, hook.Name)
return
}

if !cmd.Runnable() {
p.Error = errors.Errorf("can't attach plugin hook %q to non executable command %q", hook.Name, hook.PlaceHookOn)
return
}

preRun := cmd.PreRunE
cmd.PreRunE = func(cmd *cobra.Command, args []string) error {
if preRun != nil {
err := preRun(cmd, args)
if err != nil {
return err
}
}

return p.Interface.ExecuteHookPre(hook, args)
}

runCmd := cmd.RunE

cmd.RunE = func(cmd *cobra.Command, args []string) error {
if runCmd != nil {
err := runCmd(cmd, args)
// if the command has failed the `PostRun` will not execute. here we execute the cleanup step before returnning.
if err != nil {
p.Interface.ExecuteHookCleanUp(hook, args)
}

return err
}

time.Sleep(100 * time.Millisecond)
return nil
}

postCmd := cmd.PostRunE
cmd.PostRunE = func(cmd *cobra.Command, args []string) error {
defer p.Interface.ExecuteHookCleanUp(hook, args)

if preRun != nil {
err := postCmd(cmd, args)
if err != nil {
// dont return the error, log it and let execution continue to `Run`
return err
}
}

return p.Interface.ExecuteHookPost(hook, args)
}
}

// linkPluginCmds tries to add the plugin commands to the legacy ignite
// commands.
func linkPluginCmds(rootCmd *cobra.Command, p *plugin.Plugin) {
Expand All @@ -76,7 +162,7 @@ func linkPluginCmd(rootCmd *cobra.Command, p *plugin.Plugin, pluginCmd plugin.Co
if !strings.HasPrefix(cmdPath, "ignite") {
// cmdPath must start with `ignite ` before comparison with
// cmd.CommandPath()
cmdPath = "ignite " + cmdPath
cmdPath = igniteCmdPrefix + cmdPath
}
cmdPath = strings.TrimSpace(cmdPath)

Expand Down
Loading