Skip to content

Commit c521d53

Browse files
committed
CRXUpdateInfo
- added Id, FileName - changed Version type Functions load order (private vs public) Get-CRX. Test-CRXUpdateAvailable - support two parameters set
1 parent 74de841 commit c521d53

File tree

7 files changed

+96
-57
lines changed

7 files changed

+96
-57
lines changed

CRX/CRX.psd1

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11

22
@{
3-
RootModule = 'CRX.psm1'
4-
ModuleVersion = '0.1.1'
5-
GUID = 'b5433b6c-b423-4049-8c5e-b3a50566fcf2'
6-
Author = 'Alan Plocieniak'
7-
CompanyName = 'Alan Plocieniak'
8-
Copyright = '(c) 2025 Alan Plocieniak. All rights reserved.'
9-
Description = 'Module for downloading CRX (extension package) for Chromium-based browsers.'
3+
RootModule = 'CRX.psm1'
4+
ModuleVersion = '0.2.0'
5+
GUID = 'b5433b6c-b423-4049-8c5e-b3a50566fcf2'
6+
Author = 'Alan Plocieniak'
7+
CompanyName = 'Alan Plocieniak'
8+
Copyright = '(c) 2025 Alan Plocieniak. All rights reserved.'
9+
Description = 'Module for downloading CRX (extension package) for Chromium-based browsers.'
1010
PowerShellVersion = '5.1'
1111
CompatiblePSEditions = 'Desktop', 'Core'
12-
FunctionsToExport = '*'
12+
FunctionsToExport = '*'
1313
PrivateData = @{
1414
PSData = @{
1515
Tags = @('powershell', 'crx', 'ps', 'power-shell', 'CRX', 'chrome', 'extension' )

CRX/CRX.psm1

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
$Public = @( Get-ChildItem -Path $PSScriptRoot\Public -Recurse -Filter *.ps1 -ErrorAction SilentlyContinue )
22
$Private = @( Get-ChildItem -Path $PSScriptRoot\Private -Recurse -Filter *.ps1 -ErrorAction SilentlyContinue )
33

4-
Foreach ($import in @($Public + $Private)) {
4+
Foreach ($import in @($Private + $Public )) {
55
try {
66
. $import.fullname
77
}

CRX/Private/CRXUpdateInfo.ps1

-15
This file was deleted.

CRX/Public/Get-CRX.ps1

+17-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
11
function Get-CRX {
2-
[CmdletBinding()]
2+
[CmdletBinding(DefaultParameterSetName = 'ById')]
33
param (
4-
[Parameter(Mandatory = $true)]
4+
[Parameter(Mandatory = $true, ParameterSetName = 'ById')]
55
[string]$Id,
66

7+
[Parameter(Mandatory = $true, ParameterSetName = 'ByInfo')]
8+
$UpdateInfo,
9+
710
[Parameter(Mandatory = $true)]
811
[string]$OutputDirectory
912
)
1013

11-
$info = Get-CRXUpdateInfo -Id $Id
14+
if ($PSCmdlet.ParameterSetName -eq 'ById') {
15+
$info = Get-CRXUpdateInfo -Id $Id
16+
}
17+
else {
18+
$info = $UpdateInfo
19+
}
20+
21+
if ($null -eq $info) {
22+
return $null
23+
}
24+
1225
try {
13-
$outputPath = Join-Path -Path $OutputDirectory -ChildPath (Split-Path -Leaf $info.Url)
26+
$outputPath = Join-Path -Path $OutputDirectory -ChildPath $info.FileName
1427
Invoke-WebRequest -Uri $info.Url -OutFile $outputPath
1528
Get-Item -Path $outputPath
1629
}

CRX/Public/New-CRXUpdateInfo.ps1

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class CRXUpdateInfo {
2+
[string]$Id
3+
[string]$FileName
4+
[version]$Version
5+
[string]$Url
6+
[string]$SHA256
7+
[string]$Status
8+
[int]$Size
9+
10+
CRXUpdateInfo($obj) {
11+
if ($obj.version.Contains('.')) {
12+
$this.Version = $obj.version
13+
}
14+
else {
15+
$this.Version = $obj.version + ".0"
16+
}
17+
18+
$this.Url = $obj.codebase
19+
$this.FileName = Split-Path -Leaf $this.Url.ToLower()
20+
$this.Id = $this.FileName.Substring(0, 32)
21+
$this.SHA256 = $obj.hash_sha256
22+
$this.Status = $obj.status
23+
$this.Size = $obj.size
24+
}
25+
}
26+
27+
# work around for ps module class export issues
28+
function New-CRXUpdateInfo($ob) {
29+
return [CRXUpdateInfo]::new($ob)
30+
}

CRX/Public/Test-CRXUpdateAvailable.ps1

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
function Test-CRXUpdateAvailable {
2-
[CmdletBinding()]
2+
[CmdletBinding(DefaultParameterSetName = 'ById')]
33
param (
4-
[Parameter(Mandatory = $true)]
4+
[Parameter(Mandatory = $true, ParameterSetName = 'ById')]
55
[string]$Id,
66

7+
[Parameter(Mandatory = $true, ParameterSetName = 'ByInfo')]
8+
$UpdateInfo,
9+
710
[Parameter(Mandatory = $true)]
811
[version]$currentVersion
912
)
1013

11-
$updateInfo = Get-CRXUpdateInfo $Id
14+
if ($PSCmdlet.ParameterSetName -eq 'ById') {
15+
$updateInfo = Get-CRXUpdateInfo -Id $Id
16+
}
17+
else {
18+
$updateInfo = $UpdateInfo
19+
}
20+
1221
if ($updateInfo) {
13-
$updateVersion = [version]$updateInfo.Version
14-
return $updateVersion -gt $currentVersion
22+
return $updateInfo.Version -gt $currentVersion
1523
}
1624
else {
1725
return $false

tests/CRX.Tests.ps1

+27-24
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ if (-not (Get-Module -Name Pester)) {
33
}
44
Import-Module .\CRX\CRX.psm1 -Force
55

6+
$testUrl = 'https://localhost.com/crx/blobs/ASuc5ohLVu-itAJfZqe6NgPkB0pCREbOH49PhxJq4pMdp7MWQx-ycGQt8dsD8WUSM_dTlB5sLwXljaUve7GTKh485NrRlNGdmT7O5aT9uS4R9jmIqNJBAMZSmuV9IZ0e0VV7jGd-rrI-YR5eoIra2Q/AOCLHCCCFDKJDDGPAAAJLDGLJHLLHGMD_4_0_0_0.crx'
7+
$testExtensionId = 'aoclhcccfdkjddgpaaajldgljhllhgmd'
8+
69
Describe 'CRX.Tests' {
710
Context 'Get-CRXUpdateInfo' {
811
It 'Should return CRXUpdateInfo object' {
9-
$response = Get-CRXUpdateInfo "aoclhcccfdkjddgpaaajldgljhllhgmd"
12+
$response = Get-CRXUpdateInfo $testExtensionId
1013
$response.Url | Should -Not -BeNullOrEmpty
1114
$response.Version | Should -Not -BeNullOrEmpty
1215
}
@@ -20,48 +23,48 @@ Describe 'CRX.Tests' {
2023
Context 'Test-CRXUpdateAvailable' {
2124
It 'Should return true if update is available' {
2225
Mock -CommandName Get-CRXUpdateInfo -ModuleName CRX -MockWith {
23-
return [PSCustomObject]@{
24-
Version = "2.0.0"
25-
Url = "http://example.com"
26-
SHA256 = "dummyhash"
27-
Status = "ok"
28-
Size = 12345
26+
New-CRXUpdateInfo @{
27+
version = "2.0.0"
28+
codebase = $testUrl
29+
hash_sha256 = "dummyhash"
30+
status = "ok"
31+
size = 12345
2932
}
3033
}
31-
$result = Test-CRXUpdateAvailable -Id "aoclhcccfdkjddgpaaajldgljhllhgmd" -currentVersion "1.0.0"
34+
$result = Test-CRXUpdateAvailable -Id $testExtensionId -currentVersion "1.0.0"
3235
$result | Should -Be $true
3336
}
3437

3538
It 'Should return false if no update is available' {
3639
Mock -CommandName Get-CRXUpdateInfo -ModuleName CRX -MockWith {
37-
return [PSCustomObject]@{
38-
Version = "1.0.0"
39-
Url = "http://example.com"
40-
SHA256 = "dummyhash"
41-
Status = "ok"
42-
Size = 12345
40+
return New-CRXUpdateInfo @{
41+
version = "2.0.0"
42+
codebase = $testUrl
43+
hash_sha256 = "dummyhash"
44+
status = "ok"
45+
size = 12345
4346
}
4447
}
45-
$result = Test-CRXUpdateAvailable -Id "aoclhcccfdkjddgpaaajldgljhllhgmd" -currentVersion "1.0.0"
48+
$result = Test-CRXUpdateAvailable -Id $testExtensionId -currentVersion "2.0.0"
4649
$result | Should -Be $false
4750
}
4851

4952
It 'Should return false if update info is null' {
5053
Mock -CommandName Get-CRXUpdateInfo -ModuleName CRX -MockWith { return $null }
51-
$result = Test-CRXUpdateAvailable -Id "aoclhcccfdkjddgpaaajldgljhllhgmd" -currentVersion "1.0.0"
54+
$result = Test-CRXUpdateAvailable -Id $testExtensionId -currentVersion "1.0.0"
5255
$result | Should -Be $false
5356
}
5457
}
5558

5659
Context 'Get-CRX' {
5760
It 'Should download CRX file to specified directory' {
5861
Mock -CommandName Get-CRXUpdateInfo -ModuleName CRX -MockWith {
59-
return [PSCustomObject]@{
60-
Version = "2.0.0"
61-
Url = "http://example.com/extension.crx"
62-
SHA256 = "dummyhash"
63-
Status = "ok"
64-
Size = 12345
62+
return New-CRXUpdateInfo @{
63+
version = "2.0.0"
64+
codebase = $testUrl
65+
hash_sha256 = "dummyhash"
66+
status = "ok"
67+
size = 12345
6568
}
6669
}
6770

@@ -71,9 +74,9 @@ Describe 'CRX.Tests' {
7174
}
7275

7376
$outputDir = ".\temp"
74-
$result = Get-CRX -Id "aoclhcccfdkjddgpaaajldgljhllhgmd" -OutputDirectory $outputDir
77+
$result = Get-CRX -Id $testExtensionId -OutputDirectory $outputDir
7578

76-
$expectedPath = Join-Path -Path $outputDir -ChildPath "extension.crx"
79+
$expectedPath = Join-Path -Path $outputDir -ChildPath "$testExtensionId`_4_0_0_0.crx"
7780
Test-Path $expectedPath | Should -Be $true
7881
$result | Should -BeOfType [System.IO.FileInfo]
7982
$result.FullName | Should -Be (Resolve-Path $expectedPath | Select-Object -ExpandProperty Path)

0 commit comments

Comments
 (0)