Skip to content

Commit ccd3e69

Browse files
feat: support plugin.yml in project directory for commands and permissions
1 parent fb902de commit ccd3e69

File tree

14 files changed

+470
-146
lines changed

14 files changed

+470
-146
lines changed

cmd/cmd_build.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,11 @@ func (c *BuildCmd) Run() error {
4040
}
4141

4242
// Create the project
43-
crProject := project.Project{
44-
Dir: c.ProjectDir,
45-
}
43+
crProject := project.New(c.ProjectDir)
4644

4745
// Create the build action
4846
buildAction := build.BuildAction{
49-
Project: &crProject,
47+
Project: crProject,
5048
JarTemplate: jarTemplate,
5149
MinecraftVersion: minecraftVersion,
5250
OutputFile: c.OutputFile,

cmd/cmd_run.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,11 @@ func (c *RunCmd) Run() error {
5252
}
5353

5454
// Create the project
55-
crProject := project.Project{
56-
Dir: c.ProjectDir,
57-
}
55+
crProject := project.New(c.ProjectDir)
5856

5957
// Create the build action
6058
buildAction := build.BuildAction{
61-
Project: &crProject,
59+
Project: crProject,
6260
JarTemplate: jarTemplate,
6361
MinecraftVersion: minecraftVersion,
6462
OutputFile: outputFile,

cmd/cmd_yml.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/customrealms/cli/internal/build"
88
"github.com/customrealms/cli/internal/project"
9+
"gopkg.in/yaml.v3"
910
)
1011

1112
type YmlCmd struct {
@@ -27,22 +28,19 @@ func (c *YmlCmd) Run() error {
2728
minecraftVersion := mustMinecraftVersion(ctx, c.McVersion)
2829

2930
// Create the project
30-
crProject := project.Project{
31-
Dir: c.ProjectDir,
32-
}
31+
crProject := project.New(c.ProjectDir)
3332

34-
// Read the package.json file
35-
packageJson, err := crProject.PackageJSON()
33+
// Generate the plugin.yml file
34+
pluginYML, err := build.GeneratePluginYML(crProject, minecraftVersion)
3635
if err != nil {
37-
return err
36+
return fmt.Errorf("generating plugin.yml: %w", err)
3837
}
3938

40-
// Define the plugin.yml details for the plugin
41-
pluginYml := &build.PluginYml{
42-
MinecraftVersion: minecraftVersion,
43-
PackageJSON: packageJson,
39+
// Encode it to stdout
40+
enc := yaml.NewEncoder(os.Stdout)
41+
enc.SetIndent(2)
42+
if err := enc.Encode(pluginYML); err != nil {
43+
return fmt.Errorf("encoding plugin.yml: %w", err)
4444
}
45-
fmt.Println(pluginYml)
46-
4745
return nil
4846
}

go.mod

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ go 1.22
55
require (
66
github.com/alecthomas/kong v0.9.0
77
github.com/fsnotify/fsnotify v1.7.0
8+
github.com/stretchr/testify v1.9.0
89
golang.org/x/sync v0.7.0
10+
gopkg.in/yaml.v3 v3.0.1
911
)
1012

11-
require golang.org/x/sys v0.4.0 // indirect
13+
require (
14+
github.com/davecgh/go-spew v1.1.1 // indirect
15+
github.com/pmezard/go-difflib v1.0.0 // indirect
16+
golang.org/x/sys v0.4.0 // indirect
17+
)

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,21 @@ github.com/alecthomas/kong v0.9.0 h1:G5diXxc85KvoV2f0ZRVuMsi45IrBgx9zDNGNj165aPA
44
github.com/alecthomas/kong v0.9.0/go.mod h1:Y47y5gKfHp1hDc7CH7OeXgLIpp+Q2m1Ni0L5s3bI8Os=
55
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
66
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
7+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
8+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
79
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
810
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
911
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
1012
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
13+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
14+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
15+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
16+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
1117
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
1218
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
1319
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
1420
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
21+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
22+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
23+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
24+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/build/build.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
var webpackConfig string
1818

1919
type BuildAction struct {
20-
Project *project.Project
20+
Project project.Project
2121
JarTemplate JarTemplate
2222
MinecraftVersion minecraft.Version
2323
OutputFile string
@@ -47,8 +47,13 @@ func (a *BuildAction) Run(ctx context.Context) error {
4747
fmt.Println("============================================================")
4848

4949
// Build the local directory
50-
cmd := a.Project.CommandContext(ctx, "npx", "webpack-cli", "--mode=production", "-o", webpackOutputDir, "-c", webpackConfigFile, "--entry", "./src/main.ts")
51-
if err := cmd.Run(); err != nil {
50+
err := a.Project.Exec(ctx, "npx", "webpack-cli",
51+
"--mode=production",
52+
"-o", webpackOutputDir,
53+
"-c", webpackConfigFile,
54+
"--entry", "./src/main.ts",
55+
)
56+
if err != nil {
5257
return err
5358
}
5459

internal/build/jar.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import (
88
"io"
99
"os"
1010
"path/filepath"
11-
"strings"
1211

1312
"github.com/customrealms/cli/internal/minecraft"
13+
"github.com/customrealms/cli/internal/pluginyml"
1414
"github.com/customrealms/cli/internal/project"
15+
"gopkg.in/yaml.v3"
1516
)
1617

1718
type JarAction struct {
18-
Project *project.Project
19+
Project project.Project
1920
JarTemplate JarTemplate
2021
MinecraftVersion minecraft.Version
2122
BundleFile string
@@ -62,24 +63,18 @@ func (a *JarAction) Run(ctx context.Context) error {
6263
}
6364
defer pluginCode.Close()
6465

65-
// Read the package.json file
66-
packageJson, err := a.Project.PackageJSON()
66+
// Generate the plugin.yml file for the project
67+
pluginYML, err := GeneratePluginYML(a.Project, a.MinecraftVersion)
6768
if err != nil {
68-
return err
69-
}
70-
71-
// Define the plugin.yml details for the plugin
72-
pluginYml := PluginYml{
73-
MinecraftVersion: a.MinecraftVersion,
74-
PackageJSON: packageJson,
69+
return fmt.Errorf("generating plugin.yml: %w", err)
7570
}
7671

7772
// Produce the final JAR file
7873
if err := WriteJarFile(
7974
file,
8075
jarTemplateBuf.Bytes(),
8176
pluginCode,
82-
&pluginYml,
77+
pluginYML,
8378
); err != nil {
8479
return err
8580
}
@@ -94,7 +89,7 @@ func WriteJarFile(
9489
writer io.Writer,
9590
templateJarData []byte,
9691
pluginSourceCode io.Reader,
97-
pluginYml *PluginYml,
92+
pluginYML *pluginyml.Plugin,
9893
) error {
9994

10095
fmt.Println("============================================================")
@@ -147,8 +142,10 @@ func WriteJarFile(
147142
if err != nil {
148143
return err
149144
}
150-
if _, err := io.Copy(ymlFile, strings.NewReader(pluginYml.String())); err != nil {
151-
return err
145+
enc := yaml.NewEncoder(ymlFile)
146+
enc.SetIndent(2)
147+
if err := enc.Encode(pluginYML); err != nil {
148+
return fmt.Errorf("encoding plugin.yml: %w", err)
152149
}
153150

154151
fmt.Println(" -> DONE")

internal/build/pluginyml.go

Lines changed: 44 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,65 @@
11
package build
22

33
import (
4+
"errors"
45
"fmt"
5-
"strings"
6+
"log"
67

78
"github.com/customrealms/cli/internal/minecraft"
9+
"github.com/customrealms/cli/internal/pluginyml"
810
"github.com/customrealms/cli/internal/project"
911
)
1012

11-
const JAR_MAIN_CLASS = "io.customrealms.MainPlugin"
13+
const JarMainClass = "io.customrealms.MainPlugin"
1214

13-
type PluginYml struct {
14-
MinecraftVersion minecraft.Version
15-
PackageJSON *project.PackageJSON
16-
}
15+
func GeneratePluginYML(project project.Project, version minecraft.Version) (*pluginyml.Plugin, error) {
16+
// Read the package.json file
17+
packageJSON, err := project.PackageJSON()
18+
if err != nil {
19+
return nil, fmt.Errorf("getting package.json: %w", err)
20+
}
1721

18-
func (y *PluginYml) String() string {
19-
var lines []string
22+
// Read the plugin.yml file
23+
plugin, err := project.PluginYML()
24+
if err != nil {
25+
return nil, fmt.Errorf("getting plugin.yml: %w", err)
26+
}
2027

21-
// General plugin details
22-
lines = append(lines,
23-
fmt.Sprintf("name: %s", y.PackageJSON.Name),
24-
fmt.Sprintf("api-version: %s", y.MinecraftVersion.ApiVersion()),
25-
fmt.Sprintf("version: %s", y.PackageJSON.Version),
26-
fmt.Sprintf("main: %s", JAR_MAIN_CLASS),
27-
)
28-
if len(y.PackageJSON.Author) > 0 {
29-
lines = append(lines, fmt.Sprintf("author: %s", y.PackageJSON.Author))
28+
// If plugin.yml and package.json are both missing, it's an error
29+
if packageJSON == nil && plugin == nil {
30+
return nil, errors.New("missing both package.json and plugin.yml")
3031
}
31-
if len(y.PackageJSON.Website) > 0 {
32-
lines = append(lines, fmt.Sprintf("website: %s", y.PackageJSON.Website))
32+
33+
// If there is no plugin.yml file present, create one
34+
if plugin == nil {
35+
plugin = &pluginyml.Plugin{}
36+
plugin.Name = packageJSON.Name
3337
}
34-
lines = append(lines, "")
3538

36-
// Add the commands
37-
if len(y.PackageJSON.Commands) > 0 {
38-
lines = append(lines, "commands:")
39-
for key, attrs := range y.PackageJSON.Commands {
40-
lines = append(lines, indent(1)+fmt.Sprintf("%s:", key))
41-
if attrs != nil {
42-
if len(attrs.Description) > 0 {
43-
lines = append(lines, indent(2)+fmt.Sprintf("description: %s", attrs.Description))
44-
}
45-
if len(attrs.Aliases) > 0 {
46-
lines = append(lines, indent(2)+fmt.Sprintf("aliases: [%s]", strings.Join(attrs.Aliases, ", ")))
47-
}
48-
if len(attrs.Permission) > 0 {
49-
lines = append(lines, indent(2)+fmt.Sprintf("permission: %s", attrs.Permission))
50-
}
51-
if len(attrs.PermissionMessage) > 0 {
52-
lines = append(lines, indent(2)+fmt.Sprintf("permision-message: %s", attrs.PermissionMessage))
53-
}
54-
if len(attrs.Usage) > 0 {
55-
lines = append(lines, indent(2)+fmt.Sprintf("usage: %q", attrs.Usage))
56-
}
57-
}
58-
}
59-
lines = append(lines, "")
39+
// Set the main Java class for the plugin
40+
plugin.Main = JarMainClass
41+
42+
// Set the Bukkit API version for the plugin
43+
if version != nil {
44+
apiVersion := version.ApiVersion()
45+
plugin.ApiVersion = &apiVersion
6046
}
6147

62-
// Add the permissions
63-
if len(y.PackageJSON.Permissions) > 0 {
64-
lines = append(lines, "permissions:")
65-
for key, attrs := range y.PackageJSON.Permissions {
66-
lines = append(lines, indent(1)+fmt.Sprintf("%s:", key))
67-
if attrs != nil {
68-
if len(attrs.Description) > 0 {
69-
lines = append(lines, indent(2)+fmt.Sprintf("description: %s", attrs.Description))
70-
}
71-
if attrs.Default != nil {
72-
lines = append(lines, indent(2)+fmt.Sprintf("default: %t", *attrs.Default))
73-
}
74-
if attrs.Children != nil {
75-
lines = append(lines, indent(2)+"children:")
76-
for childKey, childVal := range attrs.Children {
77-
lines = append(lines, indent(3)+fmt.Sprintf("%s: %t", childKey, childVal))
78-
}
79-
}
80-
}
48+
// If there is a package.json file
49+
if packageJSON != nil {
50+
// Update the version if it's missing
51+
if plugin.Version == "" && packageJSON.Version != "" {
52+
plugin.Version = packageJSON.Version
53+
} else if plugin.Version == "" && packageJSON.Version == "" {
54+
log.Println("No version found in plugin.yml or package.json. Consider adding a version to package.json.")
55+
log.Println("Using version '0.0.0' as a fallback.")
56+
plugin.Version = "0.0.0"
57+
} else if plugin.Version != packageJSON.Version {
58+
log.Println("Version mismatch between plugin.yml and package.json. Consider removing `version` from plugin.yml.")
59+
log.Printf("Using version '%s' from plugin.yml", plugin.Version)
8160
}
82-
lines = append(lines, "")
8361
}
8462

85-
return strings.Join(lines, "\n")
86-
}
87-
88-
func indent(level int) string {
89-
return strings.Repeat(" ", 2*level)
63+
// Return the plugin yml
64+
return plugin, nil
9065
}

0 commit comments

Comments
 (0)