Skip to content

Commit 08c54b4

Browse files
committed
Add shared localizations strings for secrets
1 parent 1ee882b commit 08c54b4

File tree

8 files changed

+182
-22
lines changed

8 files changed

+182
-22
lines changed

src/Tools/Shared/SecretsHelpers/MsBuildProjectFinder.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Linq;
5+
using Microsoft.AspNetCore.Tools;
56
using Microsoft.Extensions.Tools.Internal;
67

7-
namespace Microsoft.Extensions.SecretManager.Tools.Internal;
8-
98
internal sealed class MsBuildProjectFinder
109
{
1110
private readonly string _directory;
@@ -34,20 +33,20 @@ public string FindMsBuildProject(string project)
3433

3534
if (projects.Count > 1)
3635
{
37-
throw new FileNotFoundException($"Multiple MSBuild project files found in '{projectPath}'. Specify which to use with the --project option.");
36+
throw new FileNotFoundException(SecretsHelpersResources.FormatError_MultipleProjectsFound(projectPath));
3837
}
3938

4039
if (projects.Count == 0)
4140
{
42-
throw new FileNotFoundException($"Could not find a MSBuild project file in '{projectPath}'. Specify which project to use with the --project option.");
41+
throw new FileNotFoundException(SecretsHelpersResources.FormatError_NoProjectsFound(projectPath));
4342
}
4443

4544
return projects[0];
4645
}
4746

4847
if (!File.Exists(projectPath))
4948
{
50-
throw new FileNotFoundException($"The project file '{projectPath}' does not exist.");
49+
throw new FileNotFoundException(SecretsHelpersResources.FormatError_NoProjectsFound(projectPath));
5150
}
5251

5352
return projectPath;

src/Tools/Shared/SecretsHelpers/ProjectIdResolver.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
using System.IO;
77
using System.Linq;
88
using System.Text;
9+
using Microsoft.AspNetCore.Tools;
910
using Microsoft.Extensions.CommandLineUtils;
1011
using Microsoft.Extensions.Tools.Internal;
1112

12-
namespace Microsoft.Extensions.SecretManager.Tools.Internal;
13-
1413
/// <summary>
1514
/// This API supports infrastructure and is not intended to be used
1615
/// directly from your code. This API may change or be removed in future releases.
@@ -42,9 +41,8 @@ public string Resolve(string project, string configuration)
4241
_reporter.Error(ex.Message);
4342
return null;
4443
}
45-
4644

47-
_reporter.Verbose($"Project file path {projectFile}.");
45+
_reporter.Verbose(SecretsHelpersResources.FormatMessage_Project_File_Path(projectFile));
4846

4947
configuration = !string.IsNullOrEmpty(configuration)
5048
? configuration
@@ -108,20 +106,20 @@ public string Resolve(string project, string configuration)
108106
_reporter.Verbose(outputBuilder.ToString());
109107
_reporter.Verbose(errorBuilder.ToString());
110108
_reporter.Error($"Exit code: {process.ExitCode}");
111-
_reporter.Error($"Could not load the MSBuild project '{projectFile}'.");
109+
_reporter.Error(SecretsHelpersResources.FormatError_ProjectFailedToLoad(projectFile));
112110
return null;
113111
}
114112

115113
if (!File.Exists(outputFile))
116114
{
117-
_reporter.Error($"Could not find the global property 'UserSecretsId' in MSBuild project '{projectFile}'. Ensure this property is set in the project or use the '--id' command line option.");
115+
_reporter.Error(SecretsHelpersResources.FormatError_ProjectMissingId(projectFile));
118116
return null;
119117
}
120118

121119
var id = File.ReadAllText(outputFile)?.Trim();
122120
if (string.IsNullOrEmpty(id))
123121
{
124-
_reporter.Error($"Could not find the global property 'UserSecretsId' in MSBuild project '{projectFile}'. Ensure this property is set in the project or use the '--id' command line option.");
122+
_reporter.Error(SecretsHelpersResources.FormatError_ProjectMissingId(projectFile));
125123
}
126124
return id;
127125

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<root>
3+
<!--
4+
Microsoft ResX Schema
5+
6+
Version 2.0
7+
8+
The primary goals of this format is to allow a simple XML format
9+
that is mostly human readable. The generation and parsing of the
10+
various data types are done through the TypeConverter classes
11+
associated with the data types.
12+
13+
Example:
14+
15+
... ado.net/XML headers & schema ...
16+
<resheader name="resmimetype">text/microsoft-resx</resheader>
17+
<resheader name="version">2.0</resheader>
18+
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
19+
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
20+
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
21+
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
22+
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
23+
<value>[base64 mime encoded serialized .NET Framework object]</value>
24+
</data>
25+
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
26+
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
27+
<comment>This is a comment</comment>
28+
</data>
29+
30+
There are any number of "resheader" rows that contain simple
31+
name/value pairs.
32+
33+
Each data row contains a name, and value. The row also contains a
34+
type or mimetype. Type corresponds to a .NET class that support
35+
text/value conversion through the TypeConverter architecture.
36+
Classes that don't support this are serialized and stored with the
37+
mimetype set.
38+
39+
The mimetype is used for serialized objects, and tells the
40+
ResXResourceReader how to depersist the object. This is currently not
41+
extensible. For a given mimetype the value must be set accordingly:
42+
43+
Note - application/x-microsoft.net.object.binary.base64 is the format
44+
that the ResXResourceWriter will generate, however the reader can
45+
read any of the formats listed below.
46+
47+
mimetype: application/x-microsoft.net.object.binary.base64
48+
value : The object must be serialized with
49+
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
50+
: and then encoded with base64 encoding.
51+
52+
mimetype: application/x-microsoft.net.object.soap.base64
53+
value : The object must be serialized with
54+
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
55+
: and then encoded with base64 encoding.
56+
57+
mimetype: application/x-microsoft.net.object.bytearray.base64
58+
value : The object must be serialized into a byte array
59+
: using a System.ComponentModel.TypeConverter
60+
: and then encoded with base64 encoding.
61+
-->
62+
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
63+
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
64+
<xsd:element name="root" msdata:IsDataSet="true">
65+
<xsd:complexType>
66+
<xsd:choice maxOccurs="unbounded">
67+
<xsd:element name="metadata">
68+
<xsd:complexType>
69+
<xsd:sequence>
70+
<xsd:element name="value" type="xsd:string" minOccurs="0" />
71+
</xsd:sequence>
72+
<xsd:attribute name="name" use="required" type="xsd:string" />
73+
<xsd:attribute name="type" type="xsd:string" />
74+
<xsd:attribute name="mimetype" type="xsd:string" />
75+
<xsd:attribute ref="xml:space" />
76+
</xsd:complexType>
77+
</xsd:element>
78+
<xsd:element name="assembly">
79+
<xsd:complexType>
80+
<xsd:attribute name="alias" type="xsd:string" />
81+
<xsd:attribute name="name" type="xsd:string" />
82+
</xsd:complexType>
83+
</xsd:element>
84+
<xsd:element name="data">
85+
<xsd:complexType>
86+
<xsd:sequence>
87+
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
88+
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
89+
</xsd:sequence>
90+
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
91+
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
92+
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
93+
<xsd:attribute ref="xml:space" />
94+
</xsd:complexType>
95+
</xsd:element>
96+
<xsd:element name="resheader">
97+
<xsd:complexType>
98+
<xsd:sequence>
99+
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
100+
</xsd:sequence>
101+
<xsd:attribute name="name" type="xsd:string" use="required" />
102+
</xsd:complexType>
103+
</xsd:element>
104+
</xsd:choice>
105+
</xsd:complexType>
106+
</xsd:element>
107+
</xsd:schema>
108+
<resheader name="resmimetype">
109+
<value>text/microsoft-resx</value>
110+
</resheader>
111+
<resheader name="version">
112+
<value>2.0</value>
113+
</resheader>
114+
<resheader name="reader">
115+
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
116+
</resheader>
117+
<resheader name="writer">
118+
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119+
</resheader>
120+
<data name="Error_InvalidSecretsId" xml:space="preserve">
121+
<value>The UserSecretsId '{userSecretsId}' cannot contain any characters that cannot be used in a file path.</value>
122+
</data>
123+
<data name="Error_MultipleProjectsFound" xml:space="preserve">
124+
<value>Multiple MSBuild project files found in '{projectPath}'. Specify which to use with the --project option.</value>
125+
</data>
126+
<data name="Error_NoProjectsFound" xml:space="preserve">
127+
<value>Could not find a MSBuild project file in '{projectPath}'. Specify which project to use with the --project option.</value>
128+
</data>
129+
<data name="Error_ProjectFailedToLoad" xml:space="preserve">
130+
<value>Could not load the MSBuild project '{project}'.</value>
131+
</data>
132+
<data name="Error_ProjectMissingId" xml:space="preserve">
133+
<value>Could not find the global property 'UserSecretsId' in MSBuild project '{project}'. Ensure this property is set in the project or use the '--id' command line option.</value>
134+
</data>
135+
<data name="Message_ProjectAlreadyInitialized" xml:space="preserve">
136+
<value>The MSBuild project '{project}' has already been initialized with a UserSecretsId.</value>
137+
</data>
138+
<data name="Message_Project_File_Path" xml:space="preserve">
139+
<value>Project file path {project}.</value>
140+
</data>
141+
<data name="Message_SetUserSecretsIdForProject" xml:space="preserve">
142+
<value>Set UserSecretsId to '{userSecretsId}' for MSBuild project '{project}'.</value>
143+
</data>
144+
</root>

src/Tools/Shared/SecretsHelpers/UserSecretsCreator.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
using System.Xml;
66
using System.Xml.Linq;
77
using System.Xml.XPath;
8+
using Microsoft.AspNetCore.Tools;
89
using Microsoft.Extensions.Tools.Internal;
910

10-
namespace Microsoft.Extensions.SecretManager.Tools.Internal;
11-
1211
internal static class UserSecretsCreator
1312
{
1413
public static string CreateUserSecretsId(IReporter reporter, string project, string workingDirectory, string overrideId = null)
@@ -24,9 +23,9 @@ public static string CreateUserSecretsId(IReporter reporter, string project, str
2423
: overrideId;
2524

2625
// Confirm secret ID does not contain invalid characters
27-
if (Path.GetInvalidPathChars().Any(invalidChar => newSecretsId.Contains(invalidChar)))
26+
if (Path.GetInvalidPathChars().Any(newSecretsId.Contains))
2827
{
29-
throw new ArgumentException($"The UserSecretsId '{newSecretsId}' cannot contain any characters that cannot be used in a file path.");
28+
throw new ArgumentException(SecretsHelpersResources.FormatError_InvalidSecretsId(newSecretsId));
3029
}
3130

3231
var existingUserSecretsId = projectDocument.XPathSelectElements("//UserSecretsId").FirstOrDefault();
@@ -37,7 +36,7 @@ public static string CreateUserSecretsId(IReporter reporter, string project, str
3736
// Only set the UserSecretsId if the user specified an explicit value
3837
if (string.IsNullOrWhiteSpace(overrideId))
3938
{
40-
reporter.Output($"The MSBuild project '{projectPath}' has already been initialized with a UserSecretsId.");
39+
reporter.Output(SecretsHelpersResources.FormatMessage_ProjectAlreadyInitialized(projectPath));
4140
return existingUserSecretsId.Value;
4241
}
4342

@@ -73,7 +72,7 @@ public static string CreateUserSecretsId(IReporter reporter, string project, str
7372
using var xw = XmlWriter.Create(projectPath, settings);
7473
projectDocument.Save(xw);
7574

76-
reporter.Output($"Set UserSecretsId to '{newSecretsId}' for MSBuild project '{projectPath}'.");
75+
reporter.Output(SecretsHelpersResources.FormatMessage_SetUserSecretsIdForProject(newSecretsId, projectPath));
7776
return newSecretsId;
7877
}
7978

src/Tools/dotnet-user-jwts/src/Helpers/DevJwtCliHelpers.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Text.Json;
77
using Microsoft.Extensions.Configuration;
88
using Microsoft.Extensions.Configuration.UserSecrets;
9-
using Microsoft.Extensions.SecretManager.Tools.Internal;
109
using Microsoft.Extensions.Tools.Internal;
1110

1211
namespace Microsoft.AspNetCore.Authentication.JwtBearer.Tools;

src/Tools/dotnet-user-jwts/src/Program.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ public void Run(string[] args)
4747
// Show help information if no subcommand/option was specified.
4848
userJwts.OnExecute(() => userJwts.ShowHelp());
4949

50-
userJwts.Execute(args);
50+
try
51+
{
52+
userJwts.Execute(args);
53+
}
54+
catch (Exception ex)
55+
{
56+
_reporter.Error(ex.Message);
57+
}
5158
}
5259
}

src/Tools/dotnet-user-jwts/src/dotnet-user-jwts.csproj

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
44
<OutputType>exe</OutputType>
@@ -18,6 +18,13 @@
1818
<None Include="$(ToolSharedSourceRoot)\SecretsHelpers\assets\SecretManager.targets" Link="assets\SecretManager.targets" CopyToOutputDirectory="PreserveNewest" />
1919
</ItemGroup>
2020

21+
<ItemGroup>
22+
<EmbeddedResource Include="$(ToolSharedSourceRoot)\SecretsHelpers\SecretsHelpersResources.resx">
23+
<ManifestResourceName>Microsoft.AspNetCore.Tools.SecretsHelpersResources</ManifestResourceName>
24+
<Generator></Generator>
25+
</EmbeddedResource>
26+
</ItemGroup>
27+
2128
<ItemGroup>
2229
<Reference Include="System.IdentityModel.Tokens.Jwt" />
2330
<Reference Include="Microsoft.Extensions.Configuration.Abstractions" />

src/Tools/dotnet-user-secrets/src/dotnet-user-secrets.csproj

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
@@ -20,6 +20,13 @@
2020
<None Include="$(ToolSharedSourceRoot)\SecretsHelpers\assets\SecretManager.targets" Link="assets\SecretManager.targets" CopyToOutputDirectory="PreserveNewest" />
2121
</ItemGroup>
2222

23+
<ItemGroup>
24+
<EmbeddedResource Include="$(ToolSharedSourceRoot)\SecretsHelpers\SecretsHelpersResources.resx">
25+
<ManifestResourceName>Microsoft.AspNetCore.Tools.SecretsHelpersResources</ManifestResourceName>
26+
<Generator></Generator>
27+
</EmbeddedResource>
28+
</ItemGroup>
29+
2330
<ItemGroup>
2431
<Reference Include="Newtonsoft.Json" />
2532
<Reference Include="Microsoft.Extensions.Configuration.UserSecrets" />

0 commit comments

Comments
 (0)