Skip to content

Commit 559c5b6

Browse files
committed
Added git branch name variable
1 parent f3d04f8 commit 559c5b6

8 files changed

Lines changed: 101 additions & 4 deletions

File tree

VisualStudioDiscordRPC.Shared/Configs/variables_config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
"name": "debug_mode",
3030
"description": "Current project debugging mode",
3131
"type": "DebugModeVariable"
32+
},
33+
{
34+
"name": "git_branch",
35+
"description": "Current git branch name",
36+
"type": "GitBranchVariable"
3237
}
3338
]
3439
}

VisualStudioDiscordRPC.Shared/Observers/GitObserver.cs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,79 @@ public string RemoteUrl
1313
get => _remoteUrl;
1414
private set
1515
{
16+
if (_remoteUrl == value)
17+
return;
18+
1619
_remoteUrl = value;
1720
RemoteUrlChanged?.Invoke(value);
1821
}
1922
}
2023

24+
public string BranchName
25+
{
26+
get => _branchName;
27+
private set
28+
{
29+
if (_branchName == value)
30+
return;
31+
32+
_branchName = value;
33+
BranchNameChanged?.Invoke(value);
34+
}
35+
}
36+
2137
public event Action<string> RemoteUrlChanged;
38+
public event Action<string> BranchNameChanged;
2239

2340
private VsObserver _vsObserver;
41+
private Repository _lastRepository;
42+
2443
private string _remoteUrl;
44+
private string _branchName;
2545

2646
public GitObserver(VsObserver vsObserver)
2747
{
2848
_vsObserver = vsObserver;
49+
OnWindowChanged(vsObserver.DTE.ActiveWindow);
2950
OnSolutionChanged(vsObserver.DTE.Solution);
3051
}
3152

53+
private void OnWindowChanged(Window window)
54+
{
55+
UpdateBranchName();
56+
}
57+
3258
private void OnSolutionChanged(Solution solution)
3359
{
3460
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();
3561

3662
string repositoryPath = solution.FullName;
3763
if (string.IsNullOrEmpty(repositoryPath))
3864
{
39-
RemoteUrl = string.Empty;
65+
_lastRepository = null;
4066
return;
4167
}
4268

4369
string repositoryName = Path.GetDirectoryName(repositoryPath);
4470
if (!Repository.IsValid(repositoryName))
71+
{
72+
_lastRepository = null;
73+
return;
74+
}
75+
76+
_lastRepository = new Repository(repositoryName);
77+
UpdateRemoteUrl();
78+
}
79+
80+
private void UpdateRemoteUrl()
81+
{
82+
if (_lastRepository == null)
4583
{
4684
RemoteUrl = string.Empty;
4785
return;
4886
}
4987

50-
Remote firstRemote = new Repository(repositoryName).Network.Remotes.FirstOrDefault();
88+
Remote firstRemote = _lastRepository.Network.Remotes.FirstOrDefault();
5189
if (firstRemote == null)
5290
{
5391
RemoteUrl = string.Empty;
@@ -57,13 +95,26 @@ private void OnSolutionChanged(Solution solution)
5795
RemoteUrl = firstRemote.Url;
5896
}
5997

98+
private void UpdateBranchName()
99+
{
100+
if (_lastRepository == null)
101+
{
102+
BranchName = string.Empty;
103+
return;
104+
}
105+
106+
BranchName = _lastRepository.Head.FriendlyName;
107+
}
108+
60109
public void Observe()
61110
{
111+
_vsObserver.WindowChanged += OnWindowChanged;
62112
_vsObserver.SolutionChanged += OnSolutionChanged;
63113
}
64114

65115
public void Unobserve()
66116
{
117+
_vsObserver.WindowChanged -= OnWindowChanged;
67118
_vsObserver.SolutionChanged -= OnSolutionChanged;
68119
}
69120
}

VisualStudioDiscordRPC.Shared/Observers/ObserverDelegates.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace VisualStudioDiscordRPC.Shared.Observers
44
{
5+
public delegate void WindowChangedHandler(Window document);
56
public delegate void DocumentChangedHandler(Document document);
67
public delegate void ProjectChangedHandler(Project project);
78
public delegate void SolutionChangedHandler(Solution solution);

VisualStudioDiscordRPC.Shared/Observers/VsObserver.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
using EnvDTE;
22
using EnvDTE80;
33
using Microsoft.VisualStudio.Shell;
4+
using System;
45

56
namespace VisualStudioDiscordRPC.Shared.Observers
67
{
78
public class VsObserver : IObserver
89
{
10+
public event WindowChangedHandler WindowChanged;
911
public event DocumentChangedHandler DocumentChanged;
1012
public event ProjectChangedHandler ProjectChanged;
1113
public event SolutionChangedHandler SolutionChanged;
@@ -44,6 +46,8 @@ private void OnWindowActivated(Window gotFocus, Window lostFocus)
4446
return;
4547
}
4648

49+
WindowChanged?.Invoke(gotFocus);
50+
4751
Solution currentSolution = gotFocus.DTE.Solution;
4852

4953
if (currentSolution?.FullName != _lastSolutionName)

VisualStudioDiscordRPC.Shared/PackageController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ private void RegisterServices()
9999
// Registering variable service.
100100
string variablesConfigJson = File.ReadAllText(PathHelper.GetPackageInstallationPath("Configs/variables_config.json"));
101101
VariableServiceConfig variablesConfig = JsonConvert.DeserializeObject<VariableServiceConfig>(variablesConfigJson);
102-
ServiceRepository.Default.AddService(new VariableService(variablesConfig, _vsObserver));
102+
ServiceRepository.Default.AddService(new VariableService(variablesConfig, _vsObserver, _gitObserver));
103103

104104
// Registering plug service.
105105
_plugService = new PlugService();

VisualStudioDiscordRPC.Shared/Services/VariableService.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class VariableService
4545
private readonly Dictionary<string, Variable> _variablesByType = new Dictionary<string, Variable>();
4646
private readonly Dictionary<string, VariableDescriptor> _variablesDescriptors = new Dictionary<string, VariableDescriptor>();
4747

48-
public VariableService(VariableServiceConfig config, VsObserver vsObserver)
48+
public VariableService(VariableServiceConfig config, VsObserver vsObserver, GitObserver gitObserver)
4949
{
5050
_vsObserver = vsObserver;
5151

@@ -55,6 +55,8 @@ public VariableService(VariableServiceConfig config, VsObserver vsObserver)
5555
RegisterVariable(new VersionVariable(_vsObserver.DTE));
5656
RegisterVariable(new EditionVariable(_vsObserver.DTE));
5757
RegisterVariable(new DebugModeVariable(_vsObserver.DTE));
58+
RegisterVariable(new GitBranchVariable(gitObserver));
59+
5860

5961
foreach (VariableInfo variableInfo in config.Variables)
6062
{
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Microsoft.VisualStudio.RpcContracts.Commands;
2+
using System;
3+
using VisualStudioDiscordRPC.Shared.Observers;
4+
5+
namespace VisualStudioDiscordRPC.Shared.Variables
6+
{
7+
public class GitBranchVariable : Variable
8+
{
9+
private readonly GitObserver _gitObserver;
10+
private string _branchName;
11+
12+
public GitBranchVariable(GitObserver gitObserver)
13+
{
14+
_gitObserver = gitObserver;
15+
}
16+
17+
public override void Initialize()
18+
{
19+
_gitObserver.BranchNameChanged += OnBranchNameChanged;
20+
}
21+
22+
private void OnBranchNameChanged(string newBranchName)
23+
{
24+
_branchName = newBranchName;
25+
RaiseChangedEvent();
26+
}
27+
28+
public override string GetData()
29+
{
30+
return string.IsNullOrEmpty(_branchName) ? "none" : _branchName;
31+
}
32+
}
33+
}

VisualStudioDiscordRPC.Shared/VisualStudioDiscordRPC.Shared.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<Compile Include="$(MSBuildThisFileDirectory)Variables\DebugModeVariable.cs" />
3030
<Compile Include="$(MSBuildThisFileDirectory)Variables\EditionVariable.cs" />
3131
<Compile Include="$(MSBuildThisFileDirectory)Variables\FileNameVariable.cs" />
32+
<Compile Include="$(MSBuildThisFileDirectory)Variables\GitBranchVariable.cs" />
3233
<Compile Include="$(MSBuildThisFileDirectory)Variables\Variable.cs" />
3334
<Compile Include="$(MSBuildThisFileDirectory)Variables\ProjectNameVariable.cs" />
3435
<Compile Include="$(MSBuildThisFileDirectory)Variables\SolutionNameVariable.cs" />

0 commit comments

Comments
 (0)