-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPSTelegramAPI.build.ps1
143 lines (113 loc) · 5.16 KB
/
PSTelegramAPI.build.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<#
.Synopsis
Build script (https://github.com/nightroman/Invoke-Build)
#>
param ($Configuration = 'Development')
#region Set-BuildEnvironment
Try { Set-BuildEnvironment -ErrorAction SilentlyContinue -Force } Catch { }
#endregion
#region use the most strict mode
Set-StrictMode -Version Latest
#endregion
#region Task to Update TLSharp Package if newer version is released
task UpdateTLSharpPackage {
# Check current TLSharp Package version
# Get TLSharp.Core.dll file properties
$ProductionPath = Get-ChildItem -Path .\lib\ -Filter TLSharp.Core.dll -Recurse | Select-Object -Expand Directory
$ProductionFolder = Split-Path $ProductionPath -Leaf
[Version]$ProductVersion = ($ProductionFolder -Split "\.")[-4,-3,-2,-1] -Join "."
Write-Output -InputObject ('ProductVersion {0}' -f $ProductVersion)
# Check latest version TLSharpPackage
$LatestPackage = Find-Package -Name TLSharp -Provider Nuget -Source 'https://www.nuget.org/api/v2'
[Version]$LatestVersion = $LatestPackage.Version
Write-Output -InputObject ('Latest Version {0}' -f $LatestVersion)
#Download latest version when newer
If ($LatestVersion -gt $ProductVersion) {
Write-Output -InputObject ('Newer version {0} available' -f $LatestVersion)
#Install TLSharp package to temp folder
$LatestPackage | Install-Package -Force -Confirm:$false | Out-Null
$LatestPackage = Get-Package -Name TLSharp
$LatestPath = Split-Path $LatestPackage.Source
$LatestFolder = Split-Path $LatestPath -Leaf
Write-Output -InputObject ('Remove current TLSharp binaries')
Remove-Item -Path $ProductionFolder -Recurse -Force -Confirm:$false
Write-Output -InputObject ('Copy TLSharp binaries to PSTelegramAPI Module')
New-Item -Path ".\lib\${LatestFolder}" -ItemType Directory | Out-Null
Get-ChildItem -Path $LatestPath -Filter *.dll -Recurse | Copy-Item -Destination ".\lib\${LatestFolder}"
}
else {
Write-Output -InputObject ('Current local version {0}. Latest version {1}' -f $ProductVersion, $LatestVersion)
}
}
#endregion
#region Task to run all Pester tests in folder .\tests
task Test {
$OutputPath = New-Item -Path '.\TestResults' -ItemType Directory -Force -Verbose
$PesterParams = @{
Script = '.\Tests'
OutputFile = "${OutputPath}\TestResults-PSTelegramAPI.xml"
CodeCoverage = 'PSTelegramAPI\*\*.ps1'
CodeCoverageOutputFile = "${OutputPath}\CodeCoverage-PSTelegramAPI.xml"
CodeCoverageOutputFileFormat = 'JaCoCo'
}
$Result = Invoke-Pester @PesterParams -PassThru
if ($Result.FailedCount -gt 0) {
throw 'Pester tests failed'
}
}
#endregion
#region Task to update the Module Manifest file with info from the Changelog in Readme.
task UpdateManifest {
# Import PlatyPS. Needed for parsing README for Change Log versions
#Import-Module -Name PlatyPS
$ManifestPath = '.\PSTelegramAPI\PSTelegramAPI.psd1'
$ModuleManifest = Test-ModuleManifest -Path $ManifestPath
[System.Version]$ManifestVersion = $ModuleManifest.Version
Write-Output -InputObject ('Manifest Version : {0}' -f $ManifestVersion)
$PSGalleryModule = Find-Module -Name PSTelegramAPI -Repository PSGallery
[System.Version]$PSGalleryVersion = $PSGalleryModule.Version
Write-Output -InputObject ('PSGallery Version : {0}' -f $PSGalleryVersion)
If ($PSGalleryVersion -ge $ManifestVersion) {
[System.Version]$Version = New-Object -TypeName System.Version -ArgumentList ($PSGalleryVersion.Major, $PSGalleryVersion.Minor, ($PSGalleryVersion.Build + 1))
Write-Output -InputObject ('Updated Version : {0}' -f $Version)
Update-ModuleManifest -ModuleVersion $Version -Path .\PSTelegramAPI\PSTelegramAPI.psd1 # -ReleaseNotes $ReleaseNotes
}
}
#endregion
#region Task to Publish Module to PowerShell Gallery
task PublishModule -If ($Configuration -eq 'Production') {
Try {
# Publish to gallery with a few restrictions
if(
$env:BHModulePath -and
$env:BHBuildSystem -ne 'Unknown' -and
$env:BHBranchName -eq "master" -and
$env:BHCommitMessage -match '!publish'
)
{
# Build a splat containing the required details and make sure to Stop for errors which will trigger the catch
$params = @{
Path = ".\PSTelegramAPI"
NuGetApiKey = $ENV:NuGetApiKey
ErrorAction = 'Stop'
}
Publish-Module @params
Write-Output -InputObject ('PSTelegramAPI PowerShell Module version published to the PowerShell Gallery')
}
else
{
"Skipping deployment: To deploy, ensure that...`n" +
"`t* You are in a known build system (Current: $ENV:BHBuildSystem)`n" +
"`t* You are committing to the master branch (Current: $ENV:BHBranchName) `n" +
"`t* Your commit message includes !deploy (Current: $ENV:BHCommitMessage)" |
Write-Host
}
}
Catch {
throw $_
}
}
#endregion
#region Default Task. Runs Test, UpdateManifest, PublishModule Tasks
task . Test, UpdateManifest, PublishModule
#endregion