Skip to content

Commit

Permalink
add user unique id store
Browse files Browse the repository at this point in the history
  • Loading branch information
david1995 committed Mar 8, 2019
1 parent 26682ef commit 9d216ee
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
9 changes: 9 additions & 0 deletions VsIntegration/Analytics/IUserUniqueIdStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace TechTalk.SpecFlow.VsIntegration.Analytics
{
public interface IUserUniqueIdStore
{
Guid Get();
}
}
55 changes: 55 additions & 0 deletions VsIntegration/Analytics/RegistryUserUniqueIdStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using Microsoft.Win32;

namespace TechTalk.SpecFlow.VsIntegration.Analytics
{
public class RegistryUserUniqueIdStore : IUserUniqueIdStore
{
private const string UserUniqueIdPath = @"Software\TechTalk\SpecFlow\Vsix";
private const string UserUniqueId = @"UserUniqueId";
private readonly Lazy<Guid> _lazyUniqueUserId;

public RegistryUserUniqueIdStore()
{
_lazyUniqueUserId = new Lazy<Guid>(FetchFromOrCreateInRegistry);
}

public Guid Get()
{
return _lazyUniqueUserId.Value;
}

public Guid FetchFromOrCreateInRegistry()
{
var rootKey = Registry.CurrentUser;
var key = rootKey.OpenSubKey(UserUniqueIdPath);
if (key == null)
{
return CreateUniqueUserIdInRegistry();
}

using (key)
{
var value = key.GetValue(UserUniqueId, null) as Guid?;
return value ?? CreateUniqueUserIdInRegistry();
}
}

public Guid CreateUniqueUserIdInRegistry()
{
var rootKey = Registry.CurrentUser;
using (var key = rootKey.CreateSubKey(UserUniqueIdPath))
{
if (key == null)
{
throw new InvalidOperationException("Could not create registry key.");
}

var newUserId = Guid.NewGuid();
key.SetValue(UserUniqueId, newUserId, RegistryValueKind.Binary);

return newUserId;
}
}
}
}
2 changes: 2 additions & 0 deletions VsIntegration/TechTalk.SpecFlow.VsIntegration.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@
<Compile Include="..\CommonAssemblyInfo.cs">
<Link>Properties\CommonAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Analytics\IUserUniqueIdStore.cs" />
<Compile Include="Analytics\RegistryUserUniqueIdStore.cs" />
<Compile Include="AutoComplete\CompletionCommandFilter.cs" />
<Compile Include="AutoComplete\CustomCompletionSet.cs" />
<Compile Include="AutoComplete\GherkinCompletionCommandFilter.cs" />
Expand Down

0 comments on commit 9d216ee

Please sign in to comment.