Skip to content

Commit

Permalink
trying to build a netcore console app
Browse files Browse the repository at this point in the history
  • Loading branch information
m0sa committed Apr 6, 2018
1 parent 5fcc364 commit faa96e0
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 22 deletions.
23 changes: 23 additions & 0 deletions StackExchange.Precompilation.Build/CompilationAssemblyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,29 @@ void Resolve(AssemblyName name, Func<Assembly> loader)
resolvedAssemblies.AddOrUpdate(keyName.Name, resolved, (key, existing) => existing); // TODO log conflicting partial binds?
}

// load the embedded compile-time references, we're gonna need them for sure
const string prefix = "embedded_ref://";
var thisAssembly = typeof(CompilationAssemblyResolver).Assembly;
thisAssembly.GetManifestResourceNames()
.AsParallel()
.Where(x => x.StartsWith(prefix))
.ForAll(resourceKey =>
{
using (var resource = thisAssembly.GetManifestResourceStream(resourceKey))
using (var ms = new MemoryStream())
{
if (resource != null)
{
resource.CopyTo(ms);
var rawAssembly = ms.ToArray();
var assembly = Assembly.Load(rawAssembly);
var name = assembly.GetName();
name.CodeBase = resourceKey;
Resolve(name, () => assembly);
}
}
});

// load all the other references
references
.AsParallel()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,25 @@
<None Include="*.targets" Pack="true" PackagePath="build" />
<None Include="App.config" Pack="true" PackagePath="tools\$(AssemblyName).exe.config" />
</ItemGroup>

<!--
Embed net462 specific compile(-run)-time references into the assembly.
This becomes a tool package, and the tool can run when compiling/targeting netstandard, at which point
we'd be missing references for the desktop specific workspace stuff (e.g CommandLineProject etc...) required
for running the tool.
-->
<Target Name="EmbedReferences" AfterTargets="ResolveAssemblyReferences">
<ItemGroup>
<EmbeddedReference Include="Microsoft.CodeAnalysis.Workspaces.Desktop" />
<!-- ... with a little bit of msbuild black magic -->
<_ReferencePath Include="@(Reference->'%(HintPath)')" Condition="'%(Reference.HintPath)' != ''" />
<_Reference_X_Embedded Include="@(_ReferencePath)">
<_Target>%(EmbeddedReference.Identity)</_Target>
</_Reference_X_Embedded>
<EmbeddedResource Include="%(_Reference_X_Embedded.Identity)" Condition="'%(_Reference_X_Embedded.FileName)' == '%(_Reference_X_Embedded._Target)'">
<LogicalName>embedded_ref://%(_Reference_X_Embedded._Target)</LogicalName>
</EmbeddedResource>
</ItemGroup>
<Message Text="Adding EmbeddedResource: %(EmbeddedResource.LogicalName) -&gt; %(EmbeddedResource.Identity)" />
</Target>
</Project>
4 changes: 1 addition & 3 deletions StackExchange.Precompilation.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test.Module", "Test.Module\Test.Module.csproj", "{5FCAECC3-787B-473F-A372-783D0C235190}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test.ConsoleApp", "Test.ConsoleApp\Test.ConsoleApp.csproj", "{BA716DA2-4E3C-4D9F-B9C2-78C0EAEF66D7}"
ProjectSection(ProjectDependencies) = postProject
{31DFCCCC-2F44-405E-A2D7-BB1AC718E7B9} = {31DFCCCC-2F44-405E-A2D7-BB1AC718E7B9}
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test.WebApp", "Test.WebApp\Test.WebApp.csproj", "{5B0105A4-256B-4A88-852C-6F5E9D185515}"
ProjectSection(ProjectDependencies) = postProject
Expand All @@ -37,6 +34,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StackExchange.Precompilatio
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test.WebApp.ExternalViews", "Test.WebApp.ExternalViews\Test.WebApp.ExternalViews.csproj", "{2BA24772-F7B0-4652-A430-2F4C2262E882}"
ProjectSection(ProjectDependencies) = postProject
{5FCAECC3-787B-473F-A372-783D0C235190} = {5FCAECC3-787B-473F-A372-783D0C235190}
{31DFCCCC-2F44-405E-A2D7-BB1AC718E7B9} = {31DFCCCC-2F44-405E-A2D7-BB1AC718E7B9}
EndProjectSection
EndProject
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net462</TargetFramework>
<TargetFrameworks>net462;netstandard20</TargetFrameworks>
<Description>Hooks into the ASP.NET MVC pipeline to enable usage of C# 7.2, and views precompiled with StackExchange.Precompilation.Build</Description>
<IsPackable>true</IsPackable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down
11 changes: 11 additions & 0 deletions Test.ConsoleApp/AliasTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#if NET462
extern alias aliastest;

namespace Test.ConsoleApp
{
class AliasTest
{
public const string DataSet = nameof(aliastest::System.Data.DataSet);
}
}
#endif
15 changes: 5 additions & 10 deletions Test.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
extern alias aliastest;

using System;
using System.Collections.Generic;
using System.Linq;
using System;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Test.Module;

namespace Test.ConsoleApp
Expand All @@ -14,9 +8,10 @@ class Program
{
static void Main(string[] args)
{
Console.WriteLine(PathMapTest());
Console.WriteLine(nameof(aliastest::System.Data.DataSet));
Console.ReadLine().Dump();
Console.WriteLine(PathMapTest().Dump());
#if NET462
Console.WriteLine(typeof(AliasTest).FullName);
#endif
}

// path mapping test, configured via <PathMap> property in the .csproj
Expand Down
17 changes: 10 additions & 7 deletions Test.ConsoleApp/Test.ConsoleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,25 @@
<PropertyGroup>
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
<OutputType>Exe</OutputType>
<TargetFramework>net462</TargetFramework>
<SEPrecompilerPath Condition="'$(SEPrecompilerPath)' == ''">$(SolutionDir)StackExchange.Precompilation.Build\obj\$(Configuration)\$(TargetFramework)\</SEPrecompilerPath>
<TargetFrameworks>net462;netcoreapp20</TargetFrameworks>
<RuntimeIdentifiers>win10-x64</RuntimeIdentifiers>
<SEPrecompilerPath Condition="'$(SEPrecompilerPath)' == ''">$(SolutionDir)StackExchange.Precompilation.Build\obj\$(Configuration)\net462\</SEPrecompilerPath>
<PathMap>$(SolutionDir)\=https://example.org/</PathMap>
<DebugType>portable</DebugType>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\StackExchange.Precompilation.Build\StackExchange.Precompilation.Build.csproj">
<Project>{31dfcccc-2f44-405e-a2d7-bb1ac718e7b9}</Project>
<Name>StackExchange.Precompilation.Build</Name>
</ProjectReference>
<ProjectReference Include="..\Test.Module\Test.Module.csproj">
<Project>{5fcaecc3-787b-473f-a372-783d0c235190}</Project>
<Name>Test.Module</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Reference Include="System.Data" Aliases="aliastest" />
<Reference Include="System.Data" Aliases="aliastest" Condition="'$(TargetFramework)' == 'net462'" />

<!-- Usually, this would be pulled in by the reference to StackExchange.Precompilation.Build nuget,
but since we don't have the package yet, msbuild complains about the .csproj not targeting netstandard,
so we're pulling it's references in manually.
-->
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="2.7.0" IncludeAssets="compile" PrivateAssets="all" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion Test.Module/Test.Module.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net462</TargetFramework>
<TargetFrameworks>net462;netstandard20</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\StackExchange.Precompilation\StackExchange.Precompilation.csproj">
Expand Down

0 comments on commit faa96e0

Please sign in to comment.