-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPask.ps1
1251 lines (1057 loc) · 36.8 KB
/
Pask.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<#
.SYNOPSIS
This script defines a collection of code snippets used by Pask
.PARAMETER BuildProperties <System.Collections.Specialized.OrderedDictionary>
The build properties in scope
The order in which the properties are added is important to maintain consistency when they are being updated
.PARAMETER Files <System.Collections.ArrayList>
The files imported in the scope
.NOTE
DO NOT MODIFY - the script is managed and updated by Pask package
#>
param(
[System.Collections.Specialized.OrderedDictionary] [Alias("BuildProperties")] ${!BuildProperties!} = @{},
[System.Collections.ArrayList] [Alias("Files")] ${!Files!} = @($MyInvocation.MyCommand.Definition)
)
<#
.SYNOPSIS
Sets a build property
.PARAMETER Name <string>
The property name
.PARAMETER Value <ScriptBlock>
The property value
.PARAMETER Default <ScriptBlock>
The default value
.PARAMETER Update <switch>
After setting the property, updates all build properties with script block value
.OUTPUTS
None
.EXAMPLE
Set a build property with explicit value
Set-Property -Name Configuration -Value Debug
.EXAMPLE
Set a build property with value of session or default
Set-Property -Name Configuration -Default Release
.EXAMPLE
Set a build property with value of session
Set-Property -Name Configuration
#>
function script:Set-BuildProperty {
[CmdletBinding(DefaultParameterSetName="ValueOfSession")]
param(
[Parameter(Mandatory=$true,Position=0)][ValidateNotNullOrEmpty()][string]$Name,
[Parameter(ParameterSetName="ExplicitValue")]$Value,
[Parameter(ParameterSetName="ValueOfSessionOrDefault")]$Default,
[switch]$Update
)
$private:PropertyValue = switch ($PsCmdlet.ParameterSetName) {
"ExplicitValue" { $Value }
"ValueOfSessionOrDefault" {
if (${!BuildProperties!}.Contains($Name)) {
${!BuildProperties!}[$Name]
} else {
Get-BuildProperty $Name $Default
}
}
"ValueOfSession" {
if (${!BuildProperties!}.Contains($Name)) {
${!BuildProperties!}[$Name]
} else {
Get-BuildProperty $Name
}
}
}
$private:VariableValue = if ($private:PropertyValue -and $private:PropertyValue.GetType() -eq [ScriptBlock]) {
& $private:PropertyValue
} else {
$private:PropertyValue
}
if(-not ${!BuildProperties!}.Contains($Name)) {
New-Variable -Name $Name -Value $private:VariableValue -Scope Script -Force
${script:!BuildProperties!} = ${!BuildProperties!} + @{$Name=$private:PropertyValue}
} else {
Set-Variable -Name $Name -Value $private:VariableValue -Scope Script -Force
${!BuildProperties!}.Set_Item($Name, $private:PropertyValue)
${script:!BuildProperties!} = ${!BuildProperties!}
}
if ($Update) {
Update-BuildProperties
}
}
Set-Alias Set-Property Set-BuildProperty -Scope Script
<#
.SYNOPSIS
Gets all build properties
.OUTPUTS <System.Collections.Specialized.OrderedDictionary>
------------------- EXAMPLE -------------------
@{
Configuration = Debug
ProjectName = 'MyProject'
}
#>
function script:Get-BuildProperties {
${!BuildProperties!}.GetEnumerator() | Foreach -Begin {
$Result = [ordered]@{}
} -Process {
$Value = Get-Variable -Name $_.Key -ValueOnly
$Result.Add($_.Key, $Value)
} -End {
$Result
}
}
Set-Alias Get-Properties Get-BuildProperties -Scope Script
<#
.SYNOPSIS
Update build properties with script block value
.OUTPUTS
None
#>
function script:Update-BuildProperties {
$private:BuildProperties = ${!BuildProperties!}.GetEnumerator() | Where { $_.Value -and $_.Value.GetType() -eq [ScriptBlock] }
$private:BuildProperties | Foreach { Set-BuildProperty -Name $_.Key -Value $_.Value }
}
Set-Alias Update-Properties Update-BuildProperties -Scope Script
<#
.SYNOPSIS
Gets or set cache entries
.PARAMETER Key <string>
The cache key
.PARAMETER Value
The cache value
.OUTPUTS
None
.EXAMPLE
Set a cache key with the current function name
Cache-Pask $MyInvocation.MyCommand.Name -Value 1
.EXAMPLE
Get a cache entry for the current function name
Cache-Pask $MyInvocation.MyCommand.Name
.EXAMPLE
Get all cache entries
Cache-Pask
#>
function script:Cache-Pask {
[CmdletBinding(DefaultParameterSetName="All")]
param(
[Parameter(Position=0)]
[Parameter(Mandatory=$true,ParameterSetName="Get")]
[Parameter(Mandatory=$true,ParameterSetName="Set")]
[string]$Key,
[Parameter(Mandatory=$true,ParameterSetName="Set")]
$Value
)
switch ($PsCmdlet.ParameterSetName) {
"All" {
${!PaskCache!}.GetEnumerator() | Foreach -Begin { $Result = @{} } -Process { $Result.Add($_.Name, $_.Value) } -End { return $Result }
}
"Get" {
return ${!PaskCache!}.$Key
}
"Set" {
if (${!PaskCache!}.$Key) {
${!PaskCache!}.$Key = $Value
} else {
${!PaskCache!} = ${!PaskCache!} + @{$Key=$Value}
}
${script:!PaskCache!} = ${!PaskCache!}
}
}
}
<#
.SYNOPSIS
Creates a new directory, if not found
.PARAMETER Path <string[]>
Absolute or relative path
.OUTPUTS <System.IO.DirectoryInfo>
The directory
#>
function script:New-Directory {
param([parameter(Mandatory=$true,ValueFromPipeline=$true)][string[]]$Path)
Begin {
$private:Result = @()
}
Process {
if (-not (Test-Path "$Path")) {
$Result += New-Item -ItemType Directory -Path "$Path" -Force
} else {
$Result += Get-Item -Path "$Path"
}
}
End {
if ($Result.Count -eq 1) {
$Result[0]
} else {
$Result
}
}
}
<#
.SYNOPSIS
Delete an item(s)
.PARAMETER Path <string[]>
The path(s) to the item(s)
Wildcards are permitted (e.g. '.\dir\*')
It does not support wildacrd like '.\**\dir' or '.\*.dll'
.OUTPUTS
None
#>
function script:Remove-PaskItem {
param([parameter(Mandatory=$true,ValueFromPipeline=$true)][string[]]$Path)
Begin { }
Process {
if (-not (Test-Path -Path $Path)) {
return
}
$ItemObject = Get-Item -Path $Path
if ($ItemObject -is [System.IO.FileInfo] -and (Test-Path $ItemObject.FullName)) {
Remove-Item -Path $ItemObject.FullName -Force | Out-Null
} elseif ($ItemObject -is [System.IO.DirectoryInfo] -and (Test-Path $ItemObject.FullName)) {
$Count = 0
$Retry = 10
do {
$Count++
try { CMD /C ("RD /S /Q ""{0}""" -f $ItemObject.FullName) 2>&1 | Out-Null } catch { }
} while ((Test-Path $ItemObject.FullName) -and $Count -lt $Retry)
if ($Count -eq $Retry -and (Test-Path $ItemObject.FullName)) {
throw ("Failed to remove {0}" -f $ItemObject.FullName)
}
} else {
$ItemObject.FullName | Remove-PaskItem
}
}
End { }
}
<#
.SYNOPSIS
Delete the content from a directory
.PARAMETER Path <string[]>
The path(s) to the directory
.OUTPUTS
None
#>
function script:Clear-Directory {
param([parameter(Mandatory=$true,ValueFromPipeline=$true)][string[]]$Path)
Begin { }
Process {
if (-not (Test-Path -Path $Path)) {
return
}
$Item = Get-Item -Path $Path
$Item | % {
if ($_ -is [System.IO.DirectoryInfo]) {
Remove-PaskItem -Path (Join-Path $_.FullName "*")
}
}
}
End { }
}
<#
.SYNOPSIS
Gets the NuGet executable
.OUTPUTS <string>
Full name
#>
function script:Get-NuGetExe {
Join-Path $PaskFullPath ".nuget\NuGet.exe"
}
<#
.SYNOPSIS
Initializes the NuGet executable by downloading it, if not found
.OUTPUTS
None
#>
function script:Initialize-NuGetExe {
$NuGet = Get-NuGetExe
if(-not (Test-Path $NuGet -PathType Leaf) -or -not (Test-Path $NuGet)) {
# Download
New-Directory (Split-Path $NuGet) | Out-Null
# Installing NuGet command line https://docs.nuget.org/consume/command-line-reference
"Downloading NuGet.exe"
$(New-Object System.Net.WebClient).DownloadFile("https://dist.nuget.org/win-x86-commandline/latest/nuget.exe", $NuGet)
}
}
<#
.SYNOPSIS
Gets the NuGet packages directory
.OUTPUTS <string>
The full path
#>
function script:Get-PackagesDir {
if(Cache-Pask $MyInvocation.MyCommand.Name) { return Cache-Pask $MyInvocation.MyCommand.Name }
$PackagesDir = Join-Path $PaskFullPath "packages"
$NuGet = Get-NuGetExe
if (Test-Path $NuGet) {
Push-Location -Path (Split-Path $NuGet)
try {
$RepositoryPath = Invoke-Command -ScriptBlock { & $NuGet config repositoryPath -AsPath 2>$1 }
if ((-not [string]::IsNullOrWhiteSpace($RepositoryPath)) -and (Test-Path $RepositoryPath -PathType Container -IsValid)) {
$PackagesDir = $RepositoryPath
}
} catch {
} finally {
Pop-Location
}
}
Cache-Pask $MyInvocation.MyCommand.Name -Value $PackagesDir
return $PackagesDir
}
<#
.SYNOPSIS
Restores NuGet packages for the solution
.OUTPUTS
None
#>
function script:Restore-NuGetPackages {
if ($Task.Name -ne $MyInvocation.MyCommand.Name) {
Write-BuildMessage "Restore NuGet packages" -ForegroundColor "Cyan"
}
Initialize-NuGetExe
Exec { & $(Get-NuGetExe) restore "$SolutionFullName" -PackagesDirectory $(Get-PackagesDir) -NonInteractive -Verbosity quiet }
}
<#
.SYNOPSIS
Restores NuGet packages marked as development-only-dependency for the solution
.OUTPUTS
None
#>
function script:Restore-NuGetDevelopmentPackages {
Initialize-NuGetExe
$PackagesDir = Get-PackagesDir
$NuGetExe = Get-NuGetExe
$PackagesToInstall = Get-SolutionPackages | Where { $_.developmentDependency -eq $true } | Where { -not (Test-Path (Get-PackageDir $_.id)) }
if ($PackagesToInstall) {
$TempPackagesConfigContent = "<?xml version=""1.0"" encoding=""utf-8""?><packages>{0}</packages>" -f (($PackagesToInstall `
| % { "<package id=""{0}"" version=""{1}"" developmentDependency=""true"" />" -f $_.id, $_.version }) -join "")
$TempPackagesConfig = New-Item -ItemType File -Name "packages.$([System.IO.Path]::GetRandomFileName()).config" -Path $Env:Temp -Value $TempPackagesConfigContent
try {
Invoke-Command -ErrorAction Stop -ScriptBlock { & $NuGetExe install "$($TempPackagesConfig.FullName)" -OutputDirectory "$PackagesDir" -NonInteractive -Verbosity quiet }
if ($LastExitCode) {
throw "Error restoring development NuGet packages"
}
} finally {
Remove-Item $TempPackagesConfig.FullName -Force
}
}
}
<#
.SYNOPSIS
Writes a message to the host with custom background and foreground color
.PARAMETER Message <string>
.PARAMETER BackgroundColor <System.ConsoleColor>
The background color
.PARAMETER ForegroundColor <System.ConsoleColor>
The text color
.OUTPUTS
None
.EXAMPLE
Write a yellow message to the host
Write-BuildMessage "Copying assembly" -ForegroundColor "Yellow"
#>
function script:Write-BuildMessage {
param(
[Parameter(ValueFromPipeline=$true,Mandatory=$true,Position=0)][string]$Message,
[string]$BackgroundColor,
[string]$ForegroundColor
)
if ($psISE) {
$OriginalBackgroundColor = $psISE.Options.ConsolePaneBackgroundColor
$OriginalForegroundColor = $psISE.Options.ConsolePaneForegroundColor
if (-not $OriginalBackgroundColor -and -not $OriginalForegroundColor) {
$Message
} else {
try {
$psISE.Options.ConsolePaneBackgroundColor = if ($BackgroundColor) { [System.Windows.Media.Colors]::$BackgroundColor } else { $OriginalBackgroundColor }
$psISE.Options.ConsolePaneForegroundColor = if ($ForegroundColor) { [System.Windows.Media.Colors]::$ForegroundColor } else { $OriginalForegroundColor }
} finally {
$Message
$psISE.Options.ConsolePaneBackgroundColor = $OriginalBackgroundColor
$psISE.Options.ConsolePaneForegroundColor = $OriginalForegroundColor
}
}
} else {
$OriginalBackgroundColor = $Host.UI.RawUI.BackgroundColor
$OriginalForegroundColor = $Host.UI.RawUI.ForegroundColor
if (-not $OriginalBackgroundColor -and -not $OriginalForegroundColor) {
$Message
} else {
try {
$Host.UI.RawUI.BackgroundColor = if ($BackgroundColor) { $BackgroundColor } else { $OriginalBackgroundColor }
$Host.UI.RawUI.ForegroundColor = if ($ForegroundColor) { $ForegroundColor } else { $OriginalForegroundColor }
} finally {
$Message
$Host.UI.RawUI.BackgroundColor = $OriginalBackgroundColor
$Host.UI.RawUI.ForegroundColor = $OriginalForegroundColor
}
}
}
}
<#
.SYNOPSIS
Gets a project full name given its base name
.PARAMETER Name <string> = $ProjectName
The project name
.OUTPUTS <string>
The full name
#>
function script:Get-ProjectFullName {
param([string]$Name = $ProjectName)
Get-SolutionProjects `
| Where { $_.Name -eq $Name } `
| Select -First 1 @{ Name = "ProjectFullName"; Expression = { Join-Path $_.Directory $_.File } } `
| Select -ExpandProperty ProjectFullName
}
<#
.SYNOPSIS
Gets all projects in the solution
.OUTPUTS <object[]>
------------------- EXAMPLE -------------------
@(
@{
Name = 'Project'
File = 'Project.csproj'
Directory = 'C:\Solution_Dir\Project_Dir'
}
)
#>
function script:Get-SolutionProjects {
if(Cache-Pask $MyInvocation.MyCommand.Name) { return Cache-Pask $MyInvocation.MyCommand.Name }
$Projects = @()
Get-Content "$SolutionFullName" |
Select-String 'Project\(' |
ForEach {
$ProjectParts = $_ -Split '[,=]' | ForEach { $_.Trim('[ "{}]') };
if($ProjectParts[2] -match ".*\.\w+proj$") {
$ProjectPathParts = $ProjectParts[2].Split("\");
$Projects += New-Object PSObject -Property @{
Name = $ProjectParts[1];
File = $ProjectPathParts[-1];
Directory = Join-Path "$SolutionFullPath" $ProjectParts[2].Replace("\$($ProjectPathParts[-1])", "");
}
}
}
Cache-Pask $MyInvocation.MyCommand.Name -Value $Projects
return $Projects
}
<#
.SYNOPSIS
Gets all NuGet packages installed in the solution
.OUTPUTS <object[]>
------------------- EXAMPLE -------------------
@(
@{
id = 'NUnit'
version = '3.2.0'
}
)
#>
function script:Get-SolutionPackages {
if(Cache-Pask $MyInvocation.MyCommand.Name) { return Cache-Pask $MyInvocation.MyCommand.Name }
$Packages = @()
foreach($Project in Get-SolutionProjects) {
$PackagesConfig = Join-Path $Project.Directory "packages.config"
if(Test-Path $PackagesConfig) {
$Packages += ([xml](Get-Content -Path "$PackagesConfig")).packages.package
}
}
$SolutionPackages = $Packages | Select -Unique id, version, developmentDependency | Sort id, version
Cache-Pask $MyInvocation.MyCommand.Name -Value $SolutionPackages
return $SolutionPackages
}
<#
.SYNOPSIS
Gets the the package directory for a given NuGet package Id
.PARAMETER PackageId <string>
A NuGet package Id installed in the solution
.OUTPUTS <string>
The package directory
#>
function script:Get-PackageDir {
param([Parameter(ValueFromPipeline=$true,Mandatory=$true)][AllowEmptyString()][string]$PackageId)
if ([string]::IsNullOrWhiteSpace($PackageId)) {
throw "PackageId cannot be empty"
}
$MatchingPackages = Get-SolutionPackages | Where { $_.id -ieq $PackageId }
if ($MatchingPackages.Count -eq 0) {
throw "Cannot find '$PackageId' NuGet package in the solution"
} elseif ($MatchingPackages.Count -gt 1) {
throw "Found multiple versions of '$PackageId' NuGet package installed in the solution"
}
return Join-Path (Get-PackagesDir) ($MatchingPackages[0].id + '.' + $MatchingPackages[0].version)
}
<#
.SYNOPSIS
Imports all PS1 files with matching name, searching sequentially in
- any Pask.* package installed in the solution
- any Pask.* project in the solution
- $BuildFullPath
Import only occur only once, files are cached
Allows to import from specific projects/packages only
.PARAMETER File <string[]>
File names (no extension)
.PARAMETER Path <string>
Path relative to $BuildFullPath
.PARAMETER Project <string[]>
Project names matching ^Pask.*
.PARAMETER Package <string[]>
Packages name matching ^Pask.*
.PARAMETER Safe <switch> = $false
Tells not to error if the file is not found;
.OUTPUTS
None
#>
function script:Import-File {
param(
[Parameter(Mandatory=$true)][string[]]$File,
[Parameter(Mandatory=$true)][string]$Path,
[ValidatePattern('^Pask.*')][string[]]$Project,
[ValidatePattern('^Pask.*')][string[]]$Package,
[switch]$Safe
)
# List of directories in which to search the files
$Directories = @()
# Search the file in Pask.* packages
if ($Package) {
foreach ($Pkg in $Package) {
foreach ($_ in (Get-SolutionPackages | Where { $_.id -eq $Pkg })) {
$Directories += Get-PackageDir $_.id
}
}
} elseif (-not $Project) {
foreach ($Pkg in (Get-SolutionPackages | Where { $_.id -match "^Pask.*" })) {
$Directories += Get-PackageDir $Pkg.id
}
}
# Search the files in Pask.* projects
if ($Project) {
foreach ($Prj in $Project) {
foreach ($_ in (Get-SolutionProjects | Where { $_.Name -eq $Prj })) {
$Directories += $_.Directory
}
}
} elseif (-not $Package) {
foreach ($Prj in (Get-SolutionProjects | Where { $_.Name -match "^Pask.*" })) {
$Directories += $Prj.Directory
}
}
# Search the files in the build directory
if (-not $Project -and -not $Package) {
$Directories += $BuildFullPath
}
foreach($F in $File) {
foreach($Directory in $Directories) {
$FileFullName = Join-Path $Directory (Join-Path $Path "$F.ps1")
if (-not (Test-Path $FileFullName)) { continue }
Get-ChildItem $FileFullName | ForEach {
if (Get-Files $_.FullName) {
$Imported = $true
} else {
${!Files!}.Add($_.FullName) | Out-Null
${script:!Files!} = ${!Files!}
. $_.FullName
$Imported = $true
}
}
}
if (-not $Imported -and -not $Safe) {
throw "Cannot import $F"
}
}
}
<#
.SYNOPSIS
Imports all tasks with matching name, searching sequentially in
- any Pask.* package installed in the solution
- any Pask.* project in the solution
- $BuildFullPath\tasks
The latter imported overrides previous tasks imported with the same name
Import only occurs once, tasks are cached
Allows to import from specific projects/packages only
.PARAMETER Task <string[]>
Tasks name
.PARAMETER Project <string[]>
Project names matching ^Pask.*
.PARAMETER Package <string[]>
Packages name matching ^Pask.*
.OUTPUTS
None
#>
function script:Import-Task {
param(
[Parameter(Mandatory=$true)][string[]]$Task,
[ValidatePattern('^Pask.*')][string[]]$Project,
[ValidatePattern('^Pask.*')][string[]]$Package
)
$Params = @{ File = $Task; Path = "tasks" }
if ($Project) { $Params.Add("Project", $Project) }
if ($Package) { $Params.Add("Package", $Package) }
Import-File @Params
}
<#
.SYNOPSIS
Imports all scripts with matching name, searching sequentially in
- any Pask.* package installed in the solution
- any Pask.* project in the solution
- $BuildFullPath\scripts
Import only occurs once, scripts are cached
Allows to import from specific projects/packages only
.PARAMETER Script <string[]>
Scripts name
.PARAMETER Project <string[]>
Project names matching ^Pask.*
.PARAMETER Package <string[]>
Packages name matching ^Pask.*
.PARAMETER Safe <switch> = $false
Tells not to error if the file is not found
.OUTPUTS
None
#>
function script:Import-Script {
param(
[Parameter(Mandatory=$true)][string[]]$Script,
[ValidatePattern('^Pask.*')][string[]]$Project,
[ValidatePattern('^Pask.*')][string[]]$Package,
[switch]$Safe
)
$Params = @{ File = $Script; Path = "scripts"; Safe = $Safe }
if ($Project) { $Params.Add("Project", $Project) }
if ($Package) { $Params.Add("Package", $Package) }
Import-File @Params
}
<#
.SYNOPSIS
Imports the Properties script for a given Pask.* package/project or all
It does always import $BuildFullPath\scripts\Properties.ps1 (if found)
Import only occur only once, properties are cached
.PARAMETER Project <string[]>
Project names matching ^Pask.*
.PARAMETER Package <string[]>
Packages name matching ^Pask.*
.PARAMETER All <switch> = $false
Tells to import all Properties file found
.OUTPUTS
None
#>
function script:Import-Properties {
param(
[ValidatePattern('^Pask.*')][string[]]$Project,
[ValidatePattern('^Pask.*')][string[]]$Package,
[switch]$All
)
$PropertiesPath = "scripts\Properties.ps1"
if ($All) {
$AllProjects = Get-SolutionProjects | Where { $_.Name -match "^Pask.*" } | Select -ExpandProperty Name | Get-Unique
if($AllProjects) { $Project = $AllProjects }
$AllPackages = Get-SolutionPackages | Where { $_.id -match "^Pask.*" } | Select -ExpandProperty id | Get-Unique
if($AllPackages) { $Package = $AllPackages }
}
# Always import solution properties
$SolutionProperties = Join-Path $BuildFullPath $PropertiesPath
if ((Test-Path $SolutionProperties) -and -not (Get-Files $SolutionProperties)) {
${!Files!}.Add($SolutionProperties) | Out-Null
${script:!Files!} = ${!Files!}
. $SolutionProperties
}
# Import properties from projects
foreach ($Prj in $Project) {
$SolutionProject = Get-SolutionProjects | Where { $_.Name -eq $Prj } | Select -First 1
if ($SolutionProject) {
$ProjectProperties = Join-Path $SolutionProject.Directory $PropertiesPath
If ((Test-Path $ProjectProperties) -and -not (Get-Files $ProjectProperties)) {
${!Files!}.Add($ProjectProperties) | Out-Null
${script:!Files!} = ${!Files!}
. $ProjectProperties
}
}
}
# Import properties from packages
foreach ($Pkg in $Package) {
if (Get-SolutionPackages | Where { $_.id -eq $Pkg }) {
$PackageProperties = Join-Path (Get-PackageDir $Pkg) $PropertiesPath
if ((Test-Path $PackageProperties) -and -not (Get-Files $PackageProperties)) {
${!Files!}.Add($PackageProperties) | Out-Null
${script:!Files!} = ${!Files!}
. $PackageProperties
}
}
}
}
<#
.SYNOPSIS
Gets a list of imported files matching a file name
.PARAMETER Name <string>
The file base name or full name; wildcards are permitted
.OUTPUTS <string[]>
The files full name
#>
function script:Get-Files {
param([string]$Name = ".*")
if([System.IO.Path]::IsPathRooted($Name)) {
${!Files!} | Where { $_ -eq $Name }
} else {
${!Files!} | Where { $_ -match ".*\\$Name\.(ps1)$" }
}
}
<#
.SYNOPSIS
Sets the default project
.PARAMETER Name <string>
The project name
.OUTPUTS
None
#>
function script:Set-Project {
param([string]$Name)
$private:Project = Get-SolutionProjects | Where { $_.Name -eq $Name } | Select -First 1
if (-not $private:Project) {
Write-BuildMessage "Cannot find project $Name" -ForegroundColor "Yellow"
# Find first project in the solution
$private:Project = Get-SolutionProjects | Select -First 1
Write-BuildMessage ("Using default project {0}" -f $private:Project.Name) -ForegroundColor "Yellow"
Set-BuildProperty -Name ProjectName -Value $private:Project.Name
Set-BuildProperty -Name ProjectFullPath -Value $private:Project.Directory
} else {
Write-BuildMessage "Setting default project $Name"
Set-BuildProperty -Name ProjectName -Value $Name
Set-BuildProperty -Name ProjectFullPath -Value $private:Project.Directory
}
Set-BuildProperty -Name ProjectFullName -Value (Get-ProjectFullName)
Set-BuildProperty -Name ArtifactName -Default { $ProjectName }
Set-BuildProperty -Name ArtifactFullPath -Value { Join-Path $BuildOutputFullPath $ArtifactName }
Update-Properties
}
<#
.SYNOPSIS
Removes recursively any pdb (Program Database) file found in a given path
.PARAMETER Path <string>
.OUTPUTS
None
#>
function script:Remove-PdbFiles {
param([string]$Path)
foreach($Item in (Get-ChildItem -Path "$Path" -Recurse -File -Include *.pdb | Select-Object -ExpandProperty FullName)) {
Remove-PaskItem $Item
}
}
<#
.SYNOPSIS
Gets the MSBuild build output directory for a given a project name
.PARAMETER ProjectName <string>
.OUTPUTS <string>
The absolute path
#>
function script:Get-ProjectBuildOutputDir {
param([Parameter(Mandatory=$true,ValueFromPipeline=$true)][string]$ProjectName)
Begin {
Import-Script Properties.MSBuild -Package Pask
$private:Result = @()
$private:SolutionProjects = Get-SolutionProjects
}
Process {
$Directory = $SolutionProjects | Where { $_.Name -eq $ProjectName } | Select -First 1 -ExpandProperty Directory
Assert ($Directory -and (Test-Path $Directory)) "Cannot find project $ProjectName directory"
if ($BuildConfiguration -and $BuildPlatform -and (Test-Path (Join-Path $Directory "bin\$BuildPlatform\$BuildConfiguration"))) {
# Project directory with build configuration and platform
$Result += Join-Path $Directory "bin\$BuildPlatform\$BuildConfiguration"
} elseif ($BuildConfiguration -and (Test-Path (Join-Path $Directory "bin\$BuildConfiguration"))) {
# Project directory with build configuration
$Result += Join-Path $Directory "bin\$BuildConfiguration"
} elseif (Test-Path (Join-Path $Directory "bin")) {
# Project directory with bin folder
$Result += Join-Path $Directory "bin"
}
}
End {
if ($Result.Count -eq 1) {
$Result[0]
} else {
$Result
}
}
}
<#
.SYNOPSIS
Gets the git executable
.OUTPUTS <string>
The full name
#>
function script:Get-GitExe {
Get-Command git -ErrorAction SilentlyContinue | Select -ExpandProperty Source
}
<#
.SYNOPSIS
Determines whether the project is in a git repository
.OUTPUTS <bool>
#>
function script:Test-GitRepository {
$GitExe = Get-GitExe
if (-not $GitExe -or -not (Test-Path $GitExe)) {
return $false
} else {
try {
& $GitExe -C "$($PaskFullPath.Trim('\'))" rev-parse --is-inside-work-tree 2>&1 | Out-Null
} catch {
return $false
}
return $true
}
}
<#
.SYNOPSIS
Gets the last git committer date
.OUTPUTS <DateTime>
#>
function script:Get-CommitterDate {
if (-not (Test-GitRepository)) {
return [DateTime]::UtcNow
}
$Date = Exec { & (Get-GitExe) -C "$($PaskFullPath.Trim('\'))" show --no-patch --format=%ci }
return ([DateTime]::Parse($Date, $null, [System.Globalization.DateTimeStyles]::RoundtripKind)).ToUniversalTime()
}
<#
.SYNOPSIS
Gets the current git branch name
.OUTPUTS <object>
Outputs $null if not in a git repository
------------------- EXAMPLE -------------------
@{
Name = 'master'
IsMaster = $true
}
#>
function script:Get-Branch {
if (-not (Test-GitRepository)) {
return $null
}
$RawName = Exec { & (Get-GitExe) -C "$($PaskFullPath.Trim('\'))" name-rev --name-only HEAD }
if ($RawName -match '/([^/]+)$') {
# In this case we resolved refs/heads/branch_name but we are only interested in branch_name
$Name = $Matches[1]
} else {
$Name = $RawName
}
# If the current revision is behind HEAD, strip out such information from the name (e.g. master~1)
return ($Name -replace "[~]\d+", "") | Select @{Name="Name"; Expression={$_}}, @{Name="IsMaster"; Expression={$_ -eq "master"}}
}
<#
.SYNOPSIS
Gets the version based on the last git committer date
.OUTPUTS <object>
--------------------------------- EXAMPLE --------------------------------