Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanbergstrom committed Mar 16, 2024
0 parents commit eb0cb61
Show file tree
Hide file tree
Showing 13 changed files with 650 additions and 0 deletions.
121 changes: 121 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# This is a basic workflow to help you get started with Actions

name: CI

defaults:
run:
shell: pwsh

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
release:
types: [ published ]

jobs:
Build:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v3
- name: Bundle up module
uses: actions/upload-artifact@v3
with:
name: module
path: ./src/
Test:
needs: Build
runs-on: macos-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v3
- name: Download module
uses: actions/download-artifact@v3
with:
name: module
path: ~/.local/share/powershell/Modules/AnyPackage.Homebrew/
- name: Install Croze
run: Install-Module Croze -Force
- name: Install AnyPackage
run: Install-Module AnyPackage -Force -AllowClobber
- name: Check Modules' Presence
run: Get-Module AnyPackage* -ListAvailable
- name: Check Provider Registration
run: Import-Module AnyPackage.Homebrew; Get-Module; Get-PackageProvider
- name: Test with Pester
run: |
Invoke-Pester -Configuration (New-PesterConfiguration -Hashtable @{
Run = @{
Exit = $true
}
Output = @{
Verbosity = 'Detailed'
}
})
- name: Upload Homebrew logs
if: always()
uses: actions/upload-artifact@v3
with:
name: Homebrew-logs
path: "~/Library/Logs/Homebrew/"
Sign:
needs: Test
if: github.event_name == 'release' && github.event.action == 'published'
runs-on: windows-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v3
- name: Download module
uses: actions/download-artifact@v3
with:
name: module
path: module
- name: Import certificate
env:
CERTIFICATE_BASE64: ${{ secrets.CERTIFICATE_BASE64 }}
CERTIFICATE_PASSWORD: ${{ secrets.CERTIFICATE_PASSWORD }}
CERTIFICATE_PASSWORD_KEY_BASE64: ${{ secrets.CERTIFICATE_PASSWORD_KEY_BASE64 }}
run: |
[convert]::FromBase64String($env:CERTIFICATE_BASE64) | Set-Content -Path cert.pfx -AsByteStream
$key = [convert]::FromBase64String($env:CERTIFICATE_PASSWORD_KEY_BASE64)
$password = ConvertTo-SecureString $env:CERTIFICATE_PASSWORD -Key $key
Import-PfxCertificate cert.pfx -Password $password -CertStoreLocation Cert:\CurrentUser\My
- name: Sign files
run: |
$config = Import-PowerShellDataFile SignSettings.psd1
$config['Certificate'] = Get-ChildItem Cert:\CurrentUser\My -CodeSigningCert
Set-Location .\module
Set-AuthenticodeSignature @config
- name: Create and sign catalog file
run: |
$config = Import-PowerShellDataFile SignSettings.psd1
$config['FilePath'] = 'AnyPackage.Homebrew.cat'
$config['Certificate'] = Get-ChildItem Cert:\CurrentUser\My -CodeSigningCert
Set-Location .\module
New-FileCatalog $config['FilePath'] -CatalogVersion 2
Set-AuthenticodeSignature @config
- name: Upload module
uses: actions/upload-artifact@v3
with:
name: module-signed
path: ./module/
Publish:
needs: Sign
if: github.event_name == 'release' && github.event.action == 'published'
runs-on: ubuntu-latest
steps:
- name: Download module
uses: actions/download-artifact@v3
with:
name: module-signed
path: '~/.local/share/powershell/Modules/AnyPackage.Homebrew'
- name: Install Croze
run: Install-Module Croze -Force
- name: Install AnyPackage
run: Install-Module AnyPackage -Force -AllowClobber
- name: Publish Module
env:
NUGET_KEY: ${{ secrets.NUGET_KEY }}
run: Write-Output "Publishing..."; Publish-Module -Name AnyPackage.Homebrew -NuGetApiKey $env:NUGET_KEY
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [0.0.1] - 2024-03-16

Initial release

[Unreleased]: https://github.com/anypackage/Homebrew/compare/v0.0.1...HEAD
[0.0.1]: https://github.com/anypackage/Homebrew/releases/tag/v0.0.1
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Ethan Bergstrom

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
85 changes: 85 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# AnyPackage.Homebrew

[![gallery-image]][gallery-site]
[![build-image]][build-site]
[![cf-image]][cf-site]

[gallery-image]: https://img.shields.io/powershellgallery/dt/AnyPackage.Homebrew
[build-image]: https://img.shields.io/github/actions/workflow/status/anypackage/Homebrew/ci.yml
[cf-image]: https://img.shields.io/codefactor/grade/github/anypackage/Homebrew
[gallery-site]: https://www.powershellgallery.com/packages/AnyPackage.Homebrew
[build-site]: https://github.com/anypackage/Homebrew/actions/workflows/ci.yml
[cf-site]: https://www.codefactor.io/repository/github/anypackage/Homebrew

AnyPackage.Homebrew is an AnyPackage provider that facilitates installing Homebrew packages from any compatible repository.

## Requirements

Your machine must have at least Windows 10 1709 or Windows 11 and either PowerShell 5.1+ or PowerShell 7.0.1+, and the Homebrew CLI utility installed. It may be already installed on your machine, but if not, Microsoft's recommended method for installing Homebrew is via the Microsoft Store as part of the [App Installer](https://www.microsoft.com/en-us/p/app-installer/9nblggh4nns1?activetab=pivot:overviewtab) package.

## Install AnyPackage.Homebrew

```PowerShell
Install-Module AnyPackage.Homebrew -Force
```

## Import AnyPackage.Homebrew

```PowerShell
Import-Module AnyPackage.Homebrew
```

## Sample usages

### Search for a package

```PowerShell
Find-Package -Name OpenJS.NodeJS
Find-Package -Name Mozilla.Firefox
```

### Install a package

```PowerShell
Find-Package OpenJS.NodeJS | Install-Package
Install-Package -Name Git.Git
```

### Get list of installed packages (with wildcard search support)

```PowerShell
Get-Package Microsoft.*
```

### Uninstall a package

```PowerShell
Get-Package OpenJS.NodeJS| Uninstall-Package
Uninstall-Package Git.Git
```

### Manage package sources

```PowerShell
Register-PackageSource privateRepo -Provider Homebrew -Location 'https://somewhere/out/there/cache'
Find-Package OpenJS.NodeJS -Source privateRepo | Install-Package
Unregister-PackageSource privateRepo
```

AnyPackage.Homebrew integrates with Homebrew.exe to manage and store source information.

## Known Issues

### Stability

Homebrew does not currently have an official module available on PowerShell Gallery, therefore this provider currently depends on a Cresendo module that is a best-effort attempt at parsing Homebrew's output. Due to Homebrew still being heavily in development, it's output patterns fluctuate regularly, making a Cresdendo-based implementation very brittle. As such, currently this provider **should not** be used in production scenarios.

### Search Specificity

Due to Homebrew's ambiguous search behavior and the risks of unintended outcomes, this provider only allows searching of packages on remote repositories by exact package ID. Fuzzy searches and searches by moniker, display name, and tag are not supported.

## Legal and Licensing

AnyPackage.Homebrew is licensed under the [MIT license](./LICENSE.txt).
5 changes: 5 additions & 0 deletions SignSettings.psd1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@{
FilePath = @('*', '.\private\*')
TimeStampServer = 'http://timestamp.sectigo.com'
HashAlgorithm = 'SHA256'
}
35 changes: 35 additions & 0 deletions src/AnyPackage.Homebrew.psd1
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@{
RootModule = 'AnyPackage.Homebrew.psm1'
ModuleVersion = '0.0.1'
CompatiblePSEditions = 'Core'
GUID = '4de67142-5a15-488f-b51f-9b28d8d16d46'
Author = 'Ethan Bergstrom'
Copyright = '(c) 2024 Ethan Bergstrom. All rights reserved.'
Description = 'AnyPackage provider that facilitates installing Homebrew packages from any compatible repository.'
PowerShellVersion = '7.0.1'
FunctionsToExport = @()
CmdletsToExport = @()
AliasesToExport = @()
RequiredModules = @(
@{
ModuleName = 'AnyPackage'
ModuleVersion = '0.5.1'
},
@{
ModuleName = 'Croze'
ModuleVersion = '0.1.2'
}
)
PrivateData = @{
AnyPackage = @{
Providers = 'Homebrew'
}
PSData = @{
Tags = @('AnyPackage','Provider','Homebrew','Windows')
LicenseUri = 'https://github.com/anypackage/Homebrew/blob/main/LICENSE'
ProjectUri = 'https://github.com/anypackage/Homebrew'
ReleaseNotes = 'Please see https://github.com/anypackage/Homebrew/blob/main/CHANGELOG.md for release notes'
}
}
HelpInfoURI = 'https://go.anypackage.dev/help'
}
86 changes: 86 additions & 0 deletions src/AnyPackage.Homebrew.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using module AnyPackage
using namespace AnyPackage.Provider

# Current script path
[string]$ScriptPath = Split-Path (Get-Variable MyInvocation -Scope Script).Value.MyCommand.Definition -Parent

# Dot sourcing private script files
Get-ChildItem $ScriptPath/private -Recurse -Filter '*.ps1' -File | ForEach-Object {
. $_.FullName
}

[PackageProvider("Homebrew")]
class HomebrewProvider : PackageProvider, IGetSource, ISetSource, IGetPackage, IFindPackage, IInstallPackage, IUninstallPackage {
[void] GetSource([SourceRequest] $Request) {
Get-HomebrewTap | Where-Object {$_.Name -Like $Request.Name} | Croze\Get-HomebrewTapInfo | ForEach-Object {
$source = [PackageSourceInfo]::new($_.Name, ($_.Remote ?? $_.Path), $true, $this.ProviderInfo)
$Request.WriteSource($source)
}
}

[void] RegisterSource([SourceRequest] $Request) {
Croze\Register-HomebrewTap -Name $Request.Name -Location $Request.Location
# Homebrew doesn't return anything after source operations, so we make up our own output object
$source = [PackageSourceInfo]::new($Request.Name, $Request.Location.TrimEnd("\"), $Request.Trusted, $this.ProviderInfo)
$Request.WriteSource($source)
}

[void] UnregisterSource([SourceRequest] $Request) {
Get-HomebrewTap | Where-Object {$_.Name -Like $Request.Name} | Get-HomebrewTapInfo | ForEach-Object {
Croze\Unregister-HomebrewTap -Name $_.Name
$sourceInfo = [PackageSourceInfo]::new($_.Name, ($_.Remote ?? $_.Path), $this.ProviderInfo)
$Request.WriteSource($sourceInfo)
}
}

[void] SetSource([SourceRequest] $Request) {
$this.RegisterSource($Request)
}

[void] GetPackage([PackageRequest] $Request) {
Get-HomebrewPackage | Write-Package
}

[void] FindPackage([PackageRequest] $Request) {
Find-HomebrewPackage | Write-Package
}

[void] InstallPackage([PackageRequest] $Request) {
# Run the package request first through Find-HomebrewPackage to determine which source to use, and filter by any version requirements
$package = Find-HomebrewPackage

$installArgs = @{
Name = "$($package.Tap)/$($package.Name)"
}

switch ($package) {
{$_.Cask} {$installArgs.Cask = $true}
{$_.Formula} {$installArgs.Formula = $true}
}

Croze\Install-HomebrewPackage @installArgs | Croze\Get-HomebrewPackageInfo | Write-Package
}

[void] UninstallPackage([PackageRequest] $Request) {
# Run the package request first through Get-HomebrewPackage to filter by any version requirements and save it off for later use
$package = Get-HomebrewPackage

$uninstallArgs = @{
Name = $package.Token ?? $package.Name
}

switch ($package) {
{$_.Cask} {$uninstallArgs.Cask = $true}
{$_.Formula} {$uninstallArgs.Formula = $true}
}

Croze\Uninstall-HomebrewPackage @uninstallArgs

# Homebrew doesn't return any output on successful uninstallation, so we have to make up a new object to satisfy AnyPackage
Write-Package $package
}
}

[guid] $id = '4de67142-5a15-488f-b51f-9b28d8d16d46'
[PackageProviderManager]::RegisterProvider($id, [HomebrewProvider], $MyInvocation.MyCommand.ScriptBlock.Module)
$MyInvocation.MyCommand.ScriptBlock.Module.OnRemove = { [PackageProviderManager]::UnregisterProvider($id) }
Loading

0 comments on commit eb0cb61

Please sign in to comment.