Skip to content

Commit

Permalink
Compile csharp datamodel before saving
Browse files Browse the repository at this point in the history
  • Loading branch information
standeren committed Dec 5, 2023
1 parent 7db6d66 commit 4a254bf
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 16 deletions.
1 change: 0 additions & 1 deletion backend/packagegroups/NuGet.props
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
<PackageReference Update="Microsoft.VisualStudio.Web.BrowserLink" Version="2.2.0" />
<PackageReference Update="HtmlAgilityPack" Version="1.11.54" />
<PackageReference Update="Microsoft.DiaSymReader.Native" Version="1.7.0" />
<PackageReference Update="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.16" />
<PackageReference Update="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.22" />
<PackageReference Update="Scrutor" Version="4.2.2" />
<PackageReference Update="Yuniql.AspNetCore" Version="1.2.25" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using Microsoft.CodeAnalysis.Emit;
using Microsoft.CodeAnalysis.Text;

namespace SharedResources.Tests
namespace Altinn.Studio.DataModeling.Converter.Csharp
{
public static class Compiler
{
Expand Down Expand Up @@ -42,13 +42,10 @@ public static Assembly CompileToAssembly(string csharpCode)
errors.AppendLine($"{diagnostic.Id}: {diagnostic.GetMessage()}");
}

throw new Exception($"Uh dude, you seem to have provoked some compilation errors with your code change. Please fix before merging! {errors}");
}
else
{
ms.Seek(0, SeekOrigin.Begin);
assembly = Assembly.Load(ms.ToArray());
throw new CsharpGenerationException($"// Compiler // CompileToAssembly // Csharp compilation failed with errors: {errors}");
}
ms.Seek(0, SeekOrigin.Begin);
assembly = Assembly.Load(ms.ToArray());
}

return assembly;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Altinn.Studio.DataModeling.Converter.Interfaces;
using Altinn.Studio.DataModeling.Metamodel;
Expand All @@ -21,22 +23,27 @@ public JsonMetadataToCsharpConverter(CSharpGenerationSettings generationSettings

private string Indent(int level = 1) => new string(' ', level * _generationSettings.IndentSize);

public void TryGenerateCsharpClass(string csharpClass)
{
Compiler.CompileToAssembly(csharpClass);
}

/// <summary>
/// Create Model from ServiceMetadata object
/// </summary>
/// <param name="serviceMetadata">ServiceMetadata object</param>
/// <returns>The model code in C#</returns>
public string CreateModelFromMetadata(ModelMetadata serviceMetadata)
{
Dictionary<string, string> classes = new Dictionary<string, string>();
Dictionary<string, string> classes = new ();

CreateModelFromMetadataRecursive(classes, serviceMetadata.Elements.Values.First(el => el.ParentElement == null), serviceMetadata, serviceMetadata.TargetNamespace);

StringBuilder writer = new StringBuilder()
.AppendLine("using System;")
.AppendLine("using System.Collections.Generic;")
.AppendLine("using System.ComponentModel.DataAnnotations;")
.AppendLine("using System.Linq;")
.AppendLine("using System.Runtime;")
.AppendLine("using System.Text.Json.Serialization;")
.AppendLine("using System.Threading.Tasks;")
.AppendLine("using System.Xml.Serialization;")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,12 @@ public interface IModelMetadataToCsharpConverter
/// <param name="serviceMetadata">ServiceMetadata object</param>
/// <returns>The model code in C#</returns>
public string CreateModelFromMetadata(ModelMetadata serviceMetadata);

/// <summary>
/// Try to generate csharp class from generated string from metadata
/// </summary>
/// <param name="csharpClass">Csharp class as string</param>
/// <returns>Boolean indicator of successful generation</returns>
public void TryGenerateCsharpClass(string csharpClass);
}
}
4 changes: 4 additions & 0 deletions backend/src/DataModeling/DataModeling.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Basic.Reference.Assemblies" />
<PackageReference Include="JsonSchema.Net" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" />
<PackageReference Include="Microsoft.CodeAnalysis.Common" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" />
</ItemGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
Expand Down
1 change: 0 additions & 1 deletion backend/src/Designer/Designer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" />
<PackageReference Include="HtmlAgilityPack" />
<PackageReference Include="Microsoft.DiaSymReader.Native" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" />
<PackageReference Include="Polly" />
<PackageReference Include="Scrutor" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Xml.Schema;
using Altinn.App.Core.Models;
using Altinn.Platform.Storage.Interface.Models;
using Altinn.Studio.DataModeling.Converter.Csharp;
using Altinn.Studio.DataModeling.Converter.Interfaces;
using Altinn.Studio.DataModeling.Converter.Json.Strategy;
using Altinn.Studio.DataModeling.Converter.Metadata;
Expand Down Expand Up @@ -285,8 +286,9 @@ private Json.Schema.JsonSchema GenerateJsonSchemaFromXsd(Stream xsdStream)

private async Task UpdateCSharpClasses(AltinnAppGitRepository altinnAppGitRepository, ModelMetadata modelMetadata, string schemaName)
{
string classes = _modelMetadataToCsharpConverter.CreateModelFromMetadata(modelMetadata);
await altinnAppGitRepository.SaveCSharpClasses(classes, schemaName);
string csharpClasses = _modelMetadataToCsharpConverter.CreateModelFromMetadata(modelMetadata);
_modelMetadataToCsharpConverter.TryGenerateCsharpClass(csharpClasses);
await altinnAppGitRepository.SaveCSharpClasses(csharpClasses, schemaName);
}

private static async Task UpdateApplicationMetadata(AltinnAppGitRepository altinnAppGitRepository, string schemaName, string typeName)
Expand Down
1 change: 0 additions & 1 deletion backend/tests/Designer.Tests/Designer.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
<PackageReference Include="Testcontainers.PostgreSql" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" />
<PackageReference Include="Microsoft.CodeAnalysis.Common" />
<PackageReference Include="coverlet.collector">
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Basic.Reference.Assemblies" />
<PackageReference Include="JsonSchema.Net" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" />
</ItemGroup>

</Project>

0 comments on commit 4a254bf

Please sign in to comment.