Skip to content

Commit 22330f4

Browse files
authored
Merge pull request #20 from PHOENIXCONTACT/fix/states-creation
Create `States` more properly
2 parents c8f337c + 9ff9b1e commit 22330f4

File tree

9 files changed

+53
-40
lines changed

9 files changed

+53
-40
lines changed

global.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/Moryx.Cli.Commands/AddStates.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ internal static CommandResult Exec(Template template, string resource, IEnumerab
1919

2020
private static CommandResult Add(Template template, string resource, IEnumerable<string> states)
2121
{
22-
var resourceFile = FindResource(template.Settings, resource);
23-
if (string.IsNullOrEmpty(resourceFile))
22+
var resourceFileName = FindResource(template.Settings, resource);
23+
if (string.IsNullOrEmpty(resourceFileName))
2424
{
2525
return CommandResult.WithError($"`{resource}` not found. Make sure that a type `{resource}` exists in the project.");
2626
}
2727

28-
28+
var resourceFile = CSharpFile.FromFile(resourceFileName);
2929
var dictionary = template.StateBaseFile(resource);
30-
var targetPath = Path.Combine(Path.GetDirectoryName(resourceFile)!, "States");
30+
var targetPath = Path.Combine(Path.GetDirectoryName(resourceFileName)!, "States");
3131
var newStateBaseFileName = Path.Combine(
3232
targetPath,
3333
Path.GetFileName(dictionary.FirstOrDefault().Value));
@@ -38,9 +38,11 @@ private static CommandResult Add(Template template, string resource, IEnumerable
3838
{
3939
var files = template.WriteFilesToDisk(dictionary);
4040

41+
var stateBaseVars = template.ReplaceVariables(template.Configuration.Add.StateBase, "", new Dictionary<string, string>{ { Template.ResourceKey, resource} });
4142
Template.ReplacePlaceHoldersInsideFiles(
4243
files,
43-
template.Configuration.Add.StateBase.Replacements);
44+
stateBaseVars
45+
);
4446

4547
if (!states.Any())
4648
{
@@ -81,13 +83,18 @@ private static CommandResult Add(Template template, string resource, IEnumerable
8183
if (!File.Exists(filename))
8284
{
8385
var files = template.WriteFilesToDisk(stateFiles);
86+
var stateVars = template.ReplaceVariables(template.Configuration.Add.State, state, new Dictionary<string, string> { { Template.ResourceKey, resource } });
8487

8588
Template.ReplacePlaceHoldersInsideFiles(
8689
files,
87-
template.Configuration.Add.State.Replacements);
90+
stateVars);
8891

8992
stateBaseTemplate = stateBaseTemplate.AddState(stateType);
9093

94+
var stateTemplate = StateTemplate.FromFile(filename);
95+
stateTemplate.NamespaceName = resourceFile.NamespaceName + ".States";
96+
stateTemplate.SaveToFile(filename);
97+
9198
msg.Add($"Successfully added {stateType} state");
9299
}
93100
else
@@ -101,6 +108,7 @@ private static CommandResult Add(Template template, string resource, IEnumerable
101108
}
102109
}
103110

111+
stateBaseTemplate.NamespaceName = resourceFile.NamespaceName + ".States";
104112
stateBaseTemplate.SaveToFile(newStateBaseFileName);
105113

106114
UpdateResource(

src/Moryx.Cli.Commands/Components/ProjectFileManipulation.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ internal class ProjectFileManipulation
1010

1111
public static void AddProjectReference(string targetProjectFileName, string referenceProjectFileName)
1212
{
13+
var targetProjectDirectory = Path.GetDirectoryName(targetProjectFileName);
14+
var referenceProjectDirectory = Path.GetDirectoryName(referenceProjectFileName);
15+
if(targetProjectDirectory == null || referenceProjectDirectory == null)
16+
return;
1317

14-
var referencePath = Path.GetRelativePath(Path.GetDirectoryName(targetProjectFileName), Path.GetDirectoryName(referenceProjectFileName));
18+
var referencePath = Path.GetRelativePath(targetProjectDirectory, referenceProjectDirectory);
1519
referencePath = Path.Combine(referencePath, Path.GetFileName(referenceProjectFileName));
1620

1721
var projectFile = LoadXml(targetProjectFileName);

src/Moryx.Cli.Commands/ExecCommand.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Net;
22
using Moryx.Cli.Commands.Options;
3-
using System.Net;
43
using System.Net.Http.Json;
54

65
namespace Moryx.Cli.Commands

src/Moryx.Cli.Templates/Components/CSharpFile.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class CSharpFile : CSharpFileBase
88
{
99
private SyntaxNode _root;
1010

11-
private NamespaceDeclarationSyntax? _namespaceDeclaration;
11+
private BaseNamespaceDeclarationSyntax? _namespaceDeclaration;
1212

1313
public List<string> Types { get; private set; }
1414

@@ -23,21 +23,25 @@ public string NamespaceName
2323
}
2424

2525

26-
public CSharpFile(string content) : base(content)
26+
public CSharpFile(string content) : base(content)
2727
{
2828
_root = _syntaxTree.GetRoot();
2929
Types = ScanTypes();
3030
_namespaceDeclaration = ScanNamespace();
3131
}
3232

3333
public static CSharpFile FromFile(string fileName)
34+
{
35+
return new CSharpFile(ReadContent(fileName));
36+
}
37+
38+
protected static string ReadContent(string fileName)
3439
{
3540
if (!File.Exists(fileName))
3641
{
3742
throw new FileNotFoundException(fileName);
3843
}
39-
var content = File.ReadAllText(fileName);
40-
return new CSharpFile(content);
44+
return File.ReadAllText(fileName);
4145
}
4246

4347
public List<string> ScanTypes()
@@ -49,18 +53,23 @@ public List<string> ScanTypes()
4953
.ToList();
5054
}
5155

52-
private NamespaceDeclarationSyntax? ScanNamespace()
56+
private BaseNamespaceDeclarationSyntax? ScanNamespace()
5357
{
5458
var root = _syntaxTree.GetRoot() as CompilationUnitSyntax;
55-
56-
return root?.DescendantNodes().OfType<NamespaceDeclarationSyntax>().FirstOrDefault();
59+
var nds = root?.DescendantNodes().OfType<NamespaceDeclarationSyntax>().FirstOrDefault();
60+
if(nds != null)
61+
{
62+
return nds;
63+
}
64+
return root?.DescendantNodes().OfType<FileScopedNamespaceDeclarationSyntax>().FirstOrDefault();
5765
}
5866

5967
private void UpdateNamespace(string @namespace)
6068
{
6169
if (_namespaceDeclaration != null)
6270
{
63-
var newNamespace = SyntaxFactory.NamespaceDeclaration(SyntaxFactory.ParseName(@namespace)).NormalizeWhitespace();
71+
var newName = SyntaxFactory.ParseName(@namespace);
72+
var newNamespace = _namespaceDeclaration.WithName(newName);
6473
_root = _root.ReplaceNode(_namespaceDeclaration, newNamespace);
6574
_content = _root.ToFullString();
6675
}

src/Moryx.Cli.Templates/StateBaseTemplate/StateBaseTemplate.Constructor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
namespace Moryx.Cli.Templates.StateBaseTemplate
1+

2+
namespace Moryx.Cli.Templates.StateBaseTemplate
23
{
34
public partial class StateBaseTemplate
45
{

src/Moryx.Cli.Templates/StateBaseTemplate/StateBaseTemplate.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Moryx.Cli.Templates.StateBaseTemplate
99
{
10-
public partial class StateBaseTemplate : CSharpFileBase
10+
public partial class StateBaseTemplate : CSharpFile
1111
{
1212
private const string StateDefinitionAttributeName = "StateDefinition";
1313
private const string IsInitialParameterName = "IsInitial";
@@ -19,10 +19,9 @@ public StateBaseTemplate(string content) : base(content)
1919

2020
public IEnumerable<StateDefinition> StateDeclarations { get; private set; } = [];
2121

22-
public static StateBaseTemplate FromFile(string fileName)
22+
public new static StateBaseTemplate FromFile(string fileName)
2323
{
24-
var content = File.ReadAllText(fileName);
25-
return new StateBaseTemplate(content);
24+
return new StateBaseTemplate(ReadContent(fileName));
2625
}
2726

2827
private void Parse(SyntaxTree syntaxTree)

src/Moryx.Cli.Templates/StateTemplate/StateTemplate.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,17 @@
88

99
namespace Moryx.Cli.Templates.StateTemplate
1010
{
11-
public class StateTemplate : CSharpFileBase
11+
public class StateTemplate : CSharpFile
1212
{
1313
private const string StateContextInterface = "IStateContext";
1414

1515
public StateTemplate(string content) : base(content)
1616
{
1717
}
1818

19-
public static StateTemplate FromFile(string fileName)
19+
public static new StateTemplate FromFile(string fileName)
2020
{
21-
if (!File.Exists(fileName))
22-
{
23-
throw new FileNotFoundException(fileName);
24-
}
25-
var content = File.ReadAllText(fileName);
26-
return new StateTemplate(content);
21+
return new StateTemplate(ReadContent(fileName));
2722
}
2823

2924
public StateTemplate ImplementIStateContext(string resource)
@@ -43,7 +38,7 @@ public StateTemplate ImplementIStateContext(string resource)
4338
return new StateTemplate(root.ToFullString());
4439
}
4540

46-
41+
4742

4843
private SyntaxNode TryToAddInitializing(SyntaxNode root, string context)
4944
{
@@ -91,8 +86,12 @@ private SyntaxNode AddStateProperty(SyntaxNode root, string context)
9186
var stateProperty = SyntaxFactory
9287
.PropertyDeclaration(SyntaxFactory.ParseTypeName(context.StateBase()), "State")
9388
.AddModifiers(SyntaxFactory.Token(SyntaxKind.InternalKeyword))
94-
.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken))
95-
.WithExpressionBody(SyntaxFactory.ArrowExpressionClause(SyntaxFactory.ParseExpression($"({context.StateBase()})CurrentState")))
89+
.AddAccessorListAccessors(
90+
SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
91+
.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)),
92+
SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration)
93+
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PrivateKeyword))
94+
.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)))
9695
.NormalizeWhitespace()
9796
;
9897

@@ -103,7 +102,7 @@ private SyntaxNode AddStateProperty(SyntaxNode root, string context)
103102
private static SyntaxNode UpdateClassDefinition(SyntaxNode root, string context)
104103
{
105104
var contextClass = root.FindClass(context);
106-
if(contextClass == null)
105+
if (contextClass == null)
107106
return root;
108107

109108
if (!contextClass.BaseList?.Types.Any(t => ((IdentifierNameSyntax)t.Type).Identifier.ValueText == StateContextInterface) ?? false)

src/Moryx.Cli.Templates/Template.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace Moryx.Cli.Templates
3232
public class Template
3333
{
3434
private const string IdentifierKey = "{id}";
35-
private const string ResourceKey = "{resource}";
35+
public const string ResourceKey = "{resource}";
3636
private const string SolutionNameKey = "{solutionname}";
3737
private const string TemplateFileExtension = ".moryxtpl";
3838
private readonly List<string> _fileNames;

0 commit comments

Comments
 (0)