public static void DeleteProjectItem(Project project, string relativePathAndName, bool deleteFile, out FileStateInfo fileStateInfo) { fileStateInfo = new FileStateInfo(); fileStateInfo.FileState = FileStateConstants.Failed; var currentProjectFile = new FileInfo(project.FileName); relativePathAndName = relativePathAndName.TrimStart(new char[] { '\\' }); var fullName = Path.Combine(currentProjectFile.Directory.FullName, relativePathAndName); fullName = fullName.Replace(@"\\", @"\"); fileStateInfo.FileName = fullName; var newFile = new FileInfo(fullName); fileStateInfo.FileState = FileStateConstants.Failed; if (newFile.Exists) { var p = FindProjectItemByFileName(project.ProjectItems, fullName); if (p != null) { p.Remove(); } if (deleteFile) { try { File.Delete(fullName); } catch { } } } }
public ProjectItem AddProjectItem(ProjectItem parent, string fileName, out FileStateInfo fileStateInfo) { fileStateInfo = new FileStateInfo(); try { fileStateInfo.FileName = fileName; var newItem = parent.ProjectItems.AddFromFileCopy(fileName); fileStateInfo.FileState = FileStateConstants.Success; return(newItem); } catch (System.Runtime.InteropServices.COMException ex) { if (ex.Message.ToLower().Contains("already exists")) { fileStateInfo.FileState = FileStateConstants.Skipped; } else { fileStateInfo.FileState = FileStateConstants.Failed; } return(null); } catch (Exception ex) { fileStateInfo.FileState = FileStateConstants.Failed; throw; } }
public ProjectItem AddProjectItem(Project project, ProjectItem parent, string fileName, string content, bool overwrite, out FileStateInfo fileStateInfo) { try { BuildCache(project); fileStateInfo = new FileStateInfo(); var fi = new FileInfo(this.GetFileName(parent)); var fullName = Path.Combine(fi.DirectoryName, fileName); fileStateInfo.FileName = fullName; try { var fi2 = new FileInfo(fullName); if (File.Exists(fullName) && !overwrite) { fileStateInfo.FileState = FileStateConstants.Skipped; return(null); } else if (fi2.Exists && (fi2.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { fileStateInfo.FileState = FileStateConstants.Failed; return(null); } else { File.WriteAllText(fullName, content); fileStateInfo.FileState = FileStateConstants.Success; } } catch (Exception ex) { fileStateInfo.FileState = FileStateConstants.Failed; //GlobalHelper.ShowError(ex); } ProjectItem newItem = null; if (_projectCache[project].Select(x => x.Name).Contains(fullName.ToLower())) { //Do Nothing } else { if (_projectItemCache.ContainsKey(fullName)) { newItem = _projectItemCache[fullName]; } else { newItem = parent.ProjectItems.AddFromFile(fullName); } } return(newItem); } catch (Exception ex) { throw; } }
public ProjectItem AddFileAsProjectItem(Project project, string fileName, string relativePathAndName, bool overwrite, out FileStateInfo fileStateInfo) { try { fileStateInfo = new FileStateInfo(); fileStateInfo.FileState = FileStateConstants.Failed; FileInfo newFile = null; if (project.Kind == Constants.vsProjectKindUnmodeled) { var applicationObject = (DTE)EnvDTEHelper.Instance._applicationObject; //Handle on Solution Explorer window var slnExplorer = applicationObject.Windows.Item(Constants.vsWindowKindSolutionExplorer); var slnHierarchy = (UIHierarchy)slnExplorer.Object; slnExplorer.Activate(); var dbProject = EnvDTEHelper.Instance.Find(project); dbProject.Select(vsUISelectionType.vsUISelectionTypeSelect); } else { var currentProjectFile = new FileInfo(project.FileName); var fullName = StringHelper.EnsureDirectorySeparatorAtEnd(currentProjectFile.Directory.FullName) + relativePathAndName; fullName = fullName.Replace(@"\\", @"\"); fileStateInfo.FileName = fullName; newFile = new FileInfo(fullName); if (newFile.Exists && !overwrite) { fileStateInfo.FileState = FileStateConstants.Skipped; return(null); } else if (newFile.Exists && (newFile.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { fileStateInfo.FileState = FileStateConstants.Failed; return(null); } if (!newFile.Directory.Exists) { newFile.Directory.Create(); } File.Copy(fileName, newFile.FullName); } try { fileStateInfo.FileState = FileStateConstants.Success; return(project.ProjectItems.AddFromFile(newFile.FullName)); } catch (Exception ex) { fileStateInfo.FileState = FileStateConstants.Failed; return(null); } } catch (Exception ex) { throw; } }
public void DeleteProjectItem(ProjectItem parent, string fileName, out FileStateInfo fileStateInfo) { fileStateInfo = new FileStateInfo(); var fi = new FileInfo(this.GetFileName(parent)); var fullName = Path.Combine(fi.DirectoryName, fileName); fullName = fullName.Replace(@"\\", @"\"); fileStateInfo.FileName = fullName; var p = parent.ProjectItems.Item(0); //var newItem = parent.ProjectItems.AddFromFile(fullName); fileStateInfo.FileState = FileStateConstants.Success; }
/// <summary> /// Just gen file with no project /// </summary> public void AddProjectItem(string fileContent, byte[] fileContentBinary, ProjectItemContentType contentType, string relativePathAndName, bool overwrite, out FileStateInfo fileStateInfo) { try { fileStateInfo = new FileStateInfo(); fileStateInfo.FileState = FileStateConstants.Failed; FileInfo newFile = null; var fullName = relativePathAndName; fullName = fullName.Replace(@"\\", @"\"); fileStateInfo.FileName = fullName; newFile = new FileInfo(fullName); if (newFile.Exists && !overwrite) { fileStateInfo.FileState = FileStateConstants.Skipped; } else { if (!newFile.Directory.Exists) { newFile.Directory.Create(); } if (contentType == ProjectItemContentType.String) { using (var sw = newFile.CreateText()) { sw.Write(fileContent); } } else { File.WriteAllBytes(newFile.FullName, fileContentBinary); } fileStateInfo.FileState = FileStateConstants.Success; } } catch (Exception ex) { throw; } }
private void projectItemGenerator_ProjectItemGenerated(object sender, ProjectItemGeneratedEventArgs e) { //.NET is a single-threaded application so we much release to let the UI refresh sometimes. if (_doEventCount % 10 == 0) Application.DoEvents(); _doEventCount++; try { const string solutionDirReplaceText="$(solutiondir)"; if (e.FullName != null && e.FullName.ToLower().Contains(solutionDirReplaceText)) { var ti = e.FullName.ToLower().IndexOf(solutionDirReplaceText); e.FullName = e.FullName.Remove(ti, solutionDirReplaceText.Length).Insert(ti, EnvDTEHelper.Instance.SolutionDirectory.FullName); } if (e.ProjectItemName != null && e.ProjectItemName.ToLower().Contains(solutionDirReplaceText)) { var ti = e.ProjectItemName.ToLower().IndexOf(solutionDirReplaceText); e.ProjectItemName = e.ProjectItemName.Remove(ti, solutionDirReplaceText.Length).Insert(ti, EnvDTEHelper.Instance.SolutionDirectory.FullName); } //Get the parent project if one exists Project project = null; ProjectItem parent = null; if (!string.IsNullOrEmpty(e.ProjectName)) { if (projectCache.ContainsKey(e.ProjectName)) { var p = projectCache[e.ProjectName]; //Test this COM object to ensure it has not expired. //If error, do nothing and the project will be requeried try { var s = p.Name; project = p; } catch (Exception) { //Do Nothing projectCache.Remove(e.ProjectName); } } var fromCache = true; if (project == null) { fromCache = false; project = EnvDTEHelper.Instance.GetProject(e.ProjectName); projectCache.Add(e.ProjectName, project); } parent = EnvDTEHelper.Instance.GetProjectItem(e.ProjectName, e.ParentItemName, e.ParentItemType); //This should not happen. If do dump the cache project and requery if (parent == null && fromCache) { if (projectCache.ContainsKey(e.ProjectName)) projectCache.Remove(e.ProjectName); project = EnvDTEHelper.Instance.GetProject(e.ProjectName); projectCache.Add(e.ProjectName, project); parent = EnvDTEHelper.Instance.GetProjectItem(e.ProjectName, e.ParentItemName, e.ParentItemType); } } var fileStateInfo = new FileStateInfo(); ProjectItem projectItem = null; if (e.ParentItemName != string.Empty) { if (e.ContentType == ProjectItemContentType.String) projectItem = EnvDTEHelper.Instance.AddProjectItem(project, parent, e.ProjectItemName, e.ProjectItemContent, e.Overwrite, out fileStateInfo); else { projectItem = EnvDTEHelper.Instance.AddProjectItem(parent, e.ProjectItemContent, out fileStateInfo); } if (fileStateInfo.FileState == EnvDTEHelper.FileStateConstants.Success) EnvDTEHelper.SetProperties(projectItem, e.Properties); } else { if (e.ContentType == ProjectItemContentType.String || e.ContentType == ProjectItemContentType.Binary) { if (project == null) EnvDTEHelper.Instance.AddProjectItem(e.ProjectItemContent, e.ProjectItemBinaryContent, e.ContentType, e.ProjectItemName, e.Overwrite, out fileStateInfo); else projectItem = EnvDTEHelper.Instance.AddProjectItem(project, e.ProjectItemContent, e.ProjectItemBinaryContent, e.ContentType, e.ProjectItemName, e.Overwrite, out fileStateInfo); } else { projectItem = EnvDTEHelper.Instance.AddFileAsProjectItem(project, e.ProjectItemContent, e.ProjectItemName, e.Overwrite, out fileStateInfo); } if (fileStateInfo.FileState == EnvDTEHelper.FileStateConstants.Success) EnvDTEHelper.SetProperties(projectItem, e.Properties); } //TEMP processedFiles.Add(fileStateInfo.FileName); //TEMP //Custom Tool Functionality if (e.RunCustomTool && projectItem != null) { if (!string.IsNullOrEmpty(e.CustomToolName)) { EnvDTEHelper.SetProperty(projectItem, "Generator", e.CustomToolName); EnvDTEHelper.SetProperty(projectItem, "CustomTool", e.CustomToolName); } //Try to run the custom tool try { var vsProjectItem = projectItem.Object as VSLangProj.VSProjectItem; if (vsProjectItem != null) vsProjectItem.RunCustomTool(); } catch { //Do Nothing } } #region Compute CRC //System.IO.StreamReader sr = System.IO.File.OpenText(""); //nHydrate.Generator.Common.Util.CRC32HashAlgorithm hash = new CRC32HashAlgorithm(); //byte[] arr = hash.ComputeHash(sr.BaseStream); //sr.Close(); //XmlHelper.AddAttribute(newfileNode, "crc", "0"); #endregion if (fileStateInfo.FileName == string.Empty) { System.Diagnostics.Debug.Write(string.Empty); } if (fileStateInfo.FileState == EnvDTEHelper.FileStateConstants.Failed) { System.Diagnostics.Debug.Write(string.Empty); } //Write Log nHydrateLog.LogInfo("Project Item Generated: {0}", e.ProjectItemName); e.FileState = fileStateInfo.FileState; e.FullName = fileStateInfo.FileName; this.OnProjectItemGenerated(sender, e); } catch (Exception ex) { this.OnProjectItemGeneratedError(this, e); nHydrateLog.LogWarning(ex); } }
private void projectItemGenerator_ProjectItemDeleted(object sender, ProjectItemDeletedEventArgs e) { try { var project = EnvDTEHelper.Instance.GetProject(e.ProjectName); var parent = EnvDTEHelper.Instance.GetProjectItem(e.ProjectName, e.ParentItemName, e.ParentItemType); var fileStateInfo = new FileStateInfo(); if (e.ParentItemName != string.Empty) { EnvDTEHelper.Instance.DeleteProjectItem(parent, e.ProjectItemName, out fileStateInfo); } else { EnvDTEHelper.DeleteProjectItem(project, e.ProjectItemName, out fileStateInfo); } Application.DoEvents(); //Write Log nHydrateLog.LogInfo("Project Item Deleted: {0}", e.ProjectItemName); e.FileState = fileStateInfo.FileState; e.FullName = fileStateInfo.FileName; //this.OnProjectItemGenerated(sender, e); } catch (Exception ex) { //this.OnProjectItemGeneratedError(this, e); nHydrateLog.LogWarning(ex); } }
public void DeleteProjectItem(ProjectItem parent, string fileName, out FileStateInfo fileStateInfo) { fileStateInfo = new FileStateInfo(); var fi = new FileInfo(this.GetFileName(parent)); var fullName = Path.Combine(fi.DirectoryName, fileName); fullName = fullName.Replace(@"\\", @"\"); fileStateInfo.FileName = fullName; var p = parent.ProjectItems.Item(0); //var newItem = parent.ProjectItems.AddFromFile(fullName); fileStateInfo.FileState = FileStateConstants.Success; }
public ProjectItem AddProjectItem(Project project, ProjectItem parent, string fileName, string content, bool overwrite, out FileStateInfo fileStateInfo) { try { BuildCache(project); fileStateInfo = new FileStateInfo(); var fi = new FileInfo(this.GetFileName(parent)); var fullName = Path.Combine(fi.DirectoryName, fileName); fileStateInfo.FileName = fullName; try { var fi2 = new FileInfo(fullName); if (File.Exists(fullName) && !overwrite) { fileStateInfo.FileState = FileStateConstants.Skipped; return null; } else if (fi2.Exists && (fi2.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { fileStateInfo.FileState = FileStateConstants.Failed; return null; } else { File.WriteAllText(fullName, content); fileStateInfo.FileState = FileStateConstants.Success; } } catch (Exception ex) { fileStateInfo.FileState = FileStateConstants.Failed; //GlobalHelper.ShowError(ex); } ProjectItem newItem = null; if (_projectCache[project].Select(x => x.Name).Contains(fullName.ToLower())) { //Do Nothing } else { if (_projectItemCache.ContainsKey(fullName)) newItem = _projectItemCache[fullName]; else newItem = parent.ProjectItems.AddFromFile(fullName); } return newItem; } catch (Exception ex) { throw; } }
public ProjectItem AddProjectItem(ProjectItem parent, string fileName, out FileStateInfo fileStateInfo) { fileStateInfo = new FileStateInfo(); try { fileStateInfo.FileName = fileName; var newItem = parent.ProjectItems.AddFromFileCopy(fileName); fileStateInfo.FileState = FileStateConstants.Success; return newItem; } catch (System.Runtime.InteropServices.COMException ex) { if (ex.Message.ToLower().Contains("already exists")) fileStateInfo.FileState = FileStateConstants.Skipped; else fileStateInfo.FileState = FileStateConstants.Failed; return null; } catch (Exception ex) { fileStateInfo.FileState = FileStateConstants.Failed; throw; } }
public ProjectItem AddProjectItem(Project project, string fileContent, byte[] fileContentBinary, ProjectItemContentType contentType, string relativePathAndName, bool overwrite, out FileStateInfo fileStateInfo) { try { BuildCache(project); fileStateInfo = new FileStateInfo(); fileStateInfo.FileState = FileStateConstants.Failed; FileInfo newFile = null; if (project.Kind == Constants.vsProjectKindUnmodeled) { var applicationObject = (DTE) EnvDTEHelper.Instance._applicationObject; //Handle on Solution Explorer window var slnExplorer = applicationObject.Windows.Item(Constants.vsWindowKindSolutionExplorer); var slnHierarchy = (UIHierarchy) slnExplorer.Object; slnExplorer.Activate(); var dbProject = EnvDTEHelper.Instance.Find(project); dbProject.Select(vsUISelectionType.vsUISelectionTypeSelect); } else { var currentProjectFile = new FileInfo(project.FileName); var fullName = StringHelper.EnsureDirectorySeperatorAtEnd(currentProjectFile.Directory.FullName) + relativePathAndName; fullName = fullName.Replace(@"\\", @"\"); fileStateInfo.FileName = fullName; newFile = new FileInfo(fullName); if (newFile.Exists && !overwrite) { fileStateInfo.FileState = FileStateConstants.Skipped; if (_projectCache[project].Select(x => x.Name).Contains(fullName.ToLower())) { //Do Nothing return null; } else { return project.ProjectItems.AddFromFile(newFile.FullName); } } else if (newFile.Exists && (newFile.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { fileStateInfo.FileState = FileStateConstants.Failed; return null; } if (!newFile.Directory.Exists) newFile.Directory.Create(); if (contentType == ProjectItemContentType.String) { using (var sw = newFile.CreateText()) { sw.Write(fileContent); } } else { File.WriteAllBytes(newFile.FullName, fileContentBinary); } } try { fileStateInfo.FileState = FileStateConstants.Success; if (_projectCache[project].Select(x => x.Name).Contains(newFile.FullName.ToLower())) { //Do Nothing return null; } else { return project.ProjectItems.AddFromFile(newFile.FullName); } } catch (Exception ex) { fileStateInfo.FileState = FileStateConstants.Failed; return null; } } catch (Exception ex) { throw; } }
/// <summary> /// Just gen file with no project /// </summary> public void AddProjectItem(string fileContent, byte[] fileContentBinary, ProjectItemContentType contentType, string relativePathAndName, bool overwrite, out FileStateInfo fileStateInfo) { try { fileStateInfo = new FileStateInfo(); fileStateInfo.FileState = FileStateConstants.Failed; FileInfo newFile = null; var fullName = relativePathAndName; fullName = fullName.Replace(@"\\", @"\"); fileStateInfo.FileName = fullName; newFile = new FileInfo(fullName); if (newFile.Exists && !overwrite) { fileStateInfo.FileState = FileStateConstants.Skipped; } else { if (!newFile.Directory.Exists) newFile.Directory.Create(); if (contentType == ProjectItemContentType.String) { using (var sw = newFile.CreateText()) { sw.Write(fileContent); } } else { File.WriteAllBytes(newFile.FullName, fileContentBinary); } fileStateInfo.FileState = FileStateConstants.Success; } } catch (Exception ex) { throw; } }
public ProjectItem AddFileAsProjectItem(Project project, string fileName, string relativePathAndName, bool overwrite, out FileStateInfo fileStateInfo) { try { fileStateInfo = new FileStateInfo(); fileStateInfo.FileState = FileStateConstants.Failed; FileInfo newFile = null; if (project.Kind == Constants.vsProjectKindUnmodeled) { var applicationObject = (DTE) EnvDTEHelper.Instance._applicationObject; //Handle on Solution Explorer window var slnExplorer = applicationObject.Windows.Item(Constants.vsWindowKindSolutionExplorer); var slnHierarchy = (UIHierarchy) slnExplorer.Object; slnExplorer.Activate(); var dbProject = EnvDTEHelper.Instance.Find(project); dbProject.Select(vsUISelectionType.vsUISelectionTypeSelect); } else { var currentProjectFile = new FileInfo(project.FileName); var fullName = StringHelper.EnsureDirectorySeperatorAtEnd(currentProjectFile.Directory.FullName) + relativePathAndName; fullName = fullName.Replace(@"\\", @"\"); fileStateInfo.FileName = fullName; newFile = new FileInfo(fullName); if (newFile.Exists && !overwrite) { fileStateInfo.FileState = FileStateConstants.Skipped; return null; } else if (newFile.Exists && (newFile.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { fileStateInfo.FileState = FileStateConstants.Failed; return null; } if (!newFile.Directory.Exists) newFile.Directory.Create(); File.Copy(fileName, newFile.FullName); } try { fileStateInfo.FileState = FileStateConstants.Success; return project.ProjectItems.AddFromFile(newFile.FullName); } catch (Exception ex) { fileStateInfo.FileState = FileStateConstants.Failed; return null; } } catch (Exception ex) { throw; } }
//public static ProjectItem AddProjectItem(Project project, string fileContent, string relativePathAndName, out FileStateInfo fileStateInfo) //{ // return AddProjectItem(project, fileContent, relativePathAndName, true, out fileStateInfo); //} public static void DeleteProjectItem(Project project, string relativePathAndName, out FileStateInfo fileStateInfo) { fileStateInfo = new FileStateInfo(); fileStateInfo.FileState = FileStateConstants.Failed; var currentProjectFile = new FileInfo(project.FileName); relativePathAndName = relativePathAndName.TrimStart(new char[] {'\\'}); var fullName = Path.Combine(currentProjectFile.Directory.FullName, relativePathAndName); fullName = fullName.Replace(@"\\", @"\"); fileStateInfo.FileName = fullName; var newFile = new FileInfo(fullName); fileStateInfo.FileState = FileStateConstants.Failed; if (newFile.Exists) { var p = FindProjectItemByFileName(project.ProjectItems, fullName); if (p != null) p.Remove(); } }
public ProjectItem AddProjectItem(Project project, string fileContent, byte[] fileContentBinary, ProjectItemContentType contentType, string relativePathAndName, bool overwrite, out FileStateInfo fileStateInfo) { try { BuildCache(project); fileStateInfo = new FileStateInfo(); fileStateInfo.FileState = FileStateConstants.Failed; FileInfo newFile = null; if (project.Kind == Constants.vsProjectKindUnmodeled) { var applicationObject = (DTE)EnvDTEHelper.Instance._applicationObject; //Handle on Solution Explorer window var slnExplorer = applicationObject.Windows.Item(Constants.vsWindowKindSolutionExplorer); var slnHierarchy = (UIHierarchy)slnExplorer.Object; slnExplorer.Activate(); var dbProject = EnvDTEHelper.Instance.Find(project); dbProject.Select(vsUISelectionType.vsUISelectionTypeSelect); } else { var currentProjectFile = new FileInfo(project.FileName); var fullName = StringHelper.EnsureDirectorySeparatorAtEnd(currentProjectFile.Directory.FullName) + relativePathAndName; fullName = fullName.Replace(@"\\", @"\"); fileStateInfo.FileName = fullName; newFile = new FileInfo(fullName); if (newFile.Exists && !overwrite) { fileStateInfo.FileState = FileStateConstants.Skipped; if (_projectCache[project].Select(x => x.Name).Contains(fullName.ToLower())) { //Do Nothing return(null); } else { return(project.ProjectItems.AddFromFile(newFile.FullName)); } } else if (newFile.Exists && (newFile.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { fileStateInfo.FileState = FileStateConstants.Failed; return(null); } if (!newFile.Directory.Exists) { newFile.Directory.Create(); } if (contentType == ProjectItemContentType.String) { using (var sw = newFile.CreateText()) { sw.Write(fileContent); } } else { File.WriteAllBytes(newFile.FullName, fileContentBinary); } } try { fileStateInfo.FileState = FileStateConstants.Success; if (_projectCache[project].Select(x => x.Name).Contains(newFile.FullName.ToLower())) { //Do Nothing return(null); } else { return(project.ProjectItems.AddFromFile(newFile.FullName)); } } catch (Exception ex) { fileStateInfo.FileState = FileStateConstants.Failed; return(null); } } catch (Exception ex) { throw; } }