Skip to content

Commit

Permalink
feat: add chocolatey support
Browse files Browse the repository at this point in the history
  • Loading branch information
r-Larch committed Nov 28, 2023
1 parent a89683b commit 514e484
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,7 @@ jobs:
Or download the Source Code and Build it on your own.
files: ./MouseTrap-${{ steps.version.outputs.version }}.zip

- name: Push to Chocolatey
env:
CHOCO_TOKEN: ${{ secrets.CHOCO_TOKEN }}
run: pwsh .\MouseTrap\choco\Push.ps1 -nuspec .\MouseTrap\choco\MouseTrap.nuspec -zip ./MouseTrap-${{ steps.version.outputs.version }}.zip -version ${{ steps.version.outputs.version_tag }}
2 changes: 2 additions & 0 deletions MouseTrap.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
.github\workflows\build.yml = .github\workflows\build.yml
LICENSE.txt = LICENSE.txt
README.md = README.md
VERIFICATION.txt = VERIFICATION.txt
EndProjectSection
EndProject
Global
Expand Down
43 changes: 43 additions & 0 deletions MouseTrap/choco/MouseTrap.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<package>
<metadata>
<id>MouseTrap</id>
<version>0.0.0</version>
<title>MouseTrap DPI aware mouse move across screens</title>
<tags>multiscreen mouse-pointer screen-scale dpi-awareness dpi-scaling cursor-moves cursor-position</tags>
<summary>A small tool to map the cursor between monitors with different DPIs</summary>
<description>
# MouseTrap

MouseTrap is a small tool to map the cursor between multiple monitors with
different resolutions and scaling settings.

## Min requirements

For this tool to function correctly you should have:

- At least **Windows 10 Creators update** (Build 1703)
- [**.NET 8 Runtime**](https://dotnet.microsoft.com/en-us/download/dotnet/8.0) (in most cases it will notify you if the runtime is missing)

## Usage and Configuration

Here you can find the [documentation](https://github.com/r-Larch/MouseTrap/blob/master/README.md).
</description>
<authors>René Larch</authors>
<owners>René Larch</owners>
<copyright>Copyright 2023 René Larch</copyright>
<licenseUrl>https://github.com/r-Larch/MouseTrap/blob/master/LICENSE.txt</licenseUrl>
<projectUrl>https://github.com/r-Larch/MouseTrap</projectUrl>
<projectSourceUrl>https://github.com/r-Larch/MouseTrap</projectSourceUrl>
<bugTrackerUrl>https://github.com/r-Larch/MouseTrap/issues</bugTrackerUrl>
<docsUrl>https://github.com/r-Larch/MouseTrap/blob/master/README.md</docsUrl>
<iconUrl>https://raw.githubusercontent.com/r-Larch/MouseTrap/master/MouseTrap/AppIcon.png</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<releaseNotes>...</releaseNotes>
</metadata>
<files>
<file src="..\..\LICENSE.txt" />
<file src="..\..\VERIFICATION.txt" />
<file src="*.ps1" target="tools" />
</files>
</package>
125 changes: 125 additions & 0 deletions MouseTrap/choco/Push.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@

param (
[string] $nuspec,
[string] $zip,
[string] $version,
[switch] $debug = $false
)


function Main() {
$dir = Split-Path -parent $nuspec;

$choco = @{
version = $version
nuspec = $nuspec
dir = $dir
installScript = [System.IO.Path]::Combine($dir, "chocolateyinstall.ps1")
uninstallScript = [System.IO.Path]::Combine($dir, "chocolateyuninstall.ps1")
}

$hash = (Get-FileHash -Algorithm SHA256 -Path $zip).Hash.ToUpper()
ChocoUpdateScript $choco $version $hash

Push-Location $dir
$previousTag = [Git]::GetVersionTags() | where { $_ -ne "v$version" } | sort -Descending | select -first 2 | select -last 1;
$history = [Git]::GetHistorySince($previousTag);
Pop-Location

ChocoUpdateNuspec $choco $history "https://api.github.com/repos/r-Larch/MouseTrap"

ChocoPublish $choco $debug

}


function ChocoUpdateScript([object] $choco, [string] $version, [string] $hash) {
$script = Get-Content $choco.installScript -Encoding UTF8 -Raw
$script = $script.replace('<version>', $version)
$script = $script.replace('<hash>', $hash)
$script | Set-Content $choco.installScript -Force -Encoding UTF8
}


function ChocoUpdateNuspec([object] $choco, [string] $history, [string] $githubRepoApi) {
$repo = $(Invoke-Webrequest $githubRepoApi).Content | ConvertFrom-Json;
$topics = $(Invoke-Webrequest $githubRepoApi/topics -Headers @{'Accept'='application/vnd.github.mercy-preview+json'}).Content | ConvertFrom-Json
$xml = [xml] $(gc -Path $choco.nuspec -Encoding UTF8);
$xml.package.metadata.version = [Regex]::Replace($version, '^v', '');
$xml.package.metadata.summary = $repo.description;
$xml.package.metadata.tags = [string]::Join(" ", $topics.names);
$xml.package.metadata.copyright = "Copyright $([DateTime]::Now.Year) René Larch";
$xml.package.metadata.releaseNotes = [string]::Join("`r`n", $history);
$xml.Save($choco.nuspec);
}


function ChocoPublish([object] $choco, [bool] $debug = $false) {

$chocoCommand = "choco";
if (!$(Get-Command $chocoCommand -errorAction SilentlyContinue)) {
Write-Host "Install chocolatey";
iwr https://chocolatey.org/install.ps1 -UseBasicParsing | iex
$env:Path += ";%ALLUSERSPROFILE%\chocolatey\bin";
}

# create the nuspec package
& $chocoCommand pack $choco.nuspec --out $choco.dir

$nupkgName = $choco.nuspec.replace('.nuspec', ".$($choco.version).nupkg");

if (!$debug) {
# if token is given, we will publish the package to Chocolatey here
if ($env:CHOCO_TOKEN) {
& $chocoCommand apiKey -k $env:CHOCO_TOKEN -source https://push.chocolatey.org/
& $chocoCommand push $nupkgName -source https://push.chocolatey.org/
} else {
Write-Warning "Chocolatey token was not set. Publication skipped."
}
} else {
# For development/debugging purposes
$script = Get-Content $choco.installScript -Encoding UTF8 -Raw
Write-Host "=============== Choco Install Script ==============="
Write-Host $script
Write-Host "===================================================="

$script = Get-Content $choco.uninstallScript -Encoding UTF8 -Raw
Write-Host "============== Choco Uninstall Script =============="
Write-Host $script
Write-Host "===================================================="

$nuspec = Get-Content $choco.nuspec -Encoding UTF8 -Raw
Write-Host "================== Nuspec ==========================="
Write-Host $nuspec
Write-Host "===================================================="

Write-Host "$chocoCommand pack " $choco.nuspec
Write-Host "$chocoCommand apiKey -k $env:CHOCO_TOKEN -source https://push.chocolatey.org/"
Write-Host "$chocoCommand push $nupkgName"
}
}


class Git {
static [string[]] GetTags() {
return $(git tag --list).Split("`n");
}
static [string[]] GetVersionTags() {
return [Git]::GetTags() | Select-String -pattern "v[\d+\.]+(-(alpha|beta))?";
}
static [string[]] GetHistorySince([string] $tagOrHash) {
if ([string]::IsNullOrEmpty($tagOrHash)){
return $(git log --oneline);
}
return $(git log "$tagOrHash..HEAD" --oneline);
}
}


#########################
# run main #
#########################

Main;

#########################
32 changes: 32 additions & 0 deletions MouseTrap/choco/chocolateyinstall.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
$ErrorActionPreference = 'Stop';

$version = '<version>'
$hash = '<hash>'
$versionNumber = [Regex]::Replace($version, 'v(\d+\.\d+\.\d+).*', '$1')

$packageName = 'mousetrap'
$toolsDir = "$(Split-Path -parent $MyInvocation.MyCommand.Definition)"
$url = "https://github.com/r-Larch/MouseTrap/releases/download/$version/MouseTrap-$versionNumber.zip"


$packageArgs = @{
packageName = $packageName
unzipLocation = $toolsDir
url = $url
checksum = $hash
checksumType = 'SHA256'
}

Install-ChocolateyZipPackage @packageArgs


$mousetrap = "$toolsDir\$packageName";

# install
& $mousetrap "-i";
if (-not $?) {
throw "error while install";
}

# start the app minimized as tray icon in taskbar.
& $mousetrap
13 changes: 13 additions & 0 deletions MouseTrap/choco/chocolateyuninstall.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

$ErrorActionPreference = 'Stop';

$packageName= 'mousetrap.exe'
$toolsDir = "$(Split-Path -parent $MyInvocation.MyCommand.Definition)"

$mousetrap = "$toolsDir\$packageName";

# uninstall
& $mousetrap "-u";
if (-not $?) {
throw "error while uninstall";
}
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,20 @@ For this tool to function correctly you should have:
You can find the latest release here:
> [Download](https://github.com/r-Larch/MouseTrap/releases)
You can install MouseTrap with **chocolatey**:
```Powershell
# install the package
choco install mousetrap
# running it
mousetrap
```

## Changelog

### Version 1.0.20
- Add `chocolatey` support

### Version 1.0.19

- **Migrate to .NET 8**
Expand Down
17 changes: 15 additions & 2 deletions VERIFICATION.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,22 @@
VERIFICATION
Verification is intended to assist the Chocolatey moderators and community
in verifying that this package's contents are trustworthy.


This package contains MouseTrap.exe; This is an open source Windows Application written by me (René Larch).

This package contains the tool MouseTrap.
This is an open source Windows Application written by me (René Larch).
You can find the source code on github:

* https://github.com/r-Larch/MouseTrap/

Files:

|-------------------------------|--------------------------------------------------------|
| Files | Description |
|-------------------------------|--------------------------------------------------------|
| MouseTrap.exe | Executable .NET wrapper to start the .NET Core Runtime |
| MouseTrap.dll | The actual application to run in the .NET Core Runtime |
| MouseTrap.pdb | Debugging symbols to have better error reports |
| MouseTrap.deps.json | Dependencies file required for the .NET Core Runtime |
| MouseTrap.runtimeconfig.json | Runtimeconfig file required for the .NET Core Runtime |
|-------------------------------|--------------------------------------------------------|

0 comments on commit 514e484

Please sign in to comment.