Skip to content

Commit d991f3b

Browse files
authored
Get-Module by name (#40)
* first try * better test * different module in test * may the force... * force help update * retry force * encoding * more diff options * undo * AlphabeticParamsOrder
1 parent 28615c8 commit d991f3b

File tree

4 files changed

+65
-9
lines changed

4 files changed

+65
-9
lines changed

Docs/Get-GitModule.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,16 @@ This cmdlet will check for existence of PowerShell module in given repository an
1212

1313
## SYNTAX
1414

15+
### ByUri
1516
```
1617
Get-GitModule [-ProjectUri] <String[]> [-Branch <String>] [-KeepTempCopy] [<CommonParameters>]
1718
```
1819

20+
### ByName
21+
```
22+
Get-GitModule -Name <String[]> [-Branch <String>] [-KeepTempCopy] [<CommonParameters>]
23+
```
24+
1925
## DESCRIPTION
2026
This cmdlet will check for existence of PowerShell module in given repository and return its version.
2127
You can also specify desired git branch.
@@ -24,7 +30,7 @@ Cmdlet requires \`git\` client tool to work.
2430
It will download (\`git clone\`) specified repository to temporary directory and analyze it.
2531
By default, it will delete this temporary copy, but if needed, it can be kept.
2632

27-
Cmdlet searches for module manifest ( .psd1) file or if that is not found for module (.psm1) file itself.
33+
Cmdlet searches for module manifest ( .psd1) file. If that is not found, then it searches for module (.psm1) file itself.
2834

2935
## EXAMPLES
3036

@@ -39,7 +45,7 @@ Root : True
3945
Git : https://github.com/iricigor/FIFA2018
4046
```
4147

42-
This cmdlet will check for existence of PowerShell module in given repository (https://github.com/iricigor/FIFA2018') and return its version (currently 0.3.46) .
48+
This cmdlet will check for existence of PowerShell module in given repository (https://github.com/iricigor/FIFA2018') and return its version (currently 0.3.46).
4349

4450
### Example 2
4551
```
@@ -90,6 +96,22 @@ Accept pipeline input: False
9096
Accept wildcard characters: False
9197
```
9298
99+
### -Name
100+
You can query already installed modules for their online version if ProjectUri is specified in the module info.
101+
To do this, just specify module name(s) with parameter -Names.
102+
103+
```yaml
104+
Type: String[]
105+
Parameter Sets: ByName
106+
Aliases:
107+
108+
Required: True
109+
Position: Named
110+
Default value: None
111+
Accept pipeline input: False
112+
Accept wildcard characters: False
113+
```
114+
93115
### -ProjectUri
94116
Mandatory parameter specifying URL or the repository.
95117
Multiple values are supported.
@@ -100,7 +122,7 @@ You can pass this parameter also via pipeline, for example via \`Find-Module\` b
100122
101123
```yaml
102124
Type: String[]
103-
Parameter Sets: (All)
125+
Parameter Sets: ByUri
104126
Aliases:
105127

106128
Required: True

Public/Get-GitModule.ps1

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ function Get-GitModule {
55
param (
66

77

8-
[Parameter(Mandatory,ValueFromPipelineByPropertyName,Position=0)]
8+
[Parameter(Mandatory,ValueFromPipelineByPropertyName,Position=0,ParameterSetName='ByUri')]
99
[string[]]$ProjectUri,
1010
# https://github.com/dfinke/InstallModuleFromGitHub
1111
# https://github.com/iricigor/FIFA2018
1212

13+
[Parameter(Mandatory,ParameterSetName='ByName')]
14+
[string[]]$Name,
15+
1316
[string]$Branch = "master",
1417
[switch]$KeepTempCopy
1518

@@ -28,7 +31,29 @@ function Get-GitModule {
2831
$tmpRoot = $env:AGENT_TEMPDIRECTORY
2932
} else {
3033
$tmpRoot = [System.IO.Path]::GetTempPath()
31-
}
34+
}
35+
36+
if ($Name) {
37+
Write-Verbose -Message "$(Get-Date -f T) searching module URIs from their names"
38+
$ProjectUri = foreach ($N1 in $Name) {
39+
$Module = Get-InstalledModule $N1 -ea 0
40+
if (!$Module) {$Module = Get-Module $N1 -ListAvailable -ea 0}
41+
42+
if (!$Module) {
43+
Write-Error "$FunctionName found no module $N1"
44+
continue
45+
}
46+
47+
if (!($Module | ? ProjectUri)) {
48+
Write-Warning "$FunctionName found module $N1, but it has no ProjectUri information"
49+
continue
50+
}
51+
52+
# return information to $ProjectUri variable
53+
$Module | Sort-Object Version | Select-Object -Last 1 -ExpandProperty ProjectUri
54+
55+
}
56+
}
3257

3358
}
3459

@@ -62,8 +87,13 @@ function Get-GitModule {
6287
}
6388
}
6489

65-
if($psd1 -is [array]) {
90+
$psd0 = $psd1 | ? BaseName -eq $ModuleName
91+
if (($psd1 -is [array]) -and (@($psd0).Count -ne 1)) {
6692
$errorText = "$FunctionName found multiple module manifests for $ModuleName"
93+
} elseif (($psd1 -is [array]) -and (@($psd0).Count -eq 1)) {
94+
$ModuleVersion = (Get-Content -Raw $psd0.FullName | Invoke-Expression).ModuleVersion
95+
$errorText = $null
96+
$psd1 = $psd0
6797
} elseif (!($psd1.FullName -is [string])) {
6898
$errorText = "$FunctionName found no module manifest for $ModuleName"
6999
} else {

Tests/functions/Get-GitModule.Tests.ps1

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ Describe "$CommandName basic testing" -Tag 'Functionality' {
3333
(Get-GitModule $moduleURL).Name | Should -Be $moduleName
3434
}
3535

36-
36+
$moduleName = 'FIFA2018'
37+
Install-Module -Name $moduleName -Repository PSGallery -Scope CurrentUser -Force
38+
It "$CommandName finds module $moduleName by name" {
39+
(Get-GitModule -Name $moduleName).Name | Should -Be $moduleName
40+
}
3741

3842
}

Tests/module/InstallModuleFromGit.Tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ Describe 'Proper Documentation' -Tag 'Documentation' {
7676

7777
# update documentation
7878
Push-Location -Path $root
79-
Update-MarkdownHelp -Path .\Docs
79+
Update-MarkdownHelp -Path .\Docs -AlphabeticParamsOrder
8080
New-ExternalHelp -Path .\Docs -OutputPath .\en-US -Force
8181

8282
# test it
83-
$diff = git diff --ignore-space-change .\Docs .\en-US
83+
$diff = git diff --ignore-space-change .\Docs .\en-US
8484
Pop-Location
8585
$diff | Should -Be $null
8686
}

0 commit comments

Comments
 (0)