1+ # Script should be executed manually by developer
2+ $ModuleName = ' InstallGitModule'
3+
4+ # check running folder
5+ if (! (Test-Path " ..\$ModuleName \$ModuleName .psd1" )) {
6+ throw " We are not in correct folder"
7+ } else {
8+ " Checking module $ ( Resolve-Path " ..\$ModuleName \$ModuleName .psd1" ) "
9+ }
10+
11+ # test manifest
12+ try {
13+ $Module = Test-ModuleManifest " $ModuleName .psd1" - ea Stop
14+ " Module $ModuleName .psd1 is OK"
15+ } catch {
16+ throw ' Module manifest not in proper format'
17+ }
18+
19+ # test version, must be x.y.z
20+ if (($Module.Version.ToString () -split ' \.' ).Count -lt 3 ) {
21+ throw " Module version must have three segments at least, currently it is $ ( $Module.Version.ToString ()) "
22+ } else {
23+ " Module version $ ( $Module.Version.ToString ()) is OK"
24+ }
25+
26+ # test if remote is not the same
27+ if (Find-Module - Name $ModuleName - RequiredVersion ($Module.Version ) - Repository PSGallery - ea 0 ) {
28+ throw ' Module with same version already exists'
29+ } else {
30+ " No module with version $ ( $Module.Version ) found online"
31+ }
32+
33+ # get nuget key from somewhere?
34+ if ($NugetKey ) {
35+ " NugetKey found"
36+ } else {
37+ throw ' Please define $NugetKey variable (run $NugetKey = Read-Host)'
38+ }
39+
40+ # copy entire folder to temp location
41+ if ($IsLinux -or $IsMacOS ) {$Destination = ' /tmp' }
42+ else {$Destination = $Env: TEMP }
43+
44+ $Destination2 = Join-Path $Destination $ModuleName
45+ " Copying to $Destination2 "
46+ if (Test-Path $Destination2 ) {Remove-Item $Destination2 - Recurse - Force}
47+ Copy-Item - Path . - Destination $Destination - Recurse # it creates folder $ModuleName
48+
49+ # remove not needed files (starting with dot and from .gitignore)
50+ " Removing not needed files"
51+ [string []]$Exclude = (Get-Content ' .gitignore' )
52+ Get-ChildItem - Path $Destination2 - Recurse - Force | where Name -Match ' ^\.' | Remove-Item - Recurse - Force
53+ Get-ChildItem - Path $Destination2 - Include $Exclude - Recurse - Force | Remove-Item - Recurse - Force
54+
55+ # publish
56+ Read-Host " All prerequisites check. Press Enter to Publish module or Ctrl+C to abort"
57+ Publish-Module - Path $Destination2 - Repository PSGallery - NuGetApiKey $NugetKey - Verbose
58+ " Module $ModuleName published to PowerShell Gallery"
0 commit comments