Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions global.json

This file was deleted.

20 changes: 14 additions & 6 deletions src/Moryx.Cli.Commands/AddStates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ internal static CommandResult Exec(Template template, string resource, IEnumerab

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


var resourceFile = CSharpFile.FromFile(resourceFileName);
var dictionary = template.StateBaseFile(resource);
var targetPath = Path.Combine(Path.GetDirectoryName(resourceFile)!, "States");
var targetPath = Path.Combine(Path.GetDirectoryName(resourceFileName)!, "States");
var newStateBaseFileName = Path.Combine(
targetPath,
Path.GetFileName(dictionary.FirstOrDefault().Value));
Expand All @@ -38,9 +38,11 @@ private static CommandResult Add(Template template, string resource, IEnumerable
{
var files = template.WriteFilesToDisk(dictionary);

var stateBaseVars = template.ReplaceVariables(template.Configuration.Add.StateBase, "", new Dictionary<string, string>{ { Template.ResourceKey, resource} });
Template.ReplacePlaceHoldersInsideFiles(
files,
template.Configuration.Add.StateBase.Replacements);
stateBaseVars
);

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

Template.ReplacePlaceHoldersInsideFiles(
files,
template.Configuration.Add.State.Replacements);
stateVars);

stateBaseTemplate = stateBaseTemplate.AddState(stateType);

var stateTemplate = StateTemplate.FromFile(filename);
stateTemplate.NamespaceName = resourceFile.NamespaceName + ".States";
stateTemplate.SaveToFile(filename);

msg.Add($"Successfully added {stateType} state");
}
else
Expand All @@ -101,6 +108,7 @@ private static CommandResult Add(Template template, string resource, IEnumerable
}
}

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

UpdateResource(
Expand Down
6 changes: 5 additions & 1 deletion src/Moryx.Cli.Commands/Components/ProjectFileManipulation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ internal class ProjectFileManipulation

public static void AddProjectReference(string targetProjectFileName, string referenceProjectFileName)
{
var targetProjectDirectory = Path.GetDirectoryName(targetProjectFileName);
var referenceProjectDirectory = Path.GetDirectoryName(referenceProjectFileName);
if(targetProjectDirectory == null || referenceProjectDirectory == null)
return;

var referencePath = Path.GetRelativePath(Path.GetDirectoryName(targetProjectFileName), Path.GetDirectoryName(referenceProjectFileName));
var referencePath = Path.GetRelativePath(targetProjectDirectory, referenceProjectDirectory);
referencePath = Path.Combine(referencePath, Path.GetFileName(referenceProjectFileName));

var projectFile = LoadXml(targetProjectFileName);
Expand Down
1 change: 0 additions & 1 deletion src/Moryx.Cli.Commands/ExecCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Net;
using Moryx.Cli.Commands.Options;
using System.Net;
using System.Net.Http.Json;

namespace Moryx.Cli.Commands
Expand Down
25 changes: 17 additions & 8 deletions src/Moryx.Cli.Templates/Components/CSharpFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class CSharpFile : CSharpFileBase
{
private SyntaxNode _root;

private NamespaceDeclarationSyntax? _namespaceDeclaration;
private BaseNamespaceDeclarationSyntax? _namespaceDeclaration;

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

Expand All @@ -23,21 +23,25 @@ public string NamespaceName
}


public CSharpFile(string content) : base(content)
public CSharpFile(string content) : base(content)
{
_root = _syntaxTree.GetRoot();
Types = ScanTypes();
_namespaceDeclaration = ScanNamespace();
}

public static CSharpFile FromFile(string fileName)
{
return new CSharpFile(ReadContent(fileName));
}

protected static string ReadContent(string fileName)
{
if (!File.Exists(fileName))
{
throw new FileNotFoundException(fileName);
}
var content = File.ReadAllText(fileName);
return new CSharpFile(content);
return File.ReadAllText(fileName);
}

public List<string> ScanTypes()
Expand All @@ -49,18 +53,23 @@ public List<string> ScanTypes()
.ToList();
}

private NamespaceDeclarationSyntax? ScanNamespace()
private BaseNamespaceDeclarationSyntax? ScanNamespace()
{
var root = _syntaxTree.GetRoot() as CompilationUnitSyntax;

return root?.DescendantNodes().OfType<NamespaceDeclarationSyntax>().FirstOrDefault();
var nds = root?.DescendantNodes().OfType<NamespaceDeclarationSyntax>().FirstOrDefault();
if(nds != null)
{
return nds;
}
return root?.DescendantNodes().OfType<FileScopedNamespaceDeclarationSyntax>().FirstOrDefault();
}

private void UpdateNamespace(string @namespace)
{
if (_namespaceDeclaration != null)
{
var newNamespace = SyntaxFactory.NamespaceDeclaration(SyntaxFactory.ParseName(@namespace)).NormalizeWhitespace();
var newName = SyntaxFactory.ParseName(@namespace);
var newNamespace = _namespaceDeclaration.WithName(newName);
_root = _root.ReplaceNode(_namespaceDeclaration, newNamespace);
_content = _root.ToFullString();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Moryx.Cli.Templates.StateBaseTemplate

namespace Moryx.Cli.Templates.StateBaseTemplate
{
public partial class StateBaseTemplate
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

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

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

public static StateBaseTemplate FromFile(string fileName)
public new static StateBaseTemplate FromFile(string fileName)
{
var content = File.ReadAllText(fileName);
return new StateBaseTemplate(content);
return new StateBaseTemplate(ReadContent(fileName));
}

private void Parse(SyntaxTree syntaxTree)
Expand Down
23 changes: 11 additions & 12 deletions src/Moryx.Cli.Templates/StateTemplate/StateTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,17 @@

namespace Moryx.Cli.Templates.StateTemplate
{
public class StateTemplate : CSharpFileBase
public class StateTemplate : CSharpFile
{
private const string StateContextInterface = "IStateContext";

public StateTemplate(string content) : base(content)
{
}

public static StateTemplate FromFile(string fileName)
public static new StateTemplate FromFile(string fileName)
{
if (!File.Exists(fileName))
{
throw new FileNotFoundException(fileName);
}
var content = File.ReadAllText(fileName);
return new StateTemplate(content);
return new StateTemplate(ReadContent(fileName));
}

public StateTemplate ImplementIStateContext(string resource)
Expand All @@ -43,7 +38,7 @@ public StateTemplate ImplementIStateContext(string resource)
return new StateTemplate(root.ToFullString());
}



private SyntaxNode TryToAddInitializing(SyntaxNode root, string context)
{
Expand Down Expand Up @@ -91,8 +86,12 @@ private SyntaxNode AddStateProperty(SyntaxNode root, string context)
var stateProperty = SyntaxFactory
.PropertyDeclaration(SyntaxFactory.ParseTypeName(context.StateBase()), "State")
.AddModifiers(SyntaxFactory.Token(SyntaxKind.InternalKeyword))
.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken))
.WithExpressionBody(SyntaxFactory.ArrowExpressionClause(SyntaxFactory.ParseExpression($"({context.StateBase()})CurrentState")))
.AddAccessorListAccessors(
SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)),
SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration)
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PrivateKeyword))
.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)))
.NormalizeWhitespace()
;

Expand All @@ -103,7 +102,7 @@ private SyntaxNode AddStateProperty(SyntaxNode root, string context)
private static SyntaxNode UpdateClassDefinition(SyntaxNode root, string context)
{
var contextClass = root.FindClass(context);
if(contextClass == null)
if (contextClass == null)
return root;

if (!contextClass.BaseList?.Types.Any(t => ((IdentifierNameSyntax)t.Type).Identifier.ValueText == StateContextInterface) ?? false)
Expand Down
2 changes: 1 addition & 1 deletion src/Moryx.Cli.Templates/Template.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace Moryx.Cli.Templates
public class Template
{
private const string IdentifierKey = "{id}";
private const string ResourceKey = "{resource}";
public const string ResourceKey = "{resource}";
private const string SolutionNameKey = "{solutionname}";
private const string TemplateFileExtension = ".moryxtpl";
private readonly List<string> _fileNames;
Expand Down