private void GetFilePathFromProject(List <string> projectPaths, out List <string> filePaths) { filePaths = new List <string>(); foreach (string path in projectPaths) { if (path != null) { Project[] projects = new Project[ProjectCollection.GlobalProjectCollection.GetLoadedProjects(path).Count]; ProjectCollection.GlobalProjectCollection.GetLoadedProjects(path).CopyTo(projects, 0); foreach (Project pro in projects) { ProjectItem[] projectItem = new ProjectItem[pro.GetItems("Compile").Count]; pro.GetItems("Compile").CopyTo(projectItem, 0); foreach (ProjectItem item in projectItem) { if (!item.EvaluatedInclude.StartsWith("Properties\\")) { var itempath = pro.DirectoryPath + "\\" + item.EvaluatedInclude; filePaths.Add(itempath); } } } } } }
private void DoAdd(string itemType, string itemPath) { var added = this.itemProject.BuildProject.AddItem(itemType, Microsoft.Build.BuildEngine.Utilities.Escape(itemPath)); Debug.Assert(added.Count == 1, "adding a file created more than 1 new item, should not be possible since we escape wildcard characters"); this.item = added[0]; }
/// <summary> /// Reevaluate all properties for the current item /// This should be call if you believe the property for this item /// may have changed since it was created/refreshed, or global properties /// this items depends on have changed. /// Be aware that there is a perf cost in calling this function. /// </summary> public void RefreshProperties() { if (HasItemBeenDeleted) { throw new InvalidOperationException("The item has been deleted."); } if (this.IsVirtual) { return; } ProjectManager.BuildProject.ReevaluateIfNecessary(); IEnumerable <ProjectItem> items = ProjectManager.BuildProject.GetItems(Item.ItemType); foreach (ProjectItem projectItem in items) { if (projectItem != null && projectItem.EvaluatedInclude.Equals(Item.EvaluatedInclude)) { item = projectItem; return; } } }
public static NuGetReference AsNuGetReference(this MBuild.ProjectItem item) { var packageName = item.EvaluatedInclude; var packageVersion = item.GetMetadataValue(MSBuildConstants.VersionElementName) ?? "0.0.0"; // Package references without versions will resolve to the lowest stable version return(new NuGetReference(packageName, packageVersion)); }
public static bool IsOrphaned(ProjectItem buildItem, ProjectBase owner) { bool considerBuildItem = (buildItem.ItemType == "Compile" || buildItem.ItemType == "Content" || buildItem.ItemType == "None"); if(!considerBuildItem) { if (owner is VisualStudioProject) { var asVisualStudioProject = owner as VisualStudioProject; if (!considerBuildItem && buildItem.ItemType == asVisualStudioProject.DefaultContentAction) { considerBuildItem = true; } } } if (considerBuildItem) { // characters like '%' are encoded, so we have to decode them: string relativeName = System.Web.HttpUtility.UrlDecode( buildItem.UnevaluatedInclude); string fullName = owner.MakeAbsolute(relativeName); return !FileManager.FileExists(fullName) && buildItem.ItemType != "ProjectReference"; } return false; }
/// <summary> /// Copies all metadata from the specified buildItem /// </summary> /// <param name="item">The destination for the metadata copy</param> /// <param name="buildItem">The source for the metadata copy</param> public static void CopyMetadata(this TaskItem item, _BE.ProjectItem buildItem) { foreach (_BE.ProjectMetadata md in buildItem.Metadata) { item.SetMetadata(md.Name, md.EvaluatedValue); } }
/// <summary> /// Constructor to create a new MSBuild.ProjectItem and add it to the project /// Only have internal constructors as the only one who should be creating /// such object is the project itself (see Project.CreateFileNode()). /// </summary> public ProjectElement(ProjectNode project, string itemPath, string itemType) { if (project == null) { throw new ArgumentNullException("project"); } if (String.IsNullOrEmpty(itemPath)) { throw new ArgumentException(SR.GetString(SR.ParameterCannotBeNullOrEmpty, CultureInfo.CurrentUICulture), "itemPath"); } if (String.IsNullOrEmpty(itemType)) { throw new ArgumentException(SR.GetString(SR.ParameterCannotBeNullOrEmpty, CultureInfo.CurrentUICulture), "itemType"); } this.itemProject = project; // create and add the item to the project this.item = project.BuildProject.AddItem(itemType, Microsoft.Build.Evaluation.ProjectCollection.Escape(itemPath))[0]; if (project.BuildProject.IsDirty) { this.ProjectManager.SetProjectFileDirty(true); } this.RefreshProperties(); }
/// <summary> /// Constructor to Wrap an existing MSBuild.ProjectItem /// 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.ProjectItem; can be null if virtualFolder is true</param> /// <param name="virtualFolder">Is this item virtual (such as reference folder)</param> internal MsBuildProjectElement(ProjectNode project, MSBuild.ProjectItem existingItem) : base(project) { Utilities.ArgumentNotNull("existingItem", existingItem); // Keep a reference to project and item _item = existingItem; }
public void AddPackageReference(string packageName, string packageVersion, string privateAssets = null, string publicAssets = null) { Microsoft.Build.Evaluation.ProjectItem projectItem = _project.AddItem("PackageReference", packageName).First(); ProjectMetadataElement packageVersionElement = projectItem.Xml.AddMetadata("Version", packageVersion); packageVersionElement.ExpressedAsAttribute = true; }
public void RemoveDocument(string documentFilePath) { string relativePath = MakeRelative(documentFilePath); Microsoft.Build.Evaluation.ProjectItem item = GetItem("Compile", relativePath); _project.RemoveItem(item); }
/// <summary> /// Constructor to create a new MSBuild.ProjectItem and add it to the project /// Only have internal constructors as the only one who should be creating /// such object is the project itself (see Project.CreateFileNode()). /// </summary> internal ProjectElement(ProjectNode project, string itemPath, string itemType) { if(project == null) { throw new ArgumentNullException("project"); } if(String.IsNullOrEmpty(itemPath)) { throw new ArgumentException(SR.GetString(SR.ParameterCannotBeNullOrEmpty, CultureInfo.CurrentUICulture), "itemPath"); } if(String.IsNullOrEmpty(itemType)) { throw new ArgumentException(SR.GetString(SR.ParameterCannotBeNullOrEmpty, CultureInfo.CurrentUICulture), "itemType"); } this.itemProject = project; // create and add the item to the project this.item = project.BuildProject.AddItem(itemType, Microsoft.Build.Evaluation.ProjectCollection.Escape(itemPath))[0]; this.itemProject.SetProjectFileDirty(true); this.RefreshProperties(); }
protected Item GetItem(Microsoft.Build.Evaluation.ProjectItem eItem) { return((eItem == null) ? default : new Item(eItem) { parentProject = this }); }
private ProjectReferenceAdapter GetProjectReferenceAdapter(ProjectItem r, bool conditionTrue) { var projectGuid = r.GetMetadataValue("Project"); var csprojRelativePath = r.EvaluatedInclude; var absoluteProjectPath = Path.Combine(ProjectDirectory.FullName, csprojRelativePath); var referencedProjectAdapter = _projectLoader.GetProject(Guid.Parse(projectGuid), absoluteProjectPath); return new ProjectReferenceAdapter(referencedProjectAdapter, () => _project.RemoveItem(r), AddBinaryReferenceIfNotExists, conditionTrue); }
/// <summary> /// Constructor to Wrap an existing MSBuild.ProjectItem /// 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.ProjectItem; can be null if virtualFolder is true</param> /// <param name="virtualFolder">Is this item virtual (such as reference folder)</param> internal MsBuildProjectElement(ProjectNode project, MSBuild.ProjectItem existingItem) : base(project) { Utilities.ArgumentNotNull(nameof(existingItem), existingItem); // Keep a reference to project and item this._item = existingItem; this._url = base.Url; }
/// <summary>Loads a <see cref="VsProjectReference"/> from a <see cref="ProjectItem"/>. </summary> /// <param name="project">The parent project. </param> /// <param name="projectItem">The <see cref="ProjectItem"/>. </param> /// <returns>The <see cref="VsProjectReference"/>. </returns> public static VsProjectReference Load(VsProject project, ProjectItem projectItem) { var path = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(project.Path), projectItem.EvaluatedInclude); path = System.IO.Path.GetFullPath(path); var name = projectItem.Metadata.Single(m => m.Name == "Name").EvaluatedValue; return new VsProjectReference(path, name); }
/// <summary> /// Calling this method remove this item from the project file. /// Once the item is delete, you should not longer be using it. /// Note that the item should be removed from the hierarchy prior to this call. /// </summary> public void RemoveFromProjectFile() { if (!deleted && item != null) { deleted = true; itemProject.BuildProject.RemoveItem(item); } itemProject = null; item = null; }
/// <summary> /// Constructor to create a new MSBuild.ProjectItem and add it to the project /// Only have internal constructors as the only one who should be creating /// such object is the project itself (see Project.CreateFileNode()). /// </summary> internal MsBuildProjectElement(ProjectNode project, string itemPath, string itemType) : base(project) { Utilities.ArgumentNotNullOrEmpty("itemPath", itemPath); Utilities.ArgumentNotNullOrEmpty("itemType", itemType); // create and add the item to the project _item = project.BuildProject.AddItem(itemType, Microsoft.Build.Evaluation.ProjectCollection.Escape(itemPath))[0]; }
/// <summary> /// Calling this method remove this item from the project file. /// Once the item is delete, you should not longer be using it. /// Note that the item should be removed from the hierarchy prior to this call. /// </summary> public void RemoveFromProjectFile() { if (!deleted && Item != null) { ProjectManager.BuildProject.RemoveItem(Item); deleted = true; } item = null; }
private string _url; // cached Url /// <summary> /// Constructor to create a new MSBuild.ProjectItem and add it to the project /// Only have internal constructors as the only one who should be creating /// such object is the project itself (see Project.CreateFileNode()). /// </summary> internal MsBuildProjectElement(ProjectNode project, string itemPath, string itemType) : base(project) { Utilities.ArgumentNotNullOrEmpty("itemPath", itemPath); Utilities.ArgumentNotNullOrEmpty("itemType", itemType); // create and add the item to the project _item = project.BuildProject.AddItem(itemType, Microsoft.Build.Evaluation.ProjectCollection.Escape(itemPath))[0]; _url = base.Url; }
/// <summary> /// Helper for metadata escaping tests /// </summary> private void SpecialCharactersInMetadataValueTests(Microsoft.Build.Evaluation.ProjectItem item) { Assert.AreEqual("%3B", item.GetMetadata("EscapedSemicolon").UnevaluatedValue); Assert.AreEqual(";", item.GetMetadata("EscapedSemicolon").EvaluatedValue); Assert.AreEqual(";", item.GetMetadataValue("EscapedSemicolon")); Assert.AreEqual("%24", item.GetMetadata("EscapedDollarSign").UnevaluatedValue); Assert.AreEqual("$", item.GetMetadata("EscapedDollarSign").EvaluatedValue); Assert.AreEqual("$", item.GetMetadataValue("EscapedDollarSign")); }
/// <summary> /// Constructor to create a new MSBuild.ProjectItem and add it to the project /// Only have internal constructors as the only one who should be creating /// such object is the project itself (see Project.CreateFileNode()). /// </summary> internal MsBuildProjectElement(ProjectNode project, string itemPath, string itemType) : base(project) { Utilities.ArgumentNotNullOrEmpty("itemPath", itemPath); Utilities.ArgumentNotNullOrEmpty("itemType", itemType); // create and add the item to the project _item = project.BuildProject.AddItem(itemType, Microsoft.Build.Evaluation.ProjectCollection.Escape(itemPath))[0]; ItemProject.SetProjectFileDirty(true); RefreshProperties(); }
public void how_to_convert_MSBuild_ProjectItem_to_VSProjectItem() { // Say you got an MSBuild ProjectItem somehow. Microsoft.Build.Evaluation.ProjectItem item = this.MsBuildLibrary.Items.First(pi => pi.UnevaluatedInclude == "Class1.cs"); VSLangProj.VSProjectItem vsItem = item.Adapt().AsVsLangItem(); Assert.IsNotNull(vsItem); // Now use item to force its custom tool to run. vsItem.RunCustomTool(); }
public void how_to_convert_MSBuild_ProjectItem_to_IItemNode() { // Say you got an MSBuild ProjectItem somehow. Microsoft.Build.Evaluation.ProjectItem item = this.MsBuildLibrary.Items.First(pi => pi.UnevaluatedInclude == "Class1.cs"); IItemNode itemNode = item.Adapt().AsItemNode(); Assert.IsNotNull(itemNode); // Now use item node to expand it (show its nested items) itemNode.Expand(); }
public void how_to_convert_MSBuild_ProjectItem_to_DTE_ProjectItem() { // Say you got an MSBuild ProjectItem somehow. Microsoft.Build.Evaluation.ProjectItem item = this.MsBuildLibrary.Items.First(pi => pi.UnevaluatedInclude == "Class1.cs"); EnvDTE.ProjectItem dteItem = item.Adapt().AsDteProjectItem(); Assert.IsNotNull(dteItem); // Now use DTE to delete the item, for example dteItem.Delete(); }
/// <summary> /// Constructor to create a new MSBuild.ProjectItem and add it to the project /// Only have internal constructors as the only one who should be creating /// such object is the project itself (see Project.CreateFileNode()). /// </summary> internal ProjectElement(ProjectNode project, string itemPath, string itemType) { Utilities.ArgumentNotNull("project", project); Utilities.ArgumentNotNull("itemPath", itemPath); Utilities.ArgumentNotNull("itemType", itemType); this.itemProject = project; // create and add the item to the project this.item = project.BuildProject.AddItem(itemType, Microsoft.Build.Evaluation.ProjectCollection.Escape(itemPath))[0]; this.itemProject.SetProjectFileDirty(true); this.RefreshProperties(); }
public VirtualEnvNode(JProjectNode project, ProjectItem item) : base(project, new MsBuildProjectElement(project, item)) { _caption = Path.GetFileName(item.EvaluatedInclude); _scheduler = TaskScheduler.FromCurrentSynchronizationContext(); _fileWatcher = new FileSystemWatcher(CommonUtils.GetAbsoluteDirectoryPath(project.ProjectHome, item.EvaluatedInclude), "*"); _fileWatcher.IncludeSubdirectories = true; _fileWatcher.Deleted += PackagesChanged; _fileWatcher.Created += PackagesChanged; _fileWatcher.EnableRaisingEvents = true; _timer = new Timer(CheckPackages); IsExpanded = false; }
public override bool IsFolderItem(MSBuild.ProjectItem buildItem) { if (string.Equals(buildItem.ItemType, JavaProjectFileConstants.SourceFolder, StringComparison.OrdinalIgnoreCase)) { return(true); } if (string.Equals(buildItem.ItemType, JavaProjectFileConstants.TestSourceFolder, StringComparison.OrdinalIgnoreCase)) { return(true); } return(base.IsFolderItem(buildItem)); }
private Microsoft.Build.Evaluation.ProjectItem GetItem(string itemType, string unevaluatedInclude) { Microsoft.Build.Evaluation.ProjectItem foundItem = null; foreach (Microsoft.Build.Evaluation.ProjectItem projectItem in _project.Items) { if (!projectItem.ItemType.Equals(itemType, StringComparison.Ordinal) || !projectItem.UnevaluatedInclude.Equals(unevaluatedInclude, StringComparison.Ordinal)) { continue; } foundItem = projectItem; break; } return(foundItem); }
public override void RefreshProperties() { ItemProject.BuildProject.ReevaluateIfNecessary(); IEnumerable <ProjectItem> items = ItemProject.BuildProject.GetItems(_item.ItemType); foreach (ProjectItem projectItem in items) { if (projectItem != null && projectItem.UnevaluatedInclude.Equals(_item.UnevaluatedInclude)) { _item = projectItem; return; } } }
public void how_to_convert_DTE_ProjectItem_to_MSBuild_ProjectItem() { // Say you got a DTE project item somehow. EnvDTE.ProjectItem dteItem = this.DteLibrary.ProjectItems.OfType <EnvDTE.ProjectItem>().First(pi => pi.Name == "Class1.cs"); Microsoft.Build.Evaluation.ProjectItem item = dteItem.Adapt().AsMsBuildItem(); Assert.IsNotNull(item); // Now use MSBuild to set/get custom metadata on the item item.SetMetadataValue("Identifier", "Foo"); string identifier = item.GetMetadataValue("Identifier"); Assert.AreEqual("Foo", identifier); }
public void SpecialCharactersInMetadataValueConstruction() { string projectString = ObjectModelHelpers.CleanupFileContents(@"<Project DefaultTargets=""Build"" ToolsVersion=""msbuilddefaulttoolsversion"" xmlns=""msbuildnamespace""> <ItemGroup> <None Include='MetadataTests'> <EscapedSemicolon>%3B</EscapedSemicolon> <EscapedDollarSign>%24</EscapedDollarSign> </None> </ItemGroup> </Project>"); System.Xml.XmlReader reader = new System.Xml.XmlTextReader(new StringReader(projectString)); Microsoft.Build.Evaluation.Project project = new Microsoft.Build.Evaluation.Project(reader); Microsoft.Build.Evaluation.ProjectItem item = project.GetItems("None").Single(); SpecialCharactersInMetadataValueTests(item); }
public override void RefreshProperties() { this.ItemProject.BuildProject.ReevaluateIfNecessary(); this._url = base.Url; IEnumerable <ProjectItem> items = this.ItemProject.BuildProject.GetItems(this._item.ItemType); foreach (var projectItem in items) { if (projectItem != null && projectItem.UnevaluatedInclude.Equals(this._item.UnevaluatedInclude)) { this._item = projectItem; return; } } }
private void LoadHintPath(ProjectItem projectItem) { HintPath = projectItem.Metadata.Any(m => m.Name == "HintPath") ? projectItem.Metadata.Single(m => m.Name == "HintPath").EvaluatedValue : null; if (HintPath != null) { var startIndex = HintPath.IndexOf("\\packages\\", StringComparison.InvariantCulture); if (startIndex != -1) { startIndex += "\\packages\\".Length; var endIndex = HintPath.IndexOf("\\", startIndex, StringComparison.InvariantCulture); if (endIndex != -1) { IsNuGetReference = true; var isVersionPart = true; var nuGetPackageVersion = string.Empty; var nuGetPackage = string.Empty; var segments = HintPath.Substring(startIndex, endIndex - startIndex).Split('.'); foreach (var segment in segments.Reverse()) { if (isVersionPart) { int number = 0; if (int.TryParse(segment, out number)) { nuGetPackageVersion = segment + "." + nuGetPackageVersion; } else { nuGetPackage = segment; isVersionPart = false; } } else nuGetPackage = segment + "." + nuGetPackage; } NuGetPackageName = nuGetPackage.Trim('.'); NuGetPackageVersion = nuGetPackageVersion.Trim('.'); } } } }
/// <summary> /// Reevaluate all properties for the current item /// This should be call if you believe the property for this item /// may have changed since it was created/refreshed, or global properties /// this items depends on have changed. /// Be aware that there is a perf cost in calling this function. /// </summary> public void RefreshProperties() { if (this.IsVirtual) { return; } itemProject.BuildProject.ReevaluateIfNecessary(); foreach (var projectItem in MSBuildProject.GetItems(itemProject.BuildProject, item.ItemType)) { if (projectItem != null && projectItem.UnevaluatedInclude.Equals(item.UnevaluatedInclude)) { item = projectItem; return; } } }
/// <summary> /// Constructor to Wrap an existing MSBuild.ProjectItem /// 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.ProjectItem; can be null if virtualFolder is true</param> /// <param name="virtualFolder">Is this item virtual (such as reference folder)</param> public ProjectElement(ProjectNode project, MSBuild.ProjectItem existingItem, bool virtualFolder) { Utilities.ArgumentNotNull("project", 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>(); } }
public void SpecialCharactersInMetadataValueEvaluation() { Microsoft.Build.Evaluation.Project project = new Microsoft.Build.Evaluation.Project(); var metadata = new Dictionary <string, string> { { "EscapedSemicolon", "%3B" }, // Microsoft.Build.Internal.Utilities.Escape(";") { "EscapedDollarSign", "%24" }, // Microsoft.Build.Internal.Utilities.Escape("$") }; Microsoft.Build.Evaluation.ProjectItem item = project.AddItem( "None", "MetadataTests", metadata).Single(); SpecialCharactersInMetadataValueTests(item); project.ReevaluateIfNecessary(); SpecialCharactersInMetadataValueTests(item); }
public InterpretersNode( PythonProjectNode project, ProjectItem item, IPythonInterpreterFactory factory, bool isInterpreterReference, bool canDelete, bool isGlobalDefault = false ) : base(project, ChooseElement(project, item)) { ExcludeNodeFromScc = true; _interpreters = project.Interpreters; _interpreterService = project.Site.GetComponentModel().GetService<IInterpreterOptionsService>(); _factory = factory; _isReference = isInterpreterReference; _canDelete = canDelete; _isGlobalDefault = isGlobalDefault; _canRemove = !isGlobalDefault; _captionSuffix = isGlobalDefault ? SR.GetString(SR.GlobalDefaultSuffix) : ""; if (Directory.Exists(_factory.Configuration.LibraryPath)) { // TODO: Need to handle watching for creation try { _fileWatcher = new FileSystemWatcher(_factory.Configuration.LibraryPath); } catch (ArgumentException) { // Path was not actually valid, despite Directory.Exists // returning true. } if (_fileWatcher != null) { try { _fileWatcher.IncludeSubdirectories = true; _fileWatcher.Deleted += PackagesChanged; _fileWatcher.Created += PackagesChanged; _fileWatcher.EnableRaisingEvents = true; // Only create the timer if the file watcher is running. _timer = new Timer(CheckPackages); } catch (IOException) { // Raced with directory deletion _fileWatcher.Dispose(); _fileWatcher = null; } } } }
/// <summary>Initializes a new instance of the <see cref="AssemblyReference"/> class. </summary> /// <param name="projectItem">The raw name. </param> internal AssemblyReference(ProjectItem projectItem) { ProjectItem = projectItem; var array = ProjectItem.EvaluatedInclude.Split(','); _name = array[0]; _version = "Any"; foreach (var tuple in array.Skip(1) .Select(n => n.Trim().Split('=')) .Select(n => new Tuple<string, string>(n[0], n[1]))) { switch (tuple.Item1) { case "Version": _version = tuple.Item2; break; } } }
private void ReplaceGuidMetadataIfExists(_BE.ProjectItem item, string metadataName) { string value = item.GetMetadata(metadataName).EvaluatedValue; if (!string.IsNullOrEmpty(value)) { try { Guid g = new Guid(value); string replaceWith; if (guidDictionary.TryGetValue(g, out replaceWith)) { //VS Guid replacements don't include braces replaceWith = "{" + replaceWith + "}"; item.SetMetadataValue(metadataName, replaceWith); } } catch (FormatException) { Log.LogWarning("Item {0} has specified {1} metadata not in the format of a Guid.", item.EvaluatedInclude, metadataName); } } }
public override IProjectItemData AddItem(string itemType, string itemValue) { Microsoft.Build.Evaluation.ProjectItem projectItem = this.AddMsBuildItem(itemType, itemValue); if (projectItem == null) { string path = base.DocumentReference.Path; string unknownError = StringTable.UnknownError; string addProjectItemAction = StringTable.AddProjectItemAction; object[] objArray = new object[] { itemType, itemValue }; ProjectLog.LogError(path, unknownError, addProjectItemAction, objArray); } else { string str = base.DocumentReference.Path; string addProjectItemAction1 = StringTable.AddProjectItemAction; object[] objArray1 = new object[] { itemType, itemValue }; ProjectLog.LogSuccess(str, addProjectItemAction1, objArray1); } if (projectItem == null) { return(null); } return(new MSBuildProjectItemData(projectItem)); }
public override void RefreshProperties() { ItemProject.BuildProject.ReevaluateIfNecessary(); _url = base.Url; IEnumerable<ProjectItem> items = ItemProject.BuildProject.GetItems(_item.ItemType); foreach (ProjectItem projectItem in items) { if (projectItem != null && projectItem.UnevaluatedInclude.Equals(_item.UnevaluatedInclude)) { _item = projectItem; return; } } }
/// <summary> /// Helper for SpecialCharactersInMetadataValue tests /// </summary> internal static void SpecialCharactersInMetadataValueTests(ProjectItem item) { Assert.Equal("%3B", item.GetMetadata("EscapedSemicolon").UnevaluatedValue); Assert.Equal("%3B", item.GetMetadata("EscapedSemicolon").EvaluatedValueEscaped); Assert.Equal(";", item.GetMetadata("EscapedSemicolon").EvaluatedValue); Assert.Equal("%3B", Project.GetMetadataValueEscaped(item, "EscapedSemicolon")); Assert.Equal(";", item.GetMetadataValue("EscapedSemicolon")); Assert.Equal("%24", item.GetMetadata("EscapedDollarSign").UnevaluatedValue); Assert.Equal("%24", item.GetMetadata("EscapedDollarSign").EvaluatedValueEscaped); Assert.Equal("$", item.GetMetadata("EscapedDollarSign").EvaluatedValue); Assert.Equal("%24", Project.GetMetadataValueEscaped(item, "EscapedDollarSign")); Assert.Equal("$", item.GetMetadataValue("EscapedDollarSign")); }
/// <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> protected 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; if(itemType == Utils.BuildAction.Folder.ToString() && itemPath[itemPath.Length - 1] != '\\') itemPath += @"\"; item = project.MSBuildProject.AddItem(itemType, itemPath)[0]; projectFile.MSBuildProject.ReevaluateIfNecessary(); }
/// <summary> /// Constructor to Wrap an existing MSBuild.ProjectItem /// 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.ProjectItem; 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.ProjectItem 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>(); }
private bool ResolveProjectReferenceItemByAssemblyName(ProjectItem reference, string mapping) { if (reference.HasMetadata("HintPath")) { var hintpath = reference.GetMetadataValue("HintPath"); var fileInfo = new FileInfo(hintpath); return fileInfo.Name.Equals(mapping, StringComparison.OrdinalIgnoreCase); } return false; }
bool IProjectItemListProvider.RemoveProjectItem(ProjectItem item) { if (item == null) throw new ArgumentNullException("item"); if (item.Project != this) throw new ArgumentException("item does not belong to this project", "item"); if (!item.IsAddedToProject) return false; MSBuildItemWrapper backend = (MSBuildItemWrapper)item.BuildItem; using (var c = OpenCurrentConfiguration()) { if (items.Remove(item)) { itemsReadOnly = null; // remove readonly variant of item list - will regenerate on next Items call c.Project.RemoveItem(backend.MSBuildItem); item.BuildItem = null; // make the item free again return true; } else { throw new InvalidOperationException("Expected that the item is added to this project!"); } } }
private static bool AddAudioBuildItemToProject(ProjectBase project, ProjectItem buildItem) { bool wasAnythingChanged = false; // This item needs an associated entry in the project // The item will be relative to the main project as opposed // to the content project, inside the CopiedXnbs directory: string copiedXnb = ProjectManager.ProjectBase.Directory + "CopiedXnbs\\content\\" + buildItem.UnevaluatedInclude; var link = buildItem.GetLink(); if(!string.IsNullOrEmpty( link )) { copiedXnb = ProjectManager.ProjectBase.Directory + "CopiedXnbs\\content\\" + link; } copiedXnb = FileManager.RemoveDotDotSlash(copiedXnb); string extension = FileManager.GetExtension(buildItem.UnevaluatedInclude); bool isIos = project is IosMonogameProject; bool isAndroid = project is AndroidProject; string whatToAddToProject = null; // bool copyOriginalFile = (isIos || isAndroid) && FileManager.GetExtension(buildItem.UnevaluatedInclude) != "wav"; if (copyOriginalFile) { // Jan 1, 2014 // Not sure why // we were making // this file absolute // using the synced project's // directory. The file will be // shared by synced and original // projects so the file needs to be // made absolute according to that project. //whatToAddToProject = project.MakeAbsolute("content/" + buildItem.Include); whatToAddToProject = ProjectManager.MakeAbsolute("content/" + buildItem.UnevaluatedInclude, true); } else { whatToAddToProject = copiedXnb; } // Both sound and music files have XNBs associated with them so let's add that: whatToAddToProject = FileManager.RemoveExtension(whatToAddToProject) + ".xnb"; copiedXnb = FileManager.RemoveExtension(copiedXnb) + ".xnb"; var item = project.GetItem(whatToAddToProject, true); if (item == null) { item = project.AddContentBuildItem(whatToAddToProject, SyncedProjectRelativeType.Linked, false); string linkToSet = null; if (!string.IsNullOrEmpty(buildItem.GetLink())) { linkToSet = "Content\\" + FileManager.RemoveExtension(buildItem.GetLink()) + ".xnb"; } else { linkToSet = "Content\\" + FileManager.RemoveExtension(buildItem.UnevaluatedInclude) + ".xnb"; } if(project is AndroidProject) { linkToSet = "Assets\\" + linkToSet; } item.SetMetadataValue("Link", linkToSet); PluginManager.ReceiveOutput("Added " + buildItem.EvaluatedInclude + " through the file " + whatToAddToProject); wasAnythingChanged = true; } wasAnythingChanged |= FixLink(item, project); if (isIos && extension == "mp3") { if (FileManager.FileExists(copiedXnb)) { ReplaceWmaReferenceToMp3ReferenceInXnb(copiedXnb, whatToAddToProject); } } // I think we want to tell the user that the XNB is missing so they know to build the PC project if(!FileManager.FileExists(copiedXnb)) { PluginManager.ReceiveError("XNB file is missing - try rebuilding PC project: " + copiedXnb); } // Music files also have a wma file: if ((extension == "mp3" || extension == "wma") && // iOS doesn't ignore MP3, so it's already there. !isIos) { if (isIos) { whatToAddToProject = "Content\\" + buildItem.UnevaluatedInclude; } else { whatToAddToProject = FileManager.RemoveExtension(whatToAddToProject) + ".wma"; } var item2 = project.GetItem(whatToAddToProject, true); if (item2 == null) { item2 = project.AddContentBuildItem(whatToAddToProject, SyncedProjectRelativeType.Linked, false); item2.SetMetadataValue("Link", "Content\\" + FileManager.RemoveExtension(buildItem.UnevaluatedInclude) + "." + FileManager.GetExtension(whatToAddToProject)); PluginManager.ReceiveOutput("Added " + buildItem.EvaluatedInclude + " through the file " + whatToAddToProject); wasAnythingChanged = true; } wasAnythingChanged |= FixLink(item2, project); } return wasAnythingChanged; }
private static bool FixLink(ProjectItem item, ProjectBase project) { bool didFix = false; string oldLink = item.GetLink(); string newLink = project.ProcessLink(oldLink); if (oldLink != newLink) { item.SetLink(newLink); didFix = true; } return didFix; }
public FactoryInfo(MSBuild.ProjectItem projectItem, bool owned) { ProjectItem = projectItem; Owned = owned; }
public MSBuildItemWrapper(MSBuildBasedProject project, MSBuild.ProjectItem item) { this.project = project; this.item = item; }
/// <summary> /// Gets the project item with the given full path. /// </summary> private string GetFullPath(ProjectItem item) { if (Path.IsPathRooted(item.EvaluatedInclude)) return item.EvaluatedInclude; return Path.Combine(Path.GetDirectoryName(fileName), item.EvaluatedInclude); }
private string GetTargetPath(ProjectItem item) { string path = item.UnevaluatedInclude; if (item.HasMetadata("Link")) { path = item.GetMetadataValue("Link"); } return Normalize(path); }
void IProjectItemListProvider.AddProjectItem(ProjectItem item) { if (item == null) throw new ArgumentNullException("item"); if (item.Project != this) throw new ArgumentException("item does not belong to this project", "item"); if (item.IsAddedToProject) throw new ArgumentException("item is already added to project", "item"); WorkbenchSingleton.AssertMainThread(); using (var c = OpenCurrentConfiguration()) { items.Add(item); itemsReadOnly = null; // remove readonly variant of item list - will regenerate on next Items call /*foreach (var g in projectFile.ItemGroups) { if (!string.IsNullOrEmpty(g.Condition) || g.Count == 0) continue; var firstItemInGroup = g.Items.First(); if (firstItemInGroup.Name == item.ItemType.ItemName) { MSBuildInternals.AddItemToGroup(g, item); return; } if (firstItemInGroup.ItemType == ItemType.Reference.ItemName) continue; if (ItemType.DefaultFileItems.Contains(new ItemType(firstItemInGroup.ItemType))) { if (ItemType.DefaultFileItems.Contains(item.ItemType)) { MSBuildInternals.AddItemToGroup(g, item); return; } else { continue; } } MSBuildInternals.AddItemToGroup(g, item); return; } var newGroup = projectFile.AddItemGroup(); MSBuildInternals.AddItemToGroup(newGroup, item);*/ string newInclude = item.TreatIncludeAsLiteral ? MSBuildInternals.Escape(item.Include) : item.Include; var newMetadata = new Dictionary<string, string>(); foreach (string name in item.MetadataNames) { newMetadata[name] = item.GetMetadata(name); } var newItems = c.Project.AddItem(item.ItemType.ItemName, newInclude, newMetadata); if (newItems.Count != 1) throw new InvalidOperationException("expected one new item, but got " + newItems.Count); item.BuildItem = new MSBuildItemWrapper((MSBuildBasedProject)item.Project, newItems[0]); Debug.Assert(item.IsAddedToProject); } }
/// <summary> /// Calling this method remove this item from the project file. /// Once the item is delete, you should not longer be using it. /// Note that the item should be removed from the hierarchy prior to this call. /// </summary> public void RemoveFromProjectFile() { if (!deleted && item != null) { deleted = true; itemProject.BuildProject.RemoveItem(item); this.itemProject.SetProjectFileDirty(true); } itemProject = null; item = null; }
//===================================================================== /// <summary> /// This constructor is used to wrap an existing project item /// </summary> /// <param name="project">The project that owns the item</param> /// <param name="existingItem">The existing item</param> /// <overloads>There are two overloads for the constructor</overloads> protected ProjectElement(SandcastleProject project, ProjectItem existingItem) { if(project == null) throw new ArgumentNullException("project"); if(existingItem == null) throw new ArgumentNullException("existingItem"); projectFile = project; item = existingItem; }
/// <summary> /// Reevaluate all properties for the current item /// This should be call if you believe the property for this item /// may have changed since it was created/refreshed, or global properties /// this items depends on have changed. /// Be aware that there is a perf cost in calling this function. /// </summary> public void RefreshProperties() { if (this.IsVirtual) return; itemProject.BuildProject.ReevaluateIfNecessary(); foreach (var projectItem in MSBuildProject.GetItems(itemProject.BuildProject, item.ItemType)) { if (projectItem != null && projectItem.UnevaluatedInclude.Equals(item.UnevaluatedInclude)) { item = projectItem; return; } } }
/// <summary> /// Remove the item from the project /// </summary> public void RemoveFromProjectFile() { projectFile.MSBuildProject.RemoveItem(item); projectFile.MSBuildProject.ReevaluateIfNecessary(); projectFile = null; item = null; }
//===================================================================== /// <summary> /// This constructor is used to wrap an existing reference /// </summary> /// <param name="project">The project that owns the reference</param> /// <param name="existingItem">The existing reference</param> /// <overloads>There are two overloads for the constructor</overloads> internal ProjectReferenceItem(SandcastleProject project, ProjectItem existingItem) : base(project, existingItem) { projectPath = new FilePath(this.Include, this.Project); projectPath.PersistablePathChanging += projectPath_PersistablePathChanging; this.GetProjectMetadata(false); this.Include = projectPath.PersistablePath; }