Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
130 changes: 103 additions & 27 deletions scripts/Update-FontsData.ps1
Original file line number Diff line number Diff line change
@@ -1,16 +1,67 @@
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
)

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
}
}

# Get the current branch and default branch information
$currentBranch = (Invoke-NativeCommand git rev-parse --abbrev-ref HEAD).Trim()
$defaultBranch = (Invoke-NativeCommand git remote show origin | Select-String 'HEAD branch:' | ForEach-Object { $_.ToString().Split(':')[1].Trim() })

Write-Output "Current branch: $currentBranch"
Write-Output "Default branch: $defaultBranch"

# Fetch latest changes from remote
Invoke-NativeCommand git fetch origin

# Get the head branch (latest default branch)
Invoke-NativeCommand git checkout $defaultBranch
Invoke-NativeCommand git pull origin $defaultBranch

# 2. Retrieve the date-time to create a unique branch name.
$timeStamp = Get-Date -Format 'yyyyMMdd-HHmmss'
$branchName = "auto-font-update-$timeStamp"

# 3. Create a new branch for the changes.
git checkout -b $branchName
# Determine target branch based on current context
if ($currentBranch -eq $defaultBranch) {
# Running on main/default branch - create new branch
$targetBranch = "auto-font-update-$timeStamp"
Write-Output "Running on default branch. Creating new branch: $targetBranch"
Invoke-NativeCommand git checkout -b $targetBranch
} else {
# Running on another branch (e.g., workflow_dispatch) - use current branch
$targetBranch = $currentBranch
Write-Output "Running on feature branch. Using existing branch: $targetBranch"
Invoke-NativeCommand git checkout $targetBranch
# Merge latest changes from default branch
Invoke-NativeCommand git merge origin/$defaultBranch
}

# 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,31 +73,56 @@ 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

# 8. Create a PR via GitHub CLI.
gh pr create `
--base main `
--head $branchName `
--title "Auto-Update: NerdFonts Data ($timeStamp)" `
--body 'This PR updates FontsData.json with the latest NerdFonts metadata.'

Write-Output 'Changes detected and PR opened.'
Write-Output 'Changes detected:'
Write-Output $changes

# Show what will be committed
Write-Output 'Diff of changes to be committed:'
Invoke-NativeCommand git diff --cached HEAD -- src/FontsData.json 2>$null
if ($LASTEXITCODE -ne 0) {
# If --cached doesn't work (no staged changes), show unstaged diff
Invoke-NativeCommand git diff HEAD -- src/FontsData.json
}

Invoke-NativeCommand git add .
Invoke-NativeCommand git commit -m "Update-FontsData via script on $timeStamp"

# Show the commit that was just created
Write-Output 'Commit created:'
Invoke-NativeCommand git log -1 --oneline

# Show diff between HEAD and previous commit
Write-Output 'Changes in this commit:'
Invoke-NativeCommand git diff HEAD~1 HEAD -- src/FontsData.json

# Push behavior depends on branch type
if ($targetBranch -eq $currentBranch -and $currentBranch -ne $defaultBranch) {
# Push to existing branch
Invoke-NativeCommand git push origin $targetBranch
Write-Output "Changes committed and pushed to existing branch: $targetBranch"
} else {
# Push new branch and create PR
Invoke-NativeCommand git push --set-upstream origin $targetBranch

Invoke-NativeCommand gh pr create `
--base $defaultBranch `
--head $targetBranch `
--title "Auto-Update: NerdFonts Data ($timeStamp)" `
--body 'This PR updates FontsData.json with the latest NerdFonts metadata.'

Write-Output "Changes detected and PR opened for branch: $targetBranch"
}
} else {
Write-Output 'No changes to commit.'
}
Loading