diff --git a/src/components/tbc.host/Components/FileEnvironment/FileEnvironment.cs b/src/components/tbc.host/Components/FileEnvironment/FileEnvironment.cs index 079b24d..1c7c53d 100644 --- a/src/components/tbc.host/Components/FileEnvironment/FileEnvironment.cs +++ b/src/components/tbc.host/Components/FileEnvironment/FileEnvironment.cs @@ -17,30 +17,41 @@ using Tbc.Host.Components.IncrementalCompiler; using Tbc.Host.Components.IncrementalCompiler.Models; using Tbc.Host.Components.TargetClient; +using Tbc.Host.Config; using Tbc.Host.Extensions; namespace Tbc.Host.Components.FileEnvironment { public partial class FileEnvironment : TransientComponentBase, IFileEnvironment, IExposeCommands, IHaveComponentsThatExposeCommands { + public FileEnvironmentConfig Config { get; } public ITargetClient Client { get; } public ICommandProcessor CommandProcessor { get; } public IFileWatcher FileWatcher { get; } public IIncrementalCompiler IncrementalCompiler { get; } public IFileSystem FileSystem { get; set; } - public FileEnvironment(IRemoteClientDefinition client, + public FileEnvironment( + FileEnvironmentConfig config, + IRemoteClientDefinition client, IFileWatcher fileWatcher, ICommandProcessor commandProcessor, Func targetClientFactory, Func incrementalCompilerFactory, ILogger logger, IFileSystem fileSystem) : base(logger) { + Config = config; Client = targetClientFactory(client); CommandProcessor = commandProcessor; FileSystem = fileSystem; FileWatcher = fileWatcher; IncrementalCompiler = incrementalCompilerFactory($"{client.Address}:{client.Port}"); IncrementalCompiler.SetRootPath(FileWatcher.WatchPath); + + if (config.LoadContext is { } lc) + { + Logger.LogInformation("Using load context from configuration: {Context}", lc); + _loadContext = Config?.LoadContext; + } } private bool _running; @@ -48,7 +59,7 @@ public FileEnvironment(IRemoteClientDefinition client, private string? _primaryTypeHint; private EmittedAssembly? _lastEmittedAssembly; private string? _loadContext; - + public async Task Run() { if (_running) @@ -90,16 +101,27 @@ private async Task TryLoadLoadContext() { if (!String.IsNullOrWhiteSpace(_loadContext)) { - var ctx = JsonConvert.DeserializeObject( - await File.ReadAllTextAsync($"{_loadContext}.json")); + try + { + var ctx = JsonConvert.DeserializeObject( + await File.ReadAllTextAsync($"{_loadContext}.json")); - if (ctx is null) - return; + Logger.LogInformation("Restoring load context: {@Context}", ctx); - foreach (var file in ctx.WatchedFiles.Select(x => new ChangedFile { Path = x, Contents = File.ReadAllText(x) })) - IncrementalCompiler.StageFile(file, silent: true); + if (ctx is null) + return; + + foreach (var file in ctx.WatchedFiles + .Where(x => !String.IsNullOrWhiteSpace(x) && FileSystem.File.Exists(x)) + .Select(x => new ChangedFile { Path = x, Contents = FileSystem.File.ReadAllText(x) })) + IncrementalCompiler.StageFile(file, silent: true); - await SetPrimaryTypeHint(ctx.PrimaryTypeHint); + await SetPrimaryTypeHint(ctx.PrimaryTypeHint); + } + catch (Exception ex) + { + Logger.LogError(ex, "Failed to process load context {Context}", _loadContext); + } } } diff --git a/src/components/tbc.host/Config/FileEnvironmentConfig.cs b/src/components/tbc.host/Config/FileEnvironmentConfig.cs new file mode 100644 index 0000000..ca0d5f2 --- /dev/null +++ b/src/components/tbc.host/Config/FileEnvironmentConfig.cs @@ -0,0 +1,6 @@ +namespace Tbc.Host.Config; + +public class FileEnvironmentConfig +{ + public string? LoadContext { get; set; } +} \ No newline at end of file diff --git a/src/components/tbc.host/Config/KnownConfigurationKeys.cs b/src/components/tbc.host/Config/KnownConfigurationKeys.cs index 0993c60..7b87068 100644 --- a/src/components/tbc.host/Config/KnownConfigurationKeys.cs +++ b/src/components/tbc.host/Config/KnownConfigurationKeys.cs @@ -3,7 +3,8 @@ namespace Tbc.Host.Config public static class KnownConfigurationKeys { public const string FileWatch = "FileWatcher"; + public const string FileEnvironment = "FileEnvironment"; public const string ServiceDiscovery = "ServiceDiscovery"; public const string AssemblyCompilation = "AssemblyCompiler"; } -} \ No newline at end of file +} diff --git a/src/components/tbc.host/Configurator.cs b/src/components/tbc.host/Configurator.cs index 4c26b6d..2defccf 100644 --- a/src/components/tbc.host/Configurator.cs +++ b/src/components/tbc.host/Configurator.cs @@ -17,6 +17,7 @@ public static class Configurator public static readonly Dictionary KnownConfigMappings = new Dictionary { [KnownConfigurationKeys.FileWatch] = typeof(FileWatchConfig), + [KnownConfigurationKeys.FileEnvironment] = typeof(FileEnvironmentConfig), [KnownConfigurationKeys.AssemblyCompilation] = typeof(AssemblyCompilationOptions), [KnownConfigurationKeys.ServiceDiscovery] = typeof(object), }; diff --git a/src/heads/tbc.host.console/ConsoleConfig.cs b/src/heads/tbc.host.console/ConsoleConfig.cs index 944ec7d..9a79191 100644 --- a/src/heads/tbc.host.console/ConsoleConfig.cs +++ b/src/heads/tbc.host.console/ConsoleConfig.cs @@ -44,7 +44,11 @@ public static Dictionary Default() RootPath = Environment.CurrentDirectory, Ignore = { "/obj/", "/bin/" } } - .ToJObject() + .ToJObject(), + + [KnownConfigurationKeys.FileEnvironment] + = new FileEnvironmentConfig { } + .ToJObject(), }; } }