5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 08:41:41 +08:00

Move watcher init to doWatcherLoop and implement -reloaddirs (#2871)

* remove random print statement

* move watcher into loop and implement reloaddirs

* Fixed -reloaddirs for issue #2829

---------

Co-authored-by: Lea Anthony <lea.anthony@gmail.com>
This commit is contained in:
David Haukeness 2023-09-06 03:33:22 -06:00 committed by GitHub
parent acf8dea170
commit 069fe18b9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 18 deletions

View File

@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
@ -138,20 +139,6 @@ func Application(f *flags.Dev, logger *clilogger.CLILogger) error {
}
}
// create the project files watcher
watcher, err := initialiseWatcher(cwd)
if err != nil {
return err
}
defer func(watcher *fsnotify.Watcher) {
err := watcher.Close()
if err != nil {
logger.Fatal(err.Error())
}
}(watcher)
logutils.LogGreen("Watching (sub)/directory: %s", cwd)
logutils.LogGreen("Using DevServer URL: %s", f.DevServerURL())
if f.FrontendDevServerURL != "" {
logutils.LogGreen("Using Frontend DevServer URL: %s", f.FrontendDevServerURL)
@ -165,7 +152,10 @@ func Application(f *flags.Dev, logger *clilogger.CLILogger) error {
}()
// Watch for changes and trigger restartApp()
debugBinaryProcess = doWatcherLoop(buildOptions, debugBinaryProcess, f, watcher, exitCodeChannel, quitChannel, f.DevServerURL(), legacyUseDevServerInsteadofCustomScheme)
debugBinaryProcess, err = doWatcherLoop(cwd, buildOptions, debugBinaryProcess, f, exitCodeChannel, quitChannel, f.DevServerURL(), legacyUseDevServerInsteadofCustomScheme)
if err != nil {
return err
}
// Kill the current program if running and remove dev binary
if err := killProcessAndCleanupBinary(debugBinaryProcess, appBinary); err != nil {
@ -337,7 +327,23 @@ func restartApp(buildOptions *build.Options, debugBinaryProcess *process.Process
}
// doWatcherLoop is the main watch loop that runs while dev is active
func doWatcherLoop(buildOptions *build.Options, debugBinaryProcess *process.Process, f *flags.Dev, watcher *fsnotify.Watcher, exitCodeChannel chan int, quitChannel chan os.Signal, devServerURL *url.URL, legacyUseDevServerInsteadofCustomScheme bool) *process.Process {
func doWatcherLoop(cwd string, buildOptions *build.Options, debugBinaryProcess *process.Process, f *flags.Dev, exitCodeChannel chan int, quitChannel chan os.Signal, devServerURL *url.URL, legacyUseDevServerInsteadofCustomScheme bool) (*process.Process, error) {
// create the project files watcher
watcher, err := initialiseWatcher(cwd)
if err != nil {
logutils.LogRed("Unable to create filesystem watcher. Reloads will not occur.")
return nil, err
}
defer func(watcher *fsnotify.Watcher) {
err := watcher.Close()
if err != nil {
log.Fatal(err.Error())
}
}(watcher)
logutils.LogGreen("Watching (sub)/directory: %s", cwd)
// Main Loop
var extensionsThatTriggerARebuild = sliceToMap(strings.Split(f.Extensions, ","))
var dirsThatTriggerAReload []string
@ -351,6 +357,12 @@ func doWatcherLoop(buildOptions *build.Options, debugBinaryProcess *process.Proc
continue
}
dirsThatTriggerAReload = append(dirsThatTriggerAReload, thePath)
err = watcher.Add(thePath)
if err != nil {
logutils.LogRed("Unable to watch path: %s due to error %v", thePath, err)
} else {
logutils.LogGreen("Watching (sub)/directory: %s", thePath)
}
}
quit := false
@ -499,7 +511,7 @@ func doWatcherLoop(buildOptions *build.Options, debugBinaryProcess *process.Proc
quit = true
}
}
return debugBinaryProcess
return debugBinaryProcess, nil
}
func joinPath(url *url.URL, subPath string) string {

View File

@ -38,7 +38,6 @@ func initialiseWatcher(cwd string) (*fsnotify.Watcher, error) {
if err != nil {
return nil, err
}
println("watching: " + dir)
}
return watcher, nil
}

View File

@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed `SetBackgroundColour` so it sets the window's background color to reduce resize flickering on Linux. Added by @lyimmi in [PR](https://github.com/wailsapp/wails/pull/2853)
- Fixed disable window resize option and wrong initial window size when its enabled. Added by @lyimmi in [PR](https://github.com/wailsapp/wails/pull/2863)
- Fixed build hook command parsing. Added by @smac89 in [PR](https://github.com/wailsapp/wails/pull/2836)
- Fixed `-reloaddir` flag to watch additional directories (non-recursively). [@haukened](https://github.com/haukened) in [PR #2871](https://github.com/wailsapp/wails/pull/2871)
- Fixed support for Go 1.21 `go.mod` files. Fixed by @stffabi in [PR](https://github.com/wailsapp/wails/pull/2876)
### Added