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

Multi targeting #276

Closed
wants to merge 14 commits into from
Closed
19 changes: 16 additions & 3 deletions src/Elements/Elements.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;net472;netcoreapp2.1</TargetFrameworks>
<AssemblyName>Hypar.Elements</AssemblyName>
<PackageTitle>Hypar Elements</PackageTitle>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand All @@ -15,13 +15,26 @@
<Version>$(Version)</Version>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0'">
<DefineConstants>NETCORE;NETSTANDARD;NETSTANDARD2_0</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.1'">
<DefineConstants>NETCORE;NETSTANDARD;NETSTANDARD2_0</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net472'">
<DefineConstants>NET47;NETFRAMEWORK</DefineConstants>
</PropertyGroup>


<ItemGroup>
<PackageReference Include="glTF2Loader" Version="1.1.3-alpha" />
<PackageReference Include="Hypar.IFC4" Version="0.1.4.1" />
<PackageReference Include="Microsoft.CodeAnalysis" Version="3.3.1" />
<PackageReference Include="Microsoft.CSharp" Version="4.5.0" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="NJsonSchema.CodeGeneration" Version="10.1.4" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="NJsonSchema.CodeGeneration" Version="10.1.8" />
<PackageReference Include="NJsonSchema.CodeGeneration.CSharp" Version="10.1.4" />
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.0-beta0007" />
<PackageReference Include="System.CodeDom" Version="4.6.0" />
Expand Down
271 changes: 178 additions & 93 deletions src/Elements/Generate/TypeGenerator.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Elements/Serialization/IFC/IFCElementExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal static List<IfcProduct> ToIfcProducts(this Element e,
IfcProductDefinitionShape shape = null;
GeometricElement geoElement = null;
Transform trans = null;
Guid id;
Guid id = Guid.Empty;

if(e is ElementInstance)
{
Expand Down
13 changes: 9 additions & 4 deletions src/Elements/Serialization/JSON/JsonInheritanceConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class JsonInheritanceConverter : Newtonsoft.Json.JsonConverter
[System.ThreadStatic]
private static bool _isWriting;

private Dictionary<string, Type> _typeCache;
private static Dictionary<string, Type> _typeCache;

[System.ThreadStatic]
private static Dictionary<Guid, Element> _elements;
Expand Down Expand Up @@ -49,7 +49,12 @@ public JsonInheritanceConverter(string discriminator)
_typeCache = BuildUserElementTypeCache();
}

private Dictionary<string, Type> BuildUserElementTypeCache()
public static void RefreshUserElementTypeCache()
{
_typeCache = BuildUserElementTypeCache();
}

private static Dictionary<string, Type> BuildUserElementTypeCache()
{
var typeCache = new Dictionary<string, Type>();

Expand All @@ -59,8 +64,8 @@ private Dictionary<string, Type> BuildUserElementTypeCache()
{
try
{
var userTypes = asm.GetTypes().Where(t=>t.GetCustomAttributes(typeof(UserElement), true).Length > 0);
foreach(var ut in userTypes)
var userTypes = asm.GetTypes().Where(t => t.GetCustomAttributes(typeof(UserElement), true).Length > 0);
foreach (var ut in userTypes)
{
typeCache.Add(ut.FullName, ut);
}
Expand Down
10 changes: 9 additions & 1 deletion test/Elements.Tests/Elements.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@

<PropertyGroup>
<AssemblyName>Hypar.Elements.Tests</AssemblyName>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFrameworks>netcoreapp2.1;net472</TargetFrameworks>
<RootNamespace>Elements</RootNamespace>
<IsPackable>false</IsPackable>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.1'">
<DefineConstants>NETCORE;NETSTANDARD;NETSTANDARD2_0</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net472'">
<DefineConstants>NET47;NETFRAMEWORK</DefineConstants>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0" />
<PackageReference Include="xunit" Version="2.3.1" />
Expand Down
12 changes: 9 additions & 3 deletions test/Elements.Tests/Grid1dTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Elements.Tests
{
public class Grid1dTests : ModelTest
{
[Fact, Trait("Category", "Examples")]
[IgnoreOnNetFrameworkFact, Trait("Category", "Examples")]
public void Grid1d()
{
this.Name = "Elements_Spatial_Grid1d";
Expand All @@ -33,7 +33,9 @@ public void Grid1d()

// Take the fourth grid segment and subdivide it by a repeating pattern
var pattern = new[] { 1.0, 1.5 };
#if NETCORE
grid[3].DivideByPattern(pattern);
#endif

// Retrieve all bottom-level cells.
// Note that grid.Cells gets the top-level cells only, and
Expand Down Expand Up @@ -223,7 +225,7 @@ public void DivideGridFromOrigin()
Assert.Equal(3, cellGeo.ToArray()[1].Length());
}

[Fact]
[IgnoreOnNetFrameworkFact]
public void DivideByPattern()
{
var grid = new Grid1d(new Domain1d(60, 150));
Expand All @@ -233,7 +235,9 @@ public void DivideByPattern()
("Glazing", 3),
("Fin", 0.2)
};
#if NETCORE
grid.DivideByPattern(pattern, PatternMode.Cycle, FixedDivisionMode.RemainderAtBothEnds);
#endif
var cells = grid.GetCells();
var types = cells.Select(c => c.Type);

Expand All @@ -244,7 +248,7 @@ public void DivideByPattern()
}
}

[Fact]
[IgnoreOnNetFrameworkFact]
public void PatternTooLongThrowsException()
{
var grid = new Grid1d(4);
Expand All @@ -254,9 +258,11 @@ public void PatternTooLongThrowsException()
("Glazing", 3),
("Fin", 0.2)
};
#if NETCORE
Exception ex = Assert.Throws<ArgumentException>(() => grid.DivideByPattern(pattern, PatternMode.None, FixedDivisionMode.RemainderAtBothEnds));

Assert.Equal("The grid could not be constructed. Pattern length exceeds grid length.", ex.Message);
#endif
}

[Fact]
Expand Down
23 changes: 16 additions & 7 deletions test/Elements.Tests/Grid2dTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Elements.Tests
{
public class Grid2dTests : ModelTest
{
[Fact, Trait("Category", "Examples")]
[IgnoreOnNetFrameworkFact, Trait("Category", "Examples")]
public void Grid2d()
{
this.Name = "Elements_Spatial_Grid2d";
Expand All @@ -22,8 +22,9 @@ public void Grid2d()

// Access the U and V axes directly and use 1d subdivision methods on them
grid.U.DivideByFixedLength(7, FixedDivisionMode.RemainderAtBothEnds);
#if NETCORE
grid.V.DivideByPattern(new[] { 2.0, 5.0 });

#endif
// Get a row by index
var fifthRow = grid.GetRowAtIndex(4);
// Divide U axis of all cells in row into panels of approximate width 1
Expand Down Expand Up @@ -97,7 +98,7 @@ public void RotationOfTransform()
Assert.Equal(0.5, grid[5, 5].V.Domain.Length, 3);
}

[Fact]
[IgnoreOnNetFrameworkFact]
public void NoExceptionsThrownWithAnyRotation()
{
for (int rotation = 0; rotation < 360; rotation += 10)
Expand Down Expand Up @@ -133,8 +134,10 @@ public void NoExceptionsThrownWithAnyRotation()
{
var vDomain = grid.CellsFlat[index].V.Domain;
var start = 0.1.MapToDomain(vDomain);
#if NETCORE
grid.CellsFlat[index].V.DivideByPattern(patterns[index % patterns.Count()], PatternMode.Cycle, FixedDivisionMode.RemainderAtBothEnds);
}
#endif
}
var cells = grid.GetCells();
var geo = cells.Select(cl => cl.GetTrimmedCellGeometry());
var types = cells.Select(cl => cl.Type);
Expand Down Expand Up @@ -184,14 +187,16 @@ public void SeparatorsFromNestedGridFromPolygons()

}

[Fact]
[IgnoreOnNetFrameworkFact]
public void GridInheritsNamesFromBothDirections()
{
var grid = new Grid2d(20, 20);
var uPattern = new[] { 1.0, 2.0, 3.0 };
var vPattern = new[] { ("Large", 5.0), ("Small", 1.0) };
#if NETCORE
grid.U.DivideByPattern(uPattern);
grid.V.DivideByPattern(vPattern);
#endif
Assert.Equal("B / Large", grid[1, 0].Type);
}

Expand All @@ -216,7 +221,7 @@ public void GetSeparators()
};
}

[Fact]
[IgnoreOnNetFrameworkFact]
public void NonXYOrientedBoundary()
{
var polygon = new Polygon(new[]
Expand All @@ -229,12 +234,14 @@ public void NonXYOrientedBoundary()
new Vector3(1.28521062051391, 5.6226002093707, 13.7492575016872),
});
var grid = new Grid2d(polygon);
#if NETCORE
grid.U.DivideByPattern(new double[] { 1, 2 });
#endif
grid.V.DivideByCount(10);
Assert.Equal(50, grid.GetCells().Count());
}

[Fact]
[IgnoreOnNetFrameworkFact]
public void XYParallelNonOrthogonalBoundary()
{
var polygon = new Polygon(new[]
Expand All @@ -245,7 +252,9 @@ public void XYParallelNonOrthogonalBoundary()
new Vector3(10.0455739172429, 3.54936529736341, 5.30811736183681),
});
var grid = new Grid2d(polygon);
#if NETCORE
grid.U.DivideByPattern(new double[] { 1, 2 });
#endif
grid.V.DivideByCount(10);
Assert.Equal(80, grid.GetCells().Count());
}
Expand Down
5 changes: 3 additions & 2 deletions test/Elements.Tests/ModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,21 @@ public void SkipsUnknownTypesDuringDeserialization()
/// Test whether two models, containing user defined types, can be
/// deserialized and merged into one model.
/// </summary>
[Fact(Skip = "ModelMerging")]
[IgnoreOnNetFrameworkFact(Skip = "ModelMerging")]
public void MergesModelsWithUserDefinedTypes()
{
var schemas = new[]{
"../../../models/Merge/Envelope.json",
"../../../models/Merge/FacadePanel.json",
"../../../models/Merge/Level.json"
};

#if NETCORE
var asm = TypeGenerator.GenerateInMemoryAssemblyFromUrisAndLoad(schemas);
var facadePanelType = asm.GetType("Elements.FacadePanel");
Assert.NotNull(facadePanelType);
var envelopeType = asm.GetType("Elements.Envelope");
Assert.NotNull(envelopeType);
#endif
var model1 = JsonConvert.DeserializeObject<Model>(File.ReadAllText("../../../models/Merge/facade.json"));
var count1 = model1.Elements.Count;

Expand Down
43 changes: 37 additions & 6 deletions test/Elements.Tests/TypeGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,26 @@ private static bool IsTravis()
}
}

public sealed class IgnoreOnNetFrameworkFact : FactAttribute
{
public IgnoreOnNetFrameworkFact()
{
if (IsFramework())
{
Skip = "Ignore on .Net Framework.";
}
}

private static bool IsFramework()
{
#if NETFRAMEWORK
return true;
#else
return false;
#endif
}
}

public sealed class IgnoreOnMacFact : FactAttribute
{
public IgnoreOnMacFact()
Expand Down Expand Up @@ -87,16 +107,24 @@ public void GeneratesCodeFromSchema()
var tmpPath = Path.GetTempPath();
var schemaPath = Path.Combine(tmpPath, "beam.json");
File.WriteAllText(schemaPath, schema);
var relPath = Path.GetRelativePath(Assembly.GetExecutingAssembly().Location, schemaPath);
#if NETFRAMEWORK
var baseUri = new Uri(Environment.CurrentDirectory);
var fullUri = new Uri(schemaPath);
var relativeUri = baseUri.MakeRelativeUri(fullUri);
var relPath = relativeUri.LocalPath;
#else
var relPath = Path.GetRelativePath(System.Environment.CurrentDirectory, schemaPath);
#endif
TypeGenerator.GenerateUserElementTypeFromUri(relPath, tmpPath, true);
var code = File.ReadAllText(Path.Combine(tmpPath, "beam.g.cs"));
}

[MultiFact(typeof(IgnoreOnMacFact), typeof(IgnoreOnTravisFact))]
[MultiFact(typeof(IgnoreOnMacFact), typeof(IgnoreOnTravisFact), typeof(IgnoreOnNetFrameworkFact))]
public void GeneratesInMemoryAssembly()
{
var uris = new []{"https://raw.githubusercontent.com/hypar-io/Schemas/master/FacadeAnchor.json",
"https://raw.githubusercontent.com/hypar-io/Schemas/master/Mullion.json"};
#if NETCORE
var asm = TypeGenerator.GenerateInMemoryAssemblyFromUrisAndLoad(uris);
var mullionType = asm.GetType("Test.Foo.Bar.Mullion");
var anchorType = asm.GetType("Test.Foo.Bar.FacadeAnchor");
Expand All @@ -113,15 +141,18 @@ public void GeneratesInMemoryAssembly()
// Profile @profile, Line @centerLine, NumericProperty @length, Transform @transform, Material @material, Representation @representation, System.Guid @id, string @name
var t = new Transform();
var m = BuiltInMaterials.Steel;
var mullion = Activator.CreateInstance(mullionType, new object[]{profile, centerLine, new NumericProperty(0, NumericPropertyUnitType.Length), t, m, new Representation(new List<SolidOperation>()), Guid.NewGuid(), "Test Mullion" });
}
var mullion = Activator.CreateInstance(mullionType, new object[]{profile, centerLine, new NumericProperty(0, NumericPropertyUnitType.Length), t, m, new Representation(new List<SolidOperation>()), false, Guid.NewGuid(), "Test Mullion" });
#endif
}

[IgnoreOnTravisFact]
[MultiFact(typeof(IgnoreOnTravisFact), typeof(IgnoreOnNetFrameworkFact))]
public void ThrowsWithBadSchema()
{
var uris = new []{"https://raw.githubusercontent.com/hypar-io/Schemas/master/ThisDoesn'tExist.json",
#if NETCORE
var uris = new []{"https://raw.githubusercontent.com/hypar-io/Schemas/master/ThisDoesntExist.json",
"https://raw.githubusercontent.com/hypar-io/Schemas/master/Mullion.json"};
Assert.Throws<Exception>(()=>TypeGenerator.GenerateInMemoryAssemblyFromUrisAndLoad(uris));
#endif
}
}
}