diff --git a/src/Tools/Shared/SecretsHelpers/MsBuildProjectFinder.cs b/src/Tools/Shared/SecretsHelpers/MsBuildProjectFinder.cs index 6be7bde5299c..e016ed37d06b 100644 --- a/src/Tools/Shared/SecretsHelpers/MsBuildProjectFinder.cs +++ b/src/Tools/Shared/SecretsHelpers/MsBuildProjectFinder.cs @@ -46,7 +46,7 @@ public string FindMsBuildProject(string project) if (!File.Exists(projectPath)) { - throw new FileNotFoundException(SecretsHelpersResources.FormatError_ProjectPath_NotFound(projectPath)); + throw new FileNotFoundException(SecretsHelpersResources.FormatError_File_NotFound(projectPath)); } return projectPath; diff --git a/src/Tools/Shared/SecretsHelpers/ProjectIdResolver.cs b/src/Tools/Shared/SecretsHelpers/ProjectIdResolver.cs index d9c794586ae4..4c3c68c77912 100644 --- a/src/Tools/Shared/SecretsHelpers/ProjectIdResolver.cs +++ b/src/Tools/Shared/SecretsHelpers/ProjectIdResolver.cs @@ -59,9 +59,9 @@ public string Resolve(string project, string configuration) UseShellExecute = false, ArgumentList = { - "msbuild", + "build", projectFile, - "/nologo", + "--no-restore", "/t:_ExtractUserSecretsMetadata", // defined in SecretManager.targets "/p:_UserSecretsMetadataFile=" + outputFile, "/p:Configuration=" + configuration, @@ -72,7 +72,7 @@ public string Resolve(string project, string configuration) }; #if DEBUG - _reporter.Verbose($"Invoking '{psi.FileName} {psi.Arguments}'"); + _reporter.Verbose($"Invoking '{psi.FileName} {string.Join(' ', psi.ArgumentList)}'"); #endif using var process = new Process() diff --git a/src/Tools/Shared/SecretsHelpers/SecretsHelpersResources.resx b/src/Tools/Shared/SecretsHelpers/SecretsHelpersResources.resx index bdc7cd88d5e9..acddbeeb55d2 100644 --- a/src/Tools/Shared/SecretsHelpers/SecretsHelpersResources.resx +++ b/src/Tools/Shared/SecretsHelpers/SecretsHelpersResources.resx @@ -124,7 +124,7 @@ Multiple MSBuild project files found in '{projectPath}'. Specify which to use with the --project option. - Could not find a MSBuild project file in '{projectPath}'. Specify which project to use with the --project option. + Could not find a MSBuild project file in '{projectPath}'. Specify which project to use with the --project option. Use --file option for file-based apps. Could not load the MSBuild project '{project}'. @@ -132,8 +132,8 @@ 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. - - The project file '{0}' does not exist. + + The file '{0}' does not exist. The MSBuild project '{project}' has already been initialized with a UserSecretsId. @@ -144,4 +144,4 @@ Set UserSecretsId to '{userSecretsId}' for MSBuild project '{project}'. - \ No newline at end of file + diff --git a/src/Tools/dotnet-user-secrets/src/CommandLineOptions.cs b/src/Tools/dotnet-user-secrets/src/CommandLineOptions.cs index af97626c7d89..5587c8e340f5 100644 --- a/src/Tools/dotnet-user-secrets/src/CommandLineOptions.cs +++ b/src/Tools/dotnet-user-secrets/src/CommandLineOptions.cs @@ -16,6 +16,7 @@ public class CommandLineOptions public bool IsHelp { get; private set; } public bool IsVerbose { get; private set; } public string Project { get; private set; } + public string File { get; private set; } public static CommandLineOptions Parse(string[] args, IConsole console) { @@ -36,6 +37,9 @@ public static CommandLineOptions Parse(string[] args, IConsole console) var optionProject = app.Option("-p|--project ", "Path to project. Defaults to searching the current directory.", CommandOptionType.SingleValue, inherited: true); + var optionFile = app.Option("-f|--file ", "Path to file-based app.", + CommandOptionType.SingleValue, inherited: true); + var optionConfig = app.Option("-c|--configuration ", "The project configuration to use. Defaults to 'Debug'.", CommandOptionType.SingleValue, inherited: true); @@ -50,7 +54,7 @@ public static CommandLineOptions Parse(string[] args, IConsole console) app.Command("remove", c => RemoveCommand.Configure(c, options)); app.Command("list", c => ListCommand.Configure(c, options)); app.Command("clear", c => ClearCommand.Configure(c, options)); - app.Command("init", c => InitCommandFactory.Configure(c, options)); + app.Command("init", c => InitCommandFactory.Configure(c, options, optionFile)); // Show help information if no subcommand/option was specified. app.OnExecute(() => app.ShowHelp()); @@ -66,6 +70,12 @@ public static CommandLineOptions Parse(string[] args, IConsole console) options.IsHelp = app.IsShowingInformation; options.IsVerbose = optionVerbose.HasValue(); options.Project = optionProject.Value(); + options.File = optionFile.Value(); + + if (options.File != null && options.Project != null) + { + throw new CommandParsingException(app, Resources.Error_ProjectAndFileOptions); + } return options; } diff --git a/src/Tools/dotnet-user-secrets/src/Internal/InitCommand.cs b/src/Tools/dotnet-user-secrets/src/Internal/InitCommand.cs index d6aa7edd15ab..cf3a9db65ba5 100644 --- a/src/Tools/dotnet-user-secrets/src/Internal/InitCommand.cs +++ b/src/Tools/dotnet-user-secrets/src/Internal/InitCommand.cs @@ -16,13 +16,18 @@ public class InitCommandFactory : ICommand { public CommandLineOptions Options { get; } - internal static void Configure(CommandLineApplication command, CommandLineOptions options) + internal static void Configure(CommandLineApplication command, CommandLineOptions options, CommandOption optionFile) { command.Description = "Set a user secrets ID to enable secret storage"; command.HelpOption(); command.OnExecute(() => { + if (optionFile.HasValue()) + { + throw new CommandParsingException(command, Resources.Error_InitNotSupportedForFileBasedApps); + } + options.Command = new InitCommandFactory(options); }); } diff --git a/src/Tools/dotnet-user-secrets/src/Program.cs b/src/Tools/dotnet-user-secrets/src/Program.cs index f01b7e434678..87f04ce466c3 100644 --- a/src/Tools/dotnet-user-secrets/src/Program.cs +++ b/src/Tools/dotnet-user-secrets/src/Program.cs @@ -106,6 +106,6 @@ internal string ResolveId(CommandLineOptions options, IReporter reporter) } var resolver = new ProjectIdResolver(reporter, _workingDirectory); - return resolver.Resolve(options.Project, options.Configuration); + return resolver.Resolve(options.Project ?? options.File, options.Configuration); } } diff --git a/src/Tools/dotnet-user-secrets/src/Resources.resx b/src/Tools/dotnet-user-secrets/src/Resources.resx index 30d696946c76..de8dae561692 100644 --- a/src/Tools/dotnet-user-secrets/src/Resources.resx +++ b/src/Tools/dotnet-user-secrets/src/Resources.resx @@ -1,4 +1,4 @@ - +