Skip to content

Commit

Permalink
support for mono, VS15.0, new csproj #155
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Dec 22, 2017
1 parent fbc2fde commit 252e6db
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/MessagePack.CodeGenerator/MessagePack.CodeGenerator.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\..\packages\Microsoft.Build.MSBuildLocator.1.0.1-preview-g6cd9a57801\build\Microsoft.Build.MSBuildLocator.props" Condition="Exists('..\..\packages\Microsoft.Build.MSBuildLocator.1.0.1-preview-g6cd9a57801\build\Microsoft.Build.MSBuildLocator.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand All @@ -11,6 +12,8 @@
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -35,6 +38,21 @@
<Reference Include="Esent.Interop, Version=1.9.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\ManagedEsent.1.9.4\lib\net40\Esent.Interop.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Build.Framework, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Build.Framework.15.6.0-preview-000022-1216653\lib\net46\Microsoft.Build.Framework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Build.MSBuildLocator, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9dff12846e04bfbd, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Build.MSBuildLocator.1.0.1-preview-g6cd9a57801\lib\net46\Microsoft.Build.MSBuildLocator.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Build.Tasks.Core, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Build.Tasks.Core.15.6.0-preview-000022-1216653\lib\net46\Microsoft.Build.Tasks.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Build.Utilities.Core, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Build.Utilities.Core.15.6.0-preview-000022-1216653\lib\net46\Microsoft.Build.Utilities.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.CodeAnalysis, Version=2.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.CodeAnalysis.Common.2.6.1\lib\netstandard1.3\Microsoft.CodeAnalysis.dll</HintPath>
</Reference>
Expand All @@ -53,6 +71,12 @@
<Reference Include="Microsoft.CodeAnalysis.Workspaces.Desktop, Version=2.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.CodeAnalysis.Workspaces.Common.2.6.1\lib\net46\Microsoft.CodeAnalysis.Workspaces.Desktop.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.RemoteControl, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.VisualStudio.RemoteControl.14.0.262-masterA5CACE98\lib\net45\Microsoft.VisualStudio.RemoteControl.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.Utilities.Internal, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.VisualStudio.Utilities.Internal.14.0.75-master5839BBFB\lib\net45\Microsoft.VisualStudio.Utilities.Internal.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.AppContext, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll</HintPath>
Expand Down Expand Up @@ -100,6 +124,10 @@
<Reference Include="System.Reflection.Metadata, Version=1.4.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Reflection.Metadata.1.4.2\lib\portable-net45+win8\System.Reflection.Metadata.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.InteropServices.RuntimeInformation, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Security.Cryptography.Algorithms, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Security.Cryptography.Algorithms.4.3.0\lib\net461\System.Security.Cryptography.Algorithms.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -167,10 +195,12 @@
<Compile Include="Mono.Options\Options.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utils\EnvironmentHelper.cs" />
<Compile Include="Utils\RoslynExtensions.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="Nuget.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
Expand Down Expand Up @@ -199,4 +229,10 @@
</Content>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>このプロジェクトは、このコンピューター上にない NuGet パッケージを参照しています。それらのパッケージをダウンロードするには、[NuGet パッケージの復元] を使用します。詳細については、http://go.microsoft.com/fwlink/?LinkID=322105 を参照してください。見つからないファイルは {0} です。</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\..\packages\Microsoft.Build.MSBuildLocator.1.0.1-preview-g6cd9a57801\build\Microsoft.Build.MSBuildLocator.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Microsoft.Build.MSBuildLocator.1.0.1-preview-g6cd9a57801\build\Microsoft.Build.MSBuildLocator.props'))" />
</Target>
</Project>
13 changes: 13 additions & 0 deletions src/MessagePack.CodeGenerator/Nuget.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageRestore>
<add key="enabled" value="True" />
</packageRestore>
<packageSources>
<clear />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
<add key="nuget.org v2" value="https://nuget.org/api/v2/" />
<add key="myget.org roslyn nightly" value="https://dotnet.myget.org/F/roslyn/api/v3/index.json" />
<add key="myget.org msbuild nightly" value="https://dotnet.myget.org/F/msbuild/api/v3/index.json" />
</packageSources>
</configuration>
132 changes: 132 additions & 0 deletions src/MessagePack.CodeGenerator/Utils/EnvironmentHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.Build.MSBuildLocator;

namespace MessagePack.CodeGenerator
{
internal static class EnvironmentHelper
{
/// <summary>
/// Setup the environment in order MSBuild to work
/// </summary>
public static void Setup()
{
if (IsMono)
{
SetupMsBuildPath(() =>
{
return GetMonoMsBuildPath(monoDir =>
{
Environment.SetEnvironmentVariable("MSBuildExtensionsPath",
Path.Combine(monoDir, "xbuild"));
});
});
return;
}
var vsInstallDir = Environment.GetEnvironmentVariable("VSINSTALLDIR");
if (string.IsNullOrEmpty(vsInstallDir) || !Directory.Exists(vsInstallDir))
{
var instance = MSBuildLocator.QueryVisualStudioInstances()
.OrderByDescending(o => o.Version)
.FirstOrDefault();
if (instance != null)
{
MSBuildLocator.RegisterInstance(instance);
}
else
{
throw new InvalidOperationException(
"Visual Studio installation directory was not found. " +
"Install Visual Studio or set the environment variable VSINSTALLDIR");
}
Environment.SetEnvironmentVariable("VSINSTALLDIR", instance.VisualStudioRootPath);
}
}

public static bool IsMono => Type.GetType("Mono.Runtime") != null;

public static bool IsNetCore =>
RuntimeInformation.FrameworkDescription.StartsWith(".NET Core"); // .NET Core 4.6.00001.0

public static bool IsNetFramework =>
RuntimeInformation.FrameworkDescription.StartsWith(".NET Framework"); // .NET Framework 4.7.2115.0

// On Mono RuntimeInformation.IsOSPlatform will always retrun true for Windows
public static bool IsWindows => Path.DirectorySeparatorChar == '\\';

public static string GetConfigurationFilePath()
{
var name = AppDomain.CurrentDomain.FriendlyName;
// On .NET Core FriendlyName as only the assembly name without the extension
if (IsNetCore)
{
name += ".dll";
}
name += ".config";
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, name);
return File.Exists(path) ? path : null;
}

public static string GetNetCoreMsBuildPath()
{
// Get the sdk path by using the .NET Core runtime assembly location
// Default locations:
// Windows -> C:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.0.0\System.Private.CoreLib.dllz
// Linux -> /usr/share/dotnet/shared/Microsoft.NETCore.App/2.0.0/System.Private.CoreLib.dll
// OSX -> /usr/local/share/dotnet/shared/Microsoft.NETCore.App/2.0.0/System.Private.CoreLib.dll
// MSBuild.dll is then located:
// Windows -> C:\Program Files\dotnet\sdk\2.0.0\MSBuild.dll
// Linux -> /usr/share/dotnet/sdk/2.0.0/MSBuild.dll
// OSX -> /usr/local/share/dotnet/sdk/2.0.0/MSBuild.dll

var assembly = typeof(System.Runtime.GCSettings).Assembly;
var assemblyDirectory = Path.GetDirectoryName(assembly.Location);
var directoryInfo = new DirectoryInfo(assemblyDirectory);
var netCoreVersion = directoryInfo.Name; // e.g. 2.0.0
// Get the dotnet folder
var dotnetFolder = directoryInfo.Parent.Parent.Parent.FullName;
// MSBuild should be located at dotnet/sdk/{version}/MSBuild.dll
var msBuildPath = Path.Combine(dotnetFolder, "sdk", netCoreVersion, "MSBuild.dll");
return File.Exists(msBuildPath) ? msBuildPath : null;
}

public static string GetMonoMsBuildPath(Action<string> monoDirectoryAction = null)
{
// Get the sdk path by using the Mono runtime assembly location
// Default locations:
// Windows -> C:\Program Files (x86)\Mono\lib\mono\4.5\mscorlib.dll
// Linux -> /usr/lib/mono/4.5/mscorlib.dll
// OSX -> /Library/Frameworks/Mono.framework/Versions/5.2.0/lib/mono/4.5/mscorlib.dll
// MSBuild.dll is then located:
// Windows -> C:\Program Files (x86)\Mono\lib\mono\msbuild\15.0\bin\MSBuild.dll
// Linux -> /usr/lib/mono/msbuild/15.0/bin/MSBuild.dll
// OSX -> /Library/Frameworks/Mono.framework/Versions/5.2.0/lib/mono/msbuild/15.0/bin/MSBuild.dll

var assembly = typeof(System.Runtime.GCSettings).Assembly;
var assemblyDirectory = Path.GetDirectoryName(assembly.Location);
var directoryInfo = new DirectoryInfo(assemblyDirectory).Parent; // get mono directory
monoDirectoryAction?.Invoke(directoryInfo.FullName);
var msBuildPath = Path.Combine(directoryInfo.FullName, "msbuild", "15.0", "bin", "MSBuild.dll");
return File.Exists(msBuildPath) ? msBuildPath : null;
}

private static void SetupMsBuildPath(Func<string> getMsBuildPathFunc)
{
var msbuildPath = Environment.GetEnvironmentVariable("MSBUILD_EXE_PATH");
if (!string.IsNullOrEmpty(msbuildPath) && File.Exists(msbuildPath))
{
return;
}
msbuildPath = getMsBuildPathFunc();
if (msbuildPath == null)
{
throw new InvalidOperationException(
"Environment variable MSBUILD_EXE_PATH is not set or is set incorrectly. " +
"Please set MSBUILD_EXE_PATH to point at MSBuild.dll.");
}
Environment.SetEnvironmentVariable("MSBUILD_EXE_PATH", msbuildPath);
}
}
}
3 changes: 3 additions & 0 deletions src/MessagePack.CodeGenerator/Utils/RoslynExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public static async Task<Compilation> GetCompilationFromProject(string csprojPat
}
}

EnvironmentHelper.Setup();

var workspace = MSBuildWorkspace.Create();
workspace.WorkspaceFailed += Workspace_WorkspaceFailed;

Expand All @@ -66,6 +68,7 @@ public static async Task<Compilation> GetCompilationFromProject(string csprojPat

private static void Workspace_WorkspaceFailed(object sender, WorkspaceDiagnosticEventArgs e)
{
Console.WriteLine(e);
// throw new Exception(e.Diagnostic.ToString());
}

Expand Down
8 changes: 8 additions & 0 deletions src/MessagePack.CodeGenerator/packages.config
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="ManagedEsent" version="1.9.4" targetFramework="net461" />
<package id="Microsoft.Build.Framework" version="15.6.0-preview-000022-1216653" targetFramework="net461" />
<package id="Microsoft.Build.MSBuildLocator" version="1.0.1-preview-g6cd9a57801" targetFramework="net461" />
<package id="Microsoft.Build.Tasks.Core" version="15.6.0-preview-000022-1216653" targetFramework="net461" />
<package id="Microsoft.Build.Utilities.Core" version="15.6.0-preview-000022-1216653" targetFramework="net461" />
<package id="Microsoft.CodeAnalysis.Analyzers" version="1.1.0" targetFramework="net461" />
<package id="Microsoft.CodeAnalysis.Common" version="2.6.1" targetFramework="net461" />
<package id="Microsoft.CodeAnalysis.CSharp" version="2.6.1" targetFramework="net461" />
<package id="Microsoft.CodeAnalysis.CSharp.Workspaces" version="2.6.1" targetFramework="net461" />
<package id="Microsoft.CodeAnalysis.Elfie" version="0.10.6-rc2" targetFramework="net461" />
<package id="Microsoft.CodeAnalysis.Workspaces.Common" version="2.6.1" targetFramework="net461" />
<package id="Microsoft.Composition" version="1.0.30" targetFramework="net461" />
<package id="Microsoft.VisualStudio.RemoteControl" version="14.0.262-masterA5CACE98" targetFramework="net461" />
<package id="Microsoft.VisualStudio.Utilities.Internal" version="14.0.75-master5839BBFB" targetFramework="net461" />
<package id="System.AppContext" version="4.3.0" targetFramework="net461" />
<package id="System.Collections" version="4.3.0" targetFramework="net461" />
<package id="System.Collections.Concurrent" version="4.3.0" targetFramework="net461" />
Expand All @@ -30,12 +36,14 @@
<package id="System.IO.FileSystem.Primitives" version="4.3.0" targetFramework="net461" />
<package id="System.Linq" version="4.3.0" targetFramework="net461" />
<package id="System.Linq.Expressions" version="4.3.0" targetFramework="net461" />
<package id="System.ObjectModel" version="4.0.12" targetFramework="net461" />
<package id="System.Reflection" version="4.3.0" targetFramework="net461" />
<package id="System.Reflection.Metadata" version="1.4.2" targetFramework="net461" />
<package id="System.Resources.ResourceManager" version="4.3.0" targetFramework="net461" />
<package id="System.Runtime" version="4.3.0" targetFramework="net461" />
<package id="System.Runtime.Extensions" version="4.3.0" targetFramework="net461" />
<package id="System.Runtime.InteropServices" version="4.3.0" targetFramework="net461" />
<package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net461" />
<package id="System.Runtime.Numerics" version="4.3.0" targetFramework="net461" />
<package id="System.Security.Cryptography.Algorithms" version="4.3.0" targetFramework="net461" />
<package id="System.Security.Cryptography.Encoding" version="4.3.0" targetFramework="net461" />
Expand Down

0 comments on commit 252e6db

Please sign in to comment.