From b126815bf4a58462547afbcc2085d30c819e7294 Mon Sep 17 00:00:00 2001 From: Oleg Shevchenko Date: Thu, 13 Apr 2017 02:48:02 +0300 Subject: [PATCH] Add user secrets support in new csproj --- .../Extensions/ProjectExtensions.cs | 71 ++++++++++++------- .../Extensions/XmlFileExtensions.cs | 10 +-- .../Extensions/XmlTagExtensions.cs | 4 -- .../Resharper.ConfigurationSense.csproj | 2 +- 4 files changed, 48 insertions(+), 39 deletions(-) diff --git a/src/Resharper.ConfigurationSense/Extensions/ProjectExtensions.cs b/src/Resharper.ConfigurationSense/Extensions/ProjectExtensions.cs index b6a2b0f..843971f 100644 --- a/src/Resharper.ConfigurationSense/Extensions/ProjectExtensions.cs +++ b/src/Resharper.ConfigurationSense/Extensions/ProjectExtensions.cs @@ -11,8 +11,6 @@ using Newtonsoft.Json.Linq; -using NuGet; - using Resharper.ConfigurationSense.Constants; using Resharper.ConfigurationSense.Models; @@ -35,7 +33,7 @@ public static IEnumerable GetJsonProjectSettings( var properties = new Queue(); properties.EnqueueRange(json.Properties()); - var secretsJson = ReadSecretsJsonSafe(project); + var secretsJson = ReadSecretsSafe(project); if (secretsJson != null) { properties.EnqueueRange(secretsJson.Properties()); @@ -126,7 +124,7 @@ public static IEnumerable GetXmlProjectSettings( } var settings = settingTag.GetChildSettings(settingsKeyAttribute, settingsValueAttributes); - result.AddRange(settings); + NuGet.CollectionExtensions.AddRange(result, settings); } } @@ -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()); + 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(); } } 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; + } } } diff --git a/src/Resharper.ConfigurationSense/Extensions/XmlFileExtensions.cs b/src/Resharper.ConfigurationSense/Extensions/XmlFileExtensions.cs index 11a67a6..8a54110 100644 --- a/src/Resharper.ConfigurationSense/Extensions/XmlFileExtensions.cs +++ b/src/Resharper.ConfigurationSense/Extensions/XmlFileExtensions.cs @@ -6,19 +6,15 @@ namespace Resharper.ConfigurationSense.Extensions { public static class XmlFileExtensions { - #region Methods - public static IEnumerable FindAllByTagName(this IXmlFile xmlFile, string tagName) { - var result = new LinkedList(); - var xmlTags = new Queue(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) @@ -26,10 +22,6 @@ public static IEnumerable FindAllByTagName(this IXmlFile xmlFile, strin xmlTags.Enqueue(innerTag); } } - - return result; } - - #endregion } } diff --git a/src/Resharper.ConfigurationSense/Extensions/XmlTagExtensions.cs b/src/Resharper.ConfigurationSense/Extensions/XmlTagExtensions.cs index d4c9259..4fcadc4 100644 --- a/src/Resharper.ConfigurationSense/Extensions/XmlTagExtensions.cs +++ b/src/Resharper.ConfigurationSense/Extensions/XmlTagExtensions.cs @@ -9,8 +9,6 @@ namespace Resharper.ConfigurationSense.Extensions { public static class XmlTagExtensions { - #region Methods - public static IEnumerable GetChildSettings( this IXmlTag xmlTag, string keyAttribute, @@ -65,7 +63,5 @@ public static string GetExternalConfigName(this IXmlTag xmlTag) return null; } - - #endregion } } diff --git a/src/Resharper.ConfigurationSense/Resharper.ConfigurationSense.csproj b/src/Resharper.ConfigurationSense/Resharper.ConfigurationSense.csproj index fdbdd80..75902bb 100644 --- a/src/Resharper.ConfigurationSense/Resharper.ConfigurationSense.csproj +++ b/src/Resharper.ConfigurationSense/Resharper.ConfigurationSense.csproj @@ -35,7 +35,7 @@ false - ReSharperPlatformVs14Resharper + ReSharperPlatformVs15Resharper