private void AddItemTemplate(Project rootproject) { DTE dte = (DTE)GetService(typeof(DTE)); string projectPath = DteHelper.BuildPath(rootproject); string folderPath = projectPath; if (!string.IsNullOrEmpty(this.Path)) { folderPath += "\\" + this.Path; } ProjectItem prItem = DteHelper.FindItemByPath(dte.Solution, folderPath); if (prItem != null) { this.NewItem = prItem.ProjectItems.AddFromTemplate(this.Template, this.ItemName); } else { Project project = DteHelper.FindProjectByPath(dte.Solution, folderPath); if (project != null) { this.NewItem = project.ProjectItems.AddFromTemplate(this.Template, this.ItemName); } else { throw new InvalidOperationException( String.Format("Cannot find insertion point {0}", folderPath)); } } }
private void AddItemTemplate(Project rootproject) { DTE dte = (DTE)GetService(typeof(DTE)); string projectPath = DteHelper.BuildPath(rootproject); string folderPath = projectPath; if (!string.IsNullOrEmpty(this.Path)) { folderPath = System.IO.Path.Combine(folderPath, this.Path); } ProjectItem prItem = DteHelper.FindItemByPath(dte.Solution, folderPath); if (prItem != null) { this.NewItem = prItem.ProjectItems.AddFromTemplate(this.Template, this.ItemName); } else { Project project = DteHelper.FindProjectByPath(dte.Solution, folderPath); if (project != null) { project.ProjectItems.AddFromTemplate(this.Template, this.ItemName); } else { throw new InvalidOperationException( String.Format(CultureInfo.CurrentUICulture, Resources.InsertionPointException, folderPath)); } } }
// Methods private void AddItemTemplate(Project rootproject) { DTE service = (DTE)this.GetService(typeof(DTE)); string str2 = DteHelper.BuildPath(rootproject); if (!string.IsNullOrEmpty(this.Path)) { str2 = System.IO.Path.Combine(str2, this.Path); } ProjectItem item = DteHelper.FindItemByPath(service.Solution, str2); if (item != null) { this.NewItem = item.ProjectItems.AddFromTemplate(this.Template, this.ItemName); } else { Project project = DteHelper.FindProjectByPath(service.Solution, str2); if (project == null) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentUICulture, "InsertionPointException", new object[] { str2 })); } project.ProjectItems.AddFromTemplate(this.Template, this.ItemName); } }
private void InternalSearchHiddenFilesInFolder(DTE service, Project project, ProjectItem hiveItem) { string pathToProject = Helpers.GetFullPathOfProjectItem(project); if (hiveItem.Kind == EnvDTE.Constants.vsProjectItemKindPhysicalFolder) { string itemFolderPath = Helpers.GetFullPathOfProjectItem(hiveItem); foreach (string childFile in Directory.GetFiles(itemFolderPath)) { //check, if each file in that folder is in the project ProjectItem foundItem = DteHelper.FindItemByPath(service.Solution, childFile); if (foundItem == null) { string relativeFileName = childFile.Substring(pathToProject.Length); Helpers.LogMessage(service, this, "Warning: File " + relativeFileName + " not found in project"); AnalyzeProjectForm form = new AnalyzeProjectForm(relativeFileName); DialogResult result = form.ShowDialog(); if (result == DialogResult.No) { //deletefile Helpers.LogMessage(service, this, "Deleted File " + relativeFileName); File.Delete(childFile); solved++; } else if (result == DialogResult.Yes) { //include in project Helpers.LogMessage(service, this, "Included File " + relativeFileName + " in project"); Helpers.AddFile(hiveItem, childFile); solved++; } else { ignored++; } } } } foreach (ProjectItem childItem in hiveItem.ProjectItems) { InternalSearchHiddenFilesInFolder(service, project, childItem); } }
/// <summary> /// The method that creates a new item from the input string. /// </summary> public override void Execute() { if (ownerItem == null && ownerProject == null) { throw new ArgumentNullException(Properties.Resources.OwnerProjectOrItemRequired); } if (ownerItem != null) { ownerProject = ownerItem.ContainingProject; // If we got a parent item, build the virtual path to it // for the real full path we will work againt. string itemPath = String.Empty; DteHelper.BuildPathFromCollection(ownerProject.ProjectItems, ownerItem, ref itemPath); path = System.IO.Path.Combine(itemPath, path); } DTE vs = GetService <DTE>(); ProjectItem containingFolder; string targetPath; using (EnsureProjectFolderAction ensureFolder = new EnsureProjectFolderAction()) { this.Site.Container.Add(ensureFolder); ensureFolder.Path = path; ensureFolder.OwnerProject = ownerProject; ensureFolder.Execute(); containingFolder = ensureFolder.TargetFolder; } if (containingFolder.Object is Project) { containingFolder = new ProjectProjectItemAdapter((Project)containingFolder.Object); } using (EvaluateExpressionAction evaluateExpression = new EvaluateExpressionAction()) { this.Site.Container.Add(evaluateExpression); evaluateExpression.Expression = path; evaluateExpression.Execute(); targetPath = evaluateExpression.ReturnValue.ToString(); } string targetFileName = System.IO.Path.GetFileName(targetPath); // This may be different than the containingFolder variable, // as the EnsureProjectFolder only ensures folders, not // hierarchy of dependent items. So we need to look for // the actual parent which may be a project item, not a folder. ProjectItem parent = DteHelper.FindItemByPath( vs.Solution, System.IO.Path.Combine( DteHelper.BuildPath(ownerProject), /* This call just strips the last segment in the path. "Directory" * can still be a file name, as in the case of dependent items */ System.IO.Path.GetDirectoryName(targetPath) ) ); if (parent == null) { parent = containingFolder; } Debug.Assert(parent != null, "Didn't find parent with path: " + targetPath); addedItem = DteHelper.FindItemByName(parent.ProjectItems, targetFileName, false); if (addedItem != null) { if (overwrite) { OverwriteFile(vs, addedItem.get_FileNames(1), content); addedItem.Delete(); } } addedItem = DteHelper.FindItemByName(parent.ProjectItems, targetFileName, false); if (addedItem == null) { // At the FS level, dependent files are always inside the folder we determined // above using the EnsureProjectFolderAction. string folderPath = System.IO.Path.Combine( System.IO.Path.GetDirectoryName(ownerProject.FileName), DteHelper.BuildPath(containingFolder)); string fullPath = System.IO.Path.Combine(folderPath, targetFileName); if (File.Exists(fullPath)) { OverwriteFile(vs, fullPath, content); addedItem = parent.ProjectItems.AddFromFile(fullPath); } else { string tempfile = System.IO.Path.GetTempFileName(); try { File.WriteAllText(tempfile, content); addedItem = parent.ProjectItems.AddFromTemplate(tempfile, targetFileName); } finally { File.Delete(tempfile); } } } if (open) { Window wnd = addedItem.Open(Constants.vsViewKindPrimary); wnd.Visible = true; wnd.Activate(); } }
private VsBoundReference GetReferenceTarget(string target, string recipeName) { VsBoundReference vsTarget = null; DTE dte = GetService <DTE>(true); target = DteHelper.ReplaceParameters(target, this.ReplacementDictionary); // Special case for "/" value. if (target.Equals("/") || target.Equals("\\")) { // "/" is the solution if we're in a solution template, the // project it this is a project template or the item if it's an item template. if (this.Project != null) { vsTarget = new ProjectReference(recipeName, this.Project); } else if (this.Item != null) { vsTarget = new ProjectItemReference(recipeName, this.Item); } else { vsTarget = new SolutionReference(recipeName, dte.Solution); } } else if (template.VSKind == WizardRunKind.AsMultiProject) { string pathToTarget = target.Substring(1); if (root != null && root.Object is SolutionFolder) { pathToTarget = DteHelper.BuildPath(root) + target; } ProjectItem prItem = DteHelper.FindItemByPath(dte.Solution, pathToTarget); if (prItem != null) { vsTarget = new ProjectItemReference(recipeName, prItem); } else { // Try finding a project. Project prj = DteHelper.FindProjectByPath(dte.Solution, pathToTarget); if (prj != null) { vsTarget = new ProjectReference(recipeName, prj); } } } else if (template.VSKind == WizardRunKind.AsNewProject && this.Project != null) { ProjectItem prItem = DteHelper.FindItemByPath(dte.Solution, DteHelper.BuildPath(this.Project) + target); // Can only refer to items. if (prItem != null) { vsTarget = new ProjectItemReference(recipeName, prItem); } } else { // We got here because there's an Item template that contains an assetname other than "\". throw new ArgumentException(String.Format( CultureInfo.CurrentCulture, Properties.Resources.Templates_ItemTargetInvalid, Path.GetFileName(template.FileName), target), "Target"); } return(vsTarget); }
/// <summary> /// Converts from a given solution relative path to a corresponding EnvDTE.ProjecItem /// in case it exists in the current solution /// </summary> /// <param name="context"></param> /// <param name="culture"></param> /// <param name="value"></param> /// <param name="throwErrors"></param> /// <returns></returns> protected virtual internal object ConvertFromInternal(ITypeDescriptorContext context, CultureInfo culture, object value, bool throwErrors) { if (value is string) { // The helper requests the service and ensures that it exists. DTE vs = (DTE)ServiceHelper.GetService(context, typeof(DTE), this); if (vs.Solution == null) { if (throwErrors) { throw new InvalidOperationException(Properties.Resources.DteElementConverter_NoSolutionOpened); } else { return(null); } } string path = (string)value; // First determine solution reference. if (path.Length == 0) { return(null); } if (path.Length == 1) { if (path == "\\" || path == "/") { return(vs.Solution); } else { if (throwErrors) { throw new FormatException(Properties.Resources.DteElementConverter_MustStartWithSlash); } else { return(null); } } } else { // Remove slash and try to locate project. if (path.StartsWith("/") || path.StartsWith("\\")) { path = path.Substring(1); } Project project = DteHelper.FindProjectByPath(vs.Solution, path); if (project != null) { return(project); } else { // Try with item. Must find something to be valid. ProjectItem item = DteHelper.FindItemByPath(vs.Solution, path); if (item != null) { return(item); } else { if (throwErrors) { throw new FormatException(Properties.Resources.DteElementConverter_CantFindElement); } else { return(null); } } } } } else { try { return(base.ConvertFrom(context, culture, value)); } catch { if (throwErrors) { throw; } else { return(null); } } } }
public object LocateTarget(IServiceProvider serviceProvider, string serializedData) { DTE vs = (DTE)serviceProvider.GetService(typeof(DTE)); return(DteHelper.FindItemByPath(vs.Solution, serializedData)); }