11#addin nuget : ? package= Cake . Git & version = 4.0 .0
2- #addin nuget: ? package = Cake . MinVer & version = 3.0 .0
32#tool nuget: ? package = NuGet . CommandLine & version = 6.9 .1
43
54using System;
5+ using System. Text . RegularExpressions ;
6+ using System . Linq ;
7+ using System . Net ;
68using System . Xml ;
79using System . Xml . Serialization ;
810using static System . IO . Directory ;
@@ -16,12 +18,14 @@ const string LibNuspecTemplateFilePath = "lib.nuspec.template";
1618const string LangNuspecTemplateFilePath = "lang.nuspec.template" ;
1719const string InstallerNuspecTemplateFilePath = "installer.nuspec.template" ;
1820const string ConfigFilePath = "build.config.xml" ;
21+ const string NugetSourceUrl = "https://api.nuget.org/v3/index.json" ;
1922
2023var target = Argument ( "target" , "build" ) ;
2124var preReleasePhase = Argument ( "pre-release-phase" , "rc" ) ;
2225var preRelease = Argument ( "pre-release" , false ) ;
2326var username = Argument ( "username" , "NaBian" ) ;
2427var email = Argument ( "email" , "[email protected] " ) ; 28+ var nugetToken = Argument ( "nuget-token" , "" ) ;
2529
2630var libVersion = "" ;
2731var nugetVersion = "" ;
@@ -57,7 +61,11 @@ Setup(context =>
5761
5862 context . Information ( $ "preReleasePhase: { preReleasePhase } ") ;
5963 context . Information ( $ "preRelease: { preRelease } ") ;
60- FillVersions ( GetAutoIncrement ( ) ) ;
64+
65+ var releaseVersion = GetNextVersion ( GetLatestVersion ( ) , preRelease , preReleasePhase ) ;
66+ libVersion = releaseVersion . Split ( '-' ) [ 0 ] + ".0" ;
67+ nugetVersion = releaseVersion ;
68+
6169 context . Information ( $ "libVersion: { libVersion } ") ;
6270 context . Information ( $ "nugetVersion: { nugetVersion } ") ;
6371
@@ -305,6 +313,22 @@ Task("create demo installers")
305313 }
306314} ) ;
307315
316+ Task ( "push to nuget" )
317+ . Does ( ( ) =>
318+ {
319+ if ( string . IsNullOrEmpty ( nugetToken ) )
320+ {
321+ throw new Exception ( "NuGet token is required!" ) ;
322+ }
323+
324+ var packages = GetFiles ( "./build/outputs/nuget/HandyControl.*nupkg" ) ;
325+ NuGetPush ( packages , new NuGetPushSettings
326+ {
327+ ApiKey = nugetToken ,
328+ Source = NugetSourceUrl
329+ } ) ;
330+ } ) ;
331+
308332Task ( "publish" )
309333 . IsDependentOn ( "update license" )
310334 . IsDependentOn ( "update version" )
@@ -321,6 +345,7 @@ Task("publish")
321345 . IsDependentOn ( "create installer nuspec files" )
322346 . IsDependentOn ( "pack nuspec files" )
323347 . IsDependentOn ( "create demo installers" )
348+ . IsDependentOn ( "push to nuget" )
324349 ;
325350
326351Task ( "build" )
@@ -374,49 +399,71 @@ private void ReplaceFileText(string filePath, string key, string value)
374399 ) ;
375400}
376401
377- private void FillVersions ( MinVerAutoIncrement ? autoIncrement )
402+ private string GetLatestVersion ( )
378403{
379- if ( autoIncrement is null )
380- {
381- return ;
382- }
404+ var lastTag = GitTags ( gitRootPath , true ) . Last ( ) ;
405+ Semver . TryParse ( lastTag . FriendlyName . Replace ( TagPrefix , "" ) , out var version ) ;
383406
384- var settings = new MinVerSettings
385- {
386- TagPrefix = TagPrefix ,
387- DefaultPreReleasePhase = preReleasePhase ,
388- AutoIncrement = autoIncrement ,
389- } ;
390- var minVer = MinVer ( settings ) ;
391-
392- libVersion = minVer . FileVersion ;
393- nugetVersion = preRelease
394- ? $ "{ minVer . Major } .{ minVer . Minor } .{ minVer . Patch } -{ minVer . PreRelease } "
395- : $ "{ minVer . Major } .{ minVer . Minor } .{ minVer . Patch } ";
407+ return version . ToString ( ) ;
396408}
397409
398- private MinVerAutoIncrement ? GetAutoIncrement ( )
410+ // author: https://github.com/orgs/HandyOrg/people/DingpingZhang
411+ private static string GetNextVersion ( string versionText , bool canBumpMinor , string previewVersion )
399412{
400- const string minorKey = "feat:" ;
401-
402- var lastTag = GitTags ( gitRootPath , true ) . Last ( ) ;
403- var sha = lastTag . Target . ToString ( ) ;
404- var logs = GitLog ( gitRootPath , sha ) ;
413+ if ( string . IsNullOrEmpty ( versionText ) )
414+ {
415+ return "0.1.0" ;
416+ }
405417
406- if ( logs . Count == 0 )
418+ if ( ! Semver . TryParse ( versionText , out var version ) )
407419 {
408- return null ;
420+ throw new InvalidOperationException ( $ "The { versionText } is not a valid version." ) ;
409421 }
410422
411- foreach ( var log in logs . Take ( logs . Count - 1 ) )
423+ bool isPreviewCurrent = ! string . IsNullOrEmpty ( version . PreviewCode ) ;
424+ bool isPreviewNext = ! string . IsNullOrEmpty ( previewVersion ) ;
425+
426+ if ( ! isPreviewCurrent && ! isPreviewNext )
412427 {
413- if ( log . Message . StartsWith ( minorKey ) )
428+ // stable -> stable: bump(stable)
429+ if ( canBumpMinor )
430+ {
431+ version . Minor ++ ;
432+ version . Patch = 0 ;
433+ }
434+ else
414435 {
415- return MinVerAutoIncrement . Minor ;
436+ version . Patch ++ ;
416437 }
417438 }
439+ else if ( ! isPreviewCurrent && isPreviewNext )
440+ {
441+ // stable -> preview: bump(stable) + rc
442+ if ( canBumpMinor )
443+ {
444+ version . Minor ++ ;
445+ version . Patch = 0 ;
446+ }
447+ else
448+ {
449+ version . Patch ++ ;
450+ }
418451
419- return MinVerAutoIncrement . Patch ;
452+ version . PreviewCode = previewVersion ;
453+ }
454+ else if ( isPreviewCurrent && ! isPreviewNext )
455+ {
456+ // preview -> stable: preview - rc
457+ version . PreviewCode = string . Empty ;
458+ version . PreviewPatch = 0 ;
459+ }
460+ else
461+ {
462+ // preview -> preview: stable + bump(rc)
463+ version . PreviewPatch ++ ;
464+ }
465+
466+ return version . ToString ( ) ;
420467}
421468
422469private bool IsFramework ( string framework ) => ! framework . Contains ( "." ) ;
@@ -506,4 +553,73 @@ public class BuildTask
506553 public bool BuildDemo { get ; set ; }
507554}
508555
556+ // author: https://github.com/orgs/HandyOrg/people/DingpingZhang
557+ public class Semver
558+ {
559+ private static readonly Regex SemverPattern = new Regex ( @"^(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)(-(?<preCode>[a-z]+)(?<prePatch>\d+)?)?$" , RegexOptions . IgnoreCase | RegexOptions . ExplicitCapture | RegexOptions . Compiled ) ;
560+
561+ public int Major ;
562+ public int Minor ;
563+ public int Patch ;
564+ public string PreviewCode ;
565+ public int PreviewPatch ;
566+
567+ public override string ToString ( ) => string . IsNullOrEmpty ( PreviewCode )
568+ ? $ "{ Major } .{ Minor } .{ Patch } "
569+ : $ "{ Major } .{ Minor } .{ Patch } -{ PreviewCode } { ( PreviewPatch == 0 ? string . Empty : PreviewPatch ) } ";
570+
571+ public static bool TryParse ( string version , out Semver semver )
572+ {
573+ var match = SemverPattern . Match ( version ) ;
574+
575+ semver = null ;
576+ if ( match . Success )
577+ {
578+ string prePatch = match . Groups [ "prePatch" ] . Value ;
579+ semver = new Semver
580+ {
581+ Major = int . Parse ( match . Groups [ "major" ] . Value ) ,
582+ Minor = int . Parse ( match . Groups [ "minor" ] . Value ) ,
583+ Patch = int . Parse ( match . Groups [ "patch" ] . Value ) ,
584+ PreviewCode = match . Groups [ "preCode" ] . Value ,
585+ PreviewPatch = string . IsNullOrEmpty ( prePatch ) ? 0 : int . Parse ( prePatch ) ,
586+ } ;
587+ }
588+
589+ return match . Success ;
590+ }
591+
592+ public static int Compare ( Semver left , Semver right )
593+ {
594+ var version1 = new Version ( left . Major , left . Minor , left . Patch ) ;
595+ var version2 = new Version ( right . Major , right . Minor , right . Patch ) ;
596+
597+ int result = version1 . CompareTo ( version2 ) ;
598+ if ( result != 0 )
599+ {
600+ return result ;
601+ }
602+
603+ var isEmptyPreviewCode1 = string . IsNullOrEmpty ( left . PreviewCode ) ;
604+ var isEmptyPreviewCode2 = string . IsNullOrEmpty ( right . PreviewCode ) ;
605+ if ( isEmptyPreviewCode1 && isEmptyPreviewCode2 )
606+ {
607+ return 0 ;
608+ }
609+
610+ if ( isEmptyPreviewCode1 )
611+ {
612+ return 1 ;
613+ }
614+
615+ if ( isEmptyPreviewCode2 )
616+ {
617+ return - 1 ;
618+ }
619+
620+ result = string . Compare ( left . PreviewCode , right . PreviewCode , ignoreCase : true ) ;
621+ return result == 0 ? left . PreviewPatch - right . PreviewPatch : result ;
622+ }
623+ }
624+
509625#endregion
0 commit comments