public MSBuildBasedProject(MSBuild.Engine engine) { if (engine == null) throw new ArgumentNullException("engine"); this.project = engine.CreateNewProject(); this.userProject = engine.CreateNewProject(); }
/// <summary> /// Overloaded constructor. /// </summary> /// <param name="project">An instance of a build project</param> /// <exception cref="ArgumentNullException">Is thrown if the passed Project is null.</exception> internal GlobalPropertyHandler(MSBuild.Project project) { Debug.Assert(project != null, "The project parameter passed cannot be null"); this.globalProjectProperties = project.GlobalProperties; Debug.Assert(project.ParentEngine != null, "The parent engine has not been initialized"); this.globalEngineProperties = project.ParentEngine.GlobalProperties; }
internal static void AddItemToGroup(MSBuild.BuildItemGroup group, ProjectItem item) { if (group == null) throw new ArgumentNullException("group"); if (item == null) throw new ArgumentNullException("item"); if (item.IsAddedToProject) throw new ArgumentException("item is already added to project", "item"); MSBuild.BuildItem newItem = group.AddNewItem(item.ItemType.ToString(), item.Include, true); foreach (string name in item.MetadataNames) { newItem.SetMetadata(name, item.GetMetadata(name)); } item.BuildItem = newItem; Debug.Assert(item.IsAddedToProject); }
/// <summary> /// Constructor to Wrap an existing MSBuild.BuildItem /// Only have internal constructors as the only one who should be creating /// such object is the project itself (see Project.CreateFileNode()). /// </summary> /// <param name="project">Project that owns this item</param> /// <param name="existingItem">an MSBuild.BuildItem; can be null if virtualFolder is true</param> /// <param name="virtualFolder">Is this item virtual (such as reference folder)</param> internal ProjectElement(ProjectNode project, MSBuild.BuildItem existingItem, bool virtualFolder) { if (project == null) throw new ArgumentNullException("project"); if (!virtualFolder && existingItem == null) throw new ArgumentNullException("existingItem"); // Keep a reference to project and item this.itemProject = project; this.item = existingItem; this.isVirtual = virtualFolder; if (this.isVirtual) this.virtualProperties = new Dictionary<string, string>(); }
/// <summary> /// Constructor to Wrap an existing MSBuild.BuildItem /// Only have internal constructors as the only one who should be creating /// such object is the project itself (see Project.CreateFileNode()). /// </summary> /// <param name="project">Project that owns this item</param> /// <param name="existingItem">an MSBuild.BuildItem; can be null if virtualFolder is true</param> /// <param name="virtualFolder">Is this item virtual (such as reference folder)</param> internal ProjectElement(ProjectNode project, MSBuild.BuildItem existingItem, bool virtualFolder) { if (project == null) throw new ArgumentNullException("project", String.Format(CultureInfo.CurrentCulture, SR.GetString(SR.AddToNullProjectError), existingItem.Include)); if (!virtualFolder && existingItem == null) throw new ArgumentNullException("existingItem"); // Keep a reference to project and item itemProject = project; item = existingItem; isVirtual = virtualFolder; if (isVirtual) virtualProperties = new Dictionary<string, string>(); }
internal static void EnsureCorrectTempProject(MSBuild.Project baseProject, string configuration, string platform, ref MSBuild.Project tempProject) { if (configuration == null && platform == null) { // unload temp project if (tempProject != null && tempProject != baseProject) { tempProject.ParentEngine.UnloadAllProjects(); } tempProject = null; return; } if (configuration == null) configuration = baseProject.GetEvaluatedProperty("Configuration"); if (platform == null) platform = baseProject.GetEvaluatedProperty("Platform"); if (tempProject != null && tempProject.GetEvaluatedProperty("Configuration") == configuration && tempProject.GetEvaluatedProperty("Platform") == platform) { // already correct return; } if (baseProject.GetEvaluatedProperty("Configuration") == configuration && baseProject.GetEvaluatedProperty("Platform") == platform) { tempProject = baseProject; return; } // create new project // unload old temp project if (tempProject != null && tempProject != baseProject) { tempProject.ParentEngine.UnloadAllProjects(); } try { MSBuild.Engine engine = CreateEngine(); tempProject = engine.CreateNewProject(); MSBuildBasedProject.InitializeMSBuildProject(tempProject); tempProject.LoadXml(baseProject.Xml); tempProject.SetProperty("Configuration", configuration); tempProject.SetProperty("Platform", platform); } catch (Exception ex) { ICSharpCode.Core.MessageService.ShowWarning(ex.ToString()); tempProject = baseProject; } }
/// <summary> /// Creates a new projectItem for the passed itemType /// </summary> public override ProjectItem CreateProjectItem(MSBuild.BuildItem item) { switch (item.Name) { case "Reference": return new ReferenceProjectItem(this, item); case "ProjectReference": return new ProjectReferenceProjectItem(this, item); case "COMReference": return new ComReferenceProjectItem(this, item); case "Import": return new ImportProjectItem(this, item); case "None": case "Compile": case "EmbeddedResource": case "Resource": case "Content": case "Folder": return new FileProjectItem(this, item); case "WebReferenceUrl": return new WebReferenceUrl(this, item); case "WebReferences": return new WebReferencesProjectItem(this, item); default: if (this.AvailableFileItemTypes.Contains(new ItemType(item.Name)) || SafeFileExists(this.Directory, item.FinalItemSpec)) { return new FileProjectItem(this, item); } else { return base.CreateProjectItem(item); } } }
/// <summary> /// Get the parent node of an msbuild item /// </summary> /// <param name="item">msbuild item</param> /// <returns>parent node</returns> private HierarchyNode GetItemParentNode(MSBuild.BuildItem item) { HierarchyNode currentParent = this; string strPath = item.FinalItemSpec; strPath = Path.GetDirectoryName(strPath); if(strPath.Length > 0) { // Use the relative to verify the folders... currentParent = this.CreateFolderNodes(strPath); } return currentParent; }
private string GetOutputPath(MSBuild.BuildPropertyGroup properties) { this.currentConfig = properties; string outputPath = GetProjectProperty("OutputPath"); if(!String.IsNullOrEmpty(outputPath)) { outputPath = outputPath.Replace('/', Path.DirectorySeparatorChar); if(outputPath[outputPath.Length - 1] != Path.DirectorySeparatorChar) outputPath += Path.DirectorySeparatorChar; } return outputPath; }
/// <summary> /// Add a dependent file node to the hierarchy /// </summary> /// <param name="item">msbuild item to add</param> /// <param name="parentNode">Parent Node</param> /// <returns>Added node</returns> private HierarchyNode AddDependentFileNodeToNode(MSBuild.BuildItem item, HierarchyNode parentNode) { FileNode node = this.CreateDependentFileNode(new ProjectElement(this, item, false)); parentNode.AddChild(node); // Make sure to set the HasNameRelation flag on the dependent node if it is related to the parent by name if(!node.HasParentNodeNameRelation && string.Compare(node.GetRelationalName(), parentNode.GetRelationalName(), StringComparison.OrdinalIgnoreCase) == 0) { node.HasParentNodeNameRelation = true; } return node; }
/// <summary> /// Add a file node to the hierarchy /// </summary> /// <param name="item">msbuild item to add</param> /// <param name="parentNode">Parent Node</param> /// <returns>Added node</returns> private HierarchyNode AddFileNodeToNode(MSBuild.BuildItem item, HierarchyNode parentNode) { FileNode node = this.CreateFileNode(new ProjectElement(this, item, false)); parentNode.AddChild(node); return node; }
static void EndXmlManipulation(MSBuild.Project project) { MarkProjectAsDirtyForReprocessXml(project); }
/// <summary> /// Changes the value of the ProjectPath property on an existing import. /// Note: this methods causes the project to recreate all imports, so existing import /// instances might not be affected. /// </summary> public static void SetImportProjectPath(MSBuildBasedProject project, MSBuild.Import import, string newRawPath) { if (project == null) throw new ArgumentNullException("project"); if (import == null) throw new ArgumentNullException("import"); if (newRawPath == null) throw new ArgumentNullException("newRawPath"); lock (project.SyncRoot) { XmlAttribute a = (XmlAttribute)typeof(MSBuild.Import).InvokeMember( "ProjectPathAttribute", BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic, null, import, null ); a.Value = newRawPath; EndXmlManipulation(project.MSBuildProject); } project.CreateItemsListFromMSBuild(); }
/// <summary> /// Gets all custom metadata names defined directly on the item, ignoring defaulted metadata entries. /// </summary> public static IList<string> GetCustomMetadataNames(MSBuild.BuildItem item) { PropertyInfo prop = typeof(MSBuild.BuildItem).GetProperty("ItemDefinitionLibrary", BindingFlags.Instance | BindingFlags.NonPublic); object oldValue = prop.GetValue(item, null); prop.SetValue(item, null, null); IList<string> result = (IList<string>)item.CustomMetadataNames; prop.SetValue(item, oldValue, null); return result; }
/// <summary> /// Adds references to this container from a MSBuild project. /// </summary> public void LoadReferencesFromBuildProject(MSBuild.Project buildProject) { foreach (string referenceType in SupportedReferenceTypes) { MSBuild.BuildItemGroup refererncesGroup = buildProject.GetEvaluatedItemsByName(referenceType); bool isAssemblyReference = referenceType == ProjectFileConstants.Reference; // If the project was loaded for browsing we should still create the nodes but as not resolved. if (this.ProjectMgr.HasPassedSecurityChecks && isAssemblyReference && this.ProjectMgr.Build(MsBuildTarget.ResolveAssemblyReferences) != MSBuildResult.Successful) { continue; } foreach (MSBuild.BuildItem item in refererncesGroup) { ProjectElement element = new ProjectElement(this.ProjectMgr, item, false); ReferenceNode node = CreateReferenceNode(referenceType, element); if (node != null) { // Make sure that we do not want to add the item twice to the ui hierarchy // We are using here the UI representation of the Node namely the Caption to find that out, in order to // avoid different representation problems. // Example :<Reference Include="EnvDTE80, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> // <Reference Include="EnvDTE80" /> bool found = false; for (HierarchyNode n = this.FirstChild; n != null && !found; n = n.NextSibling) { if (String.Compare(n.Caption, node.Caption, StringComparison.OrdinalIgnoreCase) == 0) { found = true; } } if (!found) { this.AddChild(node); } } } } }
/// <include file='doc\Project.uex' path='docs/doc[@for="Project.GetBoolAttr"]/*' /> private bool GetBoolAttr(MSBuild.PropertyGroup properties, string name) { this.currentConfig = properties; string s = GetProjectProperty(name); return (s != null && s.ToLower().Trim() == "true"); }
/// <include file='doc\Project.uex' path='docs/doc[@for="Project.GetAssemblyName"]/*' /> private string GetAssemblyName(MSBuild.PropertyGroup properties) { this.currentConfig = properties; string name = null; name = GetProjectProperty("AssemblyName"); if (name == null) name = this.Caption; string outputtype = GetProjectProperty("OutputType", false); if (outputtype == "library") { outputtype = outputtype.ToLower(); name += ".dll"; } else { name += ".exe"; } return name; }
/// <summary> /// Constructor to Wrap an existing MSBuild.Item /// Only have internal constructors as the only one who should be creating /// such object is the project itself (see Project.CreateFileNode()). /// </summary> /// <param name="project">Project that owns this item</param> /// <param name="existingItem">an MSBuild.Item; can be null if virtualFolder is true</param> /// <param name="virtualFolder">Is this item virtual (such as reference folder)</param> internal ProjectElement(Project project, MSBuild.Item existingItem, bool virtualFolder) { if (project == null) throw new ArgumentNullException("project", String.Format(SR.GetString(SR.AddToNullProjectError), existingItem.Include)); if (!virtualFolder && existingItem == null) throw new ArgumentNullException("existingItem"); // Keep a reference to project and item itemProject = project; item = existingItem; isVirtual = virtualFolder; }
/// <summary> /// For internal use only. /// This creates a copy of an existing configuration and add it to the project. /// Caller should change the condition on the PropertyGroup. /// If derived class want to accomplish this, they should call ConfigProvider.AddCfgsOfCfgName() /// It is expected that in the future MSBuild will have support for this so we don't have to /// do it manually. /// </summary> /// <param name="group">PropertyGroup to clone</param> /// <returns></returns> internal MSBuild.PropertyGroup ClonePropertyGroup(MSBuild.PropertyGroup group) { // Create a new (empty) PropertyGroup MSBuild.PropertyGroup newPropertyGroup = this.projFile.AddNewPropertyGroup(true); // Now copy everything from the group we are trying to clone to the group we are creating if (group.Condition != "") newPropertyGroup.Condition = group.Condition; foreach (MSBuild.Property prop in group) { MSBuild.Property newProperty = newPropertyGroup.AddNewProperty(prop.Name, prop.Value); if (prop.Condition != "") newProperty.Condition = prop.Condition; } return newPropertyGroup; }
internal static void MarkProjectAsDirtyForReprocessXml(MSBuild.Project project) { typeof(MSBuild.Project).InvokeMember( "MarkProjectAsDirtyForReprocessXml", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic, null, project, null ); }
private bool GetBoolAttr(MSBuild.BuildPropertyGroup properties, string name) { this.currentConfig = properties; string s = GetProjectProperty(name); return (s != null && s.ToUpperInvariant().Trim() == "TRUE"); }
/// <summary> /// Finds the <c>BuildProperty</c> object used to store <paramref name="propertyName"/> /// in the specified configuration/platform. /// </summary> /// <param name="configuration">The configuration to use.</param> /// <param name="platform">The platform to use.</param> /// <param name="propertyName">The property to look for.</param> /// <param name="group">[Out], the property group in which the property was found</param> /// <param name="location">[Out], the storage location the condition of the property /// group was referring to</param> protected MSBuild.BuildProperty FindPropertyObject(string configuration, string platform, string propertyName, out MSBuild.BuildPropertyGroup group, out PropertyStorageLocations location) { if (string.IsNullOrEmpty(configuration)) configuration = ActiveConfiguration; if (string.IsNullOrEmpty(platform)) platform = ActivePlatform; // We need to use ToArray because EvaluateMSBuildCondition invalidates the list // of property groups. foreach (MSBuild.BuildPropertyGroup g in Linq.ToList(Linq.CastTo<MSBuild.BuildPropertyGroup>(project.PropertyGroups))) { if (g.IsImported) { continue; } MSBuild.BuildProperty property = MSBuildInternals.GetProperty(g, propertyName); if (property == null) continue; if (EvaluateMSBuildCondition(configuration, platform, g.Condition)) { location = MSBuildInternals.GetLocationFromCondition(g.Condition); group = g; return property; } } location = PropertyStorageLocations.Unknown; group = null; return null; }
private string GetAssemblyName(MSBuild.BuildPropertyGroup properties) { this.currentConfig = properties; string name = null; name = GetProjectProperty(ProjectFileConstants.AssemblyName); if(name == null) name = this.Caption; string outputtype = GetProjectProperty(ProjectFileConstants.OutputType, false); if(outputtype == "library") { outputtype = outputtype.ToLowerInvariant(); name += ".dll"; } else { name += ".exe"; } return name; }
internal static void InitializeMSBuildProject(MSBuild.Project project) { project.GlobalProperties.SetProperty("BuildingInsideVisualStudio", "true"); foreach (KeyValuePair<string, string> pair in MSBuildEngine.MSBuildProperties) { project.GlobalProperties.SetProperty(pair.Key, pair.Value, true); } }
/// <summary> /// copy properties from g into a new property group for newConfiguration and newPlatform /// </summary> void CopyProperties(MSBuild.BuildPropertyGroup g, string newConfiguration, string newPlatform) { MSBuild.BuildPropertyGroup ng = project.AddNewPropertyGroup(false); ng.Condition = CreateCondition(newConfiguration, newPlatform); foreach (MSBuild.BuildProperty p in g) { ng.AddNewProperty(p.Name, p.Value); } }
static XmlElement BeginXmlManipulation(MSBuild.Project project) { return (XmlElement)typeof(MSBuild.Project).InvokeMember( "ProjectElement", BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic, null, project, null ); }
/// <summary> /// For internal use only. /// This creates a copy of an existing configuration and add it to the project. /// Caller should change the condition on the PropertyGroup. /// If derived class want to accomplish this, they should call ConfigProvider.AddCfgsOfCfgName() /// It is expected that in the future MSBuild will have support for this so we don't have to /// do it manually. /// </summary> /// <param name="group">PropertyGroup to clone</param> /// <returns></returns> internal MSBuild.BuildPropertyGroup ClonePropertyGroup(MSBuild.BuildPropertyGroup group) { // Create a new (empty) PropertyGroup MSBuild.BuildPropertyGroup newPropertyGroup = this.buildProject.AddNewPropertyGroup(false); // Now copy everything from the group we are trying to clone to the group we are creating if(!String.IsNullOrEmpty(group.Condition)) newPropertyGroup.Condition = group.Condition; foreach(MSBuild.BuildProperty prop in group) { MSBuild.BuildProperty newProperty = newPropertyGroup.AddNewProperty(prop.Name, prop.Value); if(!String.IsNullOrEmpty(prop.Condition)) newProperty.Condition = prop.Condition; } return newPropertyGroup; }
/// <summary> /// Add an item to the hierarchy based on the item path /// </summary> /// <param name="item">Item to add</param> /// <returns>Added node</returns> private HierarchyNode AddIndependentFileNode(MSBuild.BuildItem item) { HierarchyNode currentParent = GetItemParentNode(item); return AddFileNodeToNode(item, currentParent); }
/// <summary> /// This method returns new project element based on existing MSBuild item. It does not modify/add project/build hierarchy at all. /// </summary> /// <param name="item">MSBuild item instance</param> /// <returns>wrapping project element</returns> public ProjectElement GetProjectElement(MSBuild.BuildItem item) { return new ProjectElement(this, item, false); }
/// <summary> /// Removes all <Import> nodes from a project. /// </summary> public static void ClearImports(MSBuild.Project project) { if (project == null) throw new ArgumentNullException("project"); XmlElement xmlProject = BeginXmlManipulation(project); List<XmlNode> nodesToRemove = new List<XmlNode>(); foreach (XmlNode node in xmlProject.ChildNodes) { if (node.NodeType == XmlNodeType.Element && node.Name == "Import") { nodesToRemove.Add(node); } } foreach (XmlNode node in nodesToRemove) { xmlProject.RemoveChild(node); } EndXmlManipulation(project); }