|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "errors" |
| 5 | + "log" |
4 | 6 | "os" |
| 7 | + "path/filepath" |
5 | 8 |
|
6 | 9 | "github.com/customrealms/cli/internal/build" |
7 | 10 | "github.com/customrealms/cli/internal/project" |
8 | 11 | "github.com/customrealms/cli/internal/serve" |
9 | 12 | "github.com/customrealms/cli/internal/server" |
| 13 | + "github.com/fsnotify/fsnotify" |
| 14 | + "golang.org/x/sync/errgroup" |
10 | 15 | ) |
11 | 16 |
|
12 | 17 | type RunCmd struct { |
13 | 18 | ProjectDir string `name:"project" short:"p" usage:"plugin project directory" optional:""` |
14 | | - McVersion string `name:"mc" short:"mc" usage:"Minecraft version number target" optional:""` |
| 19 | + McVersion string `name:"mc" usage:"Minecraft version number target" optional:""` |
15 | 20 | TemplateJarFile string `name:"jar" short:"t" usage:"template JAR file" optional:""` |
16 | 21 | } |
17 | 22 |
|
@@ -70,11 +75,77 @@ func (c *RunCmd) Run() error { |
70 | 75 | return err |
71 | 76 | } |
72 | 77 |
|
73 | | - // Create the serve runner |
74 | | - serveAction := serve.ServeAction{ |
75 | | - MinecraftVersion: minecraftVersion, |
76 | | - PluginJarPath: outputFile, |
77 | | - ServerJarFetcher: serverJarFetcher, |
78 | | - } |
79 | | - return serveAction.Run(ctx) |
| 78 | + eg, ctx := errgroup.WithContext(ctx) |
| 79 | + |
| 80 | + chanPluginUpdated := make(chan struct{}) |
| 81 | + chanServerStopped := make(chan struct{}) |
| 82 | + eg.Go(func() error { |
| 83 | + // Create new watcher. |
| 84 | + watcher, err := fsnotify.NewWatcher() |
| 85 | + if err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + defer watcher.Close() |
| 89 | + |
| 90 | + // Start listening for events. |
| 91 | + go func() { |
| 92 | + for { |
| 93 | + select { |
| 94 | + case event, ok := <-watcher.Events: |
| 95 | + if !ok { |
| 96 | + return |
| 97 | + } |
| 98 | + if event.Has(fsnotify.Write) { |
| 99 | + // Rebuild the plugin JAR file |
| 100 | + if err := buildAction.Run(ctx); err != nil { |
| 101 | + log.Println("Error: ", err) |
| 102 | + } else { |
| 103 | + select { |
| 104 | + case chanPluginUpdated <- struct{}{}: |
| 105 | + case <-ctx.Done(): |
| 106 | + return |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + case err, ok := <-watcher.Errors: |
| 111 | + if !ok { |
| 112 | + return |
| 113 | + } |
| 114 | + log.Println("error:", err) |
| 115 | + case <-ctx.Done(): |
| 116 | + return |
| 117 | + case <-chanServerStopped: |
| 118 | + return |
| 119 | + } |
| 120 | + } |
| 121 | + }() |
| 122 | + |
| 123 | + // Add the project directory and src directory to the watcher |
| 124 | + if err := errors.Join( |
| 125 | + watcher.Add(c.ProjectDir), |
| 126 | + watcher.Add(filepath.Join(c.ProjectDir, "src")), |
| 127 | + ); err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + |
| 131 | + // Block until the server is stopped |
| 132 | + select { |
| 133 | + case <-ctx.Done(): |
| 134 | + return ctx.Err() |
| 135 | + case <-chanServerStopped: |
| 136 | + return nil |
| 137 | + } |
| 138 | + }) |
| 139 | + eg.Go(func() error { |
| 140 | + defer close(chanServerStopped) |
| 141 | + |
| 142 | + // Create the serve runner |
| 143 | + serveAction := serve.ServeAction{ |
| 144 | + MinecraftVersion: minecraftVersion, |
| 145 | + PluginJarPath: outputFile, |
| 146 | + ServerJarFetcher: serverJarFetcher, |
| 147 | + } |
| 148 | + return serveAction.Run(ctx, chanPluginUpdated) |
| 149 | + }) |
| 150 | + return eg.Wait() |
80 | 151 | } |
0 commit comments