Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/nox solution #67

Merged
merged 11 commits into from
Aug 5, 2023
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
58 changes: 58 additions & 0 deletions .nox/design/SampleCurrency.solution.nox.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#
# Cli Sample Solution definition
#
# yaml-language-server: $schema=https://raw.githubusercontent.com/NoxOrg/Nox/main/src/Nox.Core/Schemas/NoxConfiguration.json
#

#

name: CliDemo

description: A Cli Demo Nox Configuration
environments:

- name: dev
description: Used for development and testing

- name: test
description: Test environment

- name: uat
description: For them end users to check it works

- name: prod
description: Production environment used for, well - the real thing!
isProduction: true

versionControl:
provider: azureDevops
host: https://dev.azure.com/iwgplc
folders:
sourceCode: /src
containers: /docker

team:

- name: Andre Sharpe
userName: andre.sharpe@iwgplc.com
roles: [architect, owner, administrator, developer, manager]

- name: Jan Schutte
userName: jan.schutte@iwgplc.com
roles: [architect, administrator, developer, devOpsEngineer]

- name: Anton Du Plessis
userName: anton.duplessis@iwgplc.com
roles: [projectManager]

- name: Morne Van Zyl
userName: morne.vanzyl@iwgplc.com
roles: [technicalWriter]

- name: Dmytro Dorodnykh
userName: dmytro.dorodnykh@iwgplc.com
roles: [developer]

- name: Oleksandr Vlasenko
userName: oleksandr.vlasenko@regus.com
roles: [architect, developer]
76 changes: 0 additions & 76 deletions samples/Design/SampleCurrency.service.nox.yaml

This file was deleted.

4 changes: 2 additions & 2 deletions src/CoreTests/CoreTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.6.1" />
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
Expand Down
6 changes: 6 additions & 0 deletions src/Nox.Cli.Abstractions/Constants/FileExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Nox.Cli.Abstractions.Constants;

public static class FileExtension
{
public const string WorkflowDefinition = @"*.workflow.nox.yaml";
}
89 changes: 89 additions & 0 deletions src/Nox.Cli.Abstractions/Helpers/YamlHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System.Text.RegularExpressions;
using Nox.Cli.Abstractions.Exceptions;

namespace Nox.Cli.Abstractions.Helpers;

public static class YamlHelper
{
private static readonly Regex _referenceRegex = new(@"\$ref\S*:\s*(?<variable>[\w:\.\/\\]+\b[\w\-\.\/]+)\s*", RegexOptions.Compiled | RegexOptions.IgnoreCase, TimeSpan.FromSeconds(5));

/// <summary>
/// Resolve $ref &lt;path&gt; tags in a yaml source yaml file. <br/>
/// Loads the source yaml file, resolves the $ref's and replaces them with the yaml from the files specified in &lt;path&gt;<br/>
/// Note: This call is recursive, all $refs in the yaml hierarchy will be resolved.
/// Note: child nodes are added at the same indentation as the $ref tag.
/// </summary>
/// <param name="path">Full or relative path to the source yaml file.</param>
/// <returns></returns>
public static string ResolveYamlReferences(string path)
{
var sourceFullPath = Path.GetFullPath(path);
if (!File.Exists(sourceFullPath)) throw new NoxCliException($"Yaml file {path} does not exist!");
var sourcePath = Path.GetDirectoryName(sourceFullPath);

var sourceLines = File.ReadAllLines(path);
var outputLines = ResolveYamlReferences(sourceLines.ToList(), sourcePath!).Result;

return string.Join('\n', outputLines.ToArray());
}

/// <summary>
/// Resolve $ref &lt;path&gt; tags in a yaml source yaml file. <br/>
/// Loads the source yaml file, resolves the $ref's and replaces them with the yaml from the files specified in &lt;path&gt;<br/>
/// Note: This call is recursive, all $refs in the yaml hierarchy will be resolved.
/// Note: child nodes are added at the same indentation as the $ref tag.
/// </summary>
/// <param name="path">Full or relative path to the source yaml file.</param>
/// <returns></returns>
public static async Task<string> ResolveYamlReferencesAsync(string path)
{
var sourceFullPath = Path.GetFullPath(path);
if (!File.Exists(sourceFullPath)) throw new NoxCliException($"Yaml file {path} does not exist!");
var sourcePath = Path.GetDirectoryName(sourceFullPath);

var sourceLines = await File.ReadAllLinesAsync(path);
var outputLines = await ResolveYamlReferences(sourceLines.ToList(), sourcePath!);

return string.Join('\n', outputLines.ToArray());
}

private static async Task<List<string>> ResolveYamlReferences(List<string> sourceLines, string path)
{
var outputLines = new List<string>();
foreach (var sourceLine in sourceLines)
{
if (!sourceLine.TrimStart().StartsWith('#'))
{
var match = _referenceRegex.Match(sourceLine);
if (match.Success)
{
var padding = new string(' ', match.Index);
var childPath = match.Groups[1].Value;
if (!Path.IsPathRooted(childPath)) childPath = Path.Combine(path!, childPath);
if (!File.Exists(childPath)) throw new NoxCliException($"Referenced yaml file does not exist for reference: {match.Groups[1].Value}");
var childLines = await File.ReadAllLinesAsync(childPath);
foreach (var childLine in childLines)
{
outputLines.Add(padding + childLine);
}
}
else
{
outputLines.Add(sourceLine);
}
}
else
{
outputLines.Add(sourceLine);
}
}

if (outputLines.Any(ol => ol.Contains("$ref:") && !ol.TrimStart().StartsWith('#')))
{
outputLines = await ResolveYamlReferences(outputLines, path);
}

return outputLines;
}

}
6 changes: 4 additions & 2 deletions src/Nox.Cli.Abstractions/INoxWorkflowContext.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Nox.Cli.Abstractions.Caching;
using Nox.Core.Interfaces;
using Nox.Secrets.Abstractions;
using Nox.Solution;

namespace Nox.Cli.Abstractions
{
Expand All @@ -15,6 +16,7 @@ public interface INoxWorkflowContext
void SetState(ActionState state);

INoxCliCacheManager? CacheManager { get; }
void SetProjectConfiguration(IProjectConfiguration projectConfiguration);
INoxSecretsResolver? NoxSecretsResolver { get; }
void SetProjectConfiguration(NoxSolution projectConfiguration);
}
}
3 changes: 2 additions & 1 deletion src/Nox.Cli.Abstractions/Nox.Cli.Abstractions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageReference Include="Nox.Core" Version="6.0.48" />
<PackageReference Include="Nox.Secrets" Version="7.0.14" />
<PackageReference Include="Nox.Solution" Version="7.0.15" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.30.1" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.32.1" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.6.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
6 changes: 3 additions & 3 deletions src/Nox.Cli.Authentication/Nox.Cli.Authentication.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
<ItemGroup>
<PackageReference Include="Azure.Extensions.AspNetCore.DataProtection.Keys" Version="1.2.2" />
<PackageReference Include="Azure.Identity" Version="1.9.0" />
<PackageReference Include="Microsoft.AspNetCore.DataProtection.Abstractions" Version="7.0.5" />
<PackageReference Include="Microsoft.AspNetCore.DataProtection.Abstractions" Version="7.0.9" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Identity.Client" Version="4.54.1" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.30.1" />
<PackageReference Include="Microsoft.Identity.Client" Version="4.55.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.32.1" />
</ItemGroup>

<ItemGroup>
Expand Down
5 changes: 3 additions & 2 deletions src/Nox.Cli.Caching/Nox.Cli.Caching.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Nox.Core" Version="6.0.48" />
<PackageReference Include="Nox.Utilities" Version="1.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Nox.Utilities" Version="1.0.7" />
<PackageReference Include="RestSharp" Version="110.2.0" />
<PackageReference Include="Spectre.Console" Version="0.47.0" />
<PackageReference Include="YamlDotNet" Version="13.1.1" />
</ItemGroup>

</Project>
18 changes: 11 additions & 7 deletions src/Nox.Cli.Caching/NoxCliCacheManager.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
using System.Net;
using System.Net.NetworkInformation;
using System.Text.Json;
using Newtonsoft.Json;
using Nox.Cli.Abstractions;
using Nox.Cli.Abstractions.Caching;
using Nox.Cli.Abstractions.Configuration;
using Nox.Cli.Abstractions.Constants;
using Nox.Cli.Abstractions.Exceptions;
using Nox.Cli.Abstractions.Helpers;
using Nox.Cli.Configuration;
using Nox.Core.Constants;
using Nox.Core.Exceptions;
using Nox.Core.Helpers;
using Nox.Utilities.Configuration;
using Nox.Utilities.Credentials;
using RestSharp;
using Spectre.Console;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
using IDeserializer = YamlDotNet.Serialization.IDeserializer;
using JsonSerializer = System.Text.Json.JsonSerializer;

namespace Nox.Cli.Caching;
Expand Down Expand Up @@ -77,6 +78,7 @@ public bool IsOnline {
try
{
var uri = new Uri(_remoteUrl);
if (uri.Host == "localhost") return true;
var reply = ping.Send(uri.Host, 3000);
if (reply.Status == IPStatus.Success)
{
Expand Down Expand Up @@ -279,7 +281,7 @@ private void GetOnlineWorkflowsAndManifest(IDictionary<string, string> yamlFiles
Directory.CreateDirectory(_workflowCachePath);

var existingCacheList = Directory
.GetFiles(_workflowCachePath, FileExtension.WorflowDefinition)
.GetFiles(_workflowCachePath, FileExtension.WorkflowDefinition)
.Select(f => (new FileInfo(f)).Name).ToHashSet();

foreach (var file in onlineFiles!)
Expand Down Expand Up @@ -394,22 +396,22 @@ private void ResolveManifest(IDeserializer deserializer, Dictionary<string, stri
private void ResolveWorkflows(IDeserializer deserializer, Dictionary<string, string> yamlFiles)
{
_workflows = new List<IWorkflowConfiguration>();
foreach (var yaml in yamlFiles.Where(kv => kv.Key.EndsWith(FileExtension.WorflowDefinition.TrimStart('*'))))
foreach (var yaml in yamlFiles.Where(kv => kv.Key.EndsWith(FileExtension.WorkflowDefinition.TrimStart('*'))))
{
try
{
_workflows.Add(deserializer.Deserialize<WorkflowConfiguration>(yaml.Value));
}
catch (Exception ex)
{
throw new NoxYamlException($"Unable to deserialize workflow {yaml.Key}. {ex.Message}");
throw new NoxCliException($"Unable to deserialize workflow {yaml.Key}. {ex.Message}");
}
}
}

private string[] FindWorkflowsAndManifest(string searchPath = "")
{
var searchPatterns = new string[] { FileExtension.WorflowDefinition, "*.cli.nox.yaml" };
var searchPatterns = new string[] { FileExtension.WorkflowDefinition, "*.cli.nox.yaml" };

var path = string.IsNullOrEmpty(searchPath)
? new DirectoryInfo(Directory.GetCurrentDirectory())
Expand Down Expand Up @@ -480,6 +482,8 @@ private void GetOnlineTemplates()
throw new NoxCliException($"GetOnlineTemplates:-> {onlineFilesJson.ErrorException?.Message}");
}

if (onlineFilesJson.StatusCode != HttpStatusCode.OK) return;

var onlineFiles = JsonSerializer.Deserialize<List<RemoteFileInfo>>(onlineFilesJson.Content, new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
Expand Down
Loading
Loading