public void NormalizePaths(bool addImports) { List<_BE.ProjectItem> removeItems = new List<_BE.ProjectItem>(); foreach (_BE.ProjectItem folderItem in m_project.GetItems("Folder")) { string path = folderItem.Xml.Include; if (!path.Equals(GetRelativePath(path), StringComparison.OrdinalIgnoreCase)) { removeItems.Add(folderItem); continue; } if (!Directory.Exists(path)) { removeItems.Add(folderItem); continue; } } foreach (string groupName in ProjectEx.FileGroups) { List<MyImportItems> importItems = new List<MyImportItems>(); foreach (_BE.ProjectItem buildItem in m_project.GetItems(groupName)) { string projectRelativePath = GetRelativePath(buildItem.Xml.Include); // The path has to be an absolute path to somewhere // outside the project directory. if (projectRelativePath == null) { string origPath = Path.Combine(Path.GetDirectoryName(m_origProjPath), buildItem.EvaluatedInclude); if (File.Exists(origPath)) { projectRelativePath = buildItem.Xml.Include; externalFiles.Add(buildItem.Xml.Include, origPath); } else if (File.Exists(BuildTaskUtility.ExpandEnvironmentVariables(buildItem.Xml.Include))) { projectRelativePath = Path.GetFileName(buildItem.Xml.Include); externalFiles.Add( projectRelativePath, BuildTaskUtility.ExpandEnvironmentVariables(buildItem.Xml.Include)); } else if (!buildItem.IsImported) { // since the file doesn't even exist // remove it from the project removeItems.Add(buildItem); continue; } else { // This is an imported item // it can't be removed, so // ignore it. if (Task != null) { Task.Log.LogWarning("Ignoring missing imported build item, \"{0}, {1}\"", buildItem.ItemType, buildItem.Xml.Include); } continue; } } if (!buildItem.IsImported) { // Reset the path to the file relative to // the project buildItem.Xml.Include = projectRelativePath; } else if(addImports) { MyImportItems newItem = new MyImportItems(); newItem.name = buildItem.ItemType; newItem.include = projectRelativePath; newItem.meta = new Dictionary<string,string>(); foreach (_BE.ProjectMetadata meta in buildItem.Metadata) { newItem.meta.Add(meta.Name, meta.UnevaluatedValue); } importItems.Add(newItem); } } foreach (MyImportItems importItem in importItems) { m_project.AddItem(importItem.name, importItem.include, importItem.meta); } } foreach (_BE.ProjectItem removeItem in removeItems) { Task.Log.LogWarning("Removing missing build item, \"{0}, {1}\"", removeItem.ItemType, removeItem.Xml.Include); m_project.RemoveItem(removeItem); } }
public void NormalizePaths(bool addImports) { List <_BE.ProjectItem> removeItems = new List <_BE.ProjectItem>(); foreach (_BE.ProjectItem folderItem in m_project.GetItems("Folder")) { string path = folderItem.Xml.Include; if (!path.Equals(GetRelativePath(path), StringComparison.OrdinalIgnoreCase)) { removeItems.Add(folderItem); continue; } if (!Directory.Exists(path)) { removeItems.Add(folderItem); continue; } } foreach (string groupName in ProjectEx.FileGroups) { List <MyImportItems> importItems = new List <MyImportItems>(); foreach (_BE.ProjectItem buildItem in m_project.GetItems(groupName)) { string projectRelativePath = GetRelativePath(buildItem.Xml.Include); // The path has to be an absolute path to somewhere // outside the project directory. if (projectRelativePath == null) { string origPath = Path.Combine(Path.GetDirectoryName(m_origProjPath), buildItem.EvaluatedInclude); if (File.Exists(origPath)) { projectRelativePath = buildItem.Xml.Include; externalFiles.Add(buildItem.Xml.Include, origPath); } else if (File.Exists(BuildTaskUtility.ExpandEnvironmentVariables(buildItem.Xml.Include))) { projectRelativePath = Path.GetFileName(buildItem.Xml.Include); externalFiles.Add( projectRelativePath, BuildTaskUtility.ExpandEnvironmentVariables(buildItem.Xml.Include)); } else if (!buildItem.IsImported) { // since the file doesn't even exist // remove it from the project removeItems.Add(buildItem); continue; } else { // This is an imported item // it can't be removed, so // ignore it. if (Task != null) { Task.Log.LogWarning("Ignoring missing imported build item, \"{0}, {1}\"", buildItem.ItemType, buildItem.Xml.Include); } continue; } } if (!buildItem.IsImported) { // Reset the path to the file relative to // the project buildItem.Xml.Include = projectRelativePath; } else if (addImports) { MyImportItems newItem = new MyImportItems(); newItem.name = buildItem.ItemType; newItem.include = projectRelativePath; newItem.meta = new Dictionary <string, string>(); foreach (_BE.ProjectMetadata meta in buildItem.Metadata) { newItem.meta.Add(meta.Name, meta.UnevaluatedValue); } importItems.Add(newItem); } } foreach (MyImportItems importItem in importItems) { m_project.AddItem(importItem.name, importItem.include, importItem.meta); } } foreach (_BE.ProjectItem removeItem in removeItems) { Task.Log.LogWarning("Removing missing build item, \"{0}, {1}\"", removeItem.ItemType, removeItem.Xml.Include); m_project.RemoveItem(removeItem); } }