Skip to content

Commit

Permalink
feat: allow file env load context to be specified in context
Browse files Browse the repository at this point in the history
  • Loading branch information
rdavisau committed Feb 5, 2023
1 parent 6095abc commit cbd864a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,49 @@
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<FileEnvironment>, 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<IRemoteClientDefinition, ITargetClient> targetClientFactory,
Func<string, IIncrementalCompiler> incrementalCompilerFactory,
ILogger<FileEnvironment> 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;
public bool Terminated { get; set; }
private string? _primaryTypeHint;
private EmittedAssembly? _lastEmittedAssembly;
private string? _loadContext;

public async Task Run()
{
if (_running)
Expand Down Expand Up @@ -90,16 +101,27 @@ private async Task TryLoadLoadContext()
{
if (!String.IsNullOrWhiteSpace(_loadContext))
{
var ctx = JsonConvert.DeserializeObject<PersistedContext>(
await File.ReadAllTextAsync($"{_loadContext}.json"));
try
{
var ctx = JsonConvert.DeserializeObject<PersistedContext>(
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);
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/components/tbc.host/Config/FileEnvironmentConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Tbc.Host.Config;

public class FileEnvironmentConfig
{
public string? LoadContext { get; set; }
}
3 changes: 2 additions & 1 deletion src/components/tbc.host/Config/KnownConfigurationKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
}
1 change: 1 addition & 0 deletions src/components/tbc.host/Configurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public static class Configurator
public static readonly Dictionary<string, Type> KnownConfigMappings = new Dictionary<string, Type>
{
[KnownConfigurationKeys.FileWatch] = typeof(FileWatchConfig),
[KnownConfigurationKeys.FileEnvironment] = typeof(FileEnvironmentConfig),
[KnownConfigurationKeys.AssemblyCompilation] = typeof(AssemblyCompilationOptions),
[KnownConfigurationKeys.ServiceDiscovery] = typeof(object),
};
Expand Down
6 changes: 5 additions & 1 deletion src/heads/tbc.host.console/ConsoleConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ public static Dictionary<string, JObject> Default()
RootPath = Environment.CurrentDirectory,
Ignore = { "/obj/", "/bin/" }
}
.ToJObject()
.ToJObject(),

[KnownConfigurationKeys.FileEnvironment]
= new FileEnvironmentConfig { }
.ToJObject(),
};
}
}
Expand Down

0 comments on commit cbd864a

Please sign in to comment.