[go: nahoru, domu]

Skip to content

Commit

Permalink
Generator improvements: support templates for specific targets (i.e. …
Browse files Browse the repository at this point in the history
…net40, net45)
  • Loading branch information
masbicudo committed Dec 15, 2017
1 parent c03dc6d commit 831e25e
Show file tree
Hide file tree
Showing 14 changed files with 503 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,5 @@ project.lock.json
!.vs/config/
*.Signed.csproj
FluentJsonNet.Tests.*.csproj
/FluentJsonNet.Tests.net45/
/FluentJsonNet.Tests.net40/
28 changes: 14 additions & 14 deletions Fluent-Json.NET-Configuration.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentJsonNet.Tests", "Flue
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "TargetedTests", "TargetedTests", "{B5B1BB1A-8F76-4551-B079-B13E53EDE1F3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentJsonNet.Tests.net40", "FluentJsonNet.Tests\FluentJsonNet.Tests.net40.csproj", "{104BBB96-5254-42B6-B278-6CA9B37472EC}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentJsonNet.Tests.net45", "FluentJsonNet.Tests\FluentJsonNet.Tests.net45.csproj", "{301BD88F-8F56-4E75-AF98-BF6FE2BF57F1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentJsonNet.Tests.netcoreapp1.1", "FluentJsonNet.Tests\FluentJsonNet.Tests.netcoreapp1.1.csproj", "{DF4B18A7-B0FD-49AC-A8CE-949005A04D86}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentJsonNet.Tests.netcoreapp2.0", "FluentJsonNet.Tests\FluentJsonNet.Tests.netcoreapp2.0.csproj", "{68294CE2-7D08-469E-976C-55FAC4FE2925}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentJsonNet.Signed", "FluentJsonNet\FluentJsonNet.Signed.csproj", "{D29FDB7E-CC4D-4E67-B3AA-68311DA6A1CD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentJsonNet.Tests.net40", "FluentJsonNet.Tests.net40\FluentJsonNet.Tests.net40.csproj", "{6F8C92C5-38F6-460E-9DDA-6334A1575B31}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentJsonNet.Tests.net45", "FluentJsonNet.Tests.net45\FluentJsonNet.Tests.net45.csproj", "{829AA441-CAF1-4332-9D70-B228D2197D99}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -39,14 +39,6 @@ Global
{2FDD0DD1-E278-4AEE-9FD1-69702458519C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2FDD0DD1-E278-4AEE-9FD1-69702458519C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2FDD0DD1-E278-4AEE-9FD1-69702458519C}.Release|Any CPU.Build.0 = Release|Any CPU
{104BBB96-5254-42B6-B278-6CA9B37472EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{104BBB96-5254-42B6-B278-6CA9B37472EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{104BBB96-5254-42B6-B278-6CA9B37472EC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{104BBB96-5254-42B6-B278-6CA9B37472EC}.Release|Any CPU.Build.0 = Release|Any CPU
{301BD88F-8F56-4E75-AF98-BF6FE2BF57F1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{301BD88F-8F56-4E75-AF98-BF6FE2BF57F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{301BD88F-8F56-4E75-AF98-BF6FE2BF57F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{301BD88F-8F56-4E75-AF98-BF6FE2BF57F1}.Release|Any CPU.Build.0 = Release|Any CPU
{DF4B18A7-B0FD-49AC-A8CE-949005A04D86}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DF4B18A7-B0FD-49AC-A8CE-949005A04D86}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DF4B18A7-B0FD-49AC-A8CE-949005A04D86}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -59,15 +51,23 @@ Global
{D29FDB7E-CC4D-4E67-B3AA-68311DA6A1CD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D29FDB7E-CC4D-4E67-B3AA-68311DA6A1CD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D29FDB7E-CC4D-4E67-B3AA-68311DA6A1CD}.Release|Any CPU.Build.0 = Release|Any CPU
{6F8C92C5-38F6-460E-9DDA-6334A1575B31}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6F8C92C5-38F6-460E-9DDA-6334A1575B31}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6F8C92C5-38F6-460E-9DDA-6334A1575B31}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6F8C92C5-38F6-460E-9DDA-6334A1575B31}.Release|Any CPU.Build.0 = Release|Any CPU
{829AA441-CAF1-4332-9D70-B228D2197D99}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{829AA441-CAF1-4332-9D70-B228D2197D99}.Debug|Any CPU.Build.0 = Debug|Any CPU
{829AA441-CAF1-4332-9D70-B228D2197D99}.Release|Any CPU.ActiveCfg = Release|Any CPU
{829AA441-CAF1-4332-9D70-B228D2197D99}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{104BBB96-5254-42B6-B278-6CA9B37472EC} = {B5B1BB1A-8F76-4551-B079-B13E53EDE1F3}
{301BD88F-8F56-4E75-AF98-BF6FE2BF57F1} = {B5B1BB1A-8F76-4551-B079-B13E53EDE1F3}
{DF4B18A7-B0FD-49AC-A8CE-949005A04D86} = {B5B1BB1A-8F76-4551-B079-B13E53EDE1F3}
{68294CE2-7D08-469E-976C-55FAC4FE2925} = {B5B1BB1A-8F76-4551-B079-B13E53EDE1F3}
{6F8C92C5-38F6-460E-9DDA-6334A1575B31} = {B5B1BB1A-8F76-4551-B079-B13E53EDE1F3}
{829AA441-CAF1-4332-9D70-B228D2197D99} = {B5B1BB1A-8F76-4551-B079-B13E53EDE1F3}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A1D7B039-A88D-41E6-B521-FD3E2994DD7A}
Expand Down
11 changes: 11 additions & 0 deletions FluentJsonNet.Tests/Compat/TestInfra.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public static void IsTrue(bool what)

public abstract class Asserter
{
public string CurrentClass { get; set; }
public string CurrentMethod { get; set; }
public TestState State { get; set; }

public abstract void AreEqual<T>(T expected, T value);

public abstract void IsInstanceOfType(object value, Type expectedType, string message);
Expand All @@ -69,6 +73,13 @@ public abstract class Asserter

public abstract void IsTrue(bool what);
}

public enum TestState
{
Ok = 0,
Inconclusive = 1,
Error = 3,
}
}

namespace System.Reflection
Expand Down
3 changes: 2 additions & 1 deletion FluentJsonNet.Tests/FluentJsonNet.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net45;net40;netcoreapp1.1;netcoreapp2.0</TargetFrameworks>
<TargetFrameworks>net40;net45;netcoreapp1.1;netcoreapp2.0</TargetFrameworks>
<EnableDefaultCompileItems>False</EnableDefaultCompileItems>
<IsPackable>false</IsPackable>
</PropertyGroup>
Expand All @@ -13,6 +13,7 @@
<PackageReference Condition="'$(TargetFramework)'=='netcoreapp1.1'" Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Condition="'$(TargetFramework)'=='netcoreapp1.1'" Include="MSTest.TestAdapter" Version="1.1.11" />
<PackageReference Condition="'$(TargetFramework)'=='netcoreapp1.1'" Include="MSTest.TestFramework" Version="1.1.11" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
</ItemGroup>

<ItemGroup>
Expand Down
11 changes: 11 additions & 0 deletions FluentJsonNet.Tests/ProjectMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace FluentJsonNet.Tests
{
/// <summary>
/// The is a tag class that provides information about this assembly.
/// It can be used to locate the assembly: i.e. typeof(ProjectMetadata)
/// </summary>
public class ProjectMetadata
{
public readonly string Name = "FluentJsonNet.Tests";
}
}
126 changes: 117 additions & 9 deletions ProjectGenerator/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml;

namespace ProjectsGenerator
Expand Down Expand Up @@ -28,15 +32,52 @@ static void Main(string[] args)
var targets = targetsElement[0].InnerText.Split(";");
foreach (var target in targets)
{
if (File.Exists($"../{TestProjectName}/{TestProjectName}.{target}.csproj"))
continue;

var l2jsTestNew = (XmlDocument)l2jsTests.Clone();
var node = l2jsTestNew.SelectSingleNode("//TargetFrameworks");
var targetElement = l2jsTestNew.CreateElement("TargetFramework");
targetElement.InnerText = target;
node.ParentNode.ReplaceChild(targetElement, node);
l2jsTestNew.Save($"../{TestProjectName}/{TestProjectName}.{target}.csproj");
if (Directory.Exists($"Templates/{target}"))
{
var dic = new Dictionary<string, object>
{
{ $"TestProjectName", TestProjectName },
{ $"TestProjectGuid", "{6F8C92C5-38F6-460E-9DDA-6334A1575B31}" },
{ $"TestProjectRefGuid", "{2FDD0DD1-E278-4AEE-9FD1-69702458519C}" },
{ $"CurrentYear", DateTime.Now.Year },
};

string[] listIncludes = null;
if (target == "net45")
{
listIncludes = Directory.GetFiles($@"../{TestProjectName}", "*.cs", SearchOption.AllDirectories)
.Select(s => Path.GetFullPath(s).Replace(Path.GetFullPath($@"../{TestProjectName}/"), $@""))
.Where(s => !Regex.IsMatch(s, @"\bobj\b", RegexOptions.IgnoreCase) && !Regex.IsMatch(s, @"\bbin\b", RegexOptions.IgnoreCase))
.ToArray();

dic["FileInclude"] = listIncludes;
}

CreateFromTemplateFolder(dic, $"Templates/{target}", $"..");

if (target == "net45")
foreach (var file in listIncludes)
{
var targetFileName = Path.Combine(Path.GetFullPath($@"../{TestProjectName}.{target}/"), file);
Directory.CreateDirectory(Path.GetDirectoryName(targetFileName));
File.Copy(
Path.Combine(Path.GetFullPath($@"../{TestProjectName}/"), file),
targetFileName,
true);
}
}
else
{
if (File.Exists($"../{TestProjectName}/{TestProjectName}.{target}.csproj"))
continue;

var l2jsTestNew = (XmlDocument)l2jsTests.Clone();
var node = l2jsTestNew.SelectSingleNode("//TargetFrameworks");
var targetElement = l2jsTestNew.CreateElement("TargetFramework");
targetElement.InnerText = target;
node.ParentNode.ReplaceChild(targetElement, node);
l2jsTestNew.Save($"../{TestProjectName}/{TestProjectName}.{target}.csproj");
}
}

}
Expand Down Expand Up @@ -78,6 +119,73 @@ static void Main(string[] args)
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}

private static void CreateFromTemplateFolder(Dictionary<string, object> dic, string templatePath, string targetPath)
{
var dirInfo = new DirectoryInfo(templatePath);
foreach (var fileInfo in dirInfo.GetFiles())
{
var contents = File.ReadAllText(fileInfo.FullName);
contents = ReplaceTemplatePlaceholders(dic, contents);
File.WriteAllText($"{targetPath}/{ReplaceTemplatePlaceholders(dic, fileInfo.Name)}", contents);
}

foreach (var subdirInfo in dirInfo.GetDirectories())
{
var subdirTarget = $"{targetPath}/{ReplaceTemplatePlaceholders(dic, subdirInfo.Name)}";
Directory.CreateDirectory(subdirTarget);
CreateFromTemplateFolder(dic, subdirInfo.FullName, subdirTarget);
}
}

private static string ReplaceTemplatePlaceholders(Dictionary<string, object> dic, string text)
{
foreach (var kv in dic)
{
text = Regex.Replace(
text,
$@"%Begin:{kv.Key}%(?<SUBTEXT>.*?)%End:{kv.Key}%|%{kv.Key}%",
m =>
{
if (m.Groups["SUBTEXT"].Success)
{
var subresult = "";
var dic2 = new Dictionary<string, object>(dic);
if (kv.Value is IList)
{
var list = kv.Value as IList;
foreach (var item in list)
{
dic2["CurrentValue"] = item;
subresult += ReplaceTemplatePlaceholders(dic2, m.Groups["SUBTEXT"].Value);
}
}
else if (kv.Value is IDictionary<string, object>)
{
var subdic = kv.Value as IDictionary<string, object>;
foreach (var skv in subdic)
dic2[skv.Key] = skv.Value;
subresult += ReplaceTemplatePlaceholders(dic2, m.Groups["SUBTEXT"].Value);
}
else
{
dic2["Value"] = kv.Value.ToString();
subresult += ReplaceTemplatePlaceholders(dic2, m.Groups["SUBTEXT"].Value);
}
return subresult;
}
else
{
var result = kv.Value.ToString();
return result;
}
},
RegexOptions.Singleline);
}

return text;
}
}

static class ObjectExtensions
Expand Down
6 changes: 6 additions & 0 deletions ProjectGenerator/ProjectGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Remove="Templates\**" />
<EmbeddedResource Remove="Templates\**" />
<None Remove="Templates\**" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>%TestProjectGuid%</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>%TestProjectName%.net40</RootNamespace>
<AssemblyName>%TestProjectName%.net40</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Assert.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\%TestProjectName%\%TestProjectName%.csproj">
<Project>%TestProjectRefGuid%</Project>
<Name>%TestProjectName%</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
48 changes: 48 additions & 0 deletions ProjectGenerator/Templates/net40/%TestProjectName%.net40/Assert.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace %TestProjectName%.net40
{
internal class MyAssert : Asserter
{
public override void AreEqual<T>(T expected, T value)
{
var prev = Console.ForegroundColor;
var eql = EqualityComparer<T>.Default.Equals(expected, value);
Console.ForegroundColor = eql ? ConsoleColor.Green : ConsoleColor.Red;
Console.Write(eql ? " OK " : " FAIL ");
Console.ForegroundColor = prev;
Console.WriteLine($"AreEqual({ToLiteral(expected)}, {ToLiteral(value)})");
}

private static string ToLiteral<T>(T value)
{
if (value == null)
return "null";

if (value is string)
{
var val = Regex.Replace(
value.ToString(),
@"[\r\n\t\0]",
m => new[] { "\\r", "\\n", "\\t", "\\0" }["\r\n\t\0".IndexOf(m.Value)]);

return $"\"{val}\"";
}

return value.ToString();
}

public override void IsInstanceOfType(object value, Type expectedType, string message)
{
var prev = Console.ForegroundColor;
var ok = value != null && expectedType.IsAssignableFrom(value.GetType());
Console.ForegroundColor = ok ? ConsoleColor.Green : ConsoleColor.Red;
Console.Write(ok ? " OK " : " FAIL ");
Console.ForegroundColor = prev;
Console.WriteLine($"IsInstanceOfType({ToLiteral(value)}, typeof({expectedType.ToString()}), {ToLiteral(message)})");
}
}
}
Loading

0 comments on commit 831e25e

Please sign in to comment.