3
3
using System . IO ;
4
4
using System . Linq ;
5
5
using System . Management . Automation ;
6
+ using System . Management . Automation . Language ;
6
7
using System . Reflection ;
7
8
using System . Text . RegularExpressions ;
8
9
using Tools . Common . Models ;
@@ -33,26 +34,16 @@ public void BumpAllVersions()
33
34
{
34
35
var moduleName = _fileHelper . ModuleName ;
35
36
Console . WriteLine ( "Bumping version for " + moduleName + "..." ) ;
36
- using ( PowerShell powershell = PowerShell . Create ( ) )
37
- {
38
- powershell . AddScript ( "Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process;" ) ;
39
- powershell . AddScript ( "$metadata = Test-ModuleManifest -Path " + _fileHelper . OutputModuleManifestPath + ";$metadata.Version;$metadata.PrivateData.PSData.Prerelease" ) ;
40
- var cmdletResult = powershell . Invoke ( ) ;
41
- _oldVersion = cmdletResult [ 0 ] ? . ToString ( ) ;
42
- _isPreview = ! string . IsNullOrEmpty ( cmdletResult [ 1 ] ? . ToString ( ) ) ;
43
- }
44
37
45
- if ( _oldVersion == null )
46
- {
47
- throw new Exception ( "Unable to obtain old version of " + moduleName + " using the built module manifest." ) ;
48
- }
38
+ ( _oldVersion , _isPreview ) = GetOldVersion ( ) ;
49
39
50
40
_newVersion = IsNewModule ( ) ? _oldVersion : GetBumpedVersion ( ) ;
51
- if ( MinimalVersion != null && MinimalVersion > new AzurePSVersion ( _newVersion ) )
41
+ if ( MinimalVersion != null && MinimalVersion > new AzurePSVersion ( _newVersion ) )
52
42
{
53
43
Console . WriteLine ( $ "Adjust version from { _newVersion } to { MinimalVersion } due to MinimalVersion.csv") ;
54
44
_newVersion = MinimalVersion . ToString ( ) ;
55
45
}
46
+
56
47
if ( _oldVersion == _newVersion )
57
48
{
58
49
Console . WriteLine ( _fileHelper . ModuleName + " is a new module. Keeping the version at " + _oldVersion ) ;
@@ -78,6 +69,86 @@ public void BumpAllVersions()
78
69
Console . WriteLine ( "Finished bumping version " + moduleName + "\n " ) ;
79
70
}
80
71
72
+ /// <summary>
73
+ /// Get the old version of the module.
74
+ /// Compare the version from PSGallery and TestGallery to the version obtain from manifest.
75
+ /// Choose the maximum one as the old version.
76
+ /// </summary>
77
+ /// <returns>The old version and is or not preview before the bump.</returns>
78
+ public Tuple < string , bool > GetOldVersion ( )
79
+ {
80
+ string version ;
81
+ string localVersion = null , psVersion = null , testVersion = null ;
82
+ bool isPreview ;
83
+ bool localPreview = false , psPreview = false , testPreview = false ;
84
+ var moduleName = _fileHelper . ModuleName ;
85
+
86
+ using ( PowerShell powershell = PowerShell . Create ( ) )
87
+ {
88
+ powershell . AddScript ( "Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process;" ) ;
89
+ powershell . AddScript ( "$metadata = Test-ModuleManifest -Path " + _fileHelper . OutputModuleManifestPath + ";$metadata.Version;$metadata.PrivateData.PSData.Prerelease" ) ;
90
+ var cmdletResult = powershell . Invoke ( ) ;
91
+ localVersion = cmdletResult [ 0 ] ? . ToString ( ) ;
92
+ localPreview = ! string . IsNullOrEmpty ( cmdletResult [ 1 ] ? . ToString ( ) ) ;
93
+ }
94
+ if ( localVersion == null )
95
+ {
96
+ throw new Exception ( "Unable to obtain old version of " + moduleName + " using the built module manifest." ) ;
97
+ }
98
+ version = localVersion ;
99
+ isPreview = localPreview ;
100
+
101
+ using ( PowerShell powetshell = PowerShell . Create ( ) )
102
+ {
103
+ powetshell . AddScript ( "Register-PackageSource -Name PSGallery -Location https://www.powershellgallery.com/api/v2 -ProviderName PowerShellGet" ) ;
104
+ powetshell . AddScript ( "Find-Module -Name " + moduleName + " -AllowPrerelease -Repository PSGallery -AllVersions" ) ;
105
+ var cmdletResult = powetshell . Invoke ( ) ;
106
+ if ( cmdletResult . Count != 0 )
107
+ {
108
+ var psVersionImformation = cmdletResult [ 0 ] . ToString ( ) ;
109
+ Regex reg = new Regex ( "Version=(.*?)(-|;)" ) ;
110
+ Match match = reg . Match ( psVersionImformation ) ;
111
+ psVersion = match . Groups [ 1 ] . Value ;
112
+ psPreview = Regex . IsMatch ( psVersionImformation , "preview" ) ;
113
+ }
114
+ }
115
+ if ( psVersion == null )
116
+ {
117
+ psVersion = "0.1.0" ;
118
+ }
119
+ if ( new AzurePSVersion ( psVersion ) > new AzurePSVersion ( version ) )
120
+ {
121
+ version = psVersion ;
122
+ isPreview = psPreview ;
123
+ }
124
+
125
+ using ( PowerShell powetshell = PowerShell . Create ( ) )
126
+ {
127
+ powetshell . AddScript ( "Register-PackageSource -Name TestGallery -Location https://www.poshtestgallery.com/api/v2 -ProviderName PowerShellGet" ) ;
128
+ powetshell . AddScript ( "Find-Module -Name " + moduleName + " -AllowPrerelease -Repository TestGallery -AllVersions" ) ;
129
+ var cmdletResult = powetshell . Invoke ( ) ;
130
+ if ( cmdletResult . Count != 0 )
131
+ {
132
+ var testVersionImformation = cmdletResult [ 0 ] . ToString ( ) ;
133
+ Regex reg = new Regex ( "Version=(.*?)(-|;)" ) ;
134
+ Match match = reg . Match ( testVersionImformation ) ;
135
+ testVersion = match . Groups [ 1 ] . Value ;
136
+ testPreview = Regex . IsMatch ( testVersionImformation , "preview" ) ;
137
+ }
138
+ }
139
+ if ( testVersion == null )
140
+ {
141
+ testVersion = "0.1.0" ;
142
+ }
143
+ if ( new AzurePSVersion ( testVersion ) > new AzurePSVersion ( version ) )
144
+ {
145
+ version = testVersion ;
146
+ isPreview = testPreview ;
147
+ }
148
+
149
+ return Tuple . Create ( version , isPreview ) ;
150
+ }
151
+
81
152
/// <summary>
82
153
/// Apply a version bump to a given version.
83
154
/// </summary>
0 commit comments