This repository has been archived by the owner on Jul 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ruby.go
137 lines (104 loc) · 3.51 KB
/
ruby.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package templates
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"github.com/abdfnx/botway/constants"
"github.com/abdfnx/looker"
)
func MainRbContent(platform string) string {
return Content("main.rb", platform+"-ruby", "", "")
}
func RubyTemplate(botName, platform string) {
_, err := looker.LookPath("ruby")
bundlePath, berr := looker.LookPath("bundle")
if err != nil {
fmt.Print(constants.FAIL_BACKGROUND.Render("ERROR"))
fmt.Println(constants.FAIL_FOREGROUND.Render(" ruby is not installed"))
} else if berr != nil {
fmt.Print(constants.FAIL_BACKGROUND.Render("ERROR"))
fmt.Println(constants.FAIL_FOREGROUND.Render(" bundler is not installed"))
} else {
if runtime.GOOS == "windows" {
fmt.Println(constants.INFO_FOREGROUND.Render(`On Windows, follow these steps:
Download the latest libsodium-X.Y.Z-msvc.zip from https://download.libsodium.org/libsodium/releases.
From the downloaded zip file, extract the 'x64/Release/v120/dynamic/libsodium.dll' file to somewhere.
Copy that to any folder within the Ruby '$LOAD_PATH' or 'C:\Windows\System32' and rename it to 'sodium.dll'.
You can add a folder to your '$LOAD_PAT'H either at runtime or via the -I command line flag (ruby -I ./my_dlls bot.rb).`))
}
bundlerInit := bundlePath + " init"
cmd := exec.Command("bash", "-c", bundlerInit)
if runtime.GOOS == "windows" {
cmd = exec.Command("powershell.exe", bundlerInit)
}
cmd.Dir = botName
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
log.Printf("error: %v\n", err)
}
mainFile := os.WriteFile(filepath.Join(botName, "src", "main.rb"), []byte(MainRbContent(platform)), 0644)
dockerFile := os.WriteFile(filepath.Join(botName, "Dockerfile"), []byte(DockerfileContent(botName, "ruby.dockerfile", platform)), 0644)
resourcesFile := os.WriteFile(filepath.Join(botName, "resources.md"), []byte(Resources(platform, "ruby.md")), 0644)
if mainFile != nil {
log.Fatal(mainFile)
}
if dockerFile != nil {
log.Fatal(dockerFile)
}
if resourcesFile != nil {
log.Fatal(resourcesFile)
}
bundleConfig := bundlePath + " config set --local path '~/.gem'"
configCmd := exec.Command("bash", "-c", bundleConfig)
if runtime.GOOS == "windows" {
configCmd = exec.Command("powershell.exe", bundleConfig)
}
configCmd.Dir = botName
configCmd.Stdin = os.Stdin
configCmd.Stdout = os.Stdout
configCmd.Stderr = os.Stderr
err = configCmd.Run()
if err != nil {
log.Printf("error: %v\n", err)
}
pkgs := ""
if platform == "discord" {
pkgs = "discordrb --git https://github.com/shardlab/discordrb --branch main"
} else if platform == "telegram" {
pkgs = "telegram-bot-ruby"
}
bundleAdd := bundlePath + " add " + pkgs
addCmd := exec.Command("bash", "-c", bundleAdd)
if runtime.GOOS == "windows" {
addCmd = exec.Command("powershell.exe", bundleAdd)
}
addCmd.Dir = botName
addCmd.Stdin = os.Stdin
addCmd.Stdout = os.Stdout
addCmd.Stderr = os.Stderr
err = addCmd.Run()
if err != nil {
log.Printf("error: %v\n", err)
}
bundleAddBotwayrb := bundlePath + " add bwrb"
addBotwayCmd := exec.Command("bash", "-c", bundleAddBotwayrb)
if runtime.GOOS == "windows" {
addBotwayCmd = exec.Command("powershell.exe", bundleAddBotwayrb)
}
addBotwayCmd.Dir = botName
addBotwayCmd.Stdin = os.Stdin
addBotwayCmd.Stdout = os.Stdout
addBotwayCmd.Stderr = os.Stderr
err = addBotwayCmd.Run()
if err != nil {
log.Printf("error: %v\n", err)
}
CheckProject(botName, platform)
}
}