Skip to content

Commit

Permalink
Add user secrets support in new csproj
Browse files Browse the repository at this point in the history
  • Loading branch information
olsh committed Apr 12, 2017
1 parent 1b7e0f5 commit b126815
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 39 deletions.
71 changes: 46 additions & 25 deletions src/Resharper.ConfigurationSense/Extensions/ProjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

using Newtonsoft.Json.Linq;

using NuGet;

using Resharper.ConfigurationSense.Constants;
using Resharper.ConfigurationSense.Models;

Expand All @@ -35,7 +33,7 @@ public static IEnumerable<KeyValueSetting> GetJsonProjectSettings(
var properties = new Queue<JProperty>();
properties.EnqueueRange(json.Properties());

var secretsJson = ReadSecretsJsonSafe(project);
var secretsJson = ReadSecretsSafe(project);
if (secretsJson != null)
{
properties.EnqueueRange(secretsJson.Properties());
Expand Down Expand Up @@ -126,7 +124,7 @@ public static IEnumerable<KeyValueSetting> GetXmlProjectSettings(
}

var settings = settingTag.GetChildSettings(settingsKeyAttribute, settingsValueAttributes);
result.AddRange(settings);
NuGet.CollectionExtensions.AddRange(result, settings);
}
}

Expand Down Expand Up @@ -175,39 +173,62 @@ private static JObject ParseJsonProjectFile(IProjectFile projectFile)
return JObject.Parse(text);
}

[CanBeNull]
private static JObject ReadSecretsJsonSafe(IProject project)
private static string GetUserSecretId(this IProject project)
{
var xmlFile = project.ProjectFile?.GetPrimaryPsiFile() as IXmlFile;
var enumerable = xmlFile?.FindAllByTagName("UserSecretsId");
if (enumerable != null)
{
foreach (var xmlTag in enumerable)
{
if (!string.IsNullOrEmpty(xmlTag.InnerValue))
{
return xmlTag.InnerValue;
}
}
}

var projectFiles =
project.GetAllProjectFiles(
file => file.Name.Equals(FileNames.NetCoreProjectJson, StringComparison.OrdinalIgnoreCase));

foreach (var projectFile in projectFiles)
foreach (var file in projectFiles)
{
try
{
var json = ParseJsonProjectFile(projectFile);
var userSecretsId = json[SettingsConstants.NetCoreUserSecretsIdJsonProperty];
var jsonProjectFile = ParseJsonProjectFile(file);

if (userSecretsId != null && userSecretsId.Type == JTokenType.String)
{
var filePath = string.Format(
FileNames.UserSecretsPathFormat,
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
userSecretsId.Value<string>());
var jsonToken = jsonProjectFile[SettingsConstants.NetCoreUserSecretsIdJsonProperty];

var secretsFile = File.ReadAllText(filePath);
return JObject.Parse(secretsFile);
}
}
// ReSharper disable once EmptyGeneralCatchClause
// ReSharper disable once CatchAllClause
catch (Exception)
if (jsonToken != null && jsonToken.Type == JTokenType.String)
{
return jsonToken.Value<string>();
}
}

return null;
}

[CanBeNull]
private static JObject ReadSecretsSafe(IProject project)
{
try
{
var userSecretId = project.GetUserSecretId();

var filePath = string.Format(
FileNames.UserSecretsPathFormat,
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
userSecretId);

var secretsFile = File.ReadAllText(filePath);

return JObject.Parse(secretsFile);
}
// ReSharper disable once EmptyGeneralCatchClause
// ReSharper disable once CatchAllClause
catch (Exception)
{
}

return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,22 @@ namespace Resharper.ConfigurationSense.Extensions
{
public static class XmlFileExtensions
{
#region Methods

public static IEnumerable<IXmlTag> FindAllByTagName(this IXmlFile xmlFile, string tagName)
{
var result = new LinkedList<IXmlTag>();

var xmlTags = new Queue<IXmlTag>(xmlFile.InnerTags);
while (xmlTags.Count > 0)
{
var xmlTag = xmlTags.Dequeue();
if (xmlTag.GetTagName().Equals(tagName))
{
result.AddLast(xmlTag);
yield return xmlTag;
}

foreach (var innerTag in xmlTag.InnerTags)
{
xmlTags.Enqueue(innerTag);
}
}

return result;
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ namespace Resharper.ConfigurationSense.Extensions
{
public static class XmlTagExtensions
{
#region Methods

public static IEnumerable<KeyValueSetting> GetChildSettings(
this IXmlTag xmlTag,
string keyAttribute,
Expand Down Expand Up @@ -65,7 +63,5 @@ public static string GetExternalConfigName(this IXmlTag xmlTag)

return null;
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup>
<HostFullIdentifier>ReSharperPlatformVs14Resharper</HostFullIdentifier>
<HostFullIdentifier>ReSharperPlatformVs15Resharper</HostFullIdentifier>
</PropertyGroup>
<ItemGroup>
<Reference Include="antlr.runtime, Version=2.7.7.1, Culture=neutral, PublicKeyToken=0f493c87b190d7e9, processorArchitecture=MSIL">
Expand Down

0 comments on commit b126815

Please sign in to comment.