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

ProjectDescriptor.cs « MonoDevelop.Ide.Templates « MonoDevelop.Ide « core « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 78a24d98b35d9e5118fb53f78d27fddffee86a19 (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// ProjectDescriptor.cs
//
// Author:
//   Viktoria Dudka (viktoriad@remobjects.com)
//
// Copyright (c) 2009 RemObjects Software
//
// 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 MonoDevelop.Core;
using MonoDevelop.Core.Assemblies;
using MonoDevelop.Projects;
using System.IO;
using System.Collections.Generic;
using System.Xml;
using System.Linq;
using MonoDevelop.Projects.MSBuild;


namespace MonoDevelop.Ide.Templates
{
	internal class ProjectDescriptor : ISolutionItemDescriptor, ICustomProjectCIEntry
	{
		private string name;
		private string type;
		private string directory;
		private string createCondition;

		private List<FileDescriptionTemplate> files = new List<FileDescriptionTemplate> ();
		private List<SingleFileDescriptionTemplate> resources = new List<SingleFileDescriptionTemplate> ();
		private List<ProjectReferenceDescription> references = new List<ProjectReferenceDescription> ();

		private XmlElement projectOptions = null;
		private List<ProjectTemplatePackageReference> packageReferences = new List<ProjectTemplatePackageReference> ();

		protected ProjectDescriptor ()
		{
		}

		public static ProjectDescriptor CreateProjectDescriptor (XmlElement xmlElement, FilePath baseDirectory)
		{
			ProjectDescriptor projectDescriptor = new ProjectDescriptor ();

			projectDescriptor.name = xmlElement.GetAttribute ("name");
			projectDescriptor.directory = xmlElement.GetAttribute ("directory");
			projectDescriptor.createCondition = xmlElement.GetAttribute ("if");

			projectDescriptor.type = xmlElement.GetAttribute ("type");

			if (xmlElement ["Files"] != null) {
				foreach (XmlNode xmlNode in xmlElement["Files"].ChildNodes)
					if (xmlNode is XmlElement)
						projectDescriptor.files.Add (
							FileDescriptionTemplate.CreateTemplate ((XmlElement)xmlNode, baseDirectory));
			}

			if (xmlElement ["Resources"] != null) {
				foreach (XmlNode xmlNode in xmlElement["Resources"].ChildNodes) {
					if (xmlNode is XmlElement) {
						var fileTemplate = FileDescriptionTemplate.CreateTemplate ((XmlElement)xmlNode, baseDirectory);
						if (fileTemplate is SingleFileDescriptionTemplate)
							projectDescriptor.resources.Add ((SingleFileDescriptionTemplate)fileTemplate);
						else
							MessageService.ShowError (GettextCatalog.GetString ("Only single-file templates allowed to generate resource files"));
					}

				}
			}

			if (xmlElement ["References"] != null) {
				foreach (XmlNode xmlNode in xmlElement["References"].ChildNodes) {
					projectDescriptor.references.Add (new ProjectReferenceDescription ((XmlElement) xmlNode));
				}
			}

			projectDescriptor.projectOptions = xmlElement ["Options"];
			if (projectDescriptor.projectOptions == null)
				projectDescriptor.projectOptions = xmlElement.OwnerDocument.CreateElement ("Options");

			if (xmlElement ["Packages"] != null) {
				foreach (XmlNode xmlNode in xmlElement["Packages"].ChildNodes) {
					if (xmlNode is XmlElement) {
						var packageReference = ProjectTemplatePackageReference.Create ((XmlElement)xmlNode);
						projectDescriptor.packageReferences.Add (packageReference);
					}
				}
			}

			return projectDescriptor;
		}

		public SolutionItem CreateItem (ProjectCreateInformation projectCreateInformation, string defaultLanguage)
		{
			if (string.IsNullOrEmpty (projectOptions.GetAttribute ("language")) && !string.IsNullOrEmpty (defaultLanguage))
				projectOptions.SetAttribute ("language", defaultLanguage);

			var lang = projectOptions.GetAttribute ("language");
			var splitType = !string.IsNullOrEmpty (type) ? type.Split (new char [] {','}, StringSplitOptions.RemoveEmptyEntries).Select (t => t.Trim()).ToArray() : null;
			var projectTypes = splitType != null ? splitType : new string[] {lang};

			var projectType = projectTypes [0];

			string[] flavors;

			if (!Services.ProjectService.CanCreateSolutionItem (projectType, projectCreateInformation, projectOptions) && projectType != lang && !string.IsNullOrEmpty (lang)) {
				// Maybe the type of the template is just a flavor id. In that case try using the language as project type.
				projectType = lang;
				flavors = splitType ?? new string[0];
			} else
				flavors = projectTypes.Skip (1).ToArray ();

			if (!Services.ProjectService.CanCreateSolutionItem (projectType, projectCreateInformation, projectOptions)) {
				LoggingService.LogError ("Could not create project of type '" + string.Join (",", projectTypes) + "'. Project skipped");
				return null;
			}

			if (!ShouldCreateProject (projectCreateInformation))
				return null;

			Project project = Services.ProjectService.CreateProject (projectType, projectCreateInformation, projectOptions, flavors);
			return project;
		}

		public void InitializeItem (SolutionFolderItem policyParent, ProjectCreateInformation projectCreateInformation, string defaultLanguage, SolutionItem item)
		{
			MonoDevelop.Projects.Project project = item as MonoDevelop.Projects.Project;

			if (project == null) {
				MessageService.ShowError (GettextCatalog.GetString ("Can't create project with type: {0}", type));
				return;
			}

			// Set the file before setting the name, to make sure the file extension is kept
			project.FileName = Path.Combine (projectCreateInformation.ProjectBasePath, projectCreateInformation.ProjectName);
			project.Name = projectCreateInformation.ProjectName;

			var dnp = project as DotNetProject;
			if (dnp != null) {
				if (policyParent.ParentSolution != null && !policyParent.ParentSolution.FileFormat.SupportsFramework (dnp.TargetFramework)) {
					SetClosestSupportedTargetFramework (policyParent.ParentSolution.FileFormat, dnp);
				}
				var substitution = new string[,] { { "ProjectName", GetProjectNameForSubstitution (projectCreateInformation) } };
				foreach (var desc in references) {
					if (!projectCreateInformation.ShouldCreate (desc.CreateCondition))
						continue;
					var pr = desc.Create ();
					if (pr.ReferenceType == ReferenceType.Project) {
						string referencedProjectName = ReplaceParameters (pr.Reference, substitution, projectCreateInformation);
						var parsedReference = ProjectReference.RenameReference (pr, referencedProjectName);
						dnp.References.Add (parsedReference);
					} else
						dnp.References.Add (pr);
				}
			}

			foreach (SingleFileDescriptionTemplate resourceTemplate in resources) {
				try {
					if (!projectCreateInformation.ShouldCreate (resourceTemplate.CreateCondition))
						continue;
					var projectFile = new ProjectFile (resourceTemplate.SaveFile (policyParent, project, defaultLanguage, project.BaseDirectory, null));
					projectFile.BuildAction = BuildAction.EmbeddedResource;
					project.Files.Add (projectFile);
				} catch (Exception ex) {
					if (!IdeApp.IsInitialized)
						throw;
					MessageService.ShowError (GettextCatalog.GetString ("File {0} could not be written.", resourceTemplate.Name), ex);
				}
			}

			foreach (FileDescriptionTemplate fileTemplate in files) {
				try {
					if (!projectCreateInformation.ShouldCreate (fileTemplate.CreateCondition))
						continue;
					fileTemplate.SetProjectTagModel (projectCreateInformation.Parameters);
					fileTemplate.AddToProject (policyParent, project, defaultLanguage, project.BaseDirectory, null);
				} catch (Exception ex) {
					if (!IdeApp.IsInitialized)
						throw;
					MessageService.ShowError (GettextCatalog.GetString ("File {0} could not be written.", fileTemplate.Name), ex);
				} finally {
					fileTemplate.SetProjectTagModel (null);
				}
			}
		}

		static string GetProjectNameForSubstitution (ProjectCreateInformation projectCreateInformation)
		{
			var templateInformation = projectCreateInformation as ProjectTemplateCreateInformation;
			if (templateInformation != null) {
				return templateInformation.UserDefinedProjectName;
			}
			return projectCreateInformation.SolutionName;
		}

		static string ReplaceParameters (string input, string[,] substitution, ProjectCreateInformation projectCreateInformation)
		{
			string updatedText = StringParserService.Parse (input, substitution);
			return StringParserService.Parse (updatedText, projectCreateInformation.Parameters);
		}
		
		static void SetClosestSupportedTargetFramework (MSBuildFileFormat format, DotNetProject project)
		{
			// If the solution format can't write this project due to an unsupported framework, try finding the
			// closest valid framework. DOn't worry about whether it's installed, that's up to the user to correct.
			TargetFramework curFx = project.TargetFramework;
			var candidates = Runtime.SystemAssemblyService.GetTargetFrameworks ()
				.Where (fx =>
					//only frameworks with the same ID, else version comparisons are meaningless
					fx.Id.Identifier == curFx.Id.Identifier &&
					//don't consider profiles, only full frameworks
					fx.Id.Profile == null &&
					//and the project and format must support the framework
					project.SupportsFramework (fx) && format.SupportsFramework (fx))
					//FIXME: string comparisons aren't a valid way to compare profiles, but it works w/released .NET versions
				.OrderBy (fx => fx.Id.Version)
				.ToList ();

			TargetFramework newFx =
				candidates.FirstOrDefault (fx => string.CompareOrdinal (fx.Id.Version, curFx.Id.Version) > 0)
				 ?? candidates.LastOrDefault ();

			if (newFx != null)
				project.TargetFramework = newFx;
		}

		public ProjectCreateInformation CreateProjectCI (ProjectCreateInformation projectCI)
		{
			var projectCreateInformation = new ProjectCreateInformation (projectCI);
			var substitution = new string[,] { { "ProjectName", projectCreateInformation.ProjectName } };

			projectCreateInformation.ProjectName = ReplaceParameters (name, substitution, projectCreateInformation);

			if (string.IsNullOrEmpty (directory) || directory == ".")
				return projectCreateInformation;

			string dir = ReplaceParameters (directory, substitution, projectCreateInformation);
			projectCreateInformation.ProjectBasePath = Path.Combine (projectCreateInformation.SolutionPath, dir);

			if (ShouldCreateProject (projectCreateInformation) && !Directory.Exists (projectCreateInformation.ProjectBasePath))
				Directory.CreateDirectory (projectCreateInformation.ProjectBasePath);

			return projectCreateInformation;
		}

		public bool HasPackages ()
		{
			return packageReferences.Any ();
		}

		[Obsolete]
		public IList<ProjectTemplatePackageReference> GetPackageReferences ()
		{
			return packageReferences;
		}

		internal string ProjectType {
			get {
				return type;
			}
		}

		public IList<ProjectTemplatePackageReference> GetPackageReferences (ProjectCreateInformation projectCreateInformation)
		{
			return packageReferences
				.Where (packageReference => projectCreateInformation.ShouldCreate (packageReference.CreateCondition))
				.ToList ();
		}

		public bool ShouldCreateProject (ProjectCreateInformation projectCreateInformation)
		{
			return projectCreateInformation.ShouldCreate (createCondition);
		}

		class ProjectReferenceDescription
		{
			XmlElement elem;

			public ProjectReferenceDescription (XmlElement elem)
			{
				this.elem = elem;
				CreateCondition = elem.GetAttribute ("if");
			}

			public ProjectReference Create ()
			{
				var refType = elem.GetAttribute ("type");
				var projectReference = ProjectReference.CreateCustomReference ((ReferenceType)Enum.Parse (typeof(ReferenceType), refType), elem.GetAttribute ("refto"));
				var hintPath = GetMSBuildReferenceHintPath ();
				if (hintPath != null)
					projectReference.Metadata.SetValue ("HintPath", hintPath);
				
				string specificVersion = elem.GetAttribute ("SpecificVersion");
				if (!string.IsNullOrEmpty (specificVersion))
					projectReference.SpecificVersion = bool.Parse (specificVersion);
				string localCopy = elem.GetAttribute ("LocalCopy");
				if (!string.IsNullOrEmpty (localCopy) && projectReference.CanSetLocalCopy)
					projectReference.LocalCopy = bool.Parse (localCopy);
				string referenceOutputAssembly = elem.GetAttribute ("ReferenceOutputAssembly");
				if (!string.IsNullOrEmpty (referenceOutputAssembly))
					projectReference.ReferenceOutputAssembly = bool.Parse (referenceOutputAssembly);
				return projectReference;
			}

			string GetMSBuildReferenceHintPath ()
			{
				string hintPath = elem.GetAttribute ("HintPath");
				if (!string.IsNullOrEmpty (hintPath))
					return hintPath;
				return null;
			}

			public string CreateCondition { get; private set; }
		}
	}
}