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

DotNetCoreProjectTemplateWizard.cs « MonoDevelop.DotNetCore.Templating « MonoDevelop.DotNetCore « addins « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 18674cffe0add869a3cdda3169a054c0e4ebf3db (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//
// DotNetCoreProjectTemplateWizard.cs
//
// Author:
//       Matt Ward <matt.ward@xamarin.com>
//
// Copyright (c) 2016 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.Collections.Generic;
using System.Linq;
using MonoDevelop.Core.Assemblies;
using MonoDevelop.Ide.Templates;

namespace MonoDevelop.DotNetCore.Templating
{
	class DotNetCoreProjectTemplateWizard : TemplateWizard
	{
		const string defaultParameterNetCore30 = "UseNetCore30";
		const string defaultParameterNetCore20 = "UseNetCore20";
		const string defaultParameterNetCore1x = "UseNetCore1x";

		List<TargetFramework> targetFrameworks;

		public override WizardPage GetPage (int pageNumber)
		{
			var page = new DotNetCoreProjectTemplateWizardPage (this, targetFrameworks, SupportedAuthentications);
			targetFrameworks = null;
			return page;
		}

		public override int TotalPages => GetTotalPages ();

		public override string Id => "MonoDevelop.DotNetCore.ProjectTemplateWizard";

		internal IList<TargetFramework> TargetFrameworks => targetFrameworks;

		internal IReadOnlyList<AuthenticationParameter> SupportedAuthentications { get; private set; }

		/// <summary>
		/// When only .NET Core 2.0 is installed there is only one option in the drop down
		/// list for the target framework for .NET Core projects so there is no point in displaying
		/// the wizard since nothing can be changed. If .NET Core 1.0 is installed then there is at
		/// least two options available. If the .NET Standard project template is selected then there
		/// are multiple options available. So here a check is made to see if more than one target
		/// framework is available. If not then the wizard will not be displayed.
		/// </summary>
		int GetTotalPages ()
		{
			GetSupportedAuthentications ();
			GetTargetFrameworks ();
			if (targetFrameworks.Count > 1 || SupportedAuthentications.Any ())
				return 1;

			ConfigureDefaultParameters ();

			return 0;
		}

		void GetSupportedAuthentications ()
		{
			var templateId = Parameters ["TemplateId"];
			SupportedAuthentications = DotNetCoreProjectTemplateParameters.GetAuthenticationParameters (templateId);
		}

		void GetTargetFrameworks ()
		{
			if (IsSupportedParameter ("NetStandard")) {
				targetFrameworks = DotNetCoreProjectSupportedTargetFrameworks.GetNetStandardTargetFrameworks ().ToList ();

				// Use 1.x target frameworks by default if none are available from the .NET Core sdk.
				if (!targetFrameworks.Any ())
					targetFrameworks = DotNetCoreProjectSupportedTargetFrameworks.GetDefaultNetStandard1xTargetFrameworks ().ToList ();

				if (IsSupportedParameter ("FSharpNetStandard")) {
					RemoveUnsupportedNetStandardTargetFrameworksForFSharp (targetFrameworks);
				}
			} else {
				targetFrameworks = DotNetCoreProjectSupportedTargetFrameworks.GetNetCoreAppTargetFrameworksWithSdkSupport ().ToList ();

				RemoveUnsupportedNetCoreAppTargetFrameworks (targetFrameworks);

				if (!SupportsNetCore2x ()) {
					RemoveUnsupportedNetCoreApp2xTargetFrameworks (targetFrameworks);
				}
				if (!SupportsNetCore1x ()) {
					RemoveUnsupportedNetCoreApp1xTargetFrameworks (targetFrameworks);
				}
			}
		}

		/// <summary>
		/// The F# project templates do not support .NET Standard below 1.6 so do not allow them to
		/// be selected.
		/// </summary>
		static void RemoveUnsupportedNetStandardTargetFrameworksForFSharp (List<TargetFramework> targetFrameworks)
		{
			targetFrameworks.RemoveAll (framework => framework.IsLowerThanNetStandard16 ());
		}

		static void RemoveUnsupportedNetCoreAppTargetFrameworks (List<TargetFramework> targetFrameworks)
		{
			targetFrameworks.RemoveAll (framework => framework.IsNetCoreAppOrHigher (DotNetCoreSdk.DotNetCoreUnsupportedTargetFrameworkVersion));
		}

		/// <summary>
		/// FSharp class library project template and the Razor Pages project template do not support
		/// targeting 1.x versions so remove these frameworks.
		/// </summary>
		static void RemoveUnsupportedNetCoreApp1xTargetFrameworks (List<TargetFramework> targetFrameworks)
		{
			targetFrameworks.RemoveAll (framework => framework.IsNetCoreApp1x ());
		}

		/// <summary>
		/// Some templates (Blazor, Worker) are only available on 3.0.
		/// </summary>
		/// <param name="targetFrameworks"></param>
		static void RemoveUnsupportedNetCoreApp2xTargetFrameworks (List<TargetFramework> targetFrameworks)
		{
			targetFrameworks.RemoveAll (framework => !framework.IsNetCoreAppOrHigher (DotNetCoreVersion.Parse ("3.0")));
		}

		/// <summary>
		/// Set default parameter values if no wizard will be displayed.
		/// </summary>
		void ConfigureDefaultParameters ()
		{
			if (IsSupportedParameter ("NetStandard")) {
				var highestFramework = DotNetCoreProjectSupportedTargetFrameworks.GetNetStandardTargetFrameworks ().FirstOrDefault ();

				var parameter = highestFramework.GetParameterName ();
				if (!string.IsNullOrEmpty (parameter))
					Parameters [parameter] = "true";
			} else {
				if (!SupportsNetCore1x ()) {
					var highestFramework = DotNetCoreProjectSupportedTargetFrameworks.GetNetCoreAppTargetFrameworksWithSdkSupport ().FirstOrDefault ();
					if (highestFramework != null && highestFramework.IsNetCoreAppOrHigher (DotNetCoreVersion.Parse ("2.0"))) {
						var parameter = highestFramework.GetParameterName ();
						if (!string.IsNullOrEmpty (parameter))
							Parameters [parameter] = "true";
					} else {
						Parameters [defaultParameterNetCore20] = "true";
					}
				} else if (!SupportsNetCore2x ()) {
					var highestFramework = DotNetCoreProjectSupportedTargetFrameworks.GetNetCoreAppTargetFrameworksWithSdkSupport ().FirstOrDefault ();
					if (highestFramework != null && highestFramework.IsNetCoreAppOrHigher (DotNetCoreVersion.Parse ("3.0"))) {
						var parameter = highestFramework.GetParameterName ();
						if (!string.IsNullOrEmpty (parameter))
							Parameters [parameter] = "true";
					} else {
						Parameters [defaultParameterNetCore30] = "true";
					}
				} else {
					var highestFramework = DotNetCoreProjectSupportedTargetFrameworks.GetNetCoreAppTargetFrameworksWithSdkSupport ().FirstOrDefault ();
					if (highestFramework != null) {
						var parameter = highestFramework.GetParameterName ();
						if (!string.IsNullOrEmpty (parameter))
							Parameters [parameter] = "true";
					} else {
						Parameters [defaultParameterNetCore1x] = "true";
					}
				}
				ConfigureDefaultNetCoreAppFramework ();
			}
		}

		/// <summary>
		/// Framework needs to be specified for .NET Core library projects if only one runtime/sdk
		/// is available. Otherwise .NETStandard will be used for the target framework of the project.
		/// </summary>
		void ConfigureDefaultNetCoreAppFramework ()
		{
			if (!IsSupportedParameter ("NetCoreLibrary"))
				return;

			var highestFramework = DotNetCoreProjectSupportedTargetFrameworks.GetNetCoreAppTargetFrameworksWithSdkSupport ().FirstOrDefault ();
			if (highestFramework != null) {
				Parameters ["framework"] = highestFramework.Id.ShortName;
			} else {
				Parameters ["framework"] = "netcoreapp1.1";
			}
		}

		bool SupportsNetCore2x ()
		{
			bool supportsNetCore30Only = IsSupportedParameter ("AspNetCoreBlazor") ||
				IsSupportedParameter ("AspNetCoreWorker");

			return !supportsNetCore30Only;
		}

		bool SupportsNetCore1x ()
		{
			bool supportsNetCore20Only = IsSupportedParameter ("FSharpNetCoreLibrary") ||
				IsSupportedParameter ("RazorPages") ||
				IsSupportedParameter ("FSharpWebApi") ||
				IsSupportedParameter ("AspNetCoreBlazor") ||
				IsSupportedParameter ("AspNetCoreWorker");

			return !supportsNetCore20Only;
		}
	}
}