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: Improve app scaffolding #3839

Merged
merged 37 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
3a9b9ae
Add new structure for app scaffolding
Ehsan-saradar Dec 12, 2023
b918557
Fix some minor issues
Ehsan-saradar Dec 12, 2023
3ccbabe
Fix integration test plush
Ehsan-saradar Dec 12, 2023
668677d
Add go mod tidy and fmt to scaffolding apps
Ehsan-saradar Dec 13, 2023
50876ca
Add changelog
Ehsan-saradar Dec 13, 2023
c3f3871
Fix tests
Ehsan-saradar Dec 13, 2023
5c3402d
Merge branch 'main' into feat/improve-app-scaffolding
Pantani Dec 15, 2023
d3bfc84
Fix integration test package name
Ehsan-saradar Dec 18, 2023
723567c
New app.ignite.yml format
Ehsan-saradar Dec 18, 2023
d072306
Add AppYML
Ehsan-saradar Dec 19, 2023
0a4c8cf
Merge branch 'main' into feat/improve-app-scaffolding
Ehsan-saradar Dec 20, 2023
badaf84
Add go.work.sum to app scaffold .gitignore
Ehsan-saradar Jan 11, 2024
5e677f2
Upgrade app scaffold go.mod
Ehsan-saradar Jan 11, 2024
80e32b2
Rename AppYML to AppsConfig
Ehsan-saradar Jan 11, 2024
72e9b61
Merge branch 'main' into feat/improve-app-scaffolding
Ehsan-saradar Jan 11, 2024
b7d7d04
Add additional test for app scaffolding
Ehsan-saradar Jan 11, 2024
1448267
Fix changelog
Ehsan-saradar Jan 14, 2024
83f59d7
Improve app testing
Ehsan-saradar Jan 14, 2024
aa13f48
Fix app_test.go
Ehsan-saradar Jan 15, 2024
cad5b61
Move to cobra independent arch for app scaffolding
Ehsan-saradar Jan 15, 2024
80ae49c
Fix plugin test
Ehsan-saradar Jan 16, 2024
8f0b1b2
Merge branch 'main' into feat/improve-app-scaffolding
Ehsan-saradar Jan 16, 2024
b1a4a92
Fix changelog
Ehsan-saradar Jan 16, 2024
191ff97
Increase app scaffold integration test time
Ehsan-saradar Jan 16, 2024
d2b2a72
Update ignite/services/plugin/template/cmd/hello.go.plush
Ehsan-saradar Jan 16, 2024
04bbdc6
Merge branch 'main' into feat/improve-app-scaffolding
Ehsan-saradar Jan 16, 2024
eb31fb1
Merge branch 'main' into feat/improve-app-scaffolding
Pantani Jan 16, 2024
409ec7f
Update ignite/services/plugin/template/integration/app_test.go.plush
Ehsan-saradar Jan 17, 2024
8b47fc8
Update ignite/services/plugin/template/cmd/cmd.go.plush
Ehsan-saradar Jan 17, 2024
e90fd2e
Update ignite/services/plugin/template/app.ignite.yml.plush
Ehsan-saradar Jan 17, 2024
3ae61ed
Update ignite/services/plugin/template/.gitignore.plush
Ehsan-saradar Jan 17, 2024
79f2474
Add .ign files to .gitignore template
Ehsan-saradar Jan 20, 2024
316f534
Merge branch 'main' into feat/improve-app-scaffolding
Ehsan-saradar Jan 21, 2024
99a08d5
Merge branch 'main' into feat/improve-app-scaffolding
Pantani Jan 29, 2024
b6c324a
fix merge ignite/services/plugin/template/.gitignore.plush
Pantani Jan 29, 2024
b2a5e66
Merge branch 'main' into feat/improve-app-scaffolding
Pantani Jan 29, 2024
43a7fa1
Merge branch 'main' into feat/improve-app-scaffolding
mergify[bot] Jan 29, 2024
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 @@ -5,6 +5,7 @@
### Features

- [#3830](https://github.com/ignite/cli/pull/3830) Remove gRPC info from Ignite Apps errors
- [#3839](https://github.com/ignite/cli/pull/3839) New structure for app scaffolding
Ehsan-saradar marked this conversation as resolved.
Show resolved Hide resolved

### Changes

Expand Down
2 changes: 1 addition & 1 deletion ignite/services/plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ func TestPluginLoad(t *testing.T) {
manifest, err := p.Interface.Manifest(ctx)
require.NoError(err)
assert.Equal(p.name, manifest.Name)
assert.NoError(p.Interface.Execute(ctx, &ExecutedCommand{}, clientAPI))
assert.NoError(p.Interface.Execute(ctx, &ExecutedCommand{OsArgs: []string{"ignite", p.name}}, clientAPI))
jeronimoalbi marked this conversation as resolved.
Show resolved Hide resolved
assert.NoError(p.Interface.ExecuteHookPre(ctx, &ExecutedHook{}, clientAPI))
assert.NoError(p.Interface.ExecuteHookPost(ctx, &ExecutedHook{}, clientAPI))
assert.NoError(p.Interface.ExecuteHookCleanUp(ctx, &ExecutedHook{}, clientAPI))
Expand Down
17 changes: 17 additions & 0 deletions ignite/services/plugin/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ import (
"os"
"path"
"path/filepath"
"strings"

"github.com/gobuffalo/genny/v2"
"github.com/gobuffalo/plush/v4"
"golang.org/x/text/cases"
"golang.org/x/text/language"

"github.com/ignite/cli/v28/ignite/pkg/errors"
"github.com/ignite/cli/v28/ignite/pkg/gocmd"
"github.com/ignite/cli/v28/ignite/pkg/xgenny"
)

Expand All @@ -21,6 +25,7 @@ var fsPluginSource embed.FS
func Scaffold(ctx context.Context, dir, moduleName string, sharedHost bool) (string, error) {
var (
name = filepath.Base(moduleName)
title = toTitle(name)
finalDir = path.Join(dir, name)
g = genny.New()
template = xgenny.NewEmbedWalker(
Expand All @@ -42,6 +47,7 @@ func Scaffold(ctx context.Context, dir, moduleName string, sharedHost bool) (str
pctx := plush.NewContextWithContext(ctx)
pctx.Set("ModuleName", moduleName)
pctx.Set("Name", name)
pctx.Set("Title", title)
pctx.Set("SharedHost", sharedHost)

g.Transformer(xgenny.Transformer(pctx))
Expand All @@ -55,5 +61,16 @@ func Scaffold(ctx context.Context, dir, moduleName string, sharedHost bool) (str
return "", errors.WithStack(err)
}

if err := gocmd.ModTidy(ctx, finalDir); err != nil {
return "", errors.WithStack(err)
}
if err := gocmd.Fmt(ctx, finalDir); err != nil {
return "", errors.WithStack(err)
}

return finalDir, nil
}

func toTitle(s string) string {
return strings.ReplaceAll(strings.ReplaceAll(cases.Title(language.English).String(s), "_", ""), "-", "")
}
20 changes: 19 additions & 1 deletion ignite/services/plugin/template/.gitignore.plush
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
<%= Name %>
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
*.app

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work
3 changes: 3 additions & 0 deletions ignite/services/plugin/template/app.ignite.yml.plush
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= Name %>:
description: <%= Name %> is an awesome Ignite application!
path: ./
23 changes: 23 additions & 0 deletions ignite/services/plugin/template/cmd/cmd.go.plush
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

// New<%= Title %> creates a new <%= Name %> command.
func New<%= Title %>() *cobra.Command {
c := &cobra.Command{
Use: "<%= Name %>",
Short: "Say hello to the world of ignite!",
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("Hello, world!")
return nil
},
}

return c
}
Ehsan-saradar marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions ignite/services/plugin/template/go.mod.plush
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ go 1.21
require (
github.com/hashicorp/go-plugin v1.5.0
github.com/ignite/cli/v28 v28.0.0
github.com/spf13/cobra v1.7.0
Ehsan-saradar marked this conversation as resolved.
Show resolved Hide resolved
github.com/stretchr/testify v1.8.4
)
Ehsan-saradar marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package integration_test

import (
"bytes"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
Ehsan-saradar marked this conversation as resolved.
Show resolved Hide resolved
"github.com/stretchr/testify/require"

pluginsconfig "github.com/ignite/cli/v28/ignite/config/plugins"
"github.com/ignite/cli/v28/ignite/pkg/cmdrunner/step"
"github.com/ignite/cli/v28/ignite/services/plugin"
envtest "github.com/ignite/cli/v28/integration"
)

func Test<%= Title %>(t *testing.T) {
var (
require = require.New(t)
assert = assert.New(t)
env = envtest.New(t)
app = env.Scaffold("github.com/test/test")

assertPlugins = func(expectedLocalPlugins, expectedGlobalPlugins []pluginsconfig.Plugin) {
localCfg, err := pluginsconfig.ParseDir(app.SourcePath())
Ehsan-saradar marked this conversation as resolved.
Show resolved Hide resolved
require.NoError(err)
assert.ElementsMatch(expectedLocalPlugins, localCfg.Apps, "unexpected local apps")

globalCfgPath, err := plugin.PluginsPath()
require.NoError(err)
globalCfg, err := pluginsconfig.ParseDir(globalCfgPath)
require.NoError(err)
assert.ElementsMatch(expectedGlobalPlugins, globalCfg.Apps, "unexpected global apps")
}
)

dir, err := os.Getwd()
require.NoError(err)
pluginPath := filepath.Join(filepath.Dir(filepath.Dir(dir)), "<%= Name %>")

env.Must(env.Exec("add <%= Name %> plugin",
Ehsan-saradar marked this conversation as resolved.
Show resolved Hide resolved
step.NewSteps(step.New(
step.Exec(envtest.IgniteApp, "app", "install", "-g", pluginPath),
step.Workdir(app.SourcePath()),
)),
))

// one local plugin expected
assertPlugins(
nil,
[]pluginsconfig.Plugin{
{
Path: pluginPath,
},
},
)

buf := &bytes.Buffer{}
env.Must(env.Exec("run <%= Name %>",
step.NewSteps(step.New(
step.Exec(
envtest.IgniteApp,
"<%= Name %>",
),
step.Workdir(app.SourcePath()),
step.Stdout(buf),
)),
))
assert.Equal("Hello, world!\n", buf.String())
}
112 changes: 20 additions & 92 deletions ignite/services/plugin/template/main.go.plush
Original file line number Diff line number Diff line change
Expand Up @@ -2,117 +2,45 @@ package main

import (
"context"
"fmt"
"path/filepath"
"os"

hplugin "github.com/hashicorp/go-plugin"

"github.com/ignite/cli/v28/ignite/services/chain"
"github.com/ignite/cli/v28/ignite/services/plugin"
"<%= ModuleName %>/cmd"
)

type app struct{}

func (app) Manifest(ctx context.Context) (*plugin.Manifest, error) {
return &plugin.Manifest{
Name: "<%= Name %>",
// TODO: Add commands here
Commands: []*plugin.Command{
// Example of a command
{
Use: "<%= Name %>",
Short: "Explain what the command is doing...",
Long: "Long description goes here...",
Flags: []*plugin.Flag{
{Name: "my-flag", Type: plugin.FlagTypeString, Usage: "my flag description"},
},
PlaceCommandUnder: "ignite",
// Examples of adding subcommands:
// Commands: []*plugin.Command{
// {Use: "add"},
// {Use: "list"},
// {Use: "delete"},
// },
},
},
// TODO: Add hooks here
Hooks: []*plugin.Hook{},
SharedHost: <%= SharedHost %>,
}, nil
}

func (app) Execute(ctx context.Context, cmd *plugin.ExecutedCommand, api plugin.ClientAPI) error {
// TODO: write command execution here
fmt.Printf("Hello I'm the example-plugin plugin\n")
fmt.Printf("My executed command: %q\n", cmd.Path)
fmt.Printf("My args: %v\n", cmd.Args)

flags, err := cmd.NewFlags()
if err != nil {
return err
func (app) Manifest(_ context.Context) (*plugin.Manifest, error) {
m := &plugin.Manifest{
Name: "<%= Name %>",<%= if (SharedHost) { %>
SharedHost: true,<% } %>
}

myFlag, _ := flags.GetString("my-flag")
Ehsan-saradar marked this conversation as resolved.
Show resolved Hide resolved
fmt.Printf("My flags: my-flag=%q\n", myFlag)
fmt.Printf("My config parameters: %v\n", cmd.With)

// This is how the plugin can access the chain:
// c, err := getChain(cmd)
// if err != nil {
// return err
// }

// According to the number of declared commands, you may need a switch:
/*
switch cmd.Use {
case "add":
fmt.Println("Adding stuff...")
case "list":
fmt.Println("Listing stuff...")
case "delete":
fmt.Println("Deleting stuff...")
}
*/

// ClientAPI call example
fmt.Println(api.GetChainInfo(ctx))
Ehsan-saradar marked this conversation as resolved.
Show resolved Hide resolved

return nil
m.ImportCobraCommand(cmd.New<%= Title %>(), "ignite")
Ehsan-saradar marked this conversation as resolved.
Show resolved Hide resolved
return m, nil
}

func (app) ExecuteHookPre(ctx context.Context, h *plugin.ExecutedHook, api plugin.ClientAPI) error {
fmt.Printf("Executing hook pre %q\n", h.Hook.GetName())
return nil
func (app) Execute(_ context.Context, c *plugin.ExecutedCommand, _ plugin.ClientAPI) error {
// Instead of a switch on c.Use, we run the root command like if
// we were in a command line context. This implies to set os.Args
// correctly.
// Remove the first arg "ignite" from OSArgs because our <%= Name %>
// command root is "<%= Name %>" not "ignite".
os.Args = c.OsArgs[1:]
return cmd.New<%= Title %>().Execute()
}

func (app) ExecuteHookPost(ctx context.Context, h *plugin.ExecutedHook, api plugin.ClientAPI) error {
fmt.Printf("Executing hook post %q\n", h.Hook.GetName())
func (app) ExecuteHookPre(_ context.Context, _ *plugin.ExecutedHook, _ plugin.ClientAPI) error {
return nil
}

func (app) ExecuteHookCleanUp(ctx context.Context, h *plugin.ExecutedHook, api plugin.ClientAPI) error {
fmt.Printf("Executing hook cleanup %q\n", h.Hook.GetName())
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved
func (app) ExecuteHookPost(_ context.Context, _ *plugin.ExecutedHook, _ plugin.ClientAPI) error {
return nil
}

func getChain(cmd *plugin.ExecutedCommand, chainOption ...chain.Option) (*chain.Chain, error) {
Ehsan-saradar marked this conversation as resolved.
Show resolved Hide resolved
flags, err := cmd.NewFlags()
if err != nil {
return nil, err
}

var (
home, _ = flags.GetString("home")
path, _ = flags.GetString("path")
)
if home != "" {
chainOption = append(chainOption, chain.HomePath(home))
}
absPath, err := filepath.Abs(path)
if err != nil {
return nil, err
}
return chain.New(absPath, chainOption...)
func (app) ExecuteHookCleanUp(_ context.Context, _ *plugin.ExecutedHook, _ plugin.ClientAPI) error {
return nil
Ehsan-saradar marked this conversation as resolved.
Show resolved Hide resolved
}

func main() {
Expand Down
Loading