|
1 | 1 | package build |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "errors" |
4 | 5 | "fmt" |
5 | | - "strings" |
| 6 | + "log" |
6 | 7 |
|
7 | 8 | "github.com/customrealms/cli/internal/minecraft" |
| 9 | + "github.com/customrealms/cli/internal/pluginyml" |
8 | 10 | "github.com/customrealms/cli/internal/project" |
9 | 11 | ) |
10 | 12 |
|
11 | | -const JAR_MAIN_CLASS = "io.customrealms.MainPlugin" |
| 13 | +const JarMainClass = "io.customrealms.MainPlugin" |
12 | 14 |
|
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 | + } |
17 | 21 |
|
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 | + } |
20 | 27 |
|
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") |
30 | 31 | } |
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 |
33 | 37 | } |
34 | | - lines = append(lines, "") |
35 | 38 |
|
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 |
60 | 46 | } |
61 | 47 |
|
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) |
81 | 60 | } |
82 | | - lines = append(lines, "") |
83 | 61 | } |
84 | 62 |
|
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 |
90 | 65 | } |
0 commit comments