Skip to content

Commit

Permalink
Avoid Exceptions when AppConfigPath does not exist (#1218)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankBakkerNl authored Dec 23, 2024
1 parent fd051a4 commit 1f40b3b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace NetDaemon.AppModel.Internal.Compiler;

internal class SyntaxTreeResolver : ISyntaxTreeResolver
internal class SyntaxTreeResolver(IOptions<AppConfigurationLocationSetting> settings, ILogger<SyntaxTreeResolver> logger) : ISyntaxTreeResolver
{
private readonly AppConfigurationLocationSetting _settings;

public SyntaxTreeResolver(
IOptions<AppConfigurationLocationSetting> settings
)
{
_settings = settings.Value;
}
private readonly AppConfigurationLocationSetting _settings = settings.Value;

public IReadOnlyCollection<SyntaxTree> GetSyntaxTrees()
{
logger.LogDebug("Loading applications from folder {Path}", _settings.ApplicationConfigurationFolder);

var fullPath = Path.GetFullPath(_settings.ApplicationConfigurationFolder);
// Get the paths for all .cs files recursively in app folder
var csFiles = Directory.EnumerateFiles(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ public static class ConfigurationBuilderExtensions
/// <param name="appConfigPath">Path to the folder containing configurations</param>
public static IConfigurationBuilder AddJsonAppConfig(this IConfigurationBuilder builder, string appConfigPath)
{
Directory.EnumerateFiles(appConfigPath, "*.json", SearchOption.AllDirectories)
.ToList()
.ForEach(x => builder.AddJsonFile(x, false, false));
if (Directory.Exists(appConfigPath))
{
Directory.EnumerateFiles(appConfigPath, "*.json", SearchOption.AllDirectories)
.ToList()
.ForEach(x => builder.AddJsonFile(x, false, false));
}
return builder;
}

Expand All @@ -29,9 +32,12 @@ public static IConfigurationBuilder AddJsonAppConfig(this IConfigurationBuilder
/// <param name="appConfigPath">Path to the folder containing configurations</param>
public static IConfigurationBuilder AddYamlAppConfig(this IConfigurationBuilder builder, string appConfigPath)
{
Directory.EnumerateFiles(appConfigPath, "*.y*", SearchOption.AllDirectories)
.ToList()
.ForEach(x => builder.AddYamlFile(x, false, false));
if (Directory.Exists(appConfigPath))
{
Directory.EnumerateFiles(appConfigPath, "*.y*", SearchOption.AllDirectories)
.ToList()
.ForEach(x => builder.AddYamlFile(x, false, false));
}
return builder;
}

Expand Down
9 changes: 0 additions & 9 deletions src/Runtime/NetDaemon.Runtime/Internal/NetDaemonRuntime.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using System.Reactive.Linq;
using System.Reflection;
using NetDaemon.AppModel;
using NetDaemon.HassModel;

namespace NetDaemon.Runtime.Internal;

internal class NetDaemonRuntime(IHomeAssistantRunner homeAssistantRunner,
IOptions<HomeAssistantSettings> settings,
IOptions<AppConfigurationLocationSetting> locationSettings,
IServiceProvider serviceProvider,
ILogger<NetDaemonRuntime> logger,
ICacheManager cacheManager)
Expand Down Expand Up @@ -115,13 +113,6 @@ private async Task LoadNewAppContextAsync(IHomeAssistantConnection haConnection,
var appModel = serviceProvider.GetService<IAppModel>();
if (appModel == null) return;

// this logging is a bit weird in this class
if (!string.IsNullOrEmpty(locationSettings.Value.ApplicationConfigurationFolder))
logger.LogDebug("Loading applications from folder {Path}",
Path.GetFullPath(locationSettings.Value.ApplicationConfigurationFolder));
else
logger.LogDebug("Loading applications with no configuration folder");

_applicationModelContext = await appModel.LoadNewApplicationContext(CancellationToken.None).ConfigureAwait(false);

// Handle state change for apps if registered
Expand Down

0 comments on commit 1f40b3b

Please sign in to comment.