Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a form showing a table of monthly build times #15

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Backup/BuildMonitor.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{4FDB4354-8389-4675-ACED-6CA6DF570561}"
ProjectSection(SolutionItems) = preProject
.nuget\NuGet.exe = .nuget\NuGet.exe
.nuget\NuGet.targets = .nuget\NuGet.targets
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BuildMonitor", "BuildMonitor\BuildMonitor.csproj", "{700A9D28-E9DE-428B-A8F2-B40BAF4A3E87}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BuildMonitor.UnitTests", "BuildMonitor.UnitTests\BuildMonitor.UnitTests.csproj", "{4107248F-F2FC-4152-816B-E8633C4913F5}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{68E63FC4-249A-4EFE-A8D8-5154DA8E3415}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BuildMonitorPackage", "BuildMonitorPackage\BuildMonitorPackage.csproj", "{F1623C5E-614B-435B-B625-AC534EF3920F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{700A9D28-E9DE-428B-A8F2-B40BAF4A3E87}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{700A9D28-E9DE-428B-A8F2-B40BAF4A3E87}.Debug|Any CPU.Build.0 = Debug|Any CPU
{700A9D28-E9DE-428B-A8F2-B40BAF4A3E87}.Release|Any CPU.ActiveCfg = Release|Any CPU
{700A9D28-E9DE-428B-A8F2-B40BAF4A3E87}.Release|Any CPU.Build.0 = Release|Any CPU
{4107248F-F2FC-4152-816B-E8633C4913F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4107248F-F2FC-4152-816B-E8633C4913F5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4107248F-F2FC-4152-816B-E8633C4913F5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4107248F-F2FC-4152-816B-E8633C4913F5}.Release|Any CPU.Build.0 = Release|Any CPU
{F1623C5E-614B-435B-B625-AC534EF3920F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F1623C5E-614B-435B-B625-AC534EF3920F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F1623C5E-614B-435B-B625-AC534EF3920F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F1623C5E-614B-435B-B625-AC534EF3920F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{4107248F-F2FC-4152-816B-E8633C4913F5} = {68E63FC4-249A-4EFE-A8D8-5154DA8E3415}
EndGlobalSection
EndGlobal
231 changes: 231 additions & 0 deletions Backup/BuildMonitorPackage/BuildMonitorPackage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
using System;
using System.ComponentModel.Design;
using System.Runtime.InteropServices;
using BuildMonitor;
using BuildMonitor.Domain;
using BuildMonitor.UI;
using EnvDTE;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Shell;
using Constants = EnvDTE.Constants;

namespace BuildMonitorPackage
{
[InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
[Guid(GuidList.guidBuildMonitorPackagePkgString)]
[PackageRegistration(UseManagedResourcesOnly = true)]
[ProvideAutoLoad("{f1536ef8-92ec-443c-9ed7-fdadf150da82}")]
sealed class BuildMonitorPackage : Package, IVsUpdateSolutionEvents2
{
private DTE dte;
private readonly Monitor monitor;
private readonly DataAdjusterWithLogging dataAdjuster;
private BuildMonitor.Domain.Solution solution;

private IVsSolutionBuildManager2 sbm;
private uint updateSolutionEventsCookie;
private OutputWindowPane outputWindowPane;
private SolutionEvents events;
private IVsSolution2 vsSolution;

public BuildMonitorPackage()
{
Settings.CreateApplicationFolderIfNotExist();

var factory = new BuildFactory();
var repository = new BuildRepository(Settings.RepositoryPath);

monitor = new Monitor(factory, repository);
dataAdjuster = new DataAdjusterWithLogging(repository, PrintLine);
}

protected override void Initialize()
{
base.Initialize();

//if invalid data, adjust it
dataAdjuster.Adjust();

// Get solution build manager
sbm = ServiceProvider.GlobalProvider.GetService(typeof(SVsSolutionBuildManager)) as IVsSolutionBuildManager2;
if (sbm != null)
{
sbm.AdviseUpdateSolutionEvents(this, out updateSolutionEventsCookie);
}

// Must hold a reference to the solution events object or the events wont fire, garbage collection related
events = GetDTE().Events.SolutionEvents;
events.Opened += Solution_Opened;

PrintLine("Build monitor initialized");
PrintLine("Path to persist data: {0}", Settings.RepositoryPath);

monitor.SolutionBuildFinished = b =>
{
Print("[{0}] Time Elapsed: {1} \t\t", b.SessionBuildCount, b.SolutionBuildTime.ToTime());
PrintLine("Session build time: {0}\n", b.SessionMillisecondsElapsed.ToTime());
};

monitor.ProjectBuildFinished = b => PrintLine(" - {0}\t-- {1} --", b.MillisecondsElapsed.ToTime(), b.ProjectName);
}

private void Solution_Opened()
{
solution = new BuildMonitor.Domain.Solution { Name = GetSolutionName() };
PrintLine("\nSolution loaded: \t{0}", solution.Name);
PrintLine("{0}", 60.Times("-"));
}

#region Print to output window pane

private OutputWindowPane GetOutputWindowPane()
{
if (outputWindowPane == null)
{
var outputWindow = (OutputWindow)GetDTE().Windows.Item(Constants.vsWindowKindOutput).Object;
outputWindowPane = outputWindow.OutputWindowPanes.Add("Build monitor");
}
return outputWindowPane;
}

private void Print(string format, params object[] args)
{
GetOutputWindowPane().OutputString(string.Format(format, args));
}

private void PrintLine(string format, params object[] args)
{
Print(format + Environment.NewLine, args);
}

#endregion

#region Get objects from vs

private DTE GetDTE()
{
if (dte == null)
{
var serviceContainer = this as IServiceContainer;
dte = serviceContainer.GetService(typeof(SDTE)) as DTE;
}
return dte;
}

private void SetVsSolution()
{
if (vsSolution == null)
vsSolution = ServiceProvider.GlobalProvider.GetService(typeof(SVsSolution)) as IVsSolution2;
}

private string GetSolutionName()
{
SetVsSolution();
object solutionName;
vsSolution.GetProperty((int)__VSPROPID.VSPROPID_SolutionBaseName, out solutionName);
return (string)solutionName;
}

private IProject GetProject(IVsHierarchy pHierProj)
{
object n;
pHierProj.GetProperty((uint)VSConstants.VSITEMID.Root, (int)__VSHPROPID.VSHPROPID_Name, out n);
var name = n as string;

Guid id;
vsSolution.GetGuidOfProject(pHierProj, out id);

return new BuildMonitor.Domain.Project { Name = name, Id = id };
}

#endregion

int IVsUpdateSolutionEvents.UpdateSolution_Begin(ref int pfCancelUpdate)
{
// This method is called when the entire solution starts to build.
monitor.SolutionBuildStart(solution);

return VSConstants.S_OK;
}

int IVsUpdateSolutionEvents2.UpdateProjectCfg_Begin(IVsHierarchy pHierProj, IVsCfg pCfgProj, IVsCfg pCfgSln, uint dwAction, ref int pfCancel)
{
// This method is called when a specific project begins building.
var project = GetProject(pHierProj);
monitor.ProjectBuildStart(project);

return VSConstants.S_OK;
}

int IVsUpdateSolutionEvents2.UpdateProjectCfg_Done(IVsHierarchy pHierProj, IVsCfg pCfgProj, IVsCfg pCfgSln, uint dwAction, int fSuccess, int fCancel)
{
// This method is called when a specific project finishes building.
var project = GetProject(pHierProj);
monitor.ProjectBuildStop(project);

return VSConstants.S_OK;
}

int IVsUpdateSolutionEvents.UpdateSolution_Done(int fSucceeded, int fModified, int fCancelCommand)
{
// This method is called when the entire solution is done building.
monitor.SolutionBuildStop();

return VSConstants.S_OK;
}

#region empty impl. of solution events interface

int IVsUpdateSolutionEvents2.UpdateSolution_StartUpdate(ref int pfCancelUpdate)
{
return VSConstants.S_OK;
}

int IVsUpdateSolutionEvents2.UpdateSolution_Cancel()
{
return VSConstants.S_OK;
}

int IVsUpdateSolutionEvents2.OnActiveProjectCfgChange(IVsHierarchy pIVsHierarchy)
{
return VSConstants.S_OK;
}

int IVsUpdateSolutionEvents2.UpdateSolution_Begin(ref int pfCancelUpdate)
{
return VSConstants.S_OK;
}

int IVsUpdateSolutionEvents2.UpdateSolution_Done(int fSucceeded, int fModified, int fCancelCommand)
{
return VSConstants.S_OK;
}

int IVsUpdateSolutionEvents.UpdateSolution_StartUpdate(ref int pfCancelUpdate)
{
return VSConstants.S_OK;
}

int IVsUpdateSolutionEvents.UpdateSolution_Cancel()
{
return VSConstants.S_OK;
}

int IVsUpdateSolutionEvents.OnActiveProjectCfgChange(IVsHierarchy pIVsHierarchy)
{
return VSConstants.S_OK;
}

#endregion

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);

// Unadvise all events
if (sbm != null && updateSolutionEventsCookie != 0)
sbm.UnadviseUpdateSolutionEvents(updateSolutionEventsCookie);
}
}
}
Loading