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

add initial template cmds to cli #160

Merged
merged 6 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 8 additions & 0 deletions demo/rules.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
features:
- name: api
scopes:
- match: module
documents:
- source: api.go.tpl
target: "{{snake .Module.Name}}.go"

53 changes: 53 additions & 0 deletions demo/templates/api.go.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package {{snake .Module.ShortName}}


{{- range .Module.Externs }}
// extern {{.Name}}
{{ $import := .Meta.GetString "go.module" }}
{{- if $import }}
import "{{$import}}"
{{- end }}
{{- end }}

{{- range .Module.Enums }}

type {{Camel .Name}} int

{{ $enum := . }}
const (
{{- range .Members }}
{{Camel $enum.Name}}{{Camel .Name}} = iota
{{- end }}
)
{{- end }}



{{- range .Module.Structs }}

type {{Camel .Name}} struct {
{{- range .Fields }}
{{Camel .Name}} {{goType "" .}} `json:"{{snake .Name}}" yaml:"{{snake .Name}}"`
{{- end }}
};
{{- end }}

{{- range .Module.Interfaces }}

{{- range .Properties }}
var {{Camel .Name}} = {{goDefault "" .}}
{{- end }}

type I{{Camel .Name }} interface {
{{- range .Properties }}
// {{.Name}}
Set{{Camel .Name}}({{goParam "" .}})
Get{{Camel .Name}}() {{goReturn "" . }}
On{{Camel .Name}}(cb func({{goType "" .}}))
{{- end }}
{{- range .Operations }}
// {{.Name}}
{{Camel .Name}}({{goParams "" .Params}}) {{goReturn "" .Return}}
{{- end }}
};
{{- end }}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module github.com/apigear-io/cli

go 1.22
go 1.22.2

require (
github.com/apigear-io/apigear-by-example v0.1.0
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.9.0
Expand Down
59 changes: 59 additions & 0 deletions go.sum

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/apigear-io/cli/pkg/cmd/registry"
"github.com/apigear-io/cli/pkg/cmd/sim"
"github.com/apigear-io/cli/pkg/cmd/spec"
"github.com/apigear-io/cli/pkg/cmd/tpl"
"github.com/apigear-io/cli/pkg/cmd/x"

"github.com/spf13/cobra"
Expand All @@ -26,7 +27,7 @@ func NewRootCommand() *cobra.Command {
Long: `ApiGear allows you to describe interfaces and generate instrumented SDKs out of the descriptions.`,
}
cmd.SilenceErrors = false
cmd.SilenceUsage = true
cmd.SilenceUsage = false
cmd.AddCommand(gen.NewRootCommand())
cmd.AddCommand(mon.NewRootCommand())
cmd.AddCommand(cfg.NewRootCommand())
Expand All @@ -38,5 +39,6 @@ func NewRootCommand() *cobra.Command {
cmd.AddCommand(x.NewRootCommand())
cmd.AddCommand(NewUpdateCommand())
cmd.AddCommand(NewVersionCommand())
cmd.AddCommand(tpl.NewRootCommand())
return cmd
}
34 changes: 34 additions & 0 deletions pkg/cmd/tpl/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package tpl

import (
"github.com/apigear-io/cli/pkg/tpl"
"github.com/spf13/cobra"
)

func NewInfoCommand() *cobra.Command {
var dir string
var cmd = &cobra.Command{
Use: "info",
Short: "Display template information",
Long: `Display template information`,
RunE: func(cmd *cobra.Command, args []string) error {
info, err := tpl.Info(dir)
if err != nil {
return err
}
cmd.Println("# template info")
cmd.Println()
cmd.Println("## rules document")
cmd.Println()
cmd.Println(info.Rules)
cmd.Println("## template files")
cmd.Println()
for _, file := range info.Files {
cmd.Printf("- %s\n", file)
}
return nil
},
}
cmd.Flags().StringVarP(&dir, "dir", "d", ".", "template directory")
return cmd
}
25 changes: 25 additions & 0 deletions pkg/cmd/tpl/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package tpl

import (
"github.com/apigear-io/cli/pkg/tpl"
"github.com/spf13/cobra"
)

func NewInitCommand() *cobra.Command {
var dir string
var lang string
var cmd = &cobra.Command{
Use: "init",
Short: "Initialize a template",
Long: `Initialize a template`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.Printf("initializing template %s using language %s\n", dir, lang)
return tpl.InitTemplate(dir, lang)
},
}
cmd.Flags().StringVarP(&dir, "dir", "d", ".", "template directory to init")
cmd.MarkFlagRequired("dir")
cmd.Flags().StringVarP(&lang, "lang", "l", "cpp", "language to init [cpp, go, py, rs, ts, ue]")
cmd.MarkFlagRequired("lang")
return cmd
}
34 changes: 34 additions & 0 deletions pkg/cmd/tpl/lint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package tpl

import (
"github.com/apigear-io/cli/pkg/gen"
"github.com/apigear-io/cli/pkg/model"
"github.com/spf13/cobra"
)

func NewLintCommand() *cobra.Command {
var dir string
var cmd = &cobra.Command{
Use: "lint",
Short: "Lint a template directory",
Long: `Lint a template directory, it will fail if the templates can not be compiled`,
Run: func(cmd *cobra.Command, args []string) {
// trying to create a generator, it will fail
// if the templates in the dir are not valid
_, err := gen.New(gen.Options{
TemplatesDir: dir,
System: model.NewSystem("test"),
Features: []string{"all"},
Force: true,
})
if err != nil {
cmd.Printf("template dir '%s' is not valid: %s\n", dir, err)
} else {
cmd.Printf("template dir '%s' is valid\n", dir)
}
},
}
cmd.Flags().StringVarP(&dir, "dir", "d", ".", "template directory")
cmd.MarkFlagRequired("dir")
return cmd
}
22 changes: 22 additions & 0 deletions pkg/cmd/tpl/publish.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package tpl

import (
"github.com/apigear-io/cli/pkg/tpl"
"github.com/spf13/cobra"
)

func NewPublishCommand() *cobra.Command {
var dir string
var cmd = &cobra.Command{
Use: "publish",
Short: "Publish a template to a market place",
Long: `Publish a template to a market place. The template needs to be a public github repository.`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.Printf("publishing template %s\n", dir)
return tpl.PublishTemplate(dir)
},
}
cmd.Flags().StringVarP(&dir, "dir", "d", ".", "template directory")
cmd.MarkFlagRequired("dir")
return cmd
}
18 changes: 18 additions & 0 deletions pkg/cmd/tpl/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package tpl

import "github.com/spf13/cobra"

func NewRootCommand() *cobra.Command {
// cmd represents the tpl command
cmd := &cobra.Command{
Use: "template",
Aliases: []string{"tpl", "t"},
Short: "template management",
Long: `template management`,
}
cmd.AddCommand(NewInitCommand())
cmd.AddCommand(NewLintCommand())
cmd.AddCommand(NewInfoCommand())
cmd.AddCommand(NewPublishCommand())
return cmd
}
31 changes: 31 additions & 0 deletions pkg/tpl/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package tpl

import (
"os"

"github.com/apigear-io/cli/pkg/helper"
)

type TemplateInfo struct {
Rules string
Files []string
}

func Info(dir string) (*TemplateInfo, error) {
info := &TemplateInfo{}
// read rules.yaml
rules, err := os.ReadFile(helper.Join(dir, "rules.yaml"))
if err != nil {
return nil, err
}
info.Rules = string(rules)
// read files
files, err := os.ReadDir(helper.Join(dir, "templates"))
if err != nil {
return nil, err
}
for _, file := range files {
info.Files = append(info.Files, file.Name())
}
return info, nil
}
65 changes: 65 additions & 0 deletions pkg/tpl/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package tpl

import (
"fmt"
"os"

"github.com/apigear-io/apigear-by-example/tplcpp"
"github.com/apigear-io/apigear-by-example/tplgo"
"github.com/apigear-io/apigear-by-example/tplpy"
"github.com/apigear-io/apigear-by-example/tplrs"
"github.com/apigear-io/apigear-by-example/tplts"
"github.com/apigear-io/apigear-by-example/tplue"
"github.com/apigear-io/cli/pkg/helper"
)

func InitTemplate(dir string, lang string) error {
var rules []byte
var apiTpl []byte
var apiTplName string
switch lang {
case "cpp":
rules = tplcpp.RulesYaml
apiTpl = tplcpp.ApiTpl
apiTplName = tplcpp.ApiTplName
case "go":
rules = tplgo.RulesYaml
apiTpl = tplgo.ApiTpl
apiTplName = tplgo.ApiTplName
case "py":
rules = tplpy.RulesYaml
apiTpl = tplpy.ApiTpl
apiTplName = tplpy.ApiTplName
case "ts":
rules = tplts.RulesYaml
apiTpl = tplts.ApiTpl
apiTplName = tplts.ApiTplName
case "rs":
rules = tplrs.RulesYaml
apiTpl = tplrs.ApiTpl
apiTplName = tplrs.ApiTplName
case "ue":
rules = tplue.RulesYaml
apiTpl = tplue.ApiTpl
apiTplName = tplue.ApiTplName
default:
return fmt.Errorf("unsupported language: %s", lang)
}
log.Info().Msgf("init template %s", dir)
os.MkdirAll(dir, 0755)
target := helper.Join(dir, "rules.yaml")
log.Info().Msgf("write %s", target)
err := os.WriteFile(target, rules, 0644)
if err != nil {
return err
}
target = helper.Join(dir, "templates")
os.MkdirAll(target, 0755)
target = helper.Join(target, apiTplName)
log.Info().Msgf("write %s", target)
err = os.WriteFile(target, apiTpl, 0644)
if err != nil {
return err
}
return nil
}
7 changes: 7 additions & 0 deletions pkg/tpl/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package tpl

import (
zlog "github.com/apigear-io/cli/pkg/log"
)

var log = zlog.Topic("tpl")
6 changes: 6 additions & 0 deletions pkg/tpl/publish.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package tpl

func PublishTemplate(dir string) error {
log.Info().Msgf("publishing template %s. Not implemented yet", dir)
return nil
}
Loading