Skip to content

Instantly share code, notes, and snippets.

@codewithtyler
Last active August 29, 2015 14:26
Show Gist options
  • Save codewithtyler/c99b24b0919e63d57c19 to your computer and use it in GitHub Desktop.
Save codewithtyler/c99b24b0919e63d57c19 to your computer and use it in GitHub Desktop.
Implementing a wizard for use with Google Analytics
<#
# Name: add-telmetrywizard.ps1
# Author: Tyler Hughes (RandomlyKnighted)
# Purpose: This script recursively goes through all VSTemplate files located in the SideWaffle project adding a Wizard
# to each template. After implementation the wizard when used will use Google Analytics to help keep track of
# what templates are most commonly used.
# Last Updated: August 6, 2015
#>
$docsFolder = [environment]::getfolderpath("mydocuments") + "\Visual Studio 2015\Projects\side-waffle"
$files = Get-ChildItem $docsFolder *.vstemplate -Recurse | % { $_.FullName }
foreach($filePath in $files)
{
[xml]$templateXml = Get-Content $filePath
$updatedFile = $false
'Processing [{0}]' -f $filePath | Write-Host
' Adding Telemetry Wizard' | Write-Host
# Create the CustomParameters element
$customParameters = $templateXml.CreateElement($null,'CustomParameters','http://schemas.microsoft.com/developer/vstemplate/2005')
# Create the parameter elements that will pass the data to the wizard
$nameParameter = $templateXml.CreateElement($null, 'CustomParameter', 'http://schemas.microsoft.com/developer/vstemplate/2005')
$guidParameter = $templateXml.CreateElement($null, 'CustomParameter', 'http://schemas.microsoft.com/developer/vstemplate/2005')
# Get the name of the template from the VSTemplate file then create new GUID for the TemplateID
$templateName = $templateXml.VSTemplate.TemplateData.Name
$guid = [guid]::NewGuid()
# Set the values to their respective paramter elements
$nameParameter.SetAttribute("Name", "`$TemplateName`$")
$nameParameter.SetAttribute("Value", $templateName)
$guidParameter.SetAttribute("Name", "`$TemplateID`$")
$guidParameter.SetAttribute("Value", $guid)
$customParameters.AppendChild($nameParameter)
$customParameters.AppendChild($guidParameter)
# Create the WizardExtension element
$wizardExtension = $templateXml.CreateElement($null,'WizardExtension','http://schemas.microsoft.com/developer/vstemplate/2005')
$assembly = $templateXml.CreateElement($null,'Assembly','http://schemas.microsoft.com/developer/vstemplate/2005')
$fullclassname = $templateXml.CreateElement($null,'FullClassName','http://schemas.microsoft.com/developer/vstemplate/2005')
$assembly.InnerText = 'LigerShark.Templates, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
$fullclassname.InnerText = 'LigerShark.Templates.GoogleAnalyticsWizard'
$wizardExtension.AppendChild($assembly)
$wizardExtension.AppendChild($fullclassname)
# Add both elements to the VSTemplate file
$templateXml.VSTemplate.TemplateContent.AppendChild($customParameters) | Out-Null
$templateXml.VSTemplate.AppendChild($wizardExtension) | Out-Null
$updatedFile = $true
if($updatedFile)
{
$templateXml.Save($filePath)
$updatedFile = $false
}
}
using EnvDTE;
using Microsoft.VisualStudio.TemplateWizard;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LigerShark.Templates
{
public class GoogleAnalyticsWizard : IWizard
{
public void BeforeOpeningFile(ProjectItem projectItem)
{
}
// After the project template is created, send template ID to Google
public void ProjectFinishedGenerating(Project project)
{
}
// After the item template is created, send template ID to Google
public void ProjectItemFinishedGenerating(ProjectItem projectItem)
{
}
public void RunFinished()
{
}
public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
{
}
public bool ShouldAddProjectItem(string filePath)
{
return true;
}
private void sendTrackingCode(string templateID, string templateName)
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment