From 99da616aa8aad0ed959bed0edb24b7391556d7b9 Mon Sep 17 00:00:00 2001 From: Frederik Carlier Date: Fri, 13 Dec 2019 11:53:27 +0100 Subject: [PATCH] Add a {project} argument, allowing users to optionally specify the path to the .NET project they want to build. --- dotnet-rpm/PackagingRunner.cs | 56 ++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 11 deletions(-) diff --git a/dotnet-rpm/PackagingRunner.cs b/dotnet-rpm/PackagingRunner.cs index e926ffb..4d303d7 100644 --- a/dotnet-rpm/PackagingRunner.cs +++ b/dotnet-rpm/PackagingRunner.cs @@ -63,6 +63,10 @@ namespace Dotnet.Packaging "Do not restore the project before building.", CommandOptionType.NoValue); + CommandArgument projectArgument = commandLineApplication.Argument( + "project", + "The project file to operate on. If a file is not specified, the command will search the current directory for one."); + commandLineApplication.VersionOption("-v | --version", new Version(ThisAssembly.AssemblyFileVersion).ToString(3), ThisAssembly.AssemblyInformationalVersion); commandLineApplication.HelpOption("-? | -h | --help"); @@ -118,9 +122,14 @@ namespace Dotnet.Packaging { Console.WriteLine($"dotnet {this.commandName} ({ThisAssembly.AssemblyInformationalVersion})"); + if (!TryGetProjectFilePath(projectArgument, out string projectFilePath)) + { + return -1; + } + if (!noRestore.HasValue()) { - if (!this.IsPackagingTargetsInstalled()) + if (!this.IsPackagingTargetsInstalled(projectFilePath)) { return -1; } @@ -154,6 +163,8 @@ namespace Dotnet.Packaging msbuildArguments.Append($"/p:PackageDir={output.Value()} "); } + msbuildArguments.Append($"{projectFilePath} "); + return RunDotnet(msbuildArguments); }); @@ -180,17 +191,8 @@ namespace Dotnet.Packaging return process.ExitCode; } - public bool IsPackagingTargetsInstalled() + public bool IsPackagingTargetsInstalled(string projectFilePath) { - var projectFilePath = Directory.GetFiles(Environment.CurrentDirectory, "*.*proj").SingleOrDefault(); - - if (projectFilePath == null) - { - Console.Error.WriteLine($"Failed to find a .csproj file in '{Environment.CurrentDirectory}'. dotnet {this.commandName} only works if"); - Console.Error.WriteLine($"you have exactly one .csproj file in your directory. For advanced scenarios, please use 'dotnet msbuild /t:{this.msbuildTarget}'"); - return false; - } - var loggers = new IMSBuildLogger[] { new ConsoleLogger(LoggerVerbosity.Quiet) }; var project = new MSBuild.Project(projectFilePath); @@ -222,6 +224,38 @@ namespace Dotnet.Packaging return true; } + private bool TryGetProjectFilePath(CommandArgument projectArgument, out string projectFilePath) + { + if (projectArgument == null) + { + throw new ArgumentNullException(nameof(projectArgument)); + } + + if (!string.IsNullOrWhiteSpace(projectArgument.Value)) + { + projectFilePath = projectArgument.Value; + + if (!File.Exists(projectFilePath)) + { + Console.Error.WriteLine($"Could not find the project file '{projectFilePath}'."); + return false; + } + + return true; + } + + projectFilePath = Directory.GetFiles(Environment.CurrentDirectory, "*.*proj").SingleOrDefault(); + + if (projectFilePath == null) + { + Console.Error.WriteLine($"Failed to find a .*proj file in '{Environment.CurrentDirectory}'. dotnet {this.commandName} only works if"); + Console.Error.WriteLine($"you have exactly one .*proj file in your directory. For advanced scenarios, please use 'dotnet msbuild /t:{this.msbuildTarget}'"); + return false; + } + + return true; + } + class LockFile { public Dictionary Libraries;