Skip to content
This repository has been archived by the owner on Jul 20, 2024. It is now read-only.

Commit

Permalink
add botway discord dart template handler to botway CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
abdfnx committed Jul 2, 2022
1 parent dfabab0 commit 27445a9
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 10 deletions.
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,6 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.7.4/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.7.5 h1:s5PTfem8p8EbKQOctVV53k6jCJt3UX4IEJzwh+C324Q=
github.com/stretchr/testify v1.7.5/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
Expand Down Expand Up @@ -678,8 +676,6 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e h1:TsQ7F31D3bUCLeqPT0u+yjp1guoArKaNKmCr22PYgTQ=
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.0.0-20220630215102-69896b714898 h1:K7wO6V1IrczY9QOQ2WkVpw4JQSwCd52UsxVEirZUfiw=
golang.org/x/net v0.0.0-20220630215102-69896b714898/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand Down Expand Up @@ -766,8 +762,6 @@ golang.org/x/sys v0.0.0-20220318055525-2edf467146b5/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220624220833-87e55d714810 h1:rHZQSjJdAI4Xf5Qzeh2bBc5YJIkPFVM6oDtMFYmgws0=
golang.org/x/sys v0.0.0-20220624220833-87e55d714810/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220702020025-31831981b65f h1:xdsejrW/0Wf2diT5CPp3XmKUNbr7Xvw8kYilQ+6qjRY=
golang.org/x/sys v0.0.0-20220702020025-31831981b65f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
Expand Down
6 changes: 4 additions & 2 deletions internal/pipes/new/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ func updateLangs(msg tea.Msg, m model) (tea.Model, tea.Cmd) {
}
} else {
if m.PlatformChoice == 0 {
if m.LangChoice > 7 {
m.LangChoice = 7
if m.LangChoice > 8 {
m.LangChoice = 8
}
} else {
if m.LangChoice > 5 {
Expand Down Expand Up @@ -148,6 +148,8 @@ func buildBot(msg tea.Msg, m model, botName string) (tea.Model, tea.Cmd) {
l = "C#"
} else if m.LangChoice == 7 {
l = "Crystal"
} else if m.LangChoice == 8 {
l = "Dart"
}

if err := conf.Load(file.Provider(constants.BotwayConfigFile), json.Parser()); err != nil {
Expand Down
3 changes: 3 additions & 0 deletions internal/pipes/new/discord_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package new
import (
"github.com/abdfnx/botway/templates/discord/crystal"
"github.com/abdfnx/botway/templates/discord/csharp"
"github.com/abdfnx/botway/templates/discord/dart"
"github.com/abdfnx/botway/templates/discord/deno"
"github.com/abdfnx/botway/templates/discord/go"
"github.com/abdfnx/botway/templates/discord/nodejs"
Expand Down Expand Up @@ -37,5 +38,7 @@ func DiscordHandler(m model) {
csharp.DiscordCsharp(opts.BotName)
} else if m.PlatformChoice == 0 && m.LangChoice == 7 {
crystal.DiscordCrystal(opts.BotName)
} else if m.PlatformChoice == 0 && m.LangChoice == 8 {
dart.DiscordDart(opts.BotName)
}
}
6 changes: 6 additions & 0 deletions internal/pipes/new/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ func BotLang(m model) string {
return "csharp"
} else if m.LangChoice == 7 {
return "crystal"
} else if m.LangChoice == 8 {
return "dart"
}

return ""
Expand Down Expand Up @@ -97,6 +99,8 @@ func BotStartCmd(m model) string {
return "dotnet run"
} else if m.LangChoice == 7 {
return "crystal run src/main.cr"
} else if m.LangChoice == 8 {
return "dart run src/main.dart"
}

return ""
Expand Down Expand Up @@ -127,6 +131,8 @@ func BotPM(m model) string {
return "dotnet"
} else if m.LangChoice == 7 {
return "shards"
} else if m.LangChoice == 8 {
return "pub"
}

return ""
Expand Down
11 changes: 10 additions & 1 deletion internal/pipes/new/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ func langsView(m model) string {

if m.PlatformChoice == 0 {
langs += fmt.Sprintf(
"\n%s\n%s",
"\n%s\n%s\n%s",
checkbox("C#", l == 6),
checkbox("Crystal", l == 7),
checkbox("Dart", l == 8),
)
}

Expand Down Expand Up @@ -95,6 +96,8 @@ func pmsView(m model) string {
l = "C#"
} else if m.LangChoice == 7 {
l = "Crystal"
} else if m.LangChoice == 8 {
l = "Dart"
}

tpl := "Choose your favorite package manager for " + l + "\n\n"
Expand Down Expand Up @@ -142,6 +145,8 @@ func pmsView(m model) string {
langs += checkbox("dotnet", pm == 0)
} else if m.LangChoice == 7 {
langs += checkbox("shards", pm == 0)
} else if m.LangChoice == 8 {
langs += checkbox("pub", pm == 0)
}

return fmt.Sprintf(tpl, langs)
Expand Down Expand Up @@ -248,6 +253,10 @@ func finalView(m model) string {
case 7:
lang = "Crystal"
pm = "shards"

case 8:
lang = "Dart"
pm = "pub"
}

return "\n🤖 Noice, you're going to build a " + prim.Render(platform) + " bot via " + prim.Render(lang) + " with " + prim.Render(pm) + " package manager\n"
Expand Down
19 changes: 19 additions & 0 deletions templates/discord/dart/contents.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dart

import "github.com/abdfnx/botway/templates"

func DockerfileContent(botName string) string {
return templates.Content("dart.dockerfile", "dockerfiles", botName)
}

func Resources() string {
return templates.Content("discord/dart.md", "resources", "")
}

func MainDartContent() string {
return templates.Content("src/main.dart", "discord-dart", "")
}

func PubspecFileContent(botName string) string {
return templates.Content("pubspec.yaml", "discord-dart", botName)
}
64 changes: 64 additions & 0 deletions templates/discord/dart/index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package dart

import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"

"github.com/abdfnx/botway/constants"
"github.com/abdfnx/botway/templates"
"github.com/abdfnx/looker"
)

func DiscordDart(botName string) {
dartPath, err := looker.LookPath("dart")

if err != nil {
fmt.Print(constants.FAIL_BACKGROUND.Render("ERROR"))
fmt.Println(constants.FAIL_FOREGROUND.Render(" dart is not installed"))
} else {
mainFile := os.WriteFile(filepath.Join(botName, "src", "main.dart"), []byte(MainDartContent()), 0644)
pubspecFile := os.WriteFile(filepath.Join(botName, "pubspec.yaml"), []byte(PubspecFileContent(botName)), 0644)
dockerFile := os.WriteFile(filepath.Join(botName, "Dockerfile"), []byte(DockerfileContent(botName)), 0644)
resourcesFile := os.WriteFile(filepath.Join(botName, "resources.md"), []byte(Resources()), 0644)

if mainFile != nil {
log.Fatal(mainFile)
}

if pubspecFile != nil {
log.Fatal(pubspecFile)
}

if dockerFile != nil {
log.Fatal(dockerFile)
}

if resourcesFile != nil {
log.Fatal(resourcesFile)
}

dartGet := dartPath + " pub get"

getCmd := exec.Command("bash", "-c", dartGet)

if runtime.GOOS == "windows" {
getCmd = exec.Command("powershell.exe", dartGet)
}

getCmd.Dir = botName
getCmd.Stdin = os.Stdin
getCmd.Stdout = os.Stdout
getCmd.Stderr = os.Stderr
err = getCmd.Run()

if err != nil {
log.Printf("error: %v\n", err)
}

templates.CheckProject(botName, "discord")
}
}
2 changes: 1 addition & 1 deletion templates/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func Content(arg, templateName, botName string) string {
os.Exit(0)
}

if strings.Contains(arg, ".dockerfile") || strings.Contains(arg, "Cargo.toml") || strings.Contains(arg, "shard.yml") {
if strings.Contains(arg, ".dockerfile") || strings.Contains(arg, "Cargo.toml") || strings.Contains(arg, "shard.yml") || strings.Contains(arg, "pubspec.yaml") {
return strings.ReplaceAll(respone, "{{.BotName}}", botName)
} else {
return respone
Expand Down

0 comments on commit 27445a9

Please sign in to comment.