public static VCFile AddRelativeFile(this VCFilter root, string relativepath, string fullpath) { VCFilter cur = root; var dirs = relativepath.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries).ToList(); if (dirs.Count > 1) { for (int i = 0; i < dirs.Count - 1; i++) { string idir = dirs[i]; if (cur != null) { VCFilter next = null; IVCCollection subs = cur.Filters; foreach (VCFilter sub in subs) { if (sub.Name == idir) { next = sub; break; } } if (next != null) { next = cur.AddFilter(idir); cur = next; } } } } return(cur.AddFile(fullpath)); }
/// <summary> /// Add Folder to Specified Project /// </summary> /// <param name="path"></param> private VCFilter AddFolderToProject(Project proj, string dirInfo, string newDir, VCFilter filter) { VCFilter localFilter = filter; try { DirectoryInfo directoryInfo1 = new DirectoryInfo(dirInfo); directoryInfo1.CreateSubdirectory(newDir); if (localFilter == null) { localFilter = (proj.Object as VCProject).AddFilter(newDir); } else { localFilter = localFilter.AddFilter(newDir); } AddSummaryString("Folder Added: " + newDir); } catch (Exception e) { string error = e.Message; } return(localFilter); }
public VCFilter FindOrCreateFilter(VCFilter parent, string name) { foreach (VCFilter f in parent.Filters) { if (f.Name.Equals(name)) { return(f); } } return(parent.AddFilter(name)); }
private void addNewFilterRecursive(VCFilter vcFilter, string dirName, string dir) { string filterName = dirName.Substring(dir.Length + 1); VCFilter newFilter = vcFilter.AddFilter(filterName); // add files string[] fileEntries = Directory.GetFiles(dirName); foreach (string file in fileEntries) { newFilter.AddFile(file); } // add directories as filter string[] dirEntries = Directory.GetDirectories(dirName); foreach (string d in dirEntries) { addNewFilterRecursive(newFilter, d, dirName); } }
public bool toVCFilter(VCFilter parent) { bool hasFiles = files.Count > 0; if (files.Count > 0 || folders.Count > 0) { VCFilter filter = parent.AddFilter(name); foreach (var file in files) { filter.AddFile(Path.Combine(path, file)); } foreach (var folder in folders) { hasFiles |= folder.toVCFilter(filter); } if (!hasFiles) { parent.RemoveFilter(filter); } } return hasFiles; }
/// <summary> /// </summary> protected VCFilter AddFilter(string path) { VCFilter filter = null; var name = string.Empty; var dirs = path.Split(new[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries); foreach (var dir in dirs) { name = Path.Combine(name, dir); var item = ((IVCCollection)Project.Filters).Item(name); if (item != null) { filter = item; continue; } filter = filter == null?Project.AddFilter(dir) : filter.AddFilter(dir); } return(filter); }
public bool toVCFilter(VCFilter parent) { bool hasFiles = files.Count > 0; if (files.Count > 0 || folders.Count > 0) { VCFilter filter = parent.AddFilter(name); foreach (var file in files) { filter.AddFile(Path.Combine(path, file)); } foreach (var folder in folders) { hasFiles |= folder.toVCFilter(filter); } if (!hasFiles) { parent.RemoveFilter(filter); } } return(hasFiles); }
public static void addAddons(VCProject vcproject, String ofRoot, IEnumerable <String> addons) { VCFilter addonsFolder = null; try { addonsFolder = vcproject.AddFilter("addons"); }catch (Exception e) { IVCCollection filters = vcproject.Filters; foreach (var filter in filters) { if (filter is VCFilter) { if (((VCFilter)filter).Name == "addons") { addonsFolder = ((VCFilter)filter); break; } } } } if (addonsFolder != null) { foreach (var addon in addons) { VCFilter addonFolder = addonsFolder.AddFilter(addon); var addonObj = new Addon(ofRoot, addon, vcproject.ProjectDirectory); addonObj.addFilesToVCFilter(addonFolder); addonObj.addIncludePathsToVCProject(vcproject); addonObj.addLibsToVCProject(vcproject); } vcproject.Save(); } else { throw new Exception("Couldn't create or find addonsFolder"); } }
public static ProjectItem AddFileToProject(this Project project, FileInfo newSourceFile, string itemType = null) { string root = project.FullName; if (string.IsNullOrEmpty(root)) { return(null); } // TODO should refactor bunch of stuff here, get this in another place etc... // Fetch the active project EnvDTE.DTE dte = (EnvDTE.DTE)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SDTE)); VCProject prj = (VCProject)dte.Solution.Projects.Item(1).Object; // Get the source root string sourceRoot = string.Empty; IVCCollection projectFiles = prj.Files as IVCCollection; foreach (VCFile prjFile in projectFiles) { // If it's an actual file if (prjFile.FullPath.Contains(".cpp") || prjFile.FullPath.Contains(".h")) { sourceRoot = string.Concat(prjFile.FullPath.TakeWhile((c, i) => c == newSourceFile.DirectoryName[i])); break; } } // This means that the project has no files atm, so we do not know // where the source files should be stored! if (sourceRoot == string.Empty) { MessageBox.Show("Can't find the source files location!"); return(null); } // We can add filters // Find folder string filePath = newSourceFile.DirectoryName.Replace(sourceRoot, ""); // Get rid of the path until source root Queue <string> filterQueue = new Queue <string>(filePath.Split('\\')); // Now add the missing filters ProjectItem item = project.ProjectItems.Item(filterQueue.Peek()); ProjectItem lastItem = item; VCFilter filter = null; IVCCollection projFilters = prj.Filters as IVCCollection; // Initialize filter foreach (VCFilter _filter in projFilters) { if (_filter.CanonicalName.Equals(filterQueue.Peek())) { filter = _filter; break; } } // Root filter if (item == null) { filter = (VCFilter)prj.AddFilter(filterQueue.Peek()); // Reload item item = project.ProjectItems.Item(filterQueue.Peek()); lastItem = item; } string newFilterCanonPath = filterQueue.Peek(); filterQueue.Dequeue(); while (filterQueue.Count > 0) { item = item.ProjectItems.Item(filterQueue.Peek()); if (item == null) { filter = (VCFilter)filter.AddFilter(filterQueue.Peek()); newFilterCanonPath = string.Join("\\", newFilterCanonPath, filterQueue.Peek()); prj.Save(); item = lastItem.ProjectItems.Item(filterQueue.Peek()); } else { newFilterCanonPath = string.Join("\\", newFilterCanonPath, filterQueue.Peek()); // Must update the filter foreach (VCFilter _filter in (IVCCollection)prj.Filters) { if (_filter.CanonicalName.Equals(newFilterCanonPath)) { filter = _filter; break; } } } prj.Save(); lastItem = item; filterQueue.Dequeue(); } //int indexOfProjectNameSubstr = filePath.IndexOf($"{project.Name}", StringComparison.OrdinalIgnoreCase); //string filterString = filePath.Substring(indexOfProjectNameSubstr + project.Name.Length + 1); // +1 for \\ //var filters = filterString.Split('\\'); //// Add multilevel filters //ProjectItem item = project.ProjectItems.Item(filters[0]); //ProjectItem lastItem = item; //EnvDTE.DTE dte = (EnvDTE.DTE)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SDTE)); //VCProject prj = (VCProject)dte.Solution.Projects.Item(1).Object; //if (item == null) //{ // // Top level dir // prj.AddFilter(filters[0]); // item = project.ProjectItems.Item(filters[0]); //} //for (int i = 1; i < filters.Length; i++) //{ // item = item.ProjectItems.Item(filters[i]); // if (item == null) // { // //string canonicalFilterName = Path.Combine(filters.Take(i + 1).ToArray()); // string canonicalFilterName = string.Join(Path.DirectorySeparatorChar.ToString(), filters.Take(i).ToArray()); // //prj.AddFilter(canonicalFilterName); // foreach (VCFilter fltr in prj.Filters) // { // if (fltr.CanonicalName == canonicalFilterName) // { // // to i + 1 to add the next filter // for (int j = i; j < filters.Length; j++) // { // if (fltr.CanAddFilter(filters[i])) // { // fltr.AddFilter(filters[j]); // } // } // } // } // prj.Save(); // item = lastItem.ProjectItems.Item(filters[i]); // } // lastItem = item; //} //filter.AddFile(newSourceFile.FullName); lastItem.ProjectItems.AddFromFile(newSourceFile.FullName); lastItem.SetItemType(itemType); return(item); }
private void addNewFilterRecursive(VCFilter vcFilter, string dirName, string dir) { string filterName = dirName.Substring(dir.Length + 1); VCFilter newFilter = vcFilter.AddFilter(filterName); // add files string[] fileEntries = Directory.GetFiles(dirName); foreach (string file in fileEntries) { newFilter.AddFile(file); } // add directories as filter string[] dirEntries = Directory.GetDirectories(dirName); foreach (string d in dirEntries) { addNewFilterRecursive(newFilter, d, dirName); } }