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: dynamic template function #841

Closed
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ vendor
tmp
tmp.md
todo.md
docker-compose.yml
docker-compose.yml
/.idea/workspace.xml
58 changes: 39 additions & 19 deletions internal/cli/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package cli

import (
"fmt"
"github.com/nevalang/neva/internal"
"os"
"path/filepath"

"github.com/nevalang/neva/pkg"
cli "github.com/urfave/cli/v2"
"github.com/urfave/cli/v2"
)

func newNewCmd(workdir string) *cli.Command {
Expand All @@ -19,21 +20,36 @@ func newNewCmd(workdir string) *cli.Command {
if err := os.Mkdir(pathArg, 0755); err != nil {
return err
}
return createNevaMod(pathArg)
return createNevaMod(pathArg, "hello-world")
}
return createNevaMod(workdir)
return createNevaMod(workdir, "hello-world")
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "template",
Usage: "Specify the template for creating new projects.",
Required: false,

Action: func(context *cli.Context, templateName string) error {
if templateName == "" {
templateName = "hello-world"
}
pathArg := context.Args().First()
if err := os.Mkdir(pathArg, 0755); err != nil {
return err
}
return createNevaMod(pathArg, templateName)
Mr-Ao-Dragon marked this conversation as resolved.
Show resolved Hide resolved

},
},
},
}
}

func createNevaMod(path string) error {
func createNevaMod(path string, templateName string) error {
// Create neva.yml file
nevaYmlContent := fmt.Sprintf("neva: %s", pkg.Version)
if err := os.WriteFile(
filepath.Join(path, "neva.yml"),
[]byte(nevaYmlContent),
0644,
); err != nil {
if err := os.WriteFile(filepath.Join(path, "neva.yml"), []byte(nevaYmlContent), 0644); err != nil {
return err
}

Expand All @@ -43,22 +59,26 @@ func createNevaMod(path string) error {
return err
}

// Create main.neva file
mainNevaContent := `import { fmt }

def Main(start any) (stop any) {
fmt.Println
---
:start -> 'Hello, World!' -> println -> :stop
}`

mainNevaContent, err := readTemplate(templateName + ".neva")
if err != nil {
return err
}
if err := os.WriteFile(
filepath.Join(srcPath, "main.neva"),
[]byte(mainNevaContent),
0644,
); err != nil {
return err
}

return nil
}
func readTemplate(templateName string) (string, error) {
fileData, err := internal.Efs.ReadFile(fmt.Sprintf("templates/%s", templateName))
if err != nil {
if os.IsNotExist(err) {
return "", fmt.Errorf("no such template: %s, available templates see: https://github.com/nevalang/neva/tree/main/internal/templates", templateName)
}
return "", fmt.Errorf("failed to read template file %s: %w", templateName, err)
}
return string(fileData), nil
}
2 changes: 1 addition & 1 deletion internal/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package internal
import "embed"

//nolint:golint
//go:embed runtime
//go:embed runtime templates
var Efs embed.FS
11 changes: 11 additions & 0 deletions internal/templates/empty.neva
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// import as here
// eg. import { fmt }

def Main(start any) (stop any) {
// define nodes here

---
// define network here

:start -> :stop
}
7 changes: 7 additions & 0 deletions internal/templates/hello-world.neva
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { fmt }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move to internal/cli/templates, this and the next one

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I should just move all the templates to internal\cli\templates?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's was my intention :)


def Main(start any)(stop any) {
println fmt.Println<string>
---
:start -> "hello world!" -> println -> :stop
}
7 changes: 7 additions & 0 deletions internal/templates/package.neva
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// don't forget modify i/o type and name
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. looks like this template won't compile because it lacks Main component
  2. name package is a little bit ambiguous. what exactly this template should be?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This template is intended to be reference ready and is not intended to be compilable

Copy link
Collaborator

@emil14 emil14 Jan 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So what will happen if user will run neva new --template=package? It will create neva module without executable package? It's like for developing libraries?


def YourComponent(data any) (res any) {
// code as here
---
// flow as here
}
Loading