From 408de9b7f1bcee0905e855d516a57b4be124f46e Mon Sep 17 00:00:00 2001 From: NaBian <836904362@qq.com> Date: Sun, 24 Mar 2024 13:24:41 +0800 Subject: [PATCH] chore: supports automatic push to nuget. --- .github/workflows/bump-up-version.yml | 12 ++++++++ .github/workflows/push-to-myget.yml | 2 ++ build/build.cake | 44 ++++++++++++++------------- 3 files changed, 37 insertions(+), 21 deletions(-) diff --git a/.github/workflows/bump-up-version.yml b/.github/workflows/bump-up-version.yml index dfe4bc6f..512d4d28 100644 --- a/.github/workflows/bump-up-version.yml +++ b/.github/workflows/bump-up-version.yml @@ -27,7 +27,19 @@ jobs: - name: Publish working-directory: ./build run: | + git config --local user.name "${{ github.actor }}" + git config --local user.email "836904362@qq.com" dotnet new tool-manifest dotnet tool install cake.tool --version 4.0.0 dotnet tool install minver-cli --version 3.0.0 dotnet cake --username="${{ github.actor }}" --email="836904362@qq.com" --pre-release=${{ inputs.pre_release }} + + - name: Push git changes + uses: ad-m/github-push-action@master + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + branch: ${{ github.ref }} + tags: true + + - name: Push to nuget + run: dotnet nuget push ./build/outputs/nuget/*nupkg -k ${{ secrets.NUGETTOKEN }} diff --git a/.github/workflows/push-to-myget.yml b/.github/workflows/push-to-myget.yml index c899273a..9dc0357b 100644 --- a/.github/workflows/push-to-myget.yml +++ b/.github/workflows/push-to-myget.yml @@ -1,3 +1,5 @@ +name: Push to myget + on: workflow_dispatch jobs: diff --git a/build/build.cake b/build/build.cake index 17346f69..52b22085 100644 --- a/build/build.cake +++ b/build/build.cake @@ -12,11 +12,8 @@ using static System.IO.Path; const string HandyControlBirthYear = "2018"; const string TagPrefix = "v"; const string RepositoryFolder = ".."; -const string PropsFilePath = $"{RepositoryFolder}/src/Directory.Build.Props"; -const string LicenseFilePath = $"{RepositoryFolder}/LICENSE"; const string LibNuspecTemplateFilePath = "lib.nuspec.template"; const string LangNuspecTemplateFilePath = "lang.nuspec.template"; -const string ResxFileFolder = $"{RepositoryFolder}/src/Shared/HandyControl_Shared/Properties/Langs"; const string ConfigFilePath = "build.config.xml"; var target = Argument("target", "build"); @@ -32,11 +29,19 @@ var year = $"{DateTime.Now.Year}"; var copyright = $"Copyright © HandyOrg {HandyControlBirthYear}-{year}"; var libNuspecFilePath = ""; var langNuspecFilePath = ""; +var gitRootPath = GitFindRootFromPath(MakeAbsolute(new DirectoryPath("."))).FullPath; +var propsFilePath = Combine(gitRootPath, "src/Directory.Build.Props"); +var licenseFilePath = Combine(gitRootPath, "LICENSE"); +var resxFileFolder = Combine(gitRootPath, "src/Shared/HandyControl_Shared/Properties/Langs"); var buildConfig = new BuildConfig(); Setup(context => { buildConfig = LoadBuildConfig(); + nugetFolder = Combine(buildConfig.OutputsFolder, "nuget"); + libNuspecFilePath = Combine(nugetFolder, GetFileNameWithoutExtension(LibNuspecTemplateFilePath)); + langNuspecFilePath = Combine(nugetFolder, LangNuspecTemplateFilePath); + CleanDirectory(buildConfig.OutputsFolder); CreateDirectory(nugetFolder); @@ -57,9 +62,9 @@ Task("update license") { const int copyrightIndex = 2; - string[] lines = ReadAllLines(LicenseFilePath, Encoding.UTF8); + string[] lines = ReadAllLines(licenseFilePath, Encoding.UTF8); lines[copyrightIndex] = copyright; - WriteAllLines(LicenseFilePath, lines); + WriteAllLines(licenseFilePath, lines); }); Task("update version") @@ -67,11 +72,11 @@ Task("update version") { if (libVersion != "") { - var document = LoadXmlDocument(PropsFilePath); + var document = LoadXmlDocument(propsFilePath); document.DocumentElement.SelectSingleNode("//PropertyGroup/Version").InnerText = libVersion; document.DocumentElement.SelectSingleNode("//PropertyGroup/FileVersion").InnerText = libVersion; document.DocumentElement.SelectSingleNode("//PropertyGroup/AssemblyVersion").InnerText = libVersion; - SaveXmlDocument(document, PropsFilePath); + SaveXmlDocument(document, propsFilePath); } if (nugetVersion != "") @@ -84,9 +89,9 @@ Task("update version") Task("update copyright") .Does(() => { - var document = LoadXmlDocument(PropsFilePath); + var document = LoadXmlDocument(propsFilePath); document.DocumentElement.SelectSingleNode("//PropertyGroup/Copyright").InnerText = copyright; - SaveXmlDocument(document, PropsFilePath); + SaveXmlDocument(document, propsFilePath); ReplaceFileText(libNuspecFilePath, "year", year); ReplaceFileText(langNuspecFilePath, "year", year); @@ -95,20 +100,20 @@ Task("update copyright") Task("commit files") .Does(() => { - GitAddAll(RepositoryFolder); - GitCommit(RepositoryFolder, username, email, $"chore: [AUTO] bump up version to {nugetVersion}."); + GitAddAll(gitRootPath); + GitCommit(gitRootPath, username, email, $"chore: [AUTO] bump up version to {nugetVersion}."); }); Task("create tag") .Does(() => { - GitTag(RepositoryFolder, $"{TagPrefix}{nugetVersion}"); + GitTag(gitRootPath, $"{TagPrefix}{nugetVersion}"); }); Task("update nuget sha") .Does(() => { - var lastCommit = GitLogTip(RepositoryFolder); + var lastCommit = GitLogTip(gitRootPath); ReplaceFileText(libNuspecFilePath, "commit", lastCommit.Sha); ReplaceFileText(langNuspecFilePath, "commit", lastCommit.Sha); @@ -177,7 +182,7 @@ Task("build lib") DotNetBuild ( - $"{RepositoryFolder}/src/{task.Domain}/HandyControl_{task.Domain}/HandyControl_{task.Domain}.csproj", + $"{gitRootPath}/src/{task.Domain}/HandyControl_{task.Domain}/HandyControl_{task.Domain}.csproj", new DotNetBuildSettings { Configuration = task.Configuration, @@ -200,7 +205,7 @@ Task("build demo") DotNetBuild ( - $"{RepositoryFolder}/src/{task.Domain}/HandyControlDemo_{task.Domain}/HandyControlDemo_{task.Domain}.csproj", + $"{gitRootPath}/src/{task.Domain}/HandyControlDemo_{task.Domain}/HandyControlDemo_{task.Domain}.csproj", new DotNetBuildSettings { Configuration = task.Configuration, @@ -315,9 +320,9 @@ private MinVerAutoIncrement? GetAutoIncrement() { const string minorKey = "feat:"; - var lastTag = GitTags(RepositoryFolder, true).Last(); + var lastTag = GitTags(gitRootPath, true).Last(); var sha = lastTag.Target.ToString(); - var logs = GitLog(RepositoryFolder, sha); + var logs = GitLog(gitRootPath, sha); if (logs.Count == 0) { @@ -339,7 +344,7 @@ private bool IsFramework(string framework) => !framework.Contains("."); private IEnumerable GetAllLangs() { - foreach (string resxFilePath in EnumerateFiles(ResxFileFolder, "Lang.*.resx")) + foreach (string resxFilePath in EnumerateFiles(resxFileFolder, "Lang.*.resx")) { yield return GetFileNameWithoutExtension(resxFilePath.Replace("Lang.", "")); } @@ -352,9 +357,6 @@ private BuildConfig LoadBuildConfig() var outputsItem = configDocument.DocumentElement.SelectSingleNode("//config/outputsFolder"); buildConfig.OutputsFolder = outputsItem.InnerText; - nugetFolder = $"{buildConfig.OutputsFolder}/nuget"; - libNuspecFilePath = $"{nugetFolder}/{GetFileNameWithoutExtension(LibNuspecTemplateFilePath)}"; - langNuspecFilePath = $"{nugetFolder}/{LangNuspecTemplateFilePath}"; var tasksItem = configDocument.DocumentElement.SelectSingleNode("//config/tasks"); foreach (XmlNode node in tasksItem.ChildNodes)