jellyfin-sdk-csharp/nuke/Generate.cs

100 lines
3.4 KiB
C#
Raw Permalink Normal View History

2024-02-22 19:22:04 +00:00
using System;
using System.IO;
2024-02-22 19:22:04 +00:00
using System.Linq;
2024-02-25 23:03:46 +00:00
using System.Text.RegularExpressions;
using Nuke.Common;
using Nuke.Common.ProjectModel;
using Nuke.Common.Tools.DotNet;
2024-02-25 23:03:46 +00:00
public partial class Generate : NukeBuild
{
2024-02-25 23:03:46 +00:00
const string StableOpenApi = """
2024-03-10 17:55:34 +00:00
"descriptionLocation": "https://api.jellyfin.org/openapi/jellyfin-openapi-stable.json"
2024-02-25 23:03:46 +00:00
""";
const string UnstableOpenApi = """
2024-03-10 17:55:34 +00:00
"descriptionLocation": "https://api.jellyfin.org/openapi/jellyfin-openapi-unstable.json"
2024-02-25 23:03:46 +00:00
""";
static readonly string[] _ignoreFiles =
2024-02-22 19:22:04 +00:00
[
".editorconfig",
2024-02-25 23:03:46 +00:00
"kiota-lock.json"
2024-02-22 19:22:04 +00:00
];
public static int Main () => Execute<Generate>(x => x.Run);
[Parameter("Configuration to build - 'Stable' (default) or 'Unstable'")]
readonly Configuration Configuration = Configuration.Stable;
[Solution(GenerateProjects = true)]
readonly Solution Solution;
Target RestoreTools => g => g.Executes(() => DotNetTasks.DotNetToolRestore());
2024-02-22 19:22:04 +00:00
Target CleanGenerated => g => g.Executes(() =>
{
var generatedDirectory = new DirectoryInfo(Path.Combine(Solution.Jellyfin_Sdk.Directory, "Generated"));
foreach (var file in generatedDirectory.GetFiles())
{
2024-02-25 23:03:46 +00:00
if (_ignoreFiles.Contains(file.Name, StringComparer.OrdinalIgnoreCase))
2024-02-22 19:22:04 +00:00
{
continue;
}
file.Delete();
}
foreach (var directory in generatedDirectory.GetDirectories())
{
directory.Delete(recursive: true);
}
});
2024-02-25 23:03:46 +00:00
Target UpdateConfig => g => g
2024-02-22 19:22:04 +00:00
.DependsOn(RestoreTools, CleanGenerated)
.Executes(() =>
{
2024-02-25 23:03:46 +00:00
var configPath = Path.Combine(Solution.Jellyfin_Sdk.Directory, "Generated", "kiota-lock.json");
var config = File.ReadAllText(configPath);
var desiredSpecification = Configuration == Configuration.Stable
? StableOpenApi
: UnstableOpenApi;
config = OpenApiRegex().Replace(config, desiredSpecification);
File.WriteAllText(configPath, config);
});
Target Run => g => g
2024-02-25 23:03:46 +00:00
.DependsOn(UpdateConfig)
.Executes(() =>
{
DotNetTasks.DotNet(
arguments: "kiota update --output Generated",
workingDirectory: Solution.Jellyfin_Sdk.Directory);
// TODO remove when Kiota 1.1.2 is released.
var outputPath = Path.Combine(Solution.Jellyfin_Sdk.Directory, "Generated");
foreach (var file in Directory.EnumerateFiles(outputPath, "*.cs", SearchOption.AllDirectories))
{
var contents = File.ReadAllText(file);
contents = contents
.Replace(
"application/json;profile=\"CamelCase\"",
"application/json;profile=\\\"CamelCase\\\"")
.Replace(
"application/json;profile=\"PascalCase\"",
"application/json;profile=\\\"PascalCase\\\"");
File.WriteAllText(file, contents);
}
});
2024-02-25 23:03:46 +00:00
[GeneratedRegex("""
"descriptionLocation": ".+"
""")]
private static partial Regex OpenApiRegex();
}