Skip to content

Commit

Permalink
feat: support plugin.yml in project directory for commands and permis…
Browse files Browse the repository at this point in the history
…sions
  • Loading branch information
connerdouglass committed Jun 26, 2024
1 parent fb902de commit ccd3e69
Show file tree
Hide file tree
Showing 14 changed files with 470 additions and 146 deletions.
6 changes: 2 additions & 4 deletions cmd/cmd_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,11 @@ func (c *BuildCmd) Run() error {
}

// Create the project
crProject := project.Project{
Dir: c.ProjectDir,
}
crProject := project.New(c.ProjectDir)

// Create the build action
buildAction := build.BuildAction{
Project: &crProject,
Project: crProject,
JarTemplate: jarTemplate,
MinecraftVersion: minecraftVersion,
OutputFile: c.OutputFile,
Expand Down
6 changes: 2 additions & 4 deletions cmd/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,11 @@ func (c *RunCmd) Run() error {
}

// Create the project
crProject := project.Project{
Dir: c.ProjectDir,
}
crProject := project.New(c.ProjectDir)

// Create the build action
buildAction := build.BuildAction{
Project: &crProject,
Project: crProject,
JarTemplate: jarTemplate,
MinecraftVersion: minecraftVersion,
OutputFile: outputFile,
Expand Down
22 changes: 10 additions & 12 deletions cmd/cmd_yml.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/customrealms/cli/internal/build"
"github.com/customrealms/cli/internal/project"
"gopkg.in/yaml.v3"
)

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

// Create the project
crProject := project.Project{
Dir: c.ProjectDir,
}
crProject := project.New(c.ProjectDir)

// Read the package.json file
packageJson, err := crProject.PackageJSON()
// Generate the plugin.yml file
pluginYML, err := build.GeneratePluginYML(crProject, minecraftVersion)
if err != nil {
return err
return fmt.Errorf("generating plugin.yml: %w", err)
}

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

return nil
}
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ go 1.22
require (
github.com/alecthomas/kong v0.9.0
github.com/fsnotify/fsnotify v1.7.0
github.com/stretchr/testify v1.9.0
golang.org/x/sync v0.7.0
gopkg.in/yaml.v3 v3.0.1
)

require golang.org/x/sys v0.4.0 // indirect
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.4.0 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@ github.com/alecthomas/kong v0.9.0 h1:G5diXxc85KvoV2f0ZRVuMsi45IrBgx9zDNGNj165aPA
github.com/alecthomas/kong v0.9.0/go.mod h1:Y47y5gKfHp1hDc7CH7OeXgLIpp+Q2m1Ni0L5s3bI8Os=
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
11 changes: 8 additions & 3 deletions internal/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
var webpackConfig string

type BuildAction struct {
Project *project.Project
Project project.Project
JarTemplate JarTemplate
MinecraftVersion minecraft.Version
OutputFile string
Expand Down Expand Up @@ -47,8 +47,13 @@ func (a *BuildAction) Run(ctx context.Context) error {
fmt.Println("============================================================")

// Build the local directory
cmd := a.Project.CommandContext(ctx, "npx", "webpack-cli", "--mode=production", "-o", webpackOutputDir, "-c", webpackConfigFile, "--entry", "./src/main.ts")
if err := cmd.Run(); err != nil {
err := a.Project.Exec(ctx, "npx", "webpack-cli",
"--mode=production",
"-o", webpackOutputDir,
"-c", webpackConfigFile,
"--entry", "./src/main.ts",
)
if err != nil {
return err
}

Expand Down
27 changes: 12 additions & 15 deletions internal/build/jar.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
"io"
"os"
"path/filepath"
"strings"

"github.com/customrealms/cli/internal/minecraft"
"github.com/customrealms/cli/internal/pluginyml"
"github.com/customrealms/cli/internal/project"
"gopkg.in/yaml.v3"
)

type JarAction struct {
Project *project.Project
Project project.Project
JarTemplate JarTemplate
MinecraftVersion minecraft.Version
BundleFile string
Expand Down Expand Up @@ -62,24 +63,18 @@ func (a *JarAction) Run(ctx context.Context) error {
}
defer pluginCode.Close()

// Read the package.json file
packageJson, err := a.Project.PackageJSON()
// Generate the plugin.yml file for the project
pluginYML, err := GeneratePluginYML(a.Project, a.MinecraftVersion)
if err != nil {
return err
}

// Define the plugin.yml details for the plugin
pluginYml := PluginYml{
MinecraftVersion: a.MinecraftVersion,
PackageJSON: packageJson,
return fmt.Errorf("generating plugin.yml: %w", err)
}

// Produce the final JAR file
if err := WriteJarFile(
file,
jarTemplateBuf.Bytes(),
pluginCode,
&pluginYml,
pluginYML,
); err != nil {
return err
}
Expand All @@ -94,7 +89,7 @@ func WriteJarFile(
writer io.Writer,
templateJarData []byte,
pluginSourceCode io.Reader,
pluginYml *PluginYml,
pluginYML *pluginyml.Plugin,
) error {

fmt.Println("============================================================")
Expand Down Expand Up @@ -147,8 +142,10 @@ func WriteJarFile(
if err != nil {
return err
}
if _, err := io.Copy(ymlFile, strings.NewReader(pluginYml.String())); err != nil {
return err
enc := yaml.NewEncoder(ymlFile)
enc.SetIndent(2)
if err := enc.Encode(pluginYML); err != nil {
return fmt.Errorf("encoding plugin.yml: %w", err)
}

fmt.Println(" -> DONE")
Expand Down
113 changes: 44 additions & 69 deletions internal/build/pluginyml.go
Original file line number Diff line number Diff line change
@@ -1,90 +1,65 @@
package build

import (
"errors"
"fmt"
"strings"
"log"

"github.com/customrealms/cli/internal/minecraft"
"github.com/customrealms/cli/internal/pluginyml"
"github.com/customrealms/cli/internal/project"
)

const JAR_MAIN_CLASS = "io.customrealms.MainPlugin"
const JarMainClass = "io.customrealms.MainPlugin"

type PluginYml struct {
MinecraftVersion minecraft.Version
PackageJSON *project.PackageJSON
}
func GeneratePluginYML(project project.Project, version minecraft.Version) (*pluginyml.Plugin, error) {
// Read the package.json file
packageJSON, err := project.PackageJSON()
if err != nil {
return nil, fmt.Errorf("getting package.json: %w", err)
}

func (y *PluginYml) String() string {
var lines []string
// Read the plugin.yml file
plugin, err := project.PluginYML()
if err != nil {
return nil, fmt.Errorf("getting plugin.yml: %w", err)
}

// General plugin details
lines = append(lines,
fmt.Sprintf("name: %s", y.PackageJSON.Name),
fmt.Sprintf("api-version: %s", y.MinecraftVersion.ApiVersion()),
fmt.Sprintf("version: %s", y.PackageJSON.Version),
fmt.Sprintf("main: %s", JAR_MAIN_CLASS),
)
if len(y.PackageJSON.Author) > 0 {
lines = append(lines, fmt.Sprintf("author: %s", y.PackageJSON.Author))
// If plugin.yml and package.json are both missing, it's an error
if packageJSON == nil && plugin == nil {
return nil, errors.New("missing both package.json and plugin.yml")
}
if len(y.PackageJSON.Website) > 0 {
lines = append(lines, fmt.Sprintf("website: %s", y.PackageJSON.Website))

// If there is no plugin.yml file present, create one
if plugin == nil {
plugin = &pluginyml.Plugin{}
plugin.Name = packageJSON.Name
}
lines = append(lines, "")

// Add the commands
if len(y.PackageJSON.Commands) > 0 {
lines = append(lines, "commands:")
for key, attrs := range y.PackageJSON.Commands {
lines = append(lines, indent(1)+fmt.Sprintf("%s:", key))
if attrs != nil {
if len(attrs.Description) > 0 {
lines = append(lines, indent(2)+fmt.Sprintf("description: %s", attrs.Description))
}
if len(attrs.Aliases) > 0 {
lines = append(lines, indent(2)+fmt.Sprintf("aliases: [%s]", strings.Join(attrs.Aliases, ", ")))
}
if len(attrs.Permission) > 0 {
lines = append(lines, indent(2)+fmt.Sprintf("permission: %s", attrs.Permission))
}
if len(attrs.PermissionMessage) > 0 {
lines = append(lines, indent(2)+fmt.Sprintf("permision-message: %s", attrs.PermissionMessage))
}
if len(attrs.Usage) > 0 {
lines = append(lines, indent(2)+fmt.Sprintf("usage: %q", attrs.Usage))
}
}
}
lines = append(lines, "")
// Set the main Java class for the plugin
plugin.Main = JarMainClass

// Set the Bukkit API version for the plugin
if version != nil {
apiVersion := version.ApiVersion()
plugin.ApiVersion = &apiVersion
}

// Add the permissions
if len(y.PackageJSON.Permissions) > 0 {
lines = append(lines, "permissions:")
for key, attrs := range y.PackageJSON.Permissions {
lines = append(lines, indent(1)+fmt.Sprintf("%s:", key))
if attrs != nil {
if len(attrs.Description) > 0 {
lines = append(lines, indent(2)+fmt.Sprintf("description: %s", attrs.Description))
}
if attrs.Default != nil {
lines = append(lines, indent(2)+fmt.Sprintf("default: %t", *attrs.Default))
}
if attrs.Children != nil {
lines = append(lines, indent(2)+"children:")
for childKey, childVal := range attrs.Children {
lines = append(lines, indent(3)+fmt.Sprintf("%s: %t", childKey, childVal))
}
}
}
// If there is a package.json file
if packageJSON != nil {
// Update the version if it's missing
if plugin.Version == "" && packageJSON.Version != "" {
plugin.Version = packageJSON.Version
} else if plugin.Version == "" && packageJSON.Version == "" {
log.Println("No version found in plugin.yml or package.json. Consider adding a version to package.json.")
log.Println("Using version '0.0.0' as a fallback.")
plugin.Version = "0.0.0"
} else if plugin.Version != packageJSON.Version {
log.Println("Version mismatch between plugin.yml and package.json. Consider removing `version` from plugin.yml.")
log.Printf("Using version '%s' from plugin.yml", plugin.Version)
}
lines = append(lines, "")
}

return strings.Join(lines, "\n")
}

func indent(level int) string {
return strings.Repeat(" ", 2*level)
// Return the plugin yml
return plugin, nil
}
Loading

0 comments on commit ccd3e69

Please sign in to comment.