22using System . Collections . Concurrent ;
33using System . Collections . Generic ;
44using System . CommandLine ;
5- using System . IO ;
65using System . Linq ;
76using System . Threading ;
7+
88using Wyam . Common . IO ;
9+ using Wyam . Common . Tracing ;
910using Wyam . Configuration . Preprocessing ;
10- using Trace = Wyam . Common . Tracing . Trace ;
11+ using Wyam . LiveReload ;
1112
1213namespace Wyam . Commands
1314{
@@ -18,7 +19,7 @@ internal class BuildCommand : Command
1819 private readonly InterlockedBool _exit = new InterlockedBool ( false ) ;
1920 private readonly InterlockedBool _newEngine = new InterlockedBool ( false ) ;
2021 private readonly ConfigOptions _configOptions = new ConfigOptions ( ) ;
21-
22+
2223 private bool _preview = false ;
2324 private int _previewPort = 5080 ;
2425 private DirectoryPath _previewVirtualDirectory = null ;
@@ -27,7 +28,7 @@ internal class BuildCommand : Command
2728 private bool _verifyConfig = false ;
2829 private DirectoryPath _previewRoot = null ;
2930 private bool _watch = false ;
30-
31+
3132 public override string Description => "Runs the build process (this is the default command)." ;
3233
3334 public override string [ ] SupportedDirectives => new [ ]
@@ -106,7 +107,7 @@ private static void AddSettings(IDictionary<string, object> settings, IReadOnlyL
106107 {
107108 foreach ( KeyValuePair < string , object > kvp in MetadataParser . Parse ( value ) )
108109 {
109- settings [ kvp . Key ] = kvp . Value ;
110+ settings [ kvp . Key ] = kvp . Value ;
110111 }
111112 }
112113
@@ -119,7 +120,7 @@ protected override ExitCode RunCommand(Preprocessor preprocessor)
119120 {
120121 // Get the standard input stream
121122 _configOptions . Stdin = StandardInputReader . Read ( ) ;
122-
123+
123124 // Fix the root folder and other files
124125 DirectoryPath currentDirectory = Environment . CurrentDirectory ;
125126 _configOptions . RootPath = _configOptions . RootPath == null ? currentDirectory : currentDirectory . Combine ( _configOptions . RootPath ) ;
@@ -162,6 +163,15 @@ protected override ExitCode RunCommand(Preprocessor preprocessor)
162163
163164 bool messagePump = false ;
164165
166+ // Start the LiveReload server.
167+ bool runLiveReloadServer = _watch ;
168+ LiveReloadServer liveReloadServer = null ;
169+ if ( runLiveReloadServer )
170+ {
171+ liveReloadServer = new LiveReloadServer ( ) ;
172+ liveReloadServer . StartStandaloneHost ( ) ;
173+ }
174+
165175 // Start the preview server
166176 IDisposable previewServer = null ;
167177 if ( _preview )
@@ -170,7 +180,7 @@ protected override ExitCode RunCommand(Preprocessor preprocessor)
170180 DirectoryPath previewPath = _previewRoot == null
171181 ? engineManager . Engine . FileSystem . GetOutputDirectory ( ) . Path
172182 : engineManager . Engine . FileSystem . GetOutputDirectory ( _previewRoot ) . Path ;
173- previewServer = PreviewServer . Start ( previewPath , _previewPort , _previewForceExtension , _previewVirtualDirectory ) ;
183+ previewServer = PreviewServer . Start ( previewPath , _previewPort , _previewForceExtension , _previewVirtualDirectory , liveReloadServer ) ;
174184 }
175185
176186 // Start the watchers
@@ -192,7 +202,7 @@ protected override ExitCode RunCommand(Preprocessor preprocessor)
192202 {
193203 Trace . Information ( "Watching configuration file {0}" , _configOptions . ConfigFilePath ) ;
194204 configFileWatcher = new ActionFileSystemWatcher ( engineManager . Engine . FileSystem . GetOutputDirectory ( ) . Path ,
195- new [ ] { _configOptions . ConfigFilePath . Directory } , false , _configOptions . ConfigFilePath . FileName . FullPath , path =>
205+ new [ ] { _configOptions . ConfigFilePath . Directory } , false , _configOptions . ConfigFilePath . FileName . FullPath , path =>
196206 {
197207 FilePath filePath = new FilePath ( path ) ;
198208 if ( _configOptions . ConfigFilePath . Equals ( filePath ) )
@@ -228,7 +238,7 @@ protected override ExitCode RunCommand(Preprocessor preprocessor)
228238 // Wait for activity
229239 while ( true )
230240 {
231- _messageEvent . WaitOne ( ) ; // Blocks the current thread until a signal
241+ _messageEvent . WaitOne ( ) ; // Blocks the current thread until a signal
232242 if ( _exit )
233243 {
234244 break ;
@@ -283,6 +293,8 @@ protected override ExitCode RunCommand(Preprocessor preprocessor)
283293 {
284294 exitCode = ExitCode . ExecutionError ;
285295 }
296+
297+ liveReloadServer ? . RebuildCompleted ( changedFiles ) ;
286298 }
287299 }
288300
@@ -301,6 +313,7 @@ protected override ExitCode RunCommand(Preprocessor preprocessor)
301313 inputFolderWatcher ? . Dispose ( ) ;
302314 configFileWatcher ? . Dispose ( ) ;
303315 previewServer ? . Dispose ( ) ;
316+ liveReloadServer ? . Dispose ( ) ;
304317 }
305318
306319 return exitCode ;
0 commit comments