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

Automate Caching with MSBuild #5

Merged
merged 13 commits into from Mar 29, 2019
80 changes: 80 additions & 0 deletions how-to-precache-an-xaf-winforms-application.sln
Expand Up @@ -9,6 +9,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "how_to_precache_an_xaf_winf
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "how_to_precache_an_xaf_winforms_application.Win", "src\how_to_precache_an_xaf_winforms_application.Win\how_to_precache_an_xaf_winforms_application.Win.csproj", "{D05D93DF-312D-4D4E-B980-726871EC7833}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Scissors.Xaf.CacheWarmup.Attributes", "src\Scissors.Xaf.CacheWarmup.Attributes\Scissors.Xaf.CacheWarmup.Attributes.csproj", "{315DC3A4-2332-4E6F-93F5-A22376C6B96A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Scissors.Xaf.CacheWarmup.Generators", "src\Scissors.Xaf.CacheWarmup.Generators\Scissors.Xaf.CacheWarmup.Generators.csproj", "{3DF45F82-F10A-4D18-88B3-5EF32A91D631}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Scissors.Xaf.CacheWarmup.Generators.Cli", "src\Scissors.Xaf.CacheWarmup.Generators.Cli\Scissors.Xaf.CacheWarmup.Generators.Cli.csproj", "{15481A2D-D779-4D8D-B6C1-99405490C5C4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Scissors.Xaf.CacheWarmup.Generators.MsBuild", "src\Scissors.Xaf.CacheWarmup.Generators.MsBuild\Scissors.Xaf.CacheWarmup.Generators.MsBuild.csproj", "{9408D7A0-2DD9-494D-B230-F17CF19F67D6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -76,6 +84,78 @@ Global
{D05D93DF-312D-4D4E-B980-726871EC7833}.Release|x64.Build.0 = Release|Any CPU
{D05D93DF-312D-4D4E-B980-726871EC7833}.Release|x86.ActiveCfg = Release|Any CPU
{D05D93DF-312D-4D4E-B980-726871EC7833}.Release|x86.Build.0 = Release|Any CPU
{315DC3A4-2332-4E6F-93F5-A22376C6B96A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{315DC3A4-2332-4E6F-93F5-A22376C6B96A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{315DC3A4-2332-4E6F-93F5-A22376C6B96A}.Debug|x64.ActiveCfg = Debug|Any CPU
{315DC3A4-2332-4E6F-93F5-A22376C6B96A}.Debug|x64.Build.0 = Debug|Any CPU
{315DC3A4-2332-4E6F-93F5-A22376C6B96A}.Debug|x86.ActiveCfg = Debug|Any CPU
{315DC3A4-2332-4E6F-93F5-A22376C6B96A}.Debug|x86.Build.0 = Debug|Any CPU
{315DC3A4-2332-4E6F-93F5-A22376C6B96A}.EasyTest|Any CPU.ActiveCfg = Release|Any CPU
{315DC3A4-2332-4E6F-93F5-A22376C6B96A}.EasyTest|Any CPU.Build.0 = Release|Any CPU
{315DC3A4-2332-4E6F-93F5-A22376C6B96A}.EasyTest|x64.ActiveCfg = Release|Any CPU
{315DC3A4-2332-4E6F-93F5-A22376C6B96A}.EasyTest|x64.Build.0 = Release|Any CPU
{315DC3A4-2332-4E6F-93F5-A22376C6B96A}.EasyTest|x86.ActiveCfg = Release|Any CPU
{315DC3A4-2332-4E6F-93F5-A22376C6B96A}.EasyTest|x86.Build.0 = Release|Any CPU
{315DC3A4-2332-4E6F-93F5-A22376C6B96A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{315DC3A4-2332-4E6F-93F5-A22376C6B96A}.Release|Any CPU.Build.0 = Release|Any CPU
{315DC3A4-2332-4E6F-93F5-A22376C6B96A}.Release|x64.ActiveCfg = Release|Any CPU
{315DC3A4-2332-4E6F-93F5-A22376C6B96A}.Release|x64.Build.0 = Release|Any CPU
{315DC3A4-2332-4E6F-93F5-A22376C6B96A}.Release|x86.ActiveCfg = Release|Any CPU
{315DC3A4-2332-4E6F-93F5-A22376C6B96A}.Release|x86.Build.0 = Release|Any CPU
{3DF45F82-F10A-4D18-88B3-5EF32A91D631}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3DF45F82-F10A-4D18-88B3-5EF32A91D631}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3DF45F82-F10A-4D18-88B3-5EF32A91D631}.Debug|x64.ActiveCfg = Debug|Any CPU
{3DF45F82-F10A-4D18-88B3-5EF32A91D631}.Debug|x64.Build.0 = Debug|Any CPU
{3DF45F82-F10A-4D18-88B3-5EF32A91D631}.Debug|x86.ActiveCfg = Debug|Any CPU
{3DF45F82-F10A-4D18-88B3-5EF32A91D631}.Debug|x86.Build.0 = Debug|Any CPU
{3DF45F82-F10A-4D18-88B3-5EF32A91D631}.EasyTest|Any CPU.ActiveCfg = Release|Any CPU
{3DF45F82-F10A-4D18-88B3-5EF32A91D631}.EasyTest|Any CPU.Build.0 = Release|Any CPU
{3DF45F82-F10A-4D18-88B3-5EF32A91D631}.EasyTest|x64.ActiveCfg = Release|Any CPU
{3DF45F82-F10A-4D18-88B3-5EF32A91D631}.EasyTest|x64.Build.0 = Release|Any CPU
{3DF45F82-F10A-4D18-88B3-5EF32A91D631}.EasyTest|x86.ActiveCfg = Release|Any CPU
{3DF45F82-F10A-4D18-88B3-5EF32A91D631}.EasyTest|x86.Build.0 = Release|Any CPU
{3DF45F82-F10A-4D18-88B3-5EF32A91D631}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3DF45F82-F10A-4D18-88B3-5EF32A91D631}.Release|Any CPU.Build.0 = Release|Any CPU
{3DF45F82-F10A-4D18-88B3-5EF32A91D631}.Release|x64.ActiveCfg = Release|Any CPU
{3DF45F82-F10A-4D18-88B3-5EF32A91D631}.Release|x64.Build.0 = Release|Any CPU
{3DF45F82-F10A-4D18-88B3-5EF32A91D631}.Release|x86.ActiveCfg = Release|Any CPU
{3DF45F82-F10A-4D18-88B3-5EF32A91D631}.Release|x86.Build.0 = Release|Any CPU
{15481A2D-D779-4D8D-B6C1-99405490C5C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{15481A2D-D779-4D8D-B6C1-99405490C5C4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{15481A2D-D779-4D8D-B6C1-99405490C5C4}.Debug|x64.ActiveCfg = Debug|Any CPU
{15481A2D-D779-4D8D-B6C1-99405490C5C4}.Debug|x64.Build.0 = Debug|Any CPU
{15481A2D-D779-4D8D-B6C1-99405490C5C4}.Debug|x86.ActiveCfg = Debug|Any CPU
{15481A2D-D779-4D8D-B6C1-99405490C5C4}.Debug|x86.Build.0 = Debug|Any CPU
{15481A2D-D779-4D8D-B6C1-99405490C5C4}.EasyTest|Any CPU.ActiveCfg = Release|Any CPU
{15481A2D-D779-4D8D-B6C1-99405490C5C4}.EasyTest|Any CPU.Build.0 = Release|Any CPU
{15481A2D-D779-4D8D-B6C1-99405490C5C4}.EasyTest|x64.ActiveCfg = Release|Any CPU
{15481A2D-D779-4D8D-B6C1-99405490C5C4}.EasyTest|x64.Build.0 = Release|Any CPU
{15481A2D-D779-4D8D-B6C1-99405490C5C4}.EasyTest|x86.ActiveCfg = Release|Any CPU
{15481A2D-D779-4D8D-B6C1-99405490C5C4}.EasyTest|x86.Build.0 = Release|Any CPU
{15481A2D-D779-4D8D-B6C1-99405490C5C4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{15481A2D-D779-4D8D-B6C1-99405490C5C4}.Release|Any CPU.Build.0 = Release|Any CPU
{15481A2D-D779-4D8D-B6C1-99405490C5C4}.Release|x64.ActiveCfg = Release|Any CPU
{15481A2D-D779-4D8D-B6C1-99405490C5C4}.Release|x64.Build.0 = Release|Any CPU
{15481A2D-D779-4D8D-B6C1-99405490C5C4}.Release|x86.ActiveCfg = Release|Any CPU
{15481A2D-D779-4D8D-B6C1-99405490C5C4}.Release|x86.Build.0 = Release|Any CPU
{9408D7A0-2DD9-494D-B230-F17CF19F67D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9408D7A0-2DD9-494D-B230-F17CF19F67D6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9408D7A0-2DD9-494D-B230-F17CF19F67D6}.Debug|x64.ActiveCfg = Debug|Any CPU
{9408D7A0-2DD9-494D-B230-F17CF19F67D6}.Debug|x64.Build.0 = Debug|Any CPU
{9408D7A0-2DD9-494D-B230-F17CF19F67D6}.Debug|x86.ActiveCfg = Debug|Any CPU
{9408D7A0-2DD9-494D-B230-F17CF19F67D6}.Debug|x86.Build.0 = Debug|Any CPU
{9408D7A0-2DD9-494D-B230-F17CF19F67D6}.EasyTest|Any CPU.ActiveCfg = Release|Any CPU
{9408D7A0-2DD9-494D-B230-F17CF19F67D6}.EasyTest|Any CPU.Build.0 = Release|Any CPU
{9408D7A0-2DD9-494D-B230-F17CF19F67D6}.EasyTest|x64.ActiveCfg = Release|Any CPU
{9408D7A0-2DD9-494D-B230-F17CF19F67D6}.EasyTest|x64.Build.0 = Release|Any CPU
{9408D7A0-2DD9-494D-B230-F17CF19F67D6}.EasyTest|x86.ActiveCfg = Release|Any CPU
{9408D7A0-2DD9-494D-B230-F17CF19F67D6}.EasyTest|x86.Build.0 = Release|Any CPU
{9408D7A0-2DD9-494D-B230-F17CF19F67D6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9408D7A0-2DD9-494D-B230-F17CF19F67D6}.Release|Any CPU.Build.0 = Release|Any CPU
{9408D7A0-2DD9-494D-B230-F17CF19F67D6}.Release|x64.ActiveCfg = Release|Any CPU
{9408D7A0-2DD9-494D-B230-F17CF19F67D6}.Release|x64.Build.0 = Release|Any CPU
{9408D7A0-2DD9-494D-B230-F17CF19F67D6}.Release|x86.ActiveCfg = Release|Any CPU
{9408D7A0-2DD9-494D-B230-F17CF19F67D6}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion src/GlobalAssemblyInfo.cs
Expand Up @@ -10,7 +10,7 @@
[assembly: AssemblyProduct("how-to-precache-an-xaf-winforms-application.Win")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0.0+5f01b2039b28b00f6be3f964c3dda155418a96d6+topic/restore-packages")]
[assembly: AssemblyInformationalVersion("1.0.0.0+92da5a204d931da08890f9d0938b40627b7b91ed+topic/automate-caching2")]
[assembly: AssemblyCopyright("Copyright Manuel Grundner © 2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyConfiguration("")]
Expand Down
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net452</TargetFramework>
<AssemblyTitle>Scissors.Xaf.CacheWarmup.Attributes</AssemblyTitle>
<Product>Scissors.Xaf.CacheWarmup.Attributes</Product>
<Copyright>Copyright © Manuel Grundner 2019</Copyright>
</PropertyGroup>
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
</Project>
16 changes: 16 additions & 0 deletions src/Scissors.Xaf.CacheWarmup.Attributes/XafCacheWarmupAttribute.cs
@@ -0,0 +1,16 @@
using System;

namespace Scissors.Xaf.CacheWarmup.Attributes
{
[Serializable]
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = false, Inherited = false)]
public class XafCacheWarmupAttribute : Attribute
{
public Type XafApplicationType { get; }

public XafCacheWarmupAttribute(Type xafApplicationType)
{
XafApplicationType = xafApplicationType;
}
}
}
6 changes: 6 additions & 0 deletions src/Scissors.Xaf.CacheWarmup.Generators.Cli/App.config
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
</startup>
</configuration>
35 changes: 35 additions & 0 deletions src/Scissors.Xaf.CacheWarmup.Generators.Cli/Program.cs
@@ -0,0 +1,35 @@
using Scissors.Xaf.CacheWarmup.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Scissors.Xaf.CacheWarmup.Generators.Cli
{
class Program
{
static void Main(string[] args)
{
var finder = new AttributeFinder();
var assemblyPath = @"C:\F\github\how-to-precache-an-xaf-winforms-application\src\how_to_precache_an_xaf_winforms_application.Win\bin\Debug\how_to_precache_an_xaf_winforms_application.Win.exe";
var foundType = finder.FindAttribute(assemblyPath);

Console.WriteLine(foundType);

if(foundType != null)
{
var cacheGenerator = new CacheWarmupGenerator();

var cacheResult = cacheGenerator.WarmupCache(assemblyPath, foundType);
if(cacheResult != null)
{

}
}

Console.WriteLine("Done");
Console.ReadLine();
}
}
}
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net452</TargetFramework>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<AssemblyTitle>Scissors.Xaf.CacheWarmup.Generators.Cli</AssemblyTitle>
<Product>Scissors.Xaf.CacheWarmup.Generators.Cli</Product>
<Copyright>Copyright © Manuel Grundner 2019</Copyright>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Scissors.Xaf.CacheWarmup.Attributes\Scissors.Xaf.CacheWarmup.Attributes.csproj" />
<ProjectReference Include="..\Scissors.Xaf.CacheWarmup.Generators\Scissors.Xaf.CacheWarmup.Generators.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net452</TargetFramework>
<AssemblyTitle>Scissors.Xaf.CacheWarmup.Generators.MsBuild</AssemblyTitle>
<Product>Scissors.Xaf.CacheWarmup.Generators.MsBuild</Product>
<Copyright>Copyright © Manuel Grundner 2019</Copyright>
</PropertyGroup>
<ItemGroup>
<Content Include="Test.proj">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Scissors.Xaf.CacheWarmup.Generators\Scissors.Xaf.CacheWarmup.Generators.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.Build.Framework" />
<Reference Include="Microsoft.Build.Utilities.v4.0" />
</ItemGroup>
</Project>
8 changes: 8 additions & 0 deletions src/Scissors.Xaf.CacheWarmup.Generators.MsBuild/Test.proj
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultsTarget="Debug" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<UsingTask TaskName="Scissors.Xaf.CacheWarmup.Generators.MsBuild.XafCacheWarmupTask" AssemblyFile="Scissors.Xaf.CacheWarmup.Generators.MsBuild.dll" />
<Target Name="Debug">
<XafCacheWarmupTask ApplicationPath="C:\F\github\how-to-precache-an-xaf-winforms-application\src\how_to_precache_an_xaf_winforms_application.Win\bin\Debug\how_to_precache_an_xaf_winforms_application.Win.exe"/>
</Target>
</Project>
@@ -0,0 +1,40 @@
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Scissors.Xaf.CacheWarmup.Generators.MsBuild
{
public class XafCacheWarmupTask : Task
{
[Required]
public string ApplicationPath { get; set; }

public override bool Execute()
{
Console.WriteLine($"ApplicationPath: {ApplicationPath}");

var finder = new AttributeFinder();
var assemblyPath = ApplicationPath;
var foundType = finder.FindAttribute(assemblyPath);

Console.WriteLine(foundType);

if (foundType != null)
{
var cacheGenerator = new CacheWarmupGenerator();

var cacheResult = cacheGenerator.WarmupCache(assemblyPath, foundType);
if (cacheResult != null)
{
Console.WriteLine("Done");
return true;
}
}

return false;
}
}
}
47 changes: 47 additions & 0 deletions src/Scissors.Xaf.CacheWarmup.Generators/AttributeFinder.cs
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AppDomainToolkit;
using Scissors.Xaf.CacheWarmup.Attributes;
using static System.Console;

namespace Scissors.Xaf.CacheWarmup.Generators
{
public class AttributeFinder
{
public string FindAttribute(string assemblyPath)
{
using (var context = AppDomainContext.Create(new AppDomainSetup
{
ApplicationBase = Path.GetDirectoryName(typeof(AttributeFinder).Assembly.Location),
PrivateBinPath = Path.GetDirectoryName(assemblyPath),
}))
{
context.LoadAssemblyWithReferences(LoadMethod.LoadFile, assemblyPath);
return RemoteFunc.Invoke(context.Domain, new AttributeFinderRequest { AssemblyPath = assemblyPath, AttributeType = typeof(XafCacheWarmupAttribute) }, (args) =>
{
WriteLine($"Try to find {nameof(XafCacheWarmupAttribute)} in {args.AssemblyPath}");
var assembly = AppDomain.CurrentDomain.GetAssemblies().First(b => b.Location == args.AssemblyPath);

var attribute = assembly.GetCustomAttributes(false).OfType<XafCacheWarmupAttribute>().FirstOrDefault();
if(attribute != null)
{
WriteLine($"Found {nameof(XafCacheWarmupAttribute)} with '{attribute.XafApplicationType.FullName}'");
return attribute.XafApplicationType.FullName;
}
return null;
});
}
}

[Serializable]
public class AttributeFinderRequest
{
public string AssemblyPath { get; set; }
public Type AttributeType { get; set; }
}
}
}