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

Commit

Permalink
add discord php support to botway
Browse files Browse the repository at this point in the history
  • Loading branch information
abdfnx committed Jul 19, 2022
1 parent 49cf925 commit 92a8895
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 6 deletions.
5 changes: 5 additions & 0 deletions internal/dashboard/icons/glyph.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ var IconSet = map[string]*IconInfo{
"nodejs": {icon: "\ue74e", color: [3]uint8{255, 202, 61}},
"typescript": {icon: "\ue628", color: [3]uint8{3, 136, 209}},
"deno": {icon: "\ue628", color: [3]uint8{3, 136, 209}},
"csharp": {icon: "\uf81a", color: [3]uint8{2, 119, 189}},
"dart": {icon: "\ue798", color: [3]uint8{87, 182, 240}},
"java": {icon: "\uf675", color: [3]uint8{244, 68, 62}},
"kotlin": {icon: "\ue70e", color: [3]uint8{139, 195, 74}},
"other": {icon: "\ufba7", color: [3]uint8{249, 89, 63}},
}
5 changes: 5 additions & 0 deletions internal/dashboard/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,13 @@ func (b Bubble) botListView() string {

line := trunc(value.String(), b.bubbles.primaryPaginator.Width-2)


lang := gjson.Get(string(constants.BotwayConfig), "botway.bots."+v+".lang").String()

if lang == "php" || lang == "crystal" {
lang = "other"
}

icon, color := icons.GetIcon(lang)

fileIcon := lipgloss.NewStyle().Width(2).Render(fmt.Sprintf("%s%s\033[0m ", color, icon))
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 @@ -44,8 +44,8 @@ func updateLangs(msg tea.Msg, m model) (tea.Model, tea.Cmd) {
}
} else {
if m.PlatformChoice == 0 {
if m.LangChoice > 8 {
m.LangChoice = 8
if m.LangChoice > 9 {
m.LangChoice = 9
}
} else {
if m.LangChoice > 7 {
Expand Down Expand Up @@ -137,6 +137,8 @@ func buildBot(msg tea.Msg, m model) (tea.Model, tea.Cmd) {
} else if m.LangChoice == 7 {
l = "Dart"
} else if m.LangChoice == 8 {
l = "PHP"
} else if m.LangChoice == 9 {
l = "Crystal"
}

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 @@ -7,6 +7,7 @@ import (
"github.com/abdfnx/botway/templates/discord/deno"
dgo "github.com/abdfnx/botway/templates/discord/go"
"github.com/abdfnx/botway/templates/discord/nodejs"
"github.com/abdfnx/botway/templates/discord/php"
"github.com/abdfnx/botway/templates/discord/python/pip"
"github.com/abdfnx/botway/templates/discord/python/pipenv"
"github.com/abdfnx/botway/templates/discord/python/poetry"
Expand Down Expand Up @@ -44,6 +45,8 @@ func DiscordHandler(m model) {
} else if m.PlatformChoice == 0 && m.LangChoice == 7 {
dart.DiscordDart(opts.BotName)
} else if m.PlatformChoice == 0 && m.LangChoice == 8 {
php.DiscordPHP(opts.BotName)
} else if m.PlatformChoice == 0 && m.LangChoice == 9 {
crystal.DiscordCrystal(opts.BotName)
}
}
8 changes: 7 additions & 1 deletion internal/pipes/new/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func BotType(m model) string {
return "# You need to specify a platform (discord, telegram, slack)"
}

var blankLangMessage = "# You need to specify a language (python, go, nodejs, ruby, rust, deno, csharp, dart, crystal)"
var blankLangMessage = "# You need to specify a language (python, go, nodejs, ruby, rust, deno, csharp, dart, php, crystal)"

func BotLang(m model) string {
if m.LangChoice == 0 {
Expand All @@ -64,6 +64,8 @@ func BotLang(m model) string {
} else if m.LangChoice == 7 {
return "dart"
} else if m.LangChoice == 8 {
return "php"
} else if m.LangChoice == 9 {
return "crystal"
}

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

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

Expand Down
13 changes: 11 additions & 2 deletions internal/pipes/new/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,20 @@ func langsView(m model) string {

if m.PlatformChoice != 2 {
langs += fmt.Sprintf(
"\n%s\n%s\n%s\n%s\n%s",
"\n%s\n%s\n%s\n%s\n%s\n%s",
checkbox("Ruby", l == 3),
checkbox("Rust", l == 4),
checkbox("Deno", l == 5),
checkbox("C#", l == 6),
checkbox("Dart", l == 7),
checkbox("PHP", l == 8),
)
}

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

Expand Down Expand Up @@ -97,6 +98,8 @@ func pmsView(m model) string {
} else if m.LangChoice == 7 {
l = "Dart"
} else if m.LangChoice == 8 {
l = "PHP"
} else if m.LangChoice == 9 {
l = "Crystal"
}

Expand Down Expand Up @@ -148,6 +151,8 @@ func pmsView(m model) string {
} else if m.LangChoice == 7 {
langs += checkbox("pub", pm == 0)
} else if m.LangChoice == 8 {
langs += checkbox("composer", pm == 0)
} else if m.LangChoice == 9 {
langs += checkbox("shards", pm == 0)
}

Expand Down Expand Up @@ -266,6 +271,10 @@ func finalView(m model) string {
lang = "Dart"

case 8:
lang = "PHP"
pm = "composer"

case 9:
lang = "Crystal"
pm = "shards"
}
Expand Down
23 changes: 23 additions & 0 deletions templates/discord/php/contents.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package php

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

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

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

func BotwayPHPContent() string {
return templates.Content("src/botway.php", "discord-php", "")
}

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

func ComposerFileContent(botName string) string {
return templates.Content("composer.json", "discord-php", botName)
}
73 changes: 73 additions & 0 deletions templates/discord/php/index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package php

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

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

func DiscordPHP(botName string) {
_, err := looker.LookPath("php")
composerPath, serr := looker.LookPath("composer")

if err != nil {
fmt.Print(constants.FAIL_BACKGROUND.Render("ERROR"))
fmt.Println(constants.FAIL_FOREGROUND.Render(" php is not installed"))
} else if serr != nil {
fmt.Print(constants.FAIL_BACKGROUND.Render("ERROR"))
fmt.Println(constants.FAIL_FOREGROUND.Render(" composer is not installed"))
} else {
mainFile := os.WriteFile(filepath.Join(botName, "src", "main.php"), []byte(MainPHPContent()), 0644)
botwayFile := os.WriteFile(filepath.Join(botName, "src", "botway.php"), []byte(BotwayPHPContent()), 0644)
composerFile := os.WriteFile(filepath.Join(botName, "composer.json"), []byte(ComposerFileContent(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 botwayFile != nil {
log.Fatal(botwayFile)
}

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

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

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

composerInstall := composerPath + " install"

installCmd := exec.Command("bash", "-c", composerInstall)

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

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

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

templates.CheckProject(botName, "discord")
}
}
8 changes: 7 additions & 1 deletion templates/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ 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") || strings.Contains(arg, "pubspec.yaml") || strings.Contains(arg, "pyproject.toml") {
if
strings.Contains(arg, ".dockerfile") ||
strings.Contains(arg, "Cargo.toml") ||
strings.Contains(arg, "shard.yml") ||
strings.Contains(arg, "pubspec.yaml") ||
strings.Contains(arg, "pyproject.toml") ||
strings.Contains(arg, "composer.json") {
respone = strings.ReplaceAll(respone, "{{.BotName}}", botName)

viper.SetConfigType("json")
Expand Down

0 comments on commit 92a8895

Please sign in to comment.