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

OSOE-795: Upgrade to latest OC preview to test System.Text.Json #56

Merged
merged 17 commits into from
May 15, 2024
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
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using Lombiq.Hosting.MediaTheme.Bridge.Constants;
using Lombiq.Hosting.MediaTheme.Bridge.Models;
using Lombiq.Hosting.MediaTheme.Bridge.Services;
using Newtonsoft.Json.Linq;
using OrchardCore.Deployment;
using System.Collections.Generic;
using System.Text.Json.Nodes;
using System.Threading.Tasks;

namespace Lombiq.Hosting.MediaTheme.Bridge.Deployment;
Expand All @@ -23,10 +24,11 @@ public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlan

var mediaThemeState = await _mediaThemeStateStore.GetMediaThemeStateAsync();

result.Steps.Add(new JObject(
new JProperty("name", RecipeStepIds.MediaTheme),
new JProperty(nameof(MediaThemeDeploymentStep.ClearMediaThemeFolder), mediaThemeStep.ClearMediaThemeFolder),
new JProperty(nameof(MediaThemeStateDocument.BaseThemeId), mediaThemeState.BaseThemeId)
));
result.Steps.Add(new JsonObject(new Dictionary<string, JsonNode>
{
["name"] = RecipeStepIds.MediaTheme,
[nameof(MediaThemeDeploymentStep.ClearMediaThemeFolder)] = mediaThemeStep.ClearMediaThemeFolder,
[nameof(MediaThemeStateDocument.BaseThemeId)] = mediaThemeState.BaseThemeId,
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="OrchardCore.Deployment.Abstractions" Version="1.8.2" />
<PackageReference Include="OrchardCore.DisplayManagement.Abstractions" Version="1.8.2" />
<PackageReference Include="OrchardCore.Recipes.Abstractions" Version="1.8.2" />
<PackageReference Include="OrchardCore.ResourceManagement.Abstractions" Version="1.8.2" />
<PackageReference Include="OrchardCore.Media" Version="1.8.2" />
<PackageReference Include="OrchardCore.Module.Targets" Version="1.8.2" />
<PackageReference Include="OrchardCore.Deployment.Abstractions" Version="2.0.0-preview-18200" />
<PackageReference Include="OrchardCore.DisplayManagement.Abstractions" Version="2.0.0-preview-18200" />
<PackageReference Include="OrchardCore.Recipes.Abstractions" Version="2.0.0-preview-18200" />
<PackageReference Include="OrchardCore.ResourceManagement.Abstractions" Version="2.0.0-preview-18200" />
<PackageReference Include="OrchardCore.Media" Version="2.0.0-preview-18200" />
<PackageReference Include="OrchardCore.Module.Targets" Version="2.0.0-preview-18200" />
<PackageReference Include="Scrutor" Version="4.2.2" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Lombiq.Hosting.MediaTheme.Bridge.Constants;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Microsoft.Net.Http.Headers;
using OrchardCore.Media;
using OrchardCore.Routing;
using System;
Expand Down Expand Up @@ -37,8 +38,8 @@ public async Task InvokeAsync(HttpContext context)
return;
}

context.Response.StatusCode = 404;
context.Response.Headers.Append("Content-Length", "0");
context.Response.StatusCode = StatusCodes.Status404NotFound;
context.Response.Headers.Append(HeaderNames.ContentLength, "0");
await context.Response.Body.FlushAsync(context.RequestAborted);
context.Abort();
}
Expand Down
3 changes: 2 additions & 1 deletion Lombiq.Hosting.MediaTheme.Bridge/Services/MediaThemeStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using OrchardCore.Recipes.Models;
using OrchardCore.Recipes.Services;
using System;
using System.Text.Json;
using System.Threading.Tasks;

namespace Lombiq.Hosting.MediaTheme.Bridge.Services;
Expand All @@ -28,7 +29,7 @@ public async Task ExecuteAsync(RecipeExecutionContext context)
return;
}

var model = context.Step.ToObject<MediaThemeStepModel>();
var model = context.Step.Deserialize<MediaThemeStepModel>();

await _mediaThemeManager.UpdateBaseThemeAsync(model.BaseThemeId);

Expand Down
4 changes: 2 additions & 2 deletions Lombiq.Hosting.MediaTheme.Bridge/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ namespace Lombiq.Hosting.MediaTheme.Bridge;

public class Startup : StartupBase
{
// Make sure the middlewares run first so we can block Media Theme template requests in time.
public override int Order => -100;
// Make sure the middlewares run first, so we can block Media Theme template requests in time.
public override int ConfigureOrder => int.MinValue;

public override void ConfigureServices(IServiceCollection services)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

<ItemGroup>
Expand Down
76 changes: 38 additions & 38 deletions Lombiq.Hosting.MediaTheme.Deployer/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using CommandLine;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Globalization;
using System.IO.Compression;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.RegularExpressions;
using static Lombiq.Hosting.MediaTheme.Deployer.Constants.PathConstants;
using static System.Console;
Expand Down Expand Up @@ -68,6 +68,8 @@ public class CommandLineOptions

internal static partial class Program
{
private static readonly JsonSerializerOptions _indentedJsonSerializerOptions = new() { WriteIndented = true };

internal static readonly string[] FeaturesToEnable = ["Lombiq.Hosting.MediaTheme.Bridge", "Lombiq.Hosting.MediaTheme"];

public static Task Main(string[] args) =>
Expand Down Expand Up @@ -104,6 +106,19 @@ private static async Task RunOptionsAsync(CommandLineOptions options)
}
}

private static void AddFile(JsonArray files, string rootPath, string filePath)
{
// These need to use forward slashes on every platform due to Orchard's import logic.
var importPath = Path.Combine(rootPath, filePath).Replace('\\', '/');
var template = new
{
SourcePath = importPath,
TargetPath = importPath,
};

files.Add(template);
}

private static async Task RunOptionsInnerAsync(CommandLineOptions options)
{
// Creating directory for the deployment.
Expand All @@ -130,22 +145,22 @@ private static async Task RunOptionsInnerAsync(CommandLineOptions options)
throw new ArgumentException("The theme's path must be provided.");
}

var recipeSteps = new JArray();
var recipeSteps = new JsonArray();

// Creating Feature step to enable the Media Theme theme and Bridge module.
var featureStep = JObject.FromObject(new
var featureStep = new
{
name = "Feature",
enable = FeaturesToEnable,
});
};
recipeSteps.Add(featureStep);

// Creating Themes step to set Media Theme as the site theme.
var themesStep = JObject.FromObject(new
var themesStep = new
{
name = "Themes",
Site = "Lombiq.Hosting.MediaTheme",
});
};
recipeSteps.Add(themesStep);

var baseThemeId = string.IsNullOrEmpty(options.BaseThemeId) ? null : options.BaseThemeId;
Expand All @@ -163,29 +178,16 @@ private static async Task RunOptionsInnerAsync(CommandLineOptions options)
}

// Creating Media Theme step.
var mediaThemeStep = JObject.FromObject(new
var mediaThemeStep = new
{
name = "mediatheme",
ClearMediaThemeFolder = options.ClearMediaHostingFolder,
BaseThemeId = baseThemeId,
});
};
recipeSteps.Add(mediaThemeStep);

// Creating media step.
var files = new JArray();

void AddFile(string rootPath, string filePath)
{
// These need to use forward slashes on every platform due to Orchard's import logic.
var importPath = Path.Combine(rootPath, filePath).Replace('\\', '/');
var templateJObject = JObject.FromObject(new
{
SourcePath = importPath,
TargetPath = importPath,
});

files.Add(templateJObject);
}
var files = new JsonArray();

// Getting assets.
var assetsPath = Path.Combine(themePath, LocalThemeWwwRootDirectory);
Expand All @@ -195,7 +197,7 @@ void AddFile(string rootPath, string filePath)

foreach (var assetPath in allAssetsPaths)
{
AddFile(MediaThemeAssetsCopyDirectoryPath, assetPath[(assetsPath.Length + 1)..]);
AddFile(files, MediaThemeAssetsCopyDirectoryPath, assetPath[(assetsPath.Length + 1)..]);
}

// Copying assets to deployment directory.
Expand All @@ -214,7 +216,7 @@ void AddFile(string rootPath, string filePath)

foreach (var templatePath in allTemplatesPaths)
{
AddFile(MediaThemeTemplatesCopyDirectoryPath, templatePath[(templatesPath.Length + 1)..]);
AddFile(files, MediaThemeTemplatesCopyDirectoryPath, templatePath[(templatesPath.Length + 1)..]);
}

// Copying templates to deployment directory.
Expand All @@ -225,14 +227,14 @@ void AddFile(string rootPath, string filePath)
recursive: false);
}

var mediaStep = JObject.FromObject(new
var mediaStep = new
{
name = "media",
Files = files,
});
};
recipeSteps.Add(mediaStep);

CreateRecipeAndWriteIt(options, recipeSteps, newDirectoryPath);
await CreateRecipeAndWriteItAsync(options, recipeSteps, newDirectoryPath);

// Zipping the directory.
var zipFilePath = newDirectoryPath + ".zip";
Expand Down Expand Up @@ -320,10 +322,10 @@ private static string CreateNewDirectoryPath(CommandLineOptions values)
+ DateTime.Now.ToString("ddMMMyyyyHHmmss", CultureInfo.CurrentCulture); // #spell-check-ignore-line
}

private static void CreateRecipeAndWriteIt(CommandLineOptions options, JArray steps, string newDirectoryPath)
private static Task CreateRecipeAndWriteItAsync(CommandLineOptions options, JsonArray steps, string newDirectoryPath)
{
// Creating the recipe itself.
var recipe = JObject.FromObject(new
var recipe = new
{
name = options.DeploymentFileName ?? "MediaTheme",
displayName = "Media Theme",
Expand All @@ -332,17 +334,15 @@ private static void CreateRecipeAndWriteIt(CommandLineOptions options, JArray st
website = string.Empty,
version = string.Empty,
issetuprecipe = false, // #spell-check-ignore-line
categories = new JArray(),
tags = new JArray(),
categories = Enumerable.Empty<object>(),
tags = Enumerable.Empty<object>(),
steps,
});
};

// Creating JSON file.
using var file = File.CreateText(Path.Join(newDirectoryPath, RecipeFile));
using var writer = new JsonTextWriter(file) { Formatting = Formatting.Indented };
recipe.WriteTo(writer);

file.Close();
var recipePath = Path.Join(newDirectoryPath, RecipeFile);
var recipeBytes = JsonSerializer.SerializeToUtf8Bytes(recipe, recipe.GetType(), _indentedJsonSerializerOptions);
return File.WriteAllBytesAsync(recipePath, recipeBytes);
}

[GeneratedRegex(@"BaseTheme\s*=\s*""(?<baseThemeId>.*)""", RegexOptions.ExplicitCapture, matchTimeoutMilliseconds: 1000)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="OrchardCore.Theme.Targets" Version="1.8.2" />
<PackageReference Include="OrchardCore.ContentManagement" Version="1.8.2" />
<PackageReference Include="OrchardCore.DisplayManagement" Version="1.8.2" />
<PackageReference Include="OrchardCore.ResourceManagement" Version="1.8.2" />
<PackageReference Include="OrchardCore.Theme.Targets" Version="2.0.0-preview-18200" />
<PackageReference Include="OrchardCore.ContentManagement" Version="2.0.0-preview-18200" />
<PackageReference Include="OrchardCore.DisplayManagement" Version="2.0.0-preview-18200" />
<PackageReference Include="OrchardCore.ResourceManagement" Version="2.0.0-preview-18200" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Lombiq.Hosting.MediaTheme/Lombiq.Hosting.MediaTheme.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="OrchardCore.Theme.Targets" Version="1.8.2" />
<PackageReference Include="OrchardCore.Theme.Targets" Version="2.0.0-preview-18200" />
</ItemGroup>

<ItemGroup>
Expand Down