Created
September 30, 2011 14:28
-
-
Save uluhonolulu/1253903 to your computer and use it in GitHub Desktop.
Custom VS Project Template Wizard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static class ProjectChooser | |
{ | |
private static Project _webProject; | |
public static void FixOutputPath(this Project project) { | |
var webProject = project.DTE.Solution.FindWebProject(); | |
SetOutputPathTo(project, webProject); | |
} | |
public static void AddMvcReference(this Project project) { | |
var webProject = project.DTE.Solution.FindWebProject(); | |
if (webProject != null) { | |
var mvcReference = webProject | |
.GetProjectReferences() | |
.FirstOrDefault(reference => reference.Name == "System.Web.Mvc"); | |
if (mvcReference != null) { | |
project.AddThisMvcReference(mvcReference); | |
project.AddIvonnaMvcReference(mvcReference); | |
} | |
} | |
} | |
private static void AddThisMvcReference(this Project project, Reference reference) { | |
var vsProject = project.Object as VSProject2; | |
vsProject.References.Add(reference.Path); | |
} | |
private static void AddIvonnaMvcReference(this Project project, Reference reference) { | |
var vsProject = project.Object as VSProject2; | |
var version = reference.MajorVersion; | |
vsProject.References.Add("Ivonna.Framework.MVC." + version.ToString()); | |
} | |
private static void SetOutputPathTo(this Project project, Project webProject) { | |
if (webProject != null) | |
project.SetOutputPath(webProject.BinPath()); | |
} | |
private static Project FindWebProject(this Solution solution) { | |
if (_webProject == null) | |
_webProject = solution.Projects.Cast<Project>().Where(project => project.ProjectIsWeb()).FirstOrDefault(); | |
return _webProject; | |
} | |
private static void SetOutputPath(this Project project, string newPath) { | |
project.ConfigurationManager.ActiveConfiguration.Properties.Item("OutputPath").let_Value(newPath); | |
} | |
private static string BinPath(this Project project) { | |
var path = Path.GetDirectoryName(project.FileName) + @"\bin"; | |
if (!Directory.Exists(path)) | |
Directory.CreateDirectory(path); | |
return path; | |
} | |
public static bool ProjectIsWeb(this Project project) | |
{ | |
var guids = project.GetProjectTypeGuids(); | |
foreach (var webguid in GetWebGuids()) { | |
if (guids.ToUpperInvariant().Contains(webguid)) return true; | |
} | |
return false; | |
} | |
public static IEnumerable<Reference> GetProjectReferences(this Project project) { | |
var vsProject = project.Object as VSProject2; | |
return vsProject.References.Cast<Reference>(); | |
} | |
public static string GetProjectTypeGuids(this Project proj) | |
{ | |
string projectTypeGuids = ""; | |
object service; | |
Microsoft.VisualStudio.Shell.Interop.IVsSolution solution; | |
Microsoft.VisualStudio.Shell.Interop.IVsHierarchy hierarchy; | |
Microsoft.VisualStudio.Shell.Interop.IVsAggregatableProject aggregatableProject; | |
service = ServiceLocator.GetService(proj.DTE, typeof(Microsoft.VisualStudio.Shell.Interop.IVsSolution)); | |
solution = (Microsoft.VisualStudio.Shell.Interop.IVsSolution)service; | |
int result = solution.GetProjectOfUniqueName(proj.UniqueName, out hierarchy); | |
if (result == 0) | |
{ | |
try | |
{ | |
aggregatableProject = (Microsoft.VisualStudio.Shell.Interop.IVsAggregatableProject)hierarchy; | |
aggregatableProject.GetAggregateProjectTypeGuids(out projectTypeGuids); | |
} | |
catch (Exception) | |
{ | |
//Console.WriteLine(ex.ToString()); | |
return proj.Kind; | |
} | |
} | |
return projectTypeGuids; | |
} | |
static string[] GetWebGuids() { | |
return new[] {"{349C5851-65DF-11DA-9384-00065B846F21}", "{E24C65DC-7377-472B-9ABA-BC803B73C61A}"}; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static class ServiceLocator | |
{ | |
public static object GetService(object serviceProvider, System.Type type) | |
{ | |
return GetService(serviceProvider, type.GUID); | |
} | |
public static object GetService(object serviceProviderObject, System.Guid guid) | |
{ | |
object service = null; | |
IntPtr serviceIntPtr; | |
Guid SIDGuid = guid; | |
Guid IIDGuid = SIDGuid; | |
var serviceProvider = (IServiceProvider)serviceProviderObject; | |
int hr = serviceProvider.QueryService(ref SIDGuid, ref IIDGuid, out serviceIntPtr); | |
if (hr != 0) | |
{ | |
System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(hr); | |
} | |
else if (!serviceIntPtr.Equals(IntPtr.Zero)) | |
{ | |
service = System.Runtime.InteropServices.Marshal.GetObjectForIUnknown(serviceIntPtr); | |
System.Runtime.InteropServices.Marshal.Release(serviceIntPtr); | |
} | |
return service; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TheWizard:IWizard { | |
public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams) { | |
//if (automationObject is DTE2) | |
// this.vsApp = (DTE2)automationObject; | |
//else | |
// throw new ArgumentException("Unable to get a reference to the Visual Studio environment. "); | |
} | |
public void ProjectFinishedGenerating(Project project) { | |
try { | |
project.FixOutputPath(); | |
project.AddMvcReference(); | |
project.Save(project.FileName); | |
} | |
catch (Exception ex) | |
{ | |
MessageBox.Show(ex.ToString()); // I'll probably burn in hell for this | |
} | |
} | |
public void ProjectItemFinishedGenerating(ProjectItem projectItem) {} | |
public bool ShouldAddProjectItem(string filePath) { return true; } | |
public void BeforeOpeningFile(ProjectItem projectItem) {} | |
public void RunFinished() {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment