/// <summary> /// This constructor is used to create a new build item and add it to /// the project. /// </summary> /// <param name="project">The project that will own the item</param> /// <param name="itemType">The type of build item to create</param> /// <param name="itemPath">The path to the item. This can be relative /// or absolute and may contain variable references.</param> internal ProjectElement(SandcastleProject project, string itemType, string itemPath) { if (project == null) { throw new ArgumentNullException("project"); } if (String.IsNullOrEmpty(itemPath)) { throw new ArgumentException("Cannot be null or empty", "itemPath"); } if (String.IsNullOrEmpty(itemType)) { throw new ArgumentException("Cannot be null or empty", "itemType"); } projectFile = project; this.CheckProjectIsEditable(); if (itemType == Utils.BuildAction.Folder.ToString() && itemPath[itemPath.Length - 1] != '\\') { itemPath += @"\"; } item = project.MSBuildProject.AddItem(itemType, itemPath)[0]; projectFile.MarkAsDirty(); }
/// <summary> /// Set a metadata value in the project item /// </summary> /// <param name="name">The name of the metadata element</param> /// <param name="value">The value to store in the element</param> public void SetMetadata(string name, string value) { this.CheckProjectIsEditable(); // Build Action is the name, not metadata if (String.Compare(name, ProjectElement.BuildAction, StringComparison.OrdinalIgnoreCase) == 0) { item.ItemType = value; return; } // Include is an attribute, not metadata if (String.Compare(name, ProjectElement.IncludePath, StringComparison.OrdinalIgnoreCase) == 0) { item.UnevaluatedInclude = value; return; } if (String.IsNullOrEmpty(value)) { item.RemoveMetadata(name); } else { item.SetMetadataValue(name, value); } projectFile.MarkAsDirty(); }
//===================================================================== /// <summary> /// This is used to see if the project can be edited. If not, abort /// the change by throwing an exception. /// </summary> protected void CheckProjectIsEditable() { // If not associated with a project, allow it by default if (projectFile == null) { isDirty = true; } else { CancelEventArgs ce = new CancelEventArgs(); projectFile.OnQueryEditProjectFile(ce); if (ce.Cancel) { throw new OperationCanceledException( "Project cannot be edited"); } isDirty = true; projectFile.MarkAsDirty(); } }