-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGet-GitModule.ps1
115 lines (93 loc) · 4.48 KB
/
Get-GitModule.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
function Get-GitModule {
[CmdletBinding(HelpUri='https://github.com/iricigor/InstallModuleFromGit/blob/master/Docs/Get-GitModule.md')]
param (
[Parameter(Mandatory,ValueFromPipelineByPropertyName,Position=0,ParameterSetName='ByUri')]
[string[]]$ProjectUri,
# https://github.com/dfinke/InstallModuleFromGitHub
# https://github.com/iricigor/FIFA2018
[Parameter(Mandatory,ParameterSetName='ByName')]
[string[]]$Name,
[string]$Branch = "master",
[switch]$KeepTempCopy
)
BEGIN {
$FunctionName = $MyInvocation.MyCommand.Name
Write-Verbose -Message "$(Get-Date -f G) $FunctionName starting"
if (!(Test-Prerequisites)) {
throw "$FunctionName prerequisites not met"
# TODO: Add more details
}
if ($env:AGENT_TEMPDIRECTORY) {
$tmpRoot = $env:AGENT_TEMPDIRECTORY
} else {
$tmpRoot = [System.IO.Path]::GetTempPath()
}
if ($Name) {$ProjectUri = ConvertTo-Uri -Name $Name}
}
PROCESS {
foreach ($P1 in $ProjectUri) {
Write-Verbose -Message "$(Get-Date -f T) processing $P1"
$ModuleName = ($P1 -split '/')[-1]
$tempDir = Join-Path $tmpRoot $ModuleName
if (!(Test-Path $tempDir)) {
Write-Verbose -Message "$(Get-Date -f T) creating directory $tempDir"
New-Item $tempDir -ItemType Directory -Force | Out-Null
} elseif (Get-ChildItem $tempDir -Force) {
Write-Verbose -Message "$(Get-Date -f T) deleting content of temp directory $tempDir"
Remove-Item (Join-Path $tempDir '*') -Recurse -Force
}
Write-Verbose -Message "$(Get-Date -f T) cloning repository to $tempDir"
git clone $P1 --branch $Branch --single-branch $tempDir --quiet
$psd1 = Get-ChildItem $tempDir -Include *.psd1 -Recurse
if (!$psd1) {
# try to make manifest from psm1 file
Write-Verbose -Message "$(Get-Date -f T) manifest not found, searching for root module"
$psm1 = Get-ChildItem $tempDir -Include *.psm1 -Recurse
if ($psm1.FullName -is [string]) {
Write-Verbose -Message "$(Get-Date -f T) root module $($psm1.Name) found"
$psd1File = $psm1.FullName -replace 'psm1$','psd1'
New-ModuleManifest -Path $psd1File -RootModule $psm1.Name -ModuleVersion ([version]::new()) | Out-Null
$psd1 = Get-ChildItem $tempDir -Include *.psd1 -Recurse
}
}
$psd0 = $psd1 | ? BaseName -eq $ModuleName
if (($psd1 -is [array]) -and (@($psd0).Count -ne 1)) {
$errorText = "$FunctionName found multiple module manifests for $ModuleName"
} elseif (($psd1 -is [array]) -and (@($psd0).Count -eq 1)) {
$ModuleVersion = (Get-Content -Raw $psd0.FullName | Invoke-Expression).ModuleVersion
$errorText = $null
$psd1 = $psd0
} elseif (!($psd1.FullName -is [string])) {
$errorText = "$FunctionName found no module manifest for $ModuleName"
} else {
$ModuleVersion = (Get-Content -Raw $psd1.FullName | Invoke-Expression).ModuleVersion
$errorText = $null
}
if ($KeepTempCopy) {
Write-Verbose -Message "$(Get-Date -f T) not deleting temp copy"
} else {
Write-Verbose -Message "$(Get-Date -f T) deleting temp copy"
Remove-Item $tempDir -Force -Recurse -ea 0 | Out-Null
}
if ($errorText) {
# we need to throw the error after deleting temp directory
Write-Error $errorText
continue
}
# return value
Write-Verbose -Message "$(Get-Date -f T) preparing return value"
[PSCustomObject]@{
Name = $ModuleName
Version = $ModuleVersion
LocalPath = if ($KeepTempCopy) {$tempDir} else {$null}
Root = ((Split-Path $psd1.FullName -Parent) -eq $tempDir)
SameName = ($psd1.BaseName -eq $ModuleName)
ManifestName = $psd1.BaseName
GitPath = $P1
}
}
}
END {
Write-Verbose -Message "$(Get-Date -f G) $FunctionName completed"
}
}