-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
253 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ | |
TestResults | ||
lcov.info | ||
codecover | ||
**.DS_Store | ||
**.DS_Store | ||
.generated |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
src/DaemonRunner/DaemonRunner/Service/App/CodeGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
using JoySoftware.HomeAssistant.NetDaemon.DaemonRunner.Service.Config; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Runtime.CompilerServices; | ||
|
||
[assembly: InternalsVisibleTo("NetDaemon.Daemon.Tests")] | ||
|
||
namespace JoySoftware.HomeAssistant.NetDaemon.DaemonRunner.Service.App | ||
{ | ||
public class CodeGenerator | ||
{ | ||
/// <summary> | ||
/// Mapps the domain to corresponding implemented Fluent API, will be added as | ||
/// more and more entity types are supported | ||
/// </summary> | ||
private static IDictionary<string, (string, string)> _FluentApiMapper = new Dictionary<string, (string, string)> | ||
{ | ||
["light"] = ("Entity", "IEntity"), | ||
["script"] = ("Entity", "IEntity"), | ||
["scene"] = ("Entity", "IEntity"), | ||
["switch"] = ("Entity", "IEntity"), | ||
["camera"] = ("Entity", "IEntity"), | ||
["media_player"] = ("MediaPlayer", "IMediaPlayer"), | ||
["automation"] = ("Entity", "IEntity"), | ||
// ["input_boolean"], | ||
// ["remote"], | ||
// ["climate"], | ||
}; | ||
|
||
public string? GenerateCode(string nameSpace, IEnumerable<string> entities) | ||
{ | ||
var code = SyntaxFactory.CompilationUnit(); | ||
|
||
// Add Usings statements | ||
code = code.AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("JoySoftware.HomeAssistant.NetDaemon.Common"))); | ||
|
||
// Add namespace | ||
var namespaceDeclaration = SyntaxFactory.NamespaceDeclaration(SyntaxFactory.ParseName(nameSpace)).NormalizeWhitespace(); | ||
|
||
// Add support for extensions for entities | ||
var extensionClass = SyntaxFactory.ClassDeclaration("EntityExtension"); | ||
|
||
extensionClass = extensionClass.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword), | ||
SyntaxFactory.Token(SyntaxKind.StaticKeyword), SyntaxFactory.Token(SyntaxKind.PartialKeyword)); | ||
|
||
|
||
// Get all available domains, this is used to create the extensionmethods | ||
var domains = GetDomainsFromEntities(entities); | ||
|
||
foreach (var domain in domains) | ||
{ | ||
if (_FluentApiMapper.ContainsKey(domain)) | ||
{ | ||
var camelCaseDomain = domain.ToCamelCase(); | ||
var method = $@"public static {camelCaseDomain}Entities {camelCaseDomain}Ex(this NetDaemonApp app) => new {camelCaseDomain}Entities(app);"; | ||
var methodDeclaration = CSharpSyntaxTree.ParseText(method).GetRoot().ChildNodes().OfType<MethodDeclarationSyntax>().FirstOrDefault(); | ||
extensionClass = extensionClass.AddMembers(methodDeclaration); | ||
} | ||
|
||
} | ||
namespaceDeclaration = namespaceDeclaration.AddMembers(extensionClass); | ||
|
||
// Add the classes implementing the specific entities | ||
foreach (var domain in GetDomainsFromEntities(entities)) | ||
{ | ||
if (_FluentApiMapper.ContainsKey(domain)) | ||
{ | ||
var classDeclaration = $@"public partial class {domain.ToCamelCase()}Entities | ||
{{ | ||
private readonly NetDaemonApp _app; | ||
public {domain.ToCamelCase()}Entities(NetDaemonApp app) | ||
{{ | ||
_app = app; | ||
}} | ||
}}"; | ||
var entityClass = CSharpSyntaxTree.ParseText(classDeclaration).GetRoot().ChildNodes().OfType<ClassDeclarationSyntax>().FirstOrDefault(); | ||
foreach (var entity in entities.Where(n => n.StartsWith(domain))) | ||
{ | ||
var (fluent, fluentInterface) = _FluentApiMapper[domain]; | ||
|
||
var propertyCode = $@"public {fluentInterface} {entity[(entity.IndexOf(".") + 1)..].ToCamelCase()} => _app.{fluent}(""{entity}"");"; | ||
var propDeclaration = CSharpSyntaxTree.ParseText(propertyCode).GetRoot().ChildNodes().OfType<PropertyDeclarationSyntax>().FirstOrDefault(); | ||
entityClass = entityClass.AddMembers(propDeclaration); | ||
|
||
} | ||
namespaceDeclaration = namespaceDeclaration.AddMembers(entityClass); | ||
|
||
} | ||
} | ||
|
||
code = code.AddMembers(namespaceDeclaration); | ||
|
||
return code.NormalizeWhitespace(indentation: " ", eol: "\n").ToFullString(); | ||
} | ||
|
||
/// <summary> | ||
/// Returns a list of domains from all entities | ||
/// </summary> | ||
/// <param name="entities">A list of entities</param> | ||
internal static IEnumerable<string> GetDomainsFromEntities(IEnumerable<string> entities) => | ||
entities.Select(n => n[0..n.IndexOf(".")]).Distinct(); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
"log_level": "trace" | ||
"log_level": "trace", | ||
"generate_entities": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
public class HassioConfig | ||
{ | ||
|
||
[JsonPropertyName("log_level")] | ||
public string? LogLevel { get; set; } | ||
|
||
[JsonPropertyName("generate_entities")] | ||
public bool? GenerateEntitiesOnStart { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
tests/NetDaemon.Daemon.Tests/DaemonRunner/App/CodeGenTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System.Linq; | ||
using JoySoftware.HomeAssistant.NetDaemon.DaemonRunner.Service.App; | ||
using Xunit; | ||
|
||
namespace NetDaemon.Daemon.Tests.DaemonRunner.App | ||
{ | ||
public class CodeGenerationTests | ||
{ | ||
[Fact] | ||
public void TestGenerateCode() | ||
{ | ||
// ARRANGE | ||
var x = new CodeGenerator(); | ||
// ACT | ||
var code = x.GenerateCode("Netdaemon.Generated.Extensions", new string[] { "light.koket_fonster", "media_player.my_player" }); | ||
|
||
// ASSERT | ||
|
||
Assert.Equal(System.IO.File.ReadAllText("DaemonRunner/CodeGen/CodeGenTestFixture.cs"), code); | ||
// System.IO.File.WriteAllText("/root/src/tests/NetDaemon.Daemon.Tests/DaemonRunner/CodeGen/CodeGenTestFixture.cs", code); | ||
} | ||
|
||
[Fact] | ||
public void WhenGivenAnArrayOfEntitiesTheDomainShouldReturnCorrectDomains() | ||
{ | ||
// ARRANGE | ||
var entities = new string[] | ||
{ | ||
"light.the_light", | ||
"light.kitchen", | ||
"media_player.player", | ||
"scene.thescene", | ||
"switch.myswitch", | ||
"switch.myswitch2", | ||
"camera.acamera", | ||
"automation.wowautomation", | ||
"script.myscript" | ||
}; | ||
// ACT | ||
var domainsInCamelCase = CodeGenerator.GetDomainsFromEntities(entities); | ||
// ASSERT | ||
Assert.Equal(7, domainsInCamelCase.Count()); | ||
Assert.Collection(domainsInCamelCase, | ||
n => Assert.Equal("light", n), | ||
n => Assert.Equal("media_player", n), | ||
n => Assert.Equal("scene", n), | ||
n => Assert.Equal("switch", n), | ||
n => Assert.Equal("camera", n), | ||
n => Assert.Equal("automation", n), | ||
n => Assert.Equal("script", n)); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
tests/NetDaemon.Daemon.Tests/DaemonRunner/CodeGen/CodeGenTestFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using JoySoftware.HomeAssistant.NetDaemon.Common; | ||
|
||
namespace Netdaemon.Generated.Extensions | ||
{ | ||
public static partial class EntityExtension | ||
{ | ||
public static LightEntities LightEx(this NetDaemonApp app) => new LightEntities(app); | ||
public static MediaPlayerEntities MediaPlayerEx(this NetDaemonApp app) => new MediaPlayerEntities(app); | ||
} | ||
|
||
public partial class LightEntities | ||
{ | ||
private readonly NetDaemonApp _app; | ||
public LightEntities(NetDaemonApp app) | ||
{ | ||
_app = app; | ||
} | ||
|
||
public IEntity KoketFonster => _app.Entity("light.koket_fonster"); | ||
} | ||
|
||
public partial class MediaPlayerEntities | ||
{ | ||
private readonly NetDaemonApp _app; | ||
public MediaPlayerEntities(NetDaemonApp app) | ||
{ | ||
_app = app; | ||
} | ||
|
||
public IMediaPlayer MyPlayer => _app.MediaPlayer("media_player.my_player"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters