-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored code generation to use custom attribute for generation.
Definition files are no longer required to be added as AdditionalFiles
- Loading branch information
1 parent
d620ed8
commit 88836ee
Showing
20 changed files
with
829 additions
and
272 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using Microsoft.Health.Fhir.CodeGeneration; | ||
|
||
namespace SampleSourceGen; | ||
|
||
[GeneratedFhir("Education.StructureDefinition.json")] | ||
public partial class Education | ||
{ | ||
} |
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,8 @@ | ||
using Microsoft.Health.Fhir.CodeGeneration; | ||
|
||
namespace SampleSourceGen.Models; | ||
|
||
[GeneratedFhir("Models/Patient.StructureDefinition.json")] | ||
public partial class Patient | ||
{ | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using Microsoft.Health.Fhir.CodeGeneration; | ||
|
||
namespace SampleSourceGen.Models; | ||
|
||
[GeneratedFhir( | ||
"Models/Pokemon.StructureDefinition.json", | ||
TerminologyResources = new[] { "Models/PokemonType.CodeSystem.json", "Models/PokemonType.ValueSet.json" })] | ||
public partial class Pokemon | ||
{ | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,12 @@ | ||
// See https://aka.ms/new-console-template for more information | ||
|
||
using System.Text; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.Health.Fhir.SourceGenerator; | ||
using Microsoft.Health.Fhir.SpecManager.Converters; | ||
using Microsoft.Health.Fhir.SpecManager.Language; | ||
using Microsoft.Health.Fhir.SpecManager.Manager; | ||
using Microsoft.Health.Fhir.SourceGenerator.Parsing; | ||
|
||
var generator = new FhirSourceGenerator(); | ||
var resourceClass = new ResourcePartialClass(typeof(Program).Namespace!, "Patient", "Patient.StructureDefinition.json", Array.Empty<string>()); | ||
|
||
var fhirConverter = ConverterHelper.ConverterForVersion(FhirPackageCommon.FhirSequenceEnum.R4B); | ||
var emitter = new Emitter(resourceClass, diag => Console.Error.WriteLine(diag.GetMessage())); | ||
|
||
var fhirInfo = new FhirVersionInfo(FhirPackageCommon.FhirSequenceEnum.R4B); | ||
|
||
var complex = generator.ProcessFile("Patient.StructureDefinition.json", fhirInfo, fhirConverter, m => m.Resources, out var fileName, out var canonical, out var artifactClass); | ||
|
||
ILanguage language = LanguageHelper.GetLanguages("CSharpFirely2")[0]; | ||
using var memoryStream = new MemoryStream(short.MaxValue); | ||
language.Export(fhirInfo, complex, memoryStream); | ||
|
||
memoryStream.Seek(0, SeekOrigin.Begin); | ||
StreamReader reader = new StreamReader(memoryStream, Encoding.UTF8); | ||
var code = await reader.ReadToEndAsync(); | ||
var code = emitter.Emit(); | ||
|
||
Console.WriteLine(code); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<Project> | ||
|
||
<Target Name="IncludeAnalyzersInPackage" | ||
Condition="'@(ProjectReference)' != '' and @(ProjectReference->AnyHaveMetadataValue('PackAsAnalyzer', 'true'))"> | ||
<MSBuild Projects="@(ProjectReference->WithMetadataValue('PackAsAnalyzer', 'true'))" | ||
Targets="GetAnalyzerPackFiles" | ||
RemoveProperties="SetTargetFramework"> | ||
<Output TaskParameter="TargetOutputs" ItemName="_AnalyzerFile" /> | ||
</MSBuild> | ||
|
||
<ItemGroup> | ||
<Content Include="@(_AnalyzerFile)" Pack="True" Condition="!%(_AnalyzerFile.IsSymbol)" /> | ||
<!-- Symbols don't honor PackagePath. By default they are placed in lib/%(TargetFramework). | ||
Pack does honor TargetPath and does Path.Combine("lib/%(TargetFramework)", "%(TargetPath)"), | ||
so a rooted path value for TargetPath will override lib. | ||
https://github.com/NuGet/Home/issues/10860 --> | ||
<_TargetPathsToSymbols Include="@(_AnalyzerFile)" TargetPath="/%(_AnalyzerFile.PackagePath)" Condition="%(_AnalyzerFile.IsSymbol)" /> | ||
</ItemGroup> | ||
</Target> | ||
|
||
<Target Name="GetAnalyzerPackFiles" | ||
DependsOnTargets="$(GenerateNuspecDependsOn)" | ||
Returns="@(_AnalyzerPackFile)"> | ||
<PropertyGroup> | ||
<_analyzerPath>analyzers/dotnet/cs</_analyzerPath> | ||
<_analyzerPath Condition="'$(AnalyzerRoslynVersion)' != ''">$(_analyzerPath)/roslyn$(AnalyzerRoslynVersion)</_analyzerPath> | ||
<_analyzerPath Condition="'$(AnalyzerLanguage)' != ''">$(_analyzerPath)/$(AnalyzerLanguage)</_analyzerPath> | ||
</PropertyGroup> | ||
|
||
<!-- Filter on netstandard2.0 so that generator projects can multi-target for the purpose of enabling nullable reference type compiler checks. --> | ||
<ItemGroup> | ||
<_AnalyzerPackFile Include="@(_BuildOutputInPackage->WithMetadataValue('TargetFramework', 'netstandard2.0'))" IsSymbol="false" /> | ||
<_AnalyzerPackFile Include="@(_TargetPathsToSymbols->WithMetadataValue('TargetFramework', 'netstandard2.0'))" IsSymbol="true" /> | ||
<_AnalyzerPackFile PackagePath="$(_analyzerPath)/%(TargetPath)" /> | ||
</ItemGroup> | ||
|
||
<Error Text="Analyzers must target netstandard2.0 since they run in the compiler which targets netstandard2.0. $(MSBuildProjectFullPath) targets '$([MSBuild]::ValueOrDefault('$(TargetFrameworks)', '$(TargetFramework)'))' instead." | ||
Condition="'@(_AnalyzerPackFile)' == ''" /> | ||
</Target> | ||
|
||
</Project> |
25 changes: 25 additions & 0 deletions
25
src/Microsoft.Health.Fhir.CodeGeneration/GeneratedFhirAttribute.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,25 @@ | ||
using System; | ||
|
||
namespace Microsoft.Health.Fhir.CodeGeneration; | ||
|
||
[AttributeUsage(AttributeTargets.Class)] | ||
public class GeneratedFhirAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="GeneratedFhirAttribute"/> class. | ||
/// </summary> | ||
public GeneratedFhirAttribute(string structureDefinitionPath) | ||
{ | ||
StructureDefinitionPath = structureDefinitionPath; | ||
} | ||
|
||
/// <summary> | ||
/// The relative path to the structure definition resource json file. | ||
/// </summary> | ||
public string StructureDefinitionPath { get; } | ||
|
||
/// <summary> | ||
/// The relative paths to the terminology resource (CodeSystem/ValueSet) json files. | ||
/// </summary> | ||
public string[] TerminologyResources { get; set; } = Array.Empty<string>(); | ||
} |
53 changes: 53 additions & 0 deletions
53
src/Microsoft.Health.Fhir.CodeGeneration/Microsoft.Health.Fhir.CodeGeneration.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,53 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<LangVersion>10.0</LangVersion> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
<DevelopmentDependency>true</DevelopmentDependency> | ||
<PackOnBuild>true</PackOnBuild> | ||
<PackFolder>analyzers\cs</PackFolder> | ||
<Version>0.1.0-beta.2</Version> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
<BeforePack>$(BeforePack);IncludeAnalyzersInPackage;</BeforePack> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="7.0.0" PrivateAssets="all" GeneratePathProperty="true" /> | ||
<PackageReference Include="System.Text.Json" Version="7.0.2" PrivateAssets="all" GeneratePathProperty="true" /> | ||
<PackageReference Include="System.Text.Encodings.Web" Version="7.0.0" PrivateAssets="all" GeneratePathProperty="true" /> | ||
<PackageReference Include="DynamicLanguageRuntime" Version="1.3.3" PrivateAssets="all" GeneratePathProperty="true" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Include="$(PKGSystem_Text_Json)\lib\netstandard2.0\System.Text.Json.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" /> | ||
<None Include="$(PKGMicrosoft_Bcl_AsyncInterfaces)\lib\netstandard2.0\Microsoft.Bcl.AsyncInterfaces.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" /> | ||
<None Include="$(PKGSystem_Text_Encodings_Web)\lib\netstandard2.0\System.Text.Encodings.Web.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" /> | ||
<None Include="$(PKGDynamicLanguageRuntime)\lib\netstandard2.0\Microsoft.Dynamic.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" /> | ||
<None Include="$(PKGDynamicLanguageRuntime)\lib\netstandard2.0\Microsoft.Scripting.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" /> | ||
<None Include="$(PKGDynamicLanguageRuntime)\lib\netstandard2.0\Microsoft.Scripting.Metadata.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference | ||
Include="..\Microsoft.Health.Fhir.SourceGenerator\Microsoft.Health.Fhir.SourceGenerator.csproj" | ||
ReferenceOutputAssembly="false" | ||
OutputItemType="Analyzer" | ||
PackAsAnalyzer="true" /> | ||
<ProjectReference Include="..\fhirCsR2\fhirCsR2.csproj" | ||
ReferenceOutputAssembly="false" | ||
OutputItemType="Analyzer" | ||
PackAsAnalyzer="true" /> | ||
<ProjectReference Include="..\Microsoft.Health.Fhir.CodeGenCommon\Microsoft.Health.Fhir.CodeGenCommon.csproj" | ||
ReferenceOutputAssembly="false" | ||
OutputItemType="Analyzer" | ||
PackAsAnalyzer="true" /> | ||
<ProjectReference Include="..\Microsoft.Health.Fhir.SpecManager\Microsoft.Health.Fhir.SpecManager.csproj" | ||
ReferenceOutputAssembly="false" | ||
OutputItemType="Analyzer" | ||
PackAsAnalyzer="true" /> | ||
</ItemGroup> | ||
</Project> |
46 changes: 46 additions & 0 deletions
46
src/Microsoft.Health.Fhir.SourceGenerator/DiagnosticDescriptors.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,46 @@ | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace Microsoft.Health.Fhir.SourceGenerator; | ||
|
||
internal static class DiagnosticDescriptors | ||
{ | ||
public static readonly DiagnosticDescriptor FailedArtifactDef = new( | ||
"FHIRGEN001", | ||
"Invalid artifact file", | ||
"Unable to parse {1} file '{0}'. StructureDefinition files must have a snapshot.", | ||
"FhirSourceGenerator", | ||
DiagnosticSeverity.Warning, | ||
true); | ||
|
||
public static readonly DiagnosticDescriptor TypeLoaderException = new( | ||
"FHIRGEN002", | ||
"Fatal FhirCode Generation error", | ||
"Type loader exception '{0}'.", | ||
"FhirSourceGenerator", | ||
DiagnosticSeverity.Error, | ||
true); | ||
|
||
public static readonly DiagnosticDescriptor UnhandledException = new( | ||
"FHIRGEN003", | ||
"Fatal FhirCode Generation error", | ||
"Unhandled exception '{0}'.", | ||
"FhirSourceGenerator", | ||
DiagnosticSeverity.Error, | ||
true); | ||
|
||
public static readonly DiagnosticDescriptor FailedToGenerate = new( | ||
"FHIRGEN004", | ||
"Invalid .StructureDefinition.json file", | ||
"Failed to generate code for json file [{0}]{1}.", | ||
"FhirSourceGenerator", | ||
DiagnosticSeverity.Error, | ||
true); | ||
|
||
public static readonly DiagnosticDescriptor ProcessSuccess = new( | ||
"FHIRGEN005", | ||
"Parsing success", | ||
"JsonPath: {0} Canonical: {1}, Artifact Type: {2}, ResourceCount: {3}", | ||
"FhirSourceGenerator", | ||
DiagnosticSeverity.Info, | ||
true); | ||
} |
Oops, something went wrong.