-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rick Butterfield
committed
Aug 7, 2024
1 parent
bcecce0
commit fd4689d
Showing
28 changed files
with
475 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/Umbraco.Community.BlockPreview.SchemaGenerator/AppSettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Umbraco.Community.BlockPreview.SchemaGenerator | ||
{ | ||
internal class AppSettings | ||
{ | ||
public BlockPreviewDefinition BlockPreview { get; set; } | ||
|
||
internal class BlockPreviewDefinition | ||
{ | ||
public ViewLocations ViewLocations { get; set; } | ||
} | ||
} | ||
|
||
public class ViewLocations | ||
{ | ||
public List<string> BlockList { get; set; } | ||
public List<string> BlockGrid { get; set; } | ||
public List<string> RichText { get; set; } | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
src/Umbraco.Community.BlockPreview.SchemaGenerator/BlockPreviewSchemaGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using Newtonsoft.Json.Converters; | ||
using Newtonsoft.Json.Linq; | ||
using Newtonsoft.Json.Serialization; | ||
using Newtonsoft.Json; | ||
using NJsonSchema.Generation; | ||
using NJsonSchema.NewtonsoftJson.Generation; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Umbraco.Community.BlockPreview.SchemaGenerator | ||
{ | ||
internal class BlockPreviewSchemaGenerator | ||
{ | ||
private readonly JsonSchemaGenerator _schemaGenerator; | ||
|
||
public BlockPreviewSchemaGenerator() | ||
{ | ||
_schemaGenerator = new JsonSchemaGenerator(new BlockPreviewSchemaGeneratorSettings()); | ||
} | ||
|
||
public string Generate() | ||
{ | ||
var blockPreviewSchema = GenerateBlockPreviewSchema(); | ||
return blockPreviewSchema.ToString(); | ||
} | ||
|
||
private JObject GenerateBlockPreviewSchema() | ||
{ | ||
var schema = _schemaGenerator.Generate(typeof(AppSettings)); | ||
return JsonConvert.DeserializeObject<JObject>(schema.ToJson()); | ||
} | ||
} | ||
|
||
internal class BlockPreviewSchemaGeneratorSettings : NewtonsoftJsonSchemaGeneratorSettings | ||
{ | ||
public BlockPreviewSchemaGeneratorSettings() | ||
{ | ||
AlwaysAllowAdditionalObjectProperties = true; | ||
SerializerSettings = new JsonSerializerSettings() | ||
{ | ||
ContractResolver = new WritablePropertiesOnlyResolver(), | ||
}; | ||
DefaultReferenceTypeNullHandling = ReferenceTypeNullHandling.NotNull; | ||
SchemaNameGenerator = new NamespacePrefixedSchemaNameGenerator(); | ||
SerializerSettings.Converters.Add(new StringEnumConverter()); | ||
IgnoreObsoleteProperties = true; | ||
GenerateExamples = true; | ||
} | ||
|
||
private class WritablePropertiesOnlyResolver : DefaultContractResolver | ||
{ | ||
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization) | ||
{ | ||
IList<JsonProperty> props = base.CreateProperties(type, memberSerialization); | ||
var result = props.Where(p => p.Writable).ToList(); | ||
result.ForEach(x => x.PropertyName = ToPascalCase(x.PropertyName)); | ||
return result; | ||
} | ||
|
||
/// <summary> | ||
/// we serialize everything camel case inside uSync but the settings are actually PascalCase | ||
/// for appsettings.json, so we need to PascalCase each property. | ||
/// </summary> | ||
private string ToPascalCase(string str) | ||
{ | ||
if (!string.IsNullOrEmpty(str)) | ||
{ | ||
return char.ToUpperInvariant(str[0]) + str.Substring(1); | ||
} | ||
|
||
return str; | ||
} | ||
} | ||
} | ||
|
||
internal class NamespacePrefixedSchemaNameGenerator : DefaultSchemaNameGenerator | ||
{ | ||
public override string Generate(Type type) => type.Namespace.Replace(".", string.Empty) + base.Generate(type); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Umbraco.Community.BlockPreview.SchemaGenerator/Options.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using CommandLine; | ||
|
||
namespace Umbraco.Community.BlockPreview.SchemaGenerator | ||
{ | ||
internal class Options | ||
{ | ||
[Option('o', "outputFile", Required = false, | ||
HelpText = "", | ||
Default = "..\\..\\..\\..\\Umbraco.Community.BlockPreview\\appsettings-schema.blockpreview.json")] | ||
public string OutputFile { get; set; } = "..\\..\\..\\..\\Umbraco.Community.BlockPreview\\appsettings-schema.blockpreview.json"; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/Umbraco.Community.BlockPreview.SchemaGenerator/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using CommandLine; | ||
using System; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
|
||
namespace Umbraco.Community.BlockPreview.SchemaGenerator | ||
{ | ||
internal class Program | ||
{ | ||
public static async Task Main(string[] args) | ||
{ | ||
try | ||
{ | ||
await Parser.Default.ParseArguments<Options>(args) | ||
.WithParsedAsync(Execute); | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine(e); | ||
throw; | ||
} | ||
} | ||
|
||
private static async Task Execute(Options options) | ||
{ | ||
var generator = new BlockPreviewSchemaGenerator(); | ||
|
||
var schema = generator.Generate(); | ||
|
||
var path = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, options.OutputFile)); | ||
Console.WriteLine("Path to use {0}", path); | ||
Directory.CreateDirectory(Path.GetDirectoryName(path)); | ||
Console.WriteLine("Ensured directory exists"); | ||
await File.WriteAllTextAsync(path, schema); | ||
|
||
Console.WriteLine("File written at {0}", path); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...munity.BlockPreview.SchemaGenerator/Umbraco.Community.BlockPreview.SchemaGenerator.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>disable</Nullable> | ||
|
||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="NJsonSchema" Version="11.0.2" /> | ||
<PackageReference Include="NJsonSchema.NewtonsoftJson" Version="11.0.2" /> | ||
<PackageReference Include="CommandLineParser" Version="2.9.1" /> | ||
</ItemGroup> | ||
|
||
<PropertyGroup Condition="'$(Configuration)' == 'Release'"> | ||
<DocumentationFile>bin\Release\$(TargetFramework)\Umbraco.Community.BlockPreview.SchemaGenerator.xml</DocumentationFile> | ||
</PropertyGroup> | ||
|
||
<Target Name="CopyPackagesXml" BeforeTargets="Build"> | ||
<ItemGroup> | ||
<PackageReferenceFiles Include="$(NugetPackageRoot)%(PackageReference.Identity)\%(PackageReference.Version)%(PackageReference.CopyToOutputDirectory)\lib\**\*.xml" /> | ||
</ItemGroup> | ||
<Copy SourceFiles="@(PackageReferenceFiles)" DestinationFolder="$(OutDir)" /> | ||
</Target> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/Umbraco.Community.BlockPreview/Interfaces/IBackOfficeRtePreviewService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Umbraco.Community.BlockPreview.Interfaces | ||
{ | ||
public interface IBackOfficeRtePreviewService : IBackOfficePreviewService | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.