static void WaitForChanges(Options options, BuildResults results) { Watcher.Clear(); Watcher.Add(StartupPath, "*.csx"); Watcher.Add(StartupPath, "*.cs"); if (!string.IsNullOrEmpty(options.BuildScript)) { Watcher.Add(Path.GetDirectoryName(Path.GetFullPath(options.BuildScript)), "*.csx"); Watcher.Add(Path.GetDirectoryName(Path.GetFullPath(options.BuildScript)), "*.cs"); } if (results != null) { foreach (var path in results.ProbedPaths) Watcher.Add(Path.GetFullPath(path), "*.*"); foreach (var path in results.LoadedPlugins) Watcher.Add(path, "*.dll"); } var waitTask = Task.Run(() => Watcher.Wait()); while (!waitTask.IsCompleted) { if (Console.KeyAvailable) { var key = Console.ReadKey(true); if (key.Key == ConsoleKey.Enter) { nextBuildIsRebuild = true; break; } } Thread.Sleep(0); } }
public BuildResults Run(string buildScript, int logLevel, bool fullRebuild) { var context = new BuildContext(); AppDomain.CurrentDomain.AssemblyResolve += (o, e) => Resolver(context, e); var log = new BuildLog(); // if we weren't given a build script, try to find one in the current directory string scriptPath = buildScript; if (string.IsNullOrEmpty(scriptPath) || !File.Exists(scriptPath)) { var file = Path.GetDirectoryName(Directory.GetCurrentDirectory()); if (File.Exists(file + ".csx")) scriptPath = file + ".csx"; else if (File.Exists(file + ".cs")) scriptPath = file + ".cs"; else if (File.Exists("build.csx")) scriptPath = "build.csx"; else if (File.Exists("build.cs")) scriptPath = "build.cs"; else { log.Error("Could not find or open build script."); return null; } } scriptPath = Path.GetFullPath(scriptPath); Directory.SetCurrentDirectory(Path.GetDirectoryName(scriptPath)); // create the script engine string historyPath = Path.Combine(DataDirectory, Murmur.Hash(scriptPath, 144) + "_history.dat"); context.Initialize(historyPath, fullRebuild, log); var scriptEngine = new ScriptEngine(); var session = scriptEngine.CreateSession(context); // load plugins and assemblies session.AddReference(typeof(BuildContext).Assembly); session.AddReference(typeof(Enumerable).Assembly); session.AddReference(typeof(HashSet<>).Assembly); session.AddReference(typeof(ISet<>).Assembly); var code = File.ReadAllText(scriptPath); var buildResults = new BuildResults(); // import default namespaces session.ImportNamespace(typeof(BuildContext).Namespace); foreach (var n in Namespaces) session.ImportNamespace(n); try { // run the script var startTime = DateTime.Now; log.Write("Running build script ({0})", scriptPath); log.Write("Build started at {0}", startTime); log.Write("-----------------------------------------------"); log.Write(""); session.ExecuteFile(scriptPath); context.WaitAll(); context.Finished(); log.Write(""); log.Write("-----------------------------------------------"); log.Write("Build finished ({0:N2} seconds)", (DateTime.Now - startTime).TotalSeconds); log.Write( context.Stats.Failed > 0 ? ConsoleColor.Red : (ConsoleColor?)null, "{0} succeeded, {1} failed, {2} up-to-date", context.Stats.Succeeded, context.Stats.Failed, context.Stats.Skipped ); } catch (CompilationErrorException e) { foreach (var error in e.Diagnostics) { var position = error.Location.GetLineSpan(true); log.Error("({0}) {1}", position.StartLinePosition, error.Info.GetMessage()); } return null; } buildResults.ShouldRunAgain = context.ShouldRunAgain; buildResults.ProbedPaths = context.ProbedPaths.Select(p => Path.GetFullPath(p)).ToList(); buildResults.LoadedPlugins = context.Env.ReferencePaths.Select(t => Path.GetFullPath(t.Item1)).ToList(); return buildResults; }