private void UnfoldDefaultTemplate(VsTemplateLaunchPoint launchPoint) { var templatePath = this.Solution.GetTemplatePath(launchPoint); var vsTemplate = VsTemplateFile.Read(templatePath); var template = this.Templates.Find(launchPoint); this.ThrowIfNotGuidanceTemplate(template); var baseProjectName = vsTemplate.TemplateData.DefaultName; var currentSelection = this.Solution.GetSelection().FirstOrDefault(); var solutionFolder = currentSelection as ISolutionFolder; if (solutionFolder != null) { solutionFolder.Add(GenerateProjectName(solutionFolder.PhysicalPath, baseProjectName), template); } else { this.Solution.Add(GenerateProjectName(this.Solution.PhysicalPath, baseProjectName), template); } if (currentSelection != null) { currentSelection.Select(); } else { this.Solution.Select(); } }
/// <summary> /// Configures the files included in the template for VSIX packaging, /// and returns the Uri for the template. /// </summary> public IVsTemplate Configure(IItem templateItem, string displayName, string description, string path) { Guard.NotNull(() => templateItem, templateItem); // Calculate the new Identifier var unicySeed = Guid.NewGuid().ToString(@"N"); var unicyIdentifier = unicySeed.Substring(unicySeed.Length - MaxUnicyLength); var remainingNamedLength = MaxTemplateIdLength - MaxUnicyLength - 1; var namedIdentifier = path.Substring(path.Length <= remainingNamedLength ? 0 : (path.Length - remainingNamedLength)); var templateId = string.Format(CultureInfo.InvariantCulture, @"{0}-{1}", unicyIdentifier, namedIdentifier); // Update the vstemplate var template = VsTemplateFile.Read(templateItem.PhysicalPath); template.SetTemplateId(templateId); template.SetDefaultName(SanitizeName(displayName)); template.SetName(displayName); template.SetDescription(description); VsHelper.CheckOut(template.PhysicalPath); VsTemplateFile.Write(template); UpdateDirectoryProperties(templateItem); // Set VS attributes on the vstemplate file if (template.Type == VsTemplateType.Item) { templateItem.Data.ItemType = @"ItemTemplate"; } else { templateItem.Data.ItemType = @"ProjectTemplate"; } return(template); }
/// <summary> /// Executes when the wizard starts. /// </summary> public virtual void RunStarted(object automationObject, Dictionary <string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams) { Guard.NotNull(() => customParams, customParams); Guard.NotNull(() => replacementsDictionary, replacementsDictionary); this.TemplateFile = (string)customParams[0]; this.TemplateSchema = VsTemplateFile.Read(this.TemplateFile); this.activity = tracer.StartActivity(Resources.TemplateWizard_Unfolding, this.GetType().Name, this.TemplateFile); }
private void LoadTemplates() { this.templates = new List <IVsTemplate>(); // Get list of vstemplates in installation directories var installationDir = Directory.Exists(this.Extension.InstallPath) ? new DirectoryInfo(this.Extension.InstallPath) : null; if (installationDir != null) { var templateFiles = installationDir.GetFiles("*.zip", SearchOption.AllDirectories); templateFiles.ToList() .ForEach(tf => this.templates.Add(VsTemplateFile.Read(tf.FullName))); } }
/// <summary> /// Returns the installed toolkit that contains the given template. /// </summary> /// <param name="patternManager">The PatternManager service</param> /// <param name="templateFile">The full path to the vstemplate file. /// i.e. (VS2010) %localappdata%\Microsoft\VisualStudio\10.0\Extensions\[author][\[extensionname]\[version]\Assets\Templates\Projects\~PC\Template.zip\Template.vstempate /// i.e. (VS2012) %localappdata%\Microsoft\VisualStudio\11.0\VTC\[guid]\~PC\Projects\Template.zip\Template.vstemplate /// </param> /// <returns></returns> internal static IInstalledToolkitInfo FindToolkitOrThrow(IPatternManager patternManager, string templateFile) { // Find the toolkit that shares the same path as this vstemplate (VS2010 only) var toolkitInfo = patternManager.InstalledToolkits .FirstOrDefault(f => templateFile.StartsWith(f.Extension.InstallPath, StringComparison.OrdinalIgnoreCase)); #if VSVER11 || VSVER12 // In VS2012, vstemplates are loaded from cache not from toolkit installation path. if (toolkitInfo == null) { // Find the toolkit that contains the vstemplate, by TemplateID or Name if (File.Exists(templateFile)) { var template = VsTemplateFile.Read(templateFile); if (template != null) { if (!String.IsNullOrEmpty(template.TemplateData.TemplateID)) { toolkitInfo = patternManager.InstalledToolkits .FirstOrDefault(it => it.Templates.Any(t => ((t.TemplateData.TemplateID != null) && (t.TemplateData.TemplateID.Equals(template.TemplateData.TemplateID, StringComparison.OrdinalIgnoreCase))))); } else { toolkitInfo = patternManager.InstalledToolkits .FirstOrDefault(it => it.Templates.Any(t => ((t.TemplateData.Name != null) && (t.TemplateData.Name.Value.Equals(template.TemplateData.Name.Value, StringComparison.OrdinalIgnoreCase))))); } } } } #endif if (toolkitInfo == null) { throw new InvalidOperationException(string.Format( CultureInfo.CurrentCulture, Resources.InstantiationTemplateWizard_ToolkitInfoNotFound, templateFile)); } return(toolkitInfo); }
internal void ValidateHidden() { if (!string.IsNullOrEmpty(settings.TemplateAuthoringUri)) { IItemContainer item = GetVsTemplateProjectItem(); if (item != null) { //Doing all the validation in one method to avoid the performance impact of reading the file from disc IVsTemplate template = VsTemplateFile.Read(item.PhysicalPath); if (settings.CreateElementOnUnfold && template.TemplateData.Hidden.HasValue && template.TemplateData.Hidden.Value) { //should not be hidden if created on unfold context.LogWarning( string.Format( CultureInfo.CurrentCulture, Resources.Validate_TemplateSettingsHiddenIsInvalidWhenCreateOnUnfold, settings.OwnerElement.Name), Resources.Validate_TemplateSettingsHiddenIsInvalidWhenCreateOnUnfoldCode, settings.SettingsElement as ModelElement); } } } }
internal void ValidateAuthoringUriIsValidAndTemplateIsConfiguredCorrectly(bool validatingTemplate) { try { if (!string.IsNullOrEmpty(settings.TemplateAuthoringUri)) { IItemContainer item = GetVsTemplateProjectItem(); if (item != null) { //Doing all the validation in one method to avoid the performance impact of reading the file from disc IVsTemplate template = VsTemplateFile.Read(item.PhysicalPath); if (template == null) { if (!(settings.OwnerElement.Root.PatternModel.IsInTailorMode())) { context.LogError( string.Format( CultureInfo.CurrentCulture, Resources.Validate_TemplateSettingsAssociatedTemplateIsNotAValidTemplateFile, settings.OwnerElement.Name), Resources.Validate_TemplateSettingsAssociatedTemplateIsNotAValidTemplateFileCode, settings.SettingsElement as ModelElement); } return; } if (!settings.TemplateUri.EndsWith(template.TemplateData.TemplateID, StringComparison.OrdinalIgnoreCase)) { //wrong id context.LogError( string.Format( CultureInfo.CurrentCulture, Resources.Validate_TemplateSettingsTemplateIdDoesNotMatchReferencedTemplate, this.name), Resources.Validate_TemplateSettingsTemplateIdDoesNotMatchReferencedTemplateCode, settings.SettingsElement as ModelElement); } if (!((template.Type == VsTemplateType.Item && settings.TemplateUri.StartsWith(VsTemplateUri.GetUriBase(VsTemplateType.Item), StringComparison.OrdinalIgnoreCase)) || (template.Type == VsTemplateType.Project && settings.TemplateUri.StartsWith(VsTemplateUri.GetUriBase(VsTemplateType.Project), StringComparison.OrdinalIgnoreCase)) || (template.Type == VsTemplateType.ProjectGroup && settings.TemplateUri.StartsWith(VsTemplateUri.GetUriBase(VsTemplateType.ProjectGroup), StringComparison.OrdinalIgnoreCase)))) { //wrong uri for type context.LogError( string.Format( CultureInfo.CurrentCulture, Resources.Validate_TemplateSettingsTemplateTypeDoesNotMatchReferencedTemplate, settings.OwnerElement.Name, template.Type), Resources.Validate_TemplateSettingsTemplateTypeDoesNotMatchReferencedTemplateCode, settings.SettingsElement as ModelElement); } var elementReplacementsWizard = template.WizardExtension.GetExtension(TemplateWizardInfo.ElementReplacementsTemplateWizardFullTypeName); if (validatingTemplate) { var instantiationWizard = template.WizardExtension.GetExtension(TemplateWizardInfo.InstantiationTemplateWizardFullTypeName); if (instantiationWizard == null || elementReplacementsWizard == null) { //wizards not present context.LogWarning( string.Format( CultureInfo.CurrentCulture, Resources.Validate_TemplateSettingsWizardsNotPresentInTemplate, settings.OwnerElement.Name), Resources.Validate_TemplateSettingsWizardsNotPresentInTemplateCode, settings.SettingsElement as ModelElement); } else if (template.WizardExtension.ToList().IndexOf(instantiationWizard) > template.WizardExtension.ToList().IndexOf(elementReplacementsWizard)) { //wizards in wrong order context.LogWarning( string.Format( CultureInfo.CurrentCulture, Resources.Validate_TemplateSettingsWizardOrderIsNotCorrect, settings.OwnerElement.Name), Resources.Validate_TemplateSettingsWizardOrderIsNotCorrectCode, settings.SettingsElement as ModelElement); } } else { if (elementReplacementsWizard == null) { //wizards not present context.LogWarning( string.Format( CultureInfo.CurrentCulture, Resources.Validate_TemplateSettingsWizardsNotPresentInTemplate, settings.OwnerElement.Name), Resources.Validate_TemplateSettingsWizardsNotPresentInTemplateCode, settings.SettingsElement as ModelElement); } } } } } catch (Exception ex) { tracer.Error( ex, Resources.ValidationMethodFailed_Error, Reflector <TemplateValidator> .GetMethod(n => n.ValidateAuthoringUriIsValidAndTemplateIsConfiguredCorrectly(true)).Name); throw; } }
/// <summary> /// Resolves the instance. /// </summary> /// <param name="templateType">The type of the template.</param> /// <param name="templatePath">The template path.</param> protected override IVsTemplate ResolveInstance(VsTemplateType templateType, string templatePath) { return(VsTemplateFile.Read(templatePath)); }