diff --git a/NuGetPusher.csproj b/NuGetPusher.csproj new file mode 100644 index 0000000..d453e9a --- /dev/null +++ b/NuGetPusher.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + + diff --git a/NuGetPusher.sln b/NuGetPusher.sln new file mode 100644 index 0000000..59eb146 --- /dev/null +++ b/NuGetPusher.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30128.74 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NuGetPusher", "NuGetPusher.csproj", "{BCC01FA8-567B-471E-951B-0EE9B5060FC6}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {BCC01FA8-567B-471E-951B-0EE9B5060FC6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BCC01FA8-567B-471E-951B-0EE9B5060FC6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BCC01FA8-567B-471E-951B-0EE9B5060FC6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BCC01FA8-567B-471E-951B-0EE9B5060FC6}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {326023DC-73B6-45CF-9D0E-43893DE01D33} + EndGlobalSection +EndGlobal diff --git a/ProcessHelper.cs b/ProcessHelper.cs new file mode 100644 index 0000000..a2e94de --- /dev/null +++ b/ProcessHelper.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Runtime.InteropServices; +using System.Text; + +namespace NuGetPusher +{ + public static class ProcessHelper + { + public static Process Run(string exe, string arguments, string workingDirectory = ".", bool waitExit = false) + { + try + { + bool redirectStandardOutput = true; + bool redirectStandardError = true; + bool useShellExecute = false; + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + redirectStandardOutput = false; + redirectStandardError = false; + useShellExecute = true; + } + + if (waitExit) + { + redirectStandardOutput = true; + redirectStandardError = true; + useShellExecute = false; + } + + ProcessStartInfo info = new ProcessStartInfo + { + FileName = exe, + Arguments = arguments, + CreateNoWindow = true, + UseShellExecute = useShellExecute, + WorkingDirectory = workingDirectory, + RedirectStandardOutput = redirectStandardOutput, + RedirectStandardError = redirectStandardError, + }; + + using Process process = Process.Start(info); + + if (waitExit) + { + process.OutputDataReceived += (sender, e) => + { + if(null != e) + { + Console.WriteLine(e.Data); + } + }; + process.BeginOutputReadLine(); + process.WaitForExit(); + if (process.ExitCode != 0) + { + throw new Exception($"{process.StandardOutput.ReadToEnd()} {process.StandardError.ReadToEnd()}"); + } + } + + return process; + } + catch (Exception e) + { + throw new Exception($"dir: {Path.GetFullPath(workingDirectory)}, command: {exe} {arguments}", e); + } + } + } +} diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..968a74e --- /dev/null +++ b/Program.cs @@ -0,0 +1,36 @@ +using System; + +namespace NuGetPusher +{ + class Program + { + static void Main(string[] args) + { + // arg[0] $(ProjectDir)/bin/Debug 发布文件所在目录 + // arg[1] $(TargetPath) 项目程序集路径 + // arg[2] ApiKey 仓库密钥 + // arg[3] Url 仓库地址 + try + { + + if (args.Length != 4) + throw new Exception($"invalid parameters: {System.Text.Json.JsonSerializer.Serialize(args)}"); + string publishDir = args[0]; + string assemblyFile = args[1]; + string apiKey = args[2]; + string url = args[3]; + + var assemblyName = System.Reflection.AssemblyName.GetAssemblyName(assemblyFile); + var nupkgName = $"{assemblyName.Name}.{assemblyName.Version.ToString(3)}.nupkg"; + var fullName = System.IO.Path.Combine(publishDir, nupkgName); + Console.WriteLine($"nuget push {fullName} {apiKey} -Source {url}"); + ProcessHelper.Run("nuget.exe", $"push {fullName} {apiKey} -Source {url}", waitExit: true); + Console.WriteLine("publish succeed!"); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + } + } +}