Skip to content

Commit

Permalink
Merge 974d808 into b782135
Browse files Browse the repository at this point in the history
  • Loading branch information
asherw committed Aug 29, 2020
2 parents b782135 + 974d808 commit 8a72377
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 156 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace NetDaemon.Infrastructure.Extensions
{
public static class AssemblyExtensions
{
public static IEnumerable<Type> GetTypesWhereSubclassOf<T>(this Assembly assembly)
{
return assembly.GetTypes().Where(x => x.IsClass && x.IsSubclassOf(typeof(T)));
}
}
}
28 changes: 27 additions & 1 deletion src/DaemonRunner/DaemonRunner/NetDaemonExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Microsoft.AspNetCore.Hosting;
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NetDaemon.Common.Configuration;
using NetDaemon.Daemon.Config;
using NetDaemon.Service;
using NetDaemon.Service.App;

namespace NetDaemon
{
Expand All @@ -17,12 +19,36 @@ public static IHostBuilder UseNetDaemon(this IHostBuilder hostBuilder)
services.Configure<HomeAssistantSettings>(context.Configuration.GetSection("HomeAssistant"));
services.Configure<NetDaemonSettings>(context.Configuration.GetSection("NetDaemon"));
services.AddSingleton<IYamlConfig, YamlConfig>();
RegisterNetDaemonAssembly(services);
})
.ConfigureWebHostDefaults(webbuilder =>
{
webbuilder.UseKestrel(options => { });
webbuilder.UseStartup<ApiStartup>();
});
}

private static void RegisterNetDaemonAssembly(IServiceCollection services)
{
if (BypassLocalAssemblyLoading())
services.AddSingleton<IDaemonAppCompiler, LocalDaemonAppCompiler>();
else
services.AddSingleton<IDaemonAppCompiler, DaemonAppCompiler>();
}

private static bool BypassLocalAssemblyLoading()
{
var value = Environment.GetEnvironmentVariable("HASS_DISABLE_LOCAL_ASM");

if (string.IsNullOrWhiteSpace(value))
return false;

if (bool.TryParse(value, out var boolResult))
return boolResult;

return false;
}
}
}
43 changes: 43 additions & 0 deletions src/DaemonRunner/DaemonRunner/Service/App/DaemonAppCompiler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NetDaemon.Common;
using NetDaemon.Common.Configuration;
using NetDaemon.Infrastructure.Extensions;

namespace NetDaemon.Service.App
{
public class DaemonAppCompiler : IDaemonAppCompiler
{
private readonly ILogger<DaemonAppCompiler> _logger;
private readonly IOptions<NetDaemonSettings> _netDaemonSettings;

public DaemonAppCompiler(ILogger<DaemonAppCompiler> logger, IOptions<NetDaemonSettings> netDaemonSettings)
{
_logger = logger;
_netDaemonSettings = netDaemonSettings;
}

public IEnumerable<Type> GetApps()
{
var assembly = Load();
var apps = assembly.GetTypesWhereSubclassOf<NetDaemonAppBase>();

if (!apps.Any())
_logger.LogWarning("No .cs files found, please add files to {sourceFolder}/apps", _netDaemonSettings.Value.SourceFolder);

return apps;
}

public Assembly Load()
{
CollectibleAssemblyLoadContext alc;
var appFolder = Path.Combine(_netDaemonSettings.Value.SourceFolder!, "apps");
return DaemonCompiler.GetCompiledAppAssembly(out alc, appFolder!, _logger);
}
}
}
Loading

0 comments on commit 8a72377

Please sign in to comment.