Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .github/workflows/Update-FontsData.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ on:
schedule:
- cron: '0 0 * * *'

permissions: {}
permissions:
contents: write
pull-requests: write

jobs:
Update-FontsData:
Expand All @@ -15,11 +17,10 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
persist-credentials: false

- name: Update-FontsData
uses: PSModule/GitHub-Script@v1
with:
ClientID: ${{ secrets.NERDFONTS_UPDATER_BOT_CLIENT_ID }}
PrivateKey: ${{ secrets.NERDFONTS_UPDATER_BOT_PRIVATE_KEY }}
Script: scripts/Update-FontsData.ps1
63 changes: 43 additions & 20 deletions scripts/Update-FontsData.ps1
Original file line number Diff line number Diff line change
@@ -1,16 +1,42 @@
Connect-GitHubApp -Organization PSModule -Default
function Invoke-NativeCommand {
<#
.SYNOPSIS
Executes a native command with arguments.
#>
[Alias('Exec', 'Run')]
[CmdletBinding()]
param (
# The command to execute
[Parameter(Mandatory, Position = 0)]
[string]$Command,

git checkout main
git pull
# The arguments to pass to the command
[Parameter(ValueFromRemainingArguments)]
[string[]]$Arguments
)

# 2. Retrieve the date-time to create a unique branch name.
Write-Debug "Command: $Command"
Write-Debug "Arguments: $($Arguments -join ', ')"
$fullCommand = "$Command $($Arguments -join ' ')"

try {
Write-Verbose "Executing: $fullCommand"
& $Command @Arguments
if ($LASTEXITCODE -ne 0) {
$errorMessage = "Command failed with exit code $LASTEXITCODE`: $fullCommand"
Write-Error $errorMessage -ErrorId 'NativeCommandFailed' -Category OperationStopped -TargetObject $fullCommand
}
} catch {
throw
}
}

Invoke-NativeCommand git checkout main
Invoke-NativeCommand git pull
$timeStamp = Get-Date -Format 'yyyyMMdd-HHmmss'
$branchName = "auto-font-update-$timeStamp"
Invoke-NativeCommand git checkout -b $branchName

# 3. Create a new branch for the changes.
git checkout -b $branchName

# 4. Retrieve the latest font data from Nerd Fonts.
$release = Get-GitHubRelease -Owner ryanoasis -Repository nerd-fonts
$fonts = @()
$fontAssets = $release | Get-GitHubReleaseAsset | Where-Object { $_.Name -like '*.zip' }
Expand All @@ -22,25 +48,22 @@ foreach ($fontArchive in $fontAssets) {
}
}

# 5. Write results to FontsData.json.
LogGroup 'Latest Fonts' {
$fonts | Sort-Object Name | Format-Table -AutoSize | Out-String
}

$parentFolder = Split-Path -Path $PSScriptRoot -Parent
$filePath = Join-Path -Path $parentFolder -ChildPath 'src\FontsData.json'

# Make sure file exists (or overwrite).
$null = New-Item -Path $filePath -ItemType File -Force
$fonts | ConvertTo-Json | Set-Content -Path $filePath -Force

# 6. Check if anything actually changed.
# If git status --porcelain is empty, there are no new changes to commit.
$changes = git status --porcelain
$changes = Invoke-NativeCommand git status --porcelain
if (-not [string]::IsNullOrWhiteSpace($changes)) {
# 7. Commit and push changes.
git add .
git commit -m "Update-FontsData via script on $timeStamp"
git push --set-upstream origin $branchName
Invoke-NativeCommand git add .
Invoke-NativeCommand git commit -m "Update-FontsData via script on $timeStamp"
Invoke-NativeCommand git push --set-upstream origin $branchName

# 8. Create a PR via GitHub CLI.
gh pr create `
Invoke-NativeCommand gh pr create `
--base main `
--head $branchName `
--title "Auto-Update: NerdFonts Data ($timeStamp)" `
Expand Down
4 changes: 0 additions & 4 deletions src/FontsData.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
"Name": "AdwaitaMono",
"URL": "https://github.com/ryanoasis/nerd-fonts/releases/download/v3.4.0/AdwaitaMono.zip"
},
{
"Name": "Agave",
"URL": "https://github.com/ryanoasis/nerd-fonts/releases/download/v3.4.0/Agave.zip"
},
{
"Name": "AnonymousPro",
"URL": "https://github.com/ryanoasis/nerd-fonts/releases/download/v3.4.0/AnonymousPro.zip"
Expand Down
Loading