Skip to content

Commit

Permalink
CRXUpdateInfo
Browse files Browse the repository at this point in the history
- added Id, FileName
- changed Version type

Functions load order (private vs public)

Get-CRX. Test-CRXUpdateAvailable - support two parameters set
  • Loading branch information
alan-null committed Jan 28, 2025
1 parent 74de841 commit c521d53
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 57 deletions.
16 changes: 8 additions & 8 deletions CRX/CRX.psd1
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@

@{
RootModule = 'CRX.psm1'
ModuleVersion = '0.1.1'
GUID = 'b5433b6c-b423-4049-8c5e-b3a50566fcf2'
Author = 'Alan Plocieniak'
CompanyName = 'Alan Plocieniak'
Copyright = '(c) 2025 Alan Plocieniak. All rights reserved.'
Description = 'Module for downloading CRX (extension package) for Chromium-based browsers.'
RootModule = 'CRX.psm1'
ModuleVersion = '0.2.0'
GUID = 'b5433b6c-b423-4049-8c5e-b3a50566fcf2'
Author = 'Alan Plocieniak'
CompanyName = 'Alan Plocieniak'
Copyright = '(c) 2025 Alan Plocieniak. All rights reserved.'
Description = 'Module for downloading CRX (extension package) for Chromium-based browsers.'
PowerShellVersion = '5.1'
CompatiblePSEditions = 'Desktop', 'Core'
FunctionsToExport = '*'
FunctionsToExport = '*'
PrivateData = @{
PSData = @{
Tags = @('powershell', 'crx', 'ps', 'power-shell', 'CRX', 'chrome', 'extension' )
Expand Down
2 changes: 1 addition & 1 deletion CRX/CRX.psm1
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
$Public = @( Get-ChildItem -Path $PSScriptRoot\Public -Recurse -Filter *.ps1 -ErrorAction SilentlyContinue )
$Private = @( Get-ChildItem -Path $PSScriptRoot\Private -Recurse -Filter *.ps1 -ErrorAction SilentlyContinue )

Foreach ($import in @($Public + $Private)) {
Foreach ($import in @($Private + $Public )) {
try {
. $import.fullname
}
Expand Down
15 changes: 0 additions & 15 deletions CRX/Private/CRXUpdateInfo.ps1

This file was deleted.

21 changes: 17 additions & 4 deletions CRX/Public/Get-CRX.ps1
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
function Get-CRX {
[CmdletBinding()]
[CmdletBinding(DefaultParameterSetName = 'ById')]
param (
[Parameter(Mandatory = $true)]
[Parameter(Mandatory = $true, ParameterSetName = 'ById')]
[string]$Id,

[Parameter(Mandatory = $true, ParameterSetName = 'ByInfo')]
$UpdateInfo,

[Parameter(Mandatory = $true)]
[string]$OutputDirectory
)

$info = Get-CRXUpdateInfo -Id $Id
if ($PSCmdlet.ParameterSetName -eq 'ById') {
$info = Get-CRXUpdateInfo -Id $Id
}
else {
$info = $UpdateInfo
}

if ($null -eq $info) {
return $null
}

try {
$outputPath = Join-Path -Path $OutputDirectory -ChildPath (Split-Path -Leaf $info.Url)
$outputPath = Join-Path -Path $OutputDirectory -ChildPath $info.FileName
Invoke-WebRequest -Uri $info.Url -OutFile $outputPath
Get-Item -Path $outputPath
}
Expand Down
30 changes: 30 additions & 0 deletions CRX/Public/New-CRXUpdateInfo.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class CRXUpdateInfo {
[string]$Id
[string]$FileName
[version]$Version
[string]$Url
[string]$SHA256
[string]$Status
[int]$Size

CRXUpdateInfo($obj) {
if ($obj.version.Contains('.')) {
$this.Version = $obj.version
}
else {
$this.Version = $obj.version + ".0"
}

$this.Url = $obj.codebase
$this.FileName = Split-Path -Leaf $this.Url.ToLower()
$this.Id = $this.FileName.Substring(0, 32)
$this.SHA256 = $obj.hash_sha256
$this.Status = $obj.status
$this.Size = $obj.size
}
}

# work around for ps module class export issues
function New-CRXUpdateInfo($ob) {
return [CRXUpdateInfo]::new($ob)
}
18 changes: 13 additions & 5 deletions CRX/Public/Test-CRXUpdateAvailable.ps1
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
function Test-CRXUpdateAvailable {
[CmdletBinding()]
[CmdletBinding(DefaultParameterSetName = 'ById')]
param (
[Parameter(Mandatory = $true)]
[Parameter(Mandatory = $true, ParameterSetName = 'ById')]
[string]$Id,

[Parameter(Mandatory = $true, ParameterSetName = 'ByInfo')]
$UpdateInfo,

[Parameter(Mandatory = $true)]
[version]$currentVersion
)

$updateInfo = Get-CRXUpdateInfo $Id
if ($PSCmdlet.ParameterSetName -eq 'ById') {
$updateInfo = Get-CRXUpdateInfo -Id $Id
}
else {
$updateInfo = $UpdateInfo
}

if ($updateInfo) {
$updateVersion = [version]$updateInfo.Version
return $updateVersion -gt $currentVersion
return $updateInfo.Version -gt $currentVersion
}
else {
return $false
Expand Down
51 changes: 27 additions & 24 deletions tests/CRX.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ if (-not (Get-Module -Name Pester)) {
}
Import-Module .\CRX\CRX.psm1 -Force

$testUrl = 'https://localhost.com/crx/blobs/ASuc5ohLVu-itAJfZqe6NgPkB0pCREbOH49PhxJq4pMdp7MWQx-ycGQt8dsD8WUSM_dTlB5sLwXljaUve7GTKh485NrRlNGdmT7O5aT9uS4R9jmIqNJBAMZSmuV9IZ0e0VV7jGd-rrI-YR5eoIra2Q/AOCLHCCCFDKJDDGPAAAJLDGLJHLLHGMD_4_0_0_0.crx'
$testExtensionId = 'aoclhcccfdkjddgpaaajldgljhllhgmd'

Describe 'CRX.Tests' {
Context 'Get-CRXUpdateInfo' {
It 'Should return CRXUpdateInfo object' {
$response = Get-CRXUpdateInfo "aoclhcccfdkjddgpaaajldgljhllhgmd"
$response = Get-CRXUpdateInfo $testExtensionId
$response.Url | Should -Not -BeNullOrEmpty
$response.Version | Should -Not -BeNullOrEmpty
}
Expand All @@ -20,48 +23,48 @@ Describe 'CRX.Tests' {
Context 'Test-CRXUpdateAvailable' {
It 'Should return true if update is available' {
Mock -CommandName Get-CRXUpdateInfo -ModuleName CRX -MockWith {
return [PSCustomObject]@{
Version = "2.0.0"
Url = "http://example.com"
SHA256 = "dummyhash"
Status = "ok"
Size = 12345
New-CRXUpdateInfo @{
version = "2.0.0"
codebase = $testUrl
hash_sha256 = "dummyhash"
status = "ok"
size = 12345
}
}
$result = Test-CRXUpdateAvailable -Id "aoclhcccfdkjddgpaaajldgljhllhgmd" -currentVersion "1.0.0"
$result = Test-CRXUpdateAvailable -Id $testExtensionId -currentVersion "1.0.0"
$result | Should -Be $true
}

It 'Should return false if no update is available' {
Mock -CommandName Get-CRXUpdateInfo -ModuleName CRX -MockWith {
return [PSCustomObject]@{
Version = "1.0.0"
Url = "http://example.com"
SHA256 = "dummyhash"
Status = "ok"
Size = 12345
return New-CRXUpdateInfo @{
version = "2.0.0"
codebase = $testUrl
hash_sha256 = "dummyhash"
status = "ok"
size = 12345
}
}
$result = Test-CRXUpdateAvailable -Id "aoclhcccfdkjddgpaaajldgljhllhgmd" -currentVersion "1.0.0"
$result = Test-CRXUpdateAvailable -Id $testExtensionId -currentVersion "2.0.0"
$result | Should -Be $false
}

It 'Should return false if update info is null' {
Mock -CommandName Get-CRXUpdateInfo -ModuleName CRX -MockWith { return $null }
$result = Test-CRXUpdateAvailable -Id "aoclhcccfdkjddgpaaajldgljhllhgmd" -currentVersion "1.0.0"
$result = Test-CRXUpdateAvailable -Id $testExtensionId -currentVersion "1.0.0"
$result | Should -Be $false
}
}

Context 'Get-CRX' {
It 'Should download CRX file to specified directory' {
Mock -CommandName Get-CRXUpdateInfo -ModuleName CRX -MockWith {
return [PSCustomObject]@{
Version = "2.0.0"
Url = "http://example.com/extension.crx"
SHA256 = "dummyhash"
Status = "ok"
Size = 12345
return New-CRXUpdateInfo @{
version = "2.0.0"
codebase = $testUrl
hash_sha256 = "dummyhash"
status = "ok"
size = 12345
}
}

Expand All @@ -71,9 +74,9 @@ Describe 'CRX.Tests' {
}

$outputDir = ".\temp"
$result = Get-CRX -Id "aoclhcccfdkjddgpaaajldgljhllhgmd" -OutputDirectory $outputDir
$result = Get-CRX -Id $testExtensionId -OutputDirectory $outputDir

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

0 comments on commit c521d53

Please sign in to comment.