Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DotNetCoreSdk.cs « MonoDevelop.DotNetCore « MonoDevelop.DotNetCore « addins « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1eaaf19fc10613d80885ece84b59ca21a2e91516 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//
// DotNetCoreSdk.cs
//
// Author:
//       Matt Ward <matt.ward@xamarin.com>
//
// Copyright (c) 2017 Xamarin Inc. (http://xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Linq;
using MonoDevelop.Core;
using MonoDevelop.Core.Assemblies;

namespace MonoDevelop.DotNetCore
{
	public static class DotNetCoreSdk
	{
		static DotNetCoreSdk ()
		{
			var sdkPaths = new DotNetCoreSdkPaths ();
			sdkPaths.FindMSBuildSDKsPath ();

			MSBuildSDKsPath = sdkPaths.MSBuildSDKsPath;
			IsInstalled = !string.IsNullOrEmpty (MSBuildSDKsPath);
			Versions = sdkPaths.SdkVersions ?? new DotNetCoreVersion [0];

			if (!IsInstalled)
				LoggingService.LogInfo (".NET Core SDK not found.");

			if (IsInstalled)
				SetFSharpShims ();
		}

		public static bool IsInstalled { get; private set; }
		public static string MSBuildSDKsPath { get; private set; }

		internal static DotNetCoreVersion[] Versions { get; private set; }

		internal static void EnsureInitialized ()
		{
		}

		internal static DotNetCoreSdkPaths FindSdkPaths (string sdk)
		{
			var sdkPaths = new DotNetCoreSdkPaths ();
			sdkPaths.MSBuildSDKsPath = MSBuildSDKsPath;
			sdkPaths.FindSdkPaths (sdk);

			return sdkPaths;
		}

		/// <summary>
		/// Checks that the target framework (e.g. .NETCoreApp1.1 or .NETStandard2.0) is supported
		/// by the installed SDKs. Takes into account Mono having .NET Core v1 SDKs installed.
		/// </summary>
		internal static bool IsSupported (TargetFramework framework)
		{
			return IsSupported (framework.Id, Versions, MSBuildSdks.Installed);
		}

		/// <summary>
		/// Used by unit tests.
		/// </summary>
		internal static bool IsSupported (
			TargetFrameworkMoniker projectFramework,
			DotNetCoreVersion[] versions,
			bool msbuildSdksInstalled)
		{
			if (!projectFramework.IsNetStandardOrNetCoreApp ())
				return false;

			var projectFrameworkVersion = Version.Parse (projectFramework.Version);

			if (versions.Any (sdkVersion => IsSupported (projectFrameworkVersion, sdkVersion)))
				return true;

			// .NET Core 1.x is supported by the MSBuild .NET Core SDKs if they are installed with Mono.
			if (projectFrameworkVersion.Major == 1)
				return msbuildSdksInstalled;

			return false;
		}

		/// <summary>
		/// Project framework version is considered supported if the major version of the
		/// .NET Core SDK is greater or equal to the major version of the project framework.
		/// The fact that a .NET Core SDK is a preview version is ignored in this check.
		///
		/// .NET Core SDK 1.0.4 supports .NET Core 1.0 and 1.1
		/// .NET Core SDK 1.0.4 supports .NET Standard 1.0 to 1.6
		/// .NET Core SDK 2.0 supports 1.0, 1.1 and 2.0
		/// .NET Core SDK 2.0 supports .NET Standard 1.0 to 1.6 and 2.0
		/// .NET Core SDK 2.1 supports 1.0, 1.1 and 2.0
		/// .NET Core SDK 2.1 supports .NET Standard 1.0 to 1.6 and 2.0
		/// </summary>
		static bool IsSupported (Version projectFrameworkVersion, DotNetCoreVersion sdkVersion)
		{
			return sdkVersion.Major >= projectFrameworkVersion.Major;
		}

		/// <summary>
		/// This is a workaround to allow F# .NET Core 2.0 projects to be evaluated properly and compile
		/// without any errors. The better solution would be to ship the new Microsoft.FSharp.NetSdk.props
		/// and .targets files with Mono so this workaround can be removed. Setting the FSharpPropsShim
		/// and FSharpTargetsShim as environment variables allows the correct MSBuild imports to be used
		/// when building and evaluating. Just setting a global MSBuild property would fix the Build target
		/// not being found but the MSBuild project evaluation would not add the FSharp.Core PackageReference
		/// to the project.assets.json file, also all .fs files were treated as None items instead of
		/// Compile items.
		/// </summary>
		static void SetFSharpShims ()
		{
			var latestVersion = Versions.FirstOrDefault ();
			if (latestVersion != null && latestVersion.Major == 2) {
				FilePath directory = FilePath.Build (MSBuildSDKsPath, "..", "FSharp");

				Environment.SetEnvironmentVariable ("FSharpPropsShim", directory.Combine ("Microsoft.FSharp.NetSdk.props").FullPath);
				Environment.SetEnvironmentVariable ("FSharpTargetsShim", directory.Combine ("Microsoft.FSharp.NetSdk.targets").FullPath);
			}
		}
	}
}