Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
jesperweber committed Dec 22, 2023
2 parents d8a03bb + 93acb8e commit 8234ab2
Show file tree
Hide file tree
Showing 20 changed files with 351 additions and 115 deletions.
1 change: 1 addition & 0 deletions Enterspeed.Cli.sln
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "releaseNotes", "releaseNote
releaseNotes\2.0.0.md = releaseNotes\2.0.0.md
releaseNotes\2.0.1.md = releaseNotes\2.0.1.md
releaseNotes\2.0.2.md = releaseNotes\2.0.2.md
releaseNotes\3.0.0.md = releaseNotes\3.0.0.md
EndProjectSection
EndProject
Global
Expand Down
4 changes: 2 additions & 2 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
variables:
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1
vmPool: 'windows-latest'
majorVersion: 2
majorVersion: 3
minorVersion: 0
patchVersion: 2
patchVersion: 0
version: $[format('{0}.{1}.{2}', variables.majorVersion, variables.minorVersion, variables.patchVersion)]
${{ if eq(variables['Build.SourceBranch'], 'refs/heads/master') }}:
# Versioning: 1.0.0
Expand Down
6 changes: 6 additions & 0 deletions releaseNotes/3.0.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
### Breaking
- Added support for folders.
The folder structure you have in the Enterspeed app is now reflected on your local disk when using the CLI.
Likewise, if you moved a schema to a folder on your local disk and call a `save` or `import` command the folder structure is updated in the app.
With the introduction of folders, the static `partials` folder is removed and the schema type is now part of the file name ([schemaAlias].[schemaType].[format]).
If you are already working with schemas on disk, you should clear the `schemas` folder and run a new `import` command to get the new folder and file structure.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ namespace Enterspeed.Cli.Api.MappingSchema
{
public class UpdateMappingSchemaRequest : IRequest<UpdateMappingSchemaResponse>
{
public string Name { get; set; }
public string MappingSchemaId { get; set; }
public int Version { get; set; }
public string Format { get; set; }
public object Schema { get; set; }
}

public class UpdateMappingSchemaResponse
Expand All @@ -28,10 +26,11 @@ public UpdateMappingSchemaRequestHandler(IEnterspeedClient enterspeedClient)
_enterspeedClient = enterspeedClient;
}

public async Task<UpdateMappingSchemaResponse> Handle(UpdateMappingSchemaRequest updateMappingSchemaRequest, CancellationToken cancellationToken)
public async Task<UpdateMappingSchemaResponse> Handle(UpdateMappingSchemaRequest updateMappingSchemaRequest,
CancellationToken cancellationToken)
{
var request = new RestRequest(
$"tenant/mapping-schemas/{updateMappingSchemaRequest.MappingSchemaId}/version/{updateMappingSchemaRequest.Version}",
$"tenant/mapping-schemas/{updateMappingSchemaRequest.MappingSchemaId}",
Method.Put).AddJsonBody(updateMappingSchemaRequest);

var response = await _enterspeedClient.ExecuteAsync<UpdateMappingSchemaResponse>(request, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Enterspeed.Cli.Services.EnterspeedClient;
using MediatR;
using RestSharp;

namespace Enterspeed.Cli.Api.MappingSchema
{
public class UpdateMappingSchemaVersionRequest : IRequest<UpdateMappingSchemaVersionResponse>
{
public string MappingSchemaId { get; set; }
public int Version { get; set; }
public string Format { get; set; }
public object Schema { get; set; }
}

public class UpdateMappingSchemaVersionResponse
{
public string IdValue { get; set; }
public string MappingSchemaGuid { get; set; }
public int Version { get; set; }
}

public class UpdateMappingSchemaVersionRequestHandler : IRequestHandler<UpdateMappingSchemaVersionRequest, UpdateMappingSchemaVersionResponse>
{
private readonly IEnterspeedClient _enterspeedClient;

public UpdateMappingSchemaVersionRequestHandler(IEnterspeedClient enterspeedClient)
{
_enterspeedClient = enterspeedClient;
}

public async Task<UpdateMappingSchemaVersionResponse> Handle(UpdateMappingSchemaVersionRequest updateMappingSchemaVersionRequest, CancellationToken cancellationToken)
{
var request = new RestRequest(
$"tenant/mapping-schemas/{updateMappingSchemaVersionRequest.MappingSchemaId}/version/{updateMappingSchemaVersionRequest.Version}",
Method.Put).AddJsonBody(updateMappingSchemaVersionRequest);

var response = await _enterspeedClient.ExecuteAsync<UpdateMappingSchemaVersionResponse>(request, cancellationToken);
return response;
}
}
}
2 changes: 1 addition & 1 deletion src/Enterspeed.Cli/Commands/Schema/CloneSchemaCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public async Task<int> InvokeAsync(InvocationContext context)

foreach (var schema in schemaResponses)
{
_schemaFileService.CreateSchema(schema.ViewHandle, schema.Type, schema.Version);
_schemaFileService.CreateSchema(schema.ViewHandle, schema.Type, schema.Version, schema.Name);
}

_outputService.Write("Successfully cloned all schemas");
Expand Down
8 changes: 5 additions & 3 deletions src/Enterspeed.Cli/Commands/Schema/CreateSchemaCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ public async Task<int> InvokeAsync(InvocationContext context)
Format = SchemaConstants.JavascriptFormat;
}

var name = Name ?? Alias;

var createSchemaResponse = await _mediator.Send(new CreateMappingSchemaRequest
{
Name = Name ?? Alias,
Name = name,
ViewHandle = Alias,
Type = schemaType.Value.ToApiString(),
Format = Format
Expand All @@ -78,7 +80,7 @@ public async Task<int> InvokeAsync(InvocationContext context)
MappingSchemaId = createSchemaResponse.MappingSchemaGuid
});

_schemaFileService.CreateSchema(Alias, schemaType.Value, schemaResponse.Version);
_schemaFileService.CreateSchema(Alias, schemaType.Value, schemaResponse.Version, name);
}
else
{
Expand All @@ -101,7 +103,7 @@ private async Task UpdateSchemaToPartialTemplate(string mappingSchemaGuid)
{
var schema = _schemaFileService.GetSchema(Alias);

var updateSchemaResponse = await _mediator.Send(new UpdateMappingSchemaRequest
var updateSchemaResponse = await _mediator.Send(new UpdateMappingSchemaVersionRequest
{
Format = schema.Format,
MappingSchemaId = mappingSchemaGuid,
Expand Down
44 changes: 35 additions & 9 deletions src/Enterspeed.Cli/Commands/Schema/ImportSchemaCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
using Enterspeed.Cli.Services.ConsoleOutput;
using Enterspeed.Cli.Services.FileService;
using Enterspeed.Cli.Services.FileService.Models;
using Enterspeed.Cli.Services.SchemaService;
using MediatR;
using Microsoft.Extensions.Logging;

namespace Enterspeed.Cli.Commands.Schema;

internal class ImportSchemaCommand : Command
{
public ImportSchemaCommand() : base(name: "import", "Imports all schemas from the /schemas folder on the disk. Will create new schemas and update existing schemas if --override is enabled.")
public ImportSchemaCommand() : base(name: "import",
"Imports all schemas from the /schemas folder on the disk. Will create new schemas and update existing schemas if --override is enabled.")
{
AddOption(new Option<string>(new[] { "--schema-alias", "-a" }, "Provide a schema alias to only import a single schema"));
AddOption(new Option<bool>(new[] { "--override", "-o" }, "Override existing schemas"));
Expand All @@ -23,17 +25,20 @@ internal class ImportSchemaCommand : Command
private readonly IMediator _mediator;
private readonly IOutputService _outputService;
private readonly ISchemaFileService _schemaFileService;
private readonly ISchemaNameService _schemaNameService;
private readonly ILogger<CreateSchemaCommand> _logger;

public Handler(IMediator mediator,
IOutputService outputService,
ISchemaFileService schemaFileService,
ILogger<CreateSchemaCommand> logger)
ILogger<CreateSchemaCommand> logger,
ISchemaNameService schemaNameService)
{
_mediator = mediator;
_outputService = outputService;
_schemaFileService = schemaFileService;
_logger = logger;
_schemaNameService = schemaNameService;
}

public bool Override { get; set; }
Expand Down Expand Up @@ -81,12 +86,12 @@ public async Task<int> InvokeAsync(InvocationContext context)
return 0;
}

private async Task<bool> UpdateExistingSchema(SchemaFile schemaFile, QueryMappingSchemaResponse queryMappingSchemaResponse)
private async Task<bool> UpdateExistingSchema(SchemaFile schemaFile, QueryMappingSchemaResponse matchingSchema)
{
var existingSchema = await _mediator.Send(
new GetMappingSchemaRequest
{
MappingSchemaId = queryMappingSchemaResponse.Id.MappingSchemaGuid
MappingSchemaId = matchingSchema.Id.MappingSchemaGuid
}
);

Expand All @@ -96,27 +101,48 @@ private async Task<bool> UpdateExistingSchema(SchemaFile schemaFile, QueryMappin
return false;
}

var updateSchemaResponse = await _mediator.Send(new UpdateMappingSchemaRequest
var relativeDirectoryPathInEnterspeed = Path.GetDirectoryName(existingSchema.Name.TrimEnd('/'));
var relativeDirectoryPathOnDisk = schemaFile.RelativeSchemaDirectory;

if (!relativeDirectoryPathOnDisk.Equals(relativeDirectoryPathInEnterspeed))
{
await UpdateSchemaName(existingSchema, relativeDirectoryPathOnDisk);
}

var updateMappingSchemaVersionResponse = await _mediator.Send(new UpdateMappingSchemaVersionRequest
{
Format = schemaFile.Format,
MappingSchemaId = queryMappingSchemaResponse.Id.MappingSchemaGuid,
MappingSchemaId = matchingSchema.Id.MappingSchemaGuid,
Version = existingSchema.LatestVersion,
Schema = schemaFile.GetSchemaContent()
});

_outputService.Write($"Successfully updated schema: {schemaFile.Alias} Version: {updateSchemaResponse.Version}");
_outputService.Write($"Successfully updated schema: {schemaFile.Alias} Version: {updateMappingSchemaVersionResponse.Version}");

return true;
}

private async Task UpdateSchemaName(GetMappingSchemaResponse existingSchema, string relativeDirectoryPathOnDisk)
{
var name = _schemaNameService.BuildNewSchemaName(existingSchema.Name, relativeDirectoryPathOnDisk);
var updateSchemaResponse = await _mediator.Send(new UpdateMappingSchemaRequest()
{
Name = name,
MappingSchemaId = existingSchema.Version.Id.MappingSchemaGuid,
});

_outputService.Write($"Successfully updated name to: {name}");
}

/// <summary>
/// As the management API does not support upserts we first creates the empty schema and then updates the schema with content
/// </summary>
private async Task<bool> CreateNewSchema(SchemaFile schemaFile)
{
var name = _schemaNameService.GetSchemaName(schemaFile);
var createSchemaResponse = await _mediator.Send(new CreateMappingSchemaRequest
{
Name = schemaFile.Alias,
Name = name,
ViewHandle = schemaFile.Alias,
Type = schemaFile.SchemaType.ToApiString(),
Format = schemaFile.Format
Expand All @@ -130,7 +156,7 @@ private async Task<bool> CreateNewSchema(SchemaFile schemaFile)

_outputService.Write("Successfully created new schema: " + schemaFile.Alias);

var updateSchemaResponse = await _mediator.Send(new UpdateMappingSchemaRequest
var updateSchemaResponse = await _mediator.Send(new UpdateMappingSchemaVersionRequest
{
Format = schemaFile.Format,
MappingSchemaId = createSchemaResponse.MappingSchemaGuid,
Expand Down
30 changes: 27 additions & 3 deletions src/Enterspeed.Cli/Commands/Schema/SaveSchemaCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Enterspeed.Cli.Extensions;
using Enterspeed.Cli.Services.ConsoleOutput;
using Enterspeed.Cli.Services.FileService;
using Enterspeed.Cli.Services.SchemaService;
using MediatR;
using Microsoft.Extensions.Logging;

Expand All @@ -23,18 +24,21 @@ public SaveSchemaCommand() : base(name: "save", "Saves schema")
private readonly IMediator _mediator;
private readonly IOutputService _outputService;
private readonly ISchemaFileService _schemaFileService;
private readonly ISchemaNameService _schemaNameService;
private readonly ILogger<SaveSchemaCommand> _logger;

public Handler(
IMediator mediator,
IOutputService outputService,
ISchemaFileService schemaFileService,
ILogger<SaveSchemaCommand> logger)
ILogger<SaveSchemaCommand> logger,
ISchemaNameService schemaNameService)
{
_mediator = mediator;
_outputService = outputService;
_schemaFileService = schemaFileService;
_logger = logger;
_schemaNameService = schemaNameService;
}

public string Alias { get; set; }
Expand Down Expand Up @@ -76,7 +80,15 @@ public async Task<int> InvokeAsync(InvocationContext context)
return 1;
}

var updateMappingSchemaRequest = new UpdateMappingSchemaRequest
var relativeDirectoryPathInEnterspeed = Path.GetDirectoryName(existingSchema.Name.TrimEnd('/'));
var relativeDirectoryPathOnDisk = schema.RelativeSchemaDirectory;

if (!relativeDirectoryPathOnDisk.Equals(relativeDirectoryPathInEnterspeed))
{
await UpdateSchemaName(existingSchema, relativeDirectoryPathOnDisk);
}

var updateMappingSchemaVersionRequest = new UpdateMappingSchemaVersionRequest
{
Format = existingSchema.Version.Format,
MappingSchemaId = existingSchema.Version.Id.MappingSchemaGuid,
Expand All @@ -85,7 +97,7 @@ public async Task<int> InvokeAsync(InvocationContext context)
};

// Create update schema request
var updateSchemaResponse = await _mediator.Send(updateMappingSchemaRequest);
var updateMappingSchemaVersionResponse = await _mediator.Send(updateMappingSchemaVersionRequest);

var updatedSchema = await _mediator.Send(
new GetMappingSchemaRequest
Expand All @@ -99,6 +111,18 @@ public async Task<int> InvokeAsync(InvocationContext context)

return 0;
}

private async Task UpdateSchemaName(GetMappingSchemaResponse existingSchema, string relativeDirectoryPathOnDisk)
{
var name = _schemaNameService.BuildNewSchemaName(existingSchema.Name, relativeDirectoryPathOnDisk);
var updateSchemaResponse = await _mediator.Send(new UpdateMappingSchemaRequest()
{
Name = name,
MappingSchemaId = existingSchema.Version.Id.MappingSchemaGuid,
});

_outputService.Write($"Successfully updated name to: {name}");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ public class UnhandledExceptionBehaviour<TRequest, TResponse> : IPipelineBehavio

public UnhandledExceptionBehaviour(ILogger<TRequest> logger) => _logger = logger;

public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
public Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
try
{
return await next();
return next();
}
catch (Exception ex)
{
Expand Down
23 changes: 11 additions & 12 deletions src/Enterspeed.Cli/Enterspeed.Cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<Company>Enterspeed</Company>
Expand All @@ -22,18 +22,17 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="MediatR" Version="10.0.1" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="10.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
<PackageReference Include="RestSharp" Version="108.0.1" />
<PackageReference Include="Serilog" Version="2.11.0" />
<PackageReference Include="MediatR" Version="12.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageReference Include="RestSharp" Version="110.2.0" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.Exceptions" Version="8.4.0" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="5.0.1" />
<PackageReference Include="Serilog.Formatting.Compact" Version="1.1.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.3.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Serilog.Formatting.Compact" Version="2.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="System.CommandLine.Hosting" Version="0.4.0-alpha.22272.1" />
Expand Down
Loading

0 comments on commit 8234ab2

Please sign in to comment.