Skip to content

Commit

Permalink
Merge f009e3f into 2699546
Browse files Browse the repository at this point in the history
  • Loading branch information
asherw committed Sep 13, 2020
2 parents 2699546 + f009e3f commit 01eb592
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 156 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace NetDaemon.Infrastructure.Extensions
{
internal static class AssemblyExtensions
{
public static IEnumerable<Type> GetTypesWhereSubclassOf<T>(this Assembly assembly)
{
return assembly.GetTypes()
.Where(type => type.IsClass)
.Where(type => !type.IsGenericType)
.Where(type => !type.IsAbstract)
.Where(type => type.IsSubclassOf(typeof(T)));
}
}
}
21 changes: 20 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,29 @@ 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");
return bool.TryParse(value, out var result) && result;
}
}
}
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 01eb592

Please sign in to comment.