Skip to content
This repository was archived by the owner on Oct 14, 2022. It is now read-only.

Commit 61c09fc

Browse files
author
Mathias Storm
committed
Doing too much stuff to name it.. Its ready to use!
1 parent 6740919 commit 61c09fc

File tree

11 files changed

+856
-61
lines changed

11 files changed

+856
-61
lines changed

CodeStats.Client/CodeStats.Client.csproj

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
<WarningLevel>4</WarningLevel>
5555
</PropertyGroup>
5656
<ItemGroup>
57+
<Compile Include="ConfigurationRetriever.cs" />
5758
<Compile Include="FilterProvider.cs" />
5859
<Compile Include="CommandFilter.cs" />
5960
<Compile Include="OptionsPage\SettingsPage.cs">
@@ -63,31 +64,30 @@
6364
<DependentUpon>SettingsPage.cs</DependentUpon>
6465
</Compile>
6566
<Compile Include="Properties\AssemblyInfo.cs" />
67+
<Compile Include="Pulse.cs" />
6668
</ItemGroup>
6769
<ItemGroup>
70+
<None Include="app.config" />
6871
<None Include="Key.snk" />
69-
<None Include="packages.config" />
72+
<None Include="packages.config">
73+
<SubType>Designer</SubType>
74+
</None>
7075
<None Include="source.extension.vsixmanifest">
7176
<SubType>Designer</SubType>
7277
</None>
7378
</ItemGroup>
7479
<ItemGroup>
80+
<Content Include="CodeStats_logo.png">
81+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
82+
<IncludeInVSIX>true</IncludeInVSIX>
83+
</Content>
7584
<Content Include="index.html" />
7685
<Content Include="stylesheet.css" />
7786
</ItemGroup>
7887
<ItemGroup>
7988
<Reference Include="EnvDTE, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
8089
<EmbedInteropTypes>False</EmbedInteropTypes>
8190
</Reference>
82-
<Reference Include="EnvDTE100, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
83-
<EmbedInteropTypes>False</EmbedInteropTypes>
84-
</Reference>
85-
<Reference Include="EnvDTE80, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
86-
<EmbedInteropTypes>False</EmbedInteropTypes>
87-
</Reference>
88-
<Reference Include="EnvDTE90, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
89-
<EmbedInteropTypes>False</EmbedInteropTypes>
90-
</Reference>
9191
<Reference Include="Microsoft.CSharp" />
9292
<Reference Include="Microsoft.VisualStudio.CommandBars, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
9393
<EmbedInteropTypes>False</EmbedInteropTypes>
@@ -181,6 +181,9 @@
181181
<HintPath>..\packages\Microsoft.VisualStudio.Validation.15.3.15\lib\net45\Microsoft.VisualStudio.Validation.dll</HintPath>
182182
<Private>True</Private>
183183
</Reference>
184+
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
185+
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
186+
</Reference>
184187
<Reference Include="PresentationCore" />
185188
<Reference Include="PresentationFramework" />
186189
<Reference Include="stdole, Version=7.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
@@ -192,6 +195,7 @@
192195
<Reference Include="System.Data.DataSetExtensions" />
193196
<Reference Include="System.Design" />
194197
<Reference Include="System.Drawing" />
198+
<Reference Include="System.Net.Http" />
195199
<Reference Include="System.Windows.Forms" />
196200
<Reference Include="System.Xaml" />
197201
<Reference Include="System.Xml" />

CodeStats.Client/CodeStats_logo.png

25.4 KB
Loading

CodeStats.Client/CommandFilter.cs

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ public class CommandFilter : IOleCommandTarget
2222
internal bool Added { get; set; }
2323
private IAdornmentLayer _adornmentLayer;
2424
public string MachineKey { get; set; }
25-
internal Dictionary<string, int> Experiences = new Dictionary<string, int>();
2625
private Task _publisher = null;
26+
private readonly ConfigurationRetriever _config = new ConfigurationRetriever();
27+
Pulse pulse = new Pulse();
28+
2729

2830
public CommandFilter(IWpfTextView textView)
2931
{
@@ -36,49 +38,22 @@ int IOleCommandTarget.Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt
3638
if (nCmdID != (uint)VSConstants.VSStd2KCmdID.TYPECHAR && nCmdID != (uint)VSConstants.VSStd2KCmdID.BACKSPACE)
3739
return NextTarget.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
3840

39-
incrementOrAdd(_textView.TextBuffer.ContentType.DisplayName);
40-
MessageBox.Show("Added one to: " + _textView.TextBuffer.ContentType.DisplayName);
41+
pulse.IncrementExperience(_textView.TextBuffer.ContentType.DisplayName);
4142

4243
if (_publisher == null)
4344
{
44-
_publisher = Task.Factory.StartNew(() =>
45+
_publisher = Task.Factory.StartNew(async () =>
4546
{
4647
Thread.Sleep(10 * 1000); // Hopefully 10 seconds?
4748

48-
string experiences = "";
49-
foreach (var experience in Experiences)
50-
{
51-
experiences += $"In Language {experience.Key}, you earned {experience.Value} xp\n";
52-
}
53-
54-
EnvDTE.DTE dte = Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
55-
// Access options page
56-
var props = dte.get_Properties(@"CodeStats", "General");
57-
var pathProperty = props.Item("MachineKey");
58-
var machineKey = pathProperty.Value as string;
59-
60-
MessageBox.Show(experiences);
61-
MessageBox.Show("Pushing to " + machineKey);
62-
Experiences.Clear();
49+
await pulse.Execute();
6350
_publisher = null;
6451
});
6552
}
6653

6754
return NextTarget.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
6855
}
6956

70-
private void incrementOrAdd(string lang)
71-
{
72-
if (Experiences.ContainsKey(lang))
73-
{
74-
Experiences[lang]++;
75-
}
76-
else
77-
{
78-
Experiences.Add(lang, 1);
79-
}
80-
}
81-
8257
int IOleCommandTarget.QueryStatus(ref Guid pguidCmdGroup, uint cCmds, OLECMD[] prgCmds, IntPtr pCmdText)
8358
{
8459
return NextTarget.QueryStatus(ref pguidCmdGroup, cCmds, prgCmds, pCmdText);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace CodeStats.Client
8+
{
9+
public class ConfigurationRetriever
10+
{
11+
private readonly EnvDTE.DTE DTE = null;
12+
13+
public ConfigurationRetriever()
14+
{
15+
DTE = Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
16+
}
17+
18+
private string getConfigValueByKey(string key)
19+
{
20+
var props = DTE.get_Properties(@"CodeStats", "General");
21+
var pathProperty = props.Item(key);
22+
return pathProperty.Value as string;
23+
}
24+
25+
public string GetMachineKey() => getConfigValueByKey("MachineKey");
26+
public string GetPulseApiUrl() => getConfigValueByKey("PulseApiUrl");
27+
}
28+
}

CodeStats.Client/OptionsPage/SettingsPage.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,25 @@ public string MachineKey
2424
}
2525
}
2626

27+
private string _pulseApiUrl = "";
28+
[Category("CodeStats Settings")]
29+
[DisplayName("API Url")]
30+
[Description("The Pulse API that requests get sent to. DO NOT CHANGE if you dont know what you are doing!")]
31+
public string PulseApiUrl
32+
{
33+
get { return _pulseApiUrl; }
34+
set
35+
{
36+
_pulseApiUrl = value;
37+
}
38+
}
39+
2740
public SettingsPage()
2841
{
2942
InitializeComponent();
43+
if(PulseApiUrl == "")
44+
PulseApiUrl = "https://codestats.net/api/my/pulses";
45+
3046
}
3147
}
3248
}

CodeStats.Client/Pulse.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Net.Http;
6+
using System.Net.Http.Headers;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace CodeStats.Client
11+
{
12+
public class Pulse
13+
{
14+
[JsonProperty("coded_at")]
15+
public DateTime CodedAt { get; set; } = DateTime.Now;
16+
[JsonProperty("xps")]
17+
public List<PulseXp> Experiences { get; set; } = new List<PulseXp>();
18+
19+
private static readonly HttpClient _client = new HttpClient();
20+
private readonly ConfigurationRetriever config = new ConfigurationRetriever();
21+
22+
public class PulseXp
23+
{
24+
[JsonProperty("language")]
25+
public string Language { get; set; }
26+
[JsonProperty("xp")]
27+
public int Xp { get; set; }
28+
}
29+
30+
public void IncrementExperience(string language)
31+
{
32+
var result = Experiences.FirstOrDefault(pulseXp => pulseXp.Language == language);
33+
if (result == null)
34+
{
35+
Experiences.Add(new PulseXp
36+
{
37+
Language = language,
38+
Xp = 1,
39+
});
40+
return;
41+
}
42+
43+
result.Xp++;
44+
}
45+
46+
public async Task Execute()
47+
{
48+
var apiKey = config.GetMachineKey();
49+
var pluseApiUrl = config.GetPulseApiUrl();
50+
_client.DefaultRequestHeaders.Add("User-Agent", "CodeStats.Client/0.0.1");
51+
52+
if (apiKey == "")
53+
return;
54+
55+
var requestContent = new StringContent(JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented));
56+
requestContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
57+
requestContent.Headers.Add("X-API-Token", apiKey);
58+
59+
var response = await _client.PostAsync(pluseApiUrl, requestContent);
60+
await response.Content.ReadAsStringAsync();
61+
Experiences.Clear();
62+
}
63+
}
64+
}

CodeStats.Client/app.config

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<runtime>
4+
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
5+
<dependentAssembly>
6+
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
7+
<bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.0.0.0" />
8+
</dependentAssembly>
9+
</assemblyBinding>
10+
</runtime>
11+
</configuration>

CodeStats.Client/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@
2323
<package id="Microsoft.VisualStudio.Utilities" version="15.0.26606" targetFramework="net461" />
2424
<package id="Microsoft.VisualStudio.Validation" version="15.3.15" targetFramework="net461" />
2525
<package id="Microsoft.VSSDK.BuildTools" version="15.1.192" targetFramework="net461" developmentDependency="true" />
26+
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net461" />
2627
</packages>
Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
3-
<Metadata>
4-
<Identity Id="CodeStats.Client.5c3c0aa0-0bc8-43c5-afc9-5aa55662794b" Version="1.0" Language="en-US" Publisher="Company" />
5-
<DisplayName>CodeStats.Client</DisplayName>
6-
<Description>Empty VSIX Project.</Description>
7-
</Metadata>
8-
<Installation>
9-
<InstallationTarget Id="Microsoft.VisualStudio.Community" Version="[15.0]" />
10-
</Installation>
11-
<Dependencies>
12-
<Dependency Id="Microsoft.Framework.NDP" DisplayName="Microsoft .NET Framework" d:Source="Manual" Version="[4.5,)" />
13-
<Dependency Id="Microsoft.VisualStudio.MPF.15.0" DisplayName="Visual Studio MPF 15.0" d:Source="Installed" Version="[15.0]" />
14-
</Dependencies>
15-
<Prerequisites>
16-
<Prerequisite Id="Microsoft.VisualStudio.Component.CoreEditor" Version="[15.0,16.0)" DisplayName="Visual Studio core editor" />
17-
</Prerequisites>
18-
<Assets>
19-
<Asset Type="Microsoft.VisualStudio.MefComponent" d:Source="Project" d:ProjectName="%CurrentProject%" Path="|%CurrentProject%|" />
20-
<Asset Type="Microsoft.VisualStudio.ToolboxControl" d:Source="Project" d:ProjectName="%CurrentProject%" Path="|%CurrentProject%;PkgdefProjectOutputGroup|" />
21-
<Asset Type="Microsoft.VisualStudio.VsPackage" d:Source="Project" d:ProjectName="%CurrentProject%" Path="|%CurrentProject%;PkgdefProjectOutputGroup|" />
22-
</Assets>
3+
<Metadata>
4+
<Identity Id="CodeStats.Client.5c3c0aa0-0bc8-43c5-afc9-5aa55662794b" Version="0.5.3" Language="en-US" Publisher="Mathias Storm" />
5+
<DisplayName>CodeStats.Client</DisplayName>
6+
<Description xml:space="preserve">A Visual Studio Plugin for CodeStats. You can configure it under Extras -&gt; Options and set your machine</Description>
7+
<MoreInfo>https://github.com/mastorm/CodeStats.Client</MoreInfo>
8+
<Icon>CodeStats_logo.png</Icon>
9+
<Tags>code, tracking, statistics, analytics</Tags>
10+
</Metadata>
11+
<Installation>
12+
<InstallationTarget Id="Microsoft.VisualStudio.Community" Version="[15.0]" />
13+
<InstallationTarget Version="[15.0,16.0)" Id="Microsoft.VisualStudio.Pro" />
14+
<InstallationTarget Version="[15.0,16.0)" Id="Microsoft.VisualStudio.Enterprise" />
15+
</Installation>
16+
<Dependencies>
17+
<Dependency Id="Microsoft.Framework.NDP" DisplayName="Microsoft .NET Framework" d:Source="Manual" Version="[4.5,)" />
18+
<Dependency Id="Microsoft.VisualStudio.MPF.15.0" DisplayName="Visual Studio MPF 15.0" d:Source="Installed" Version="[15.0]" />
19+
</Dependencies>
20+
<Prerequisites>
21+
<Prerequisite Id="Microsoft.VisualStudio.Component.CoreEditor" Version="[15.0,16.0)" DisplayName="Visual Studio core editor" />
22+
</Prerequisites>
23+
<Assets>
24+
<Asset Type="Microsoft.VisualStudio.MefComponent" d:Source="Project" d:ProjectName="%CurrentProject%" Path="|%CurrentProject%|" />
25+
<Asset Type="Microsoft.VisualStudio.ToolboxControl" d:Source="Project" d:ProjectName="%CurrentProject%" Path="|%CurrentProject%;PkgdefProjectOutputGroup|" />
26+
<Asset Type="Microsoft.VisualStudio.VsPackage" d:Source="Project" d:ProjectName="%CurrentProject%" Path="|%CurrentProject%;PkgdefProjectOutputGroup|" />
27+
</Assets>
2328
</PackageManifest>

0 commit comments

Comments
 (0)