/// <summary> /// Gets the <see cref="IVsTemplate"/> interface once it is sited. /// <seealso cref="AssetReference.OnSited"/> /// </summary> protected override void OnSited() { base.OnSited(); IConfigurationService config = GetService <IConfigurationService>(true); // Only look for base path if it's relative. if (!Path.IsPathRooted(base.AssetName)) { base.AssetName = new FileInfo(Path.Combine( config.BasePath + @"\Templates\", base.AssetName)).FullName; base.AssetName = new CompatibleUri(base.AssetName).LocalPath; } if (!File.Exists(this.AssetName)) { // Template does not exist, check with the Guidance package and update the template path int relPathStartBefore = 0; string probeTemplateName = this.AssetName; while (!File.Exists(this.AssetName)) { int relPathStart = probeTemplateName.IndexOf(@"\Templates\", relPathStartBefore, StringComparison.InvariantCultureIgnoreCase); if (relPathStart == -1 || relPathStart <= relPathStartBefore) { throw new FileNotFoundException(Properties.Resources.Templates_TemplateNotFound, this.AssetName); } relPathStartBefore = relPathStart; string relPath = probeTemplateName.Substring(relPathStart + 1); this.AssetName = Path.Combine(config.BasePath, relPath); } } IVsTemplatesService infosvc = GetService <IVsTemplatesService>(true); this.template = infosvc.GetTemplate(base.AssetName); }
private void LoadTemplateData(Dictionary <string, string> replacementsDictionary, string templateFileName) { try { templateFileName = new CompatibleUri(templateFileName).LocalPath; this.templateDictionary = new TemplateDictionaryService(replacementsDictionary); IVsTemplatesService templatesService = (IVsTemplatesService)GetService(typeof(IVsTemplatesService)); this.template = templatesService.GetTemplate(templateFileName); } catch (Exception e) { ErrorHelper.Show((IUIService)GetService(typeof(IUIService)), e, Properties.Resources.Templates_InvalidWizardData); throw new WizardCancelledException(); } }
private void InitializeTemplateCommands() { IMenuCommandService mcs = GetService <IMenuCommandService>(true); IVsTemplatesService templatesService = GetService <IVsTemplatesService>(true); Guid guidancePackageGuid = new Guid(guidancePackage.Configuration.Guid); IAssetDescription[] templates = templatesService.GetHostAssets(guidancePackage.BasePath); foreach (IVsTemplate template in templates) { if (template.Kind != TemplateKind.Solution) { MenuCommand menuCmd = new TemplateMenuCommand(template, this.guidancePackage, this); mcs.AddCommand(menuCmd); commands.Add(menuCmd); } } }
private bool IsTemplateVisible(string templateFileName, Guid projectFactory) { if (!File.Exists(templateFileName)) { return(false); } string folder = Path.GetDirectoryName(templateFileName); if (IsCustomTemplateFolder(folder, projectFactory)) { try { IVsTemplatesService templatesService = (IVsTemplatesService)GetService(typeof(IVsTemplatesService)); string[] vszParts = Path.GetFileNameWithoutExtension(templateFileName).Split('x'); if (vszParts.Length == 2) { Guid package = new Guid(vszParts[0]); int iTemplate = int.Parse(vszParts[1], NumberStyles.HexNumber); IVsTemplate template = templatesService.GetTemplate(package, iTemplate); if (projectFactory.Equals(Guid.Empty)) { return(template != null && template.IsVisibleInAddNewDialogBox); } else { return(template != null && template.IsVisibleInAddNewDialogBox && template.ProjectFactory.Equals(projectFactory)); } } } catch { return(false); } return(false); } return(true); }
void EnvDTE.IDTWizard.Execute(object Application, int hwndOwner, ref object[] ContextParams, ref object[] CustomParams, ref EnvDTE.wizardResult retval) { IVsTemplate vsTemplate = null; try { DTE dte = new DTETemplate((DTE)Application); CustomParams[0] = Environment.ExpandEnvironmentVariables((string)CustomParams[0]); if (!Path.IsPathRooted((string)CustomParams[0]) && CustomParams.Length >= 2) { var guidancePackageName = (string)CustomParams[1]; var guidancePackageConfigurationFile = RecipeManager.GetConfigurationFile(RecipeManagerPackage.Singleton, guidancePackageName); if (!string.IsNullOrEmpty(guidancePackageConfigurationFile)) { var template = Path.Combine(Path.GetDirectoryName(guidancePackageConfigurationFile), (string)CustomParams[0]); if (File.Exists(template)) { CustomParams[0] = template; } } } string templateFileName = (string)CustomParams[0]; IVsTemplatesService templateService = (IVsTemplatesService) ServiceHelper.GetService( (IRecipeManagerService) new VsServiceProvider(Application).GetService(typeof(IRecipeManagerService)), typeof(IVsTemplatesService), this); vsTemplate = templateService.GetTemplate(templateFileName); string wizardKind = ((string)ContextParams[0]).ToUpper(CultureInfo.InvariantCulture); if (wizardKind == Constants.vsWizardNewProject) { string destDir = (string)ContextParams[2]; //Web projects can pass in an empty directory, if so don't create the dest directory. if ((new System.Uri(destDir)).IsFile) { Directory.CreateDirectory(destDir); } //If adding the project as exclusive, close the current solution then check to see if a // solution name is specified. If so, then create Ona solution with that name. if (((bool)ContextParams[4]) == true) { vsPromptResult promptResult = dte.ItemOperations.PromptToSave; if (promptResult == vsPromptResult.vsPromptResultCancelled) { retval = wizardResult.wizardResultCancel; return; } dte.Solution.Close(false); if (string.IsNullOrEmpty(((string)ContextParams[5])) == false) { dte.Solution.Create(destDir, ((string)ContextParams[5])); } ContextParams[4] = false; } // Create a new Solution Folder for the multiproject template else if (vsTemplate.VSKind == WizardRunKind.AsMultiProject) { string folderName = (string)ContextParams[1]; if (dte.SelectedItems.Count == 1) { object item = DteHelper.GetTarget(dte); if (item is Solution2) { ((Solution2)item).AddSolutionFolder(folderName); } else if (item is Project) { SolutionFolder slnFolder = (SolutionFolder)(((Project)item).Object); slnFolder.AddSolutionFolder(folderName); } } } } // Pre-fill state with context parameters. context = new System.Collections.Specialized.HybridDictionary(); // See http://msdn.microsoft.com/library/en-us/vsintro7/html/vxlrfcontextparamsenum.asp string kind = ((string)ContextParams[0]).ToUpper(); if (kind == Constants.vsWizardNewProject.ToUpper()) { FillNewProject(ContextParams, context); } else if (kind == Constants.vsWizardAddSubProject.ToUpper()) { FillAddSubProject(ContextParams, context); } else if (kind == Constants.vsWizardAddItem.ToUpper()) { FillAddItem(ContextParams, context); } IDTWizard wizard = new Microsoft.VisualStudio.TemplateWizard.Wizard(); wizard.Execute(dte, hwndOwner, ref ContextParams, ref CustomParams, ref retval); } catch (Exception ex) { retval = wizardResult.wizardResultCancel; if (!(ex is COMException) || ((COMException)ex).ErrorCode != VSConstants.E_ABORT) { ErrorHelper.Show(this.Site, ex); } } finally { Debug.Assert(UnfoldTemplate.UnfoldingTemplates.Count == 0); UnfoldTemplate.UnfoldingTemplates.Clear(); } }