Beispiel #1
0
		public static ReferenceProjectItem GetReference(MSBuildBasedProject project, string referenceName)
		{
			foreach (ReferenceProjectItem referenceProjectItem in project.GetItemsOfType(ItemType.Reference)) {
				if (referenceProjectItem.Include == referenceName) {
					return referenceProjectItem;
				}
			}
			return null;
		}
Beispiel #2
0
		public CSharpProject(MSBuildBasedProject project)
		{
			this.IProject = project;
			
			CompilerSettings.AllowUnsafeBlocks = GetBoolProperty("AllowUnsafeBlocks") ?? false;
			CompilerSettings.CheckForOverflow = GetBoolProperty("CheckForOverflowUnderflow") ?? false;
			string defineConstants = project.GetEvaluatedProperty("DefineConstants");
			foreach (string symbol in defineConstants.Split(new char[] { ';', ',' }, StringSplitOptions.RemoveEmptyEntries))
				this.CompilerSettings.ConditionalSymbols.Add(symbol.Trim());
			
			// Parse the C# code files
			foreach (var item in project.GetItemsOfType(ItemType.Compile)) {
				var file = new CSharpFile(this, FileName.Create(item.FileName));
				Files.Add(file);
			}
		}
		internal static void ResolveAssemblyReferences(MSBuildBasedProject baseProject, ReferenceProjectItem[] referenceReplacements)
		{
			MSBuild.Engine tempEngine;
			MSBuild.Project tempProject;
			IEnumerable<ReferenceProjectItem> references;
			
			lock (baseProject.SyncRoot) {
				// create a copy of the project
				tempEngine = CreateEngine();
				tempProject = tempEngine.CreateNewProject();
				// tell MSBuild the path so that projects containing <Import Project="relativePath" />
				// can be loaded
				tempProject.FullFileName = baseProject.MSBuildProject.FullFileName;
				MSBuildBasedProject.InitializeMSBuildProject(tempProject);
				tempProject.LoadXml(baseProject.MSBuildProject.Xml);
				tempProject.SetProperty("Configuration", baseProject.ActiveConfiguration);
				tempProject.SetProperty("Platform", baseProject.ActivePlatform);
				tempProject.SetProperty("BuildingProject", "false");
				
				if (referenceReplacements == null) {
					references = baseProject.GetItemsOfType(ItemType.Reference).OfType<ReferenceProjectItem>();
					
					// remove the "Private" meta data
					foreach (MSBuild.BuildItemGroup itemGroup in tempProject.ItemGroups) {
						// skip item groups from imported projects
						if (itemGroup.IsImported)
							continue;
						foreach (MSBuild.BuildItem item in itemGroup) {
							if (item.Name == ItemType.Reference.ItemName) {
								item.RemoveMetadata("Private");
							}
						}
					}
				} else {
					references = referenceReplacements;
					
					// replace all references in the project with the referenceReplacements
					foreach (MSBuild.BuildItemGroup itemGroup in tempProject.ItemGroups) {
						// skip item groups from imported projects
						if (itemGroup.IsImported)
							continue;
						foreach (MSBuild.BuildItem item in itemGroup.ToArray()) {
							if (item.Name == ItemType.Reference.ItemName) {
								itemGroup.RemoveItem(item);
							}
						}
					}
					foreach (ReferenceProjectItem item in referenceReplacements) {
						tempProject.AddNewItem("Reference", item.Include, true);
					}
				}
			}
			var referenceDict = new Dictionary<string, ReferenceProjectItem>();
			foreach (ReferenceProjectItem item in references) {
				// references could be duplicate, so we cannot use referenceDict.Add or reference.ToDictionary
				referenceDict[item.Include] = item;
			}
			
			
			#if DEBUG
			//engine.RegisterLogger(new MSBuild.ConsoleLogger(Microsoft.Build.Framework.LoggerVerbosity.Detailed));
			#endif
			
			//Environment.CurrentDirectory = Path.GetDirectoryName(tempProject.FullFileName);
			lock (MSBuildInternals.InProcessMSBuildLock) {
				if (!tempProject.Build("ResolveAssemblyReferences")) {
					LoggingService.Warn("ResolveAssemblyReferences exited with error");
					return;
				}
			}
			
			foreach (MSBuild.BuildItem item in tempProject.GetEvaluatedItemsByName("_ResolveAssemblyReferenceResolvedFiles")) {
				string originalInclude = item.GetEvaluatedMetadata("OriginalItemSpec");
				ReferenceProjectItem reference;
				if (referenceDict.TryGetValue(originalInclude, out reference)) {
					reference.AssemblyName = new Dom.DomAssemblyName(item.GetEvaluatedMetadata("FusionName"));
					//string fullPath = item.GetEvaluatedMetadata("FullPath"); is incorrect for relative paths
					string fullPath = FileUtility.GetAbsolutePath(baseProject.Directory, item.GetEvaluatedMetadata("Identity"));
					reference.FileName = fullPath;
					reference.Redist = item.GetEvaluatedMetadata("Redist");
					//LoggingService.Debug("Got information about " + originalInclude + "; fullpath=" + fullPath);
					reference.DefaultCopyLocalValue = bool.Parse(item.GetEvaluatedMetadata("CopyLocal"));
				} else {
					LoggingService.Warn("Unknown item " + originalInclude);
				}
			}
			
			tempEngine.UnloadAllProjects(); // unload temp project
		}