Skip to content

Commit 1f40b3b

Browse files
authoredDec 23, 2024
Avoid Exceptions when AppConfigPath does not exist (#1218)
1 parent fd051a4 commit 1f40b3b

File tree

3 files changed

+17
-24
lines changed

3 files changed

+17
-24
lines changed
 

‎src/AppModel/NetDaemon.AppModel.SourceDeployedApps/Compiler/SyntaxTreeResolver.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
using Microsoft.CodeAnalysis;
22
using Microsoft.CodeAnalysis.CSharp;
33
using Microsoft.CodeAnalysis.Text;
4+
using Microsoft.Extensions.Logging;
45
using Microsoft.Extensions.Options;
56

67
namespace NetDaemon.AppModel.Internal.Compiler;
78

8-
internal class SyntaxTreeResolver : ISyntaxTreeResolver
9+
internal class SyntaxTreeResolver(IOptions<AppConfigurationLocationSetting> settings, ILogger<SyntaxTreeResolver> logger) : ISyntaxTreeResolver
910
{
10-
private readonly AppConfigurationLocationSetting _settings;
11-
12-
public SyntaxTreeResolver(
13-
IOptions<AppConfigurationLocationSetting> settings
14-
)
15-
{
16-
_settings = settings.Value;
17-
}
11+
private readonly AppConfigurationLocationSetting _settings = settings.Value;
1812

1913
public IReadOnlyCollection<SyntaxTree> GetSyntaxTrees()
2014
{
15+
logger.LogDebug("Loading applications from folder {Path}", _settings.ApplicationConfigurationFolder);
16+
2117
var fullPath = Path.GetFullPath(_settings.ApplicationConfigurationFolder);
2218
// Get the paths for all .cs files recursively in app folder
2319
var csFiles = Directory.EnumerateFiles(

‎src/AppModel/NetDaemon.AppModel/Common/Extensions/IConfigurationBuilderExtensions.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ public static class ConfigurationBuilderExtensions
1616
/// <param name="appConfigPath">Path to the folder containing configurations</param>
1717
public static IConfigurationBuilder AddJsonAppConfig(this IConfigurationBuilder builder, string appConfigPath)
1818
{
19-
Directory.EnumerateFiles(appConfigPath, "*.json", SearchOption.AllDirectories)
20-
.ToList()
21-
.ForEach(x => builder.AddJsonFile(x, false, false));
19+
if (Directory.Exists(appConfigPath))
20+
{
21+
Directory.EnumerateFiles(appConfigPath, "*.json", SearchOption.AllDirectories)
22+
.ToList()
23+
.ForEach(x => builder.AddJsonFile(x, false, false));
24+
}
2225
return builder;
2326
}
2427

@@ -29,9 +32,12 @@ public static IConfigurationBuilder AddJsonAppConfig(this IConfigurationBuilder
2932
/// <param name="appConfigPath">Path to the folder containing configurations</param>
3033
public static IConfigurationBuilder AddYamlAppConfig(this IConfigurationBuilder builder, string appConfigPath)
3134
{
32-
Directory.EnumerateFiles(appConfigPath, "*.y*", SearchOption.AllDirectories)
33-
.ToList()
34-
.ForEach(x => builder.AddYamlFile(x, false, false));
35+
if (Directory.Exists(appConfigPath))
36+
{
37+
Directory.EnumerateFiles(appConfigPath, "*.y*", SearchOption.AllDirectories)
38+
.ToList()
39+
.ForEach(x => builder.AddYamlFile(x, false, false));
40+
}
3541
return builder;
3642
}
3743

‎src/Runtime/NetDaemon.Runtime/Internal/NetDaemonRuntime.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
using System.Reactive.Linq;
2-
using System.Reflection;
32
using NetDaemon.AppModel;
43
using NetDaemon.HassModel;
54

65
namespace NetDaemon.Runtime.Internal;
76

87
internal class NetDaemonRuntime(IHomeAssistantRunner homeAssistantRunner,
98
IOptions<HomeAssistantSettings> settings,
10-
IOptions<AppConfigurationLocationSetting> locationSettings,
119
IServiceProvider serviceProvider,
1210
ILogger<NetDaemonRuntime> logger,
1311
ICacheManager cacheManager)
@@ -115,13 +113,6 @@ private async Task LoadNewAppContextAsync(IHomeAssistantConnection haConnection,
115113
var appModel = serviceProvider.GetService<IAppModel>();
116114
if (appModel == null) return;
117115

118-
// this logging is a bit weird in this class
119-
if (!string.IsNullOrEmpty(locationSettings.Value.ApplicationConfigurationFolder))
120-
logger.LogDebug("Loading applications from folder {Path}",
121-
Path.GetFullPath(locationSettings.Value.ApplicationConfigurationFolder));
122-
else
123-
logger.LogDebug("Loading applications with no configuration folder");
124-
125116
_applicationModelContext = await appModel.LoadNewApplicationContext(CancellationToken.None).ConfigureAwait(false);
126117

127118
// Handle state change for apps if registered

0 commit comments

Comments
 (0)