Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
6 changes: 3 additions & 3 deletions .github/workflows/Update-FontsData.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ on:
schedule:
- cron: '0 0 * * *'

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

jobs:
Update-FontsData:
Expand All @@ -23,4 +21,6 @@ jobs:
- 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
129 changes: 77 additions & 52 deletions scripts/Update-FontsData.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,53 @@

try {
Write-Verbose "Executing: $fullCommand"
& $cmd @arguments
$output = & $cmd @arguments
if ($LASTEXITCODE -ne 0) {
$errorMessage = "Command failed with exit code $LASTEXITCODE`: $fullCommand"
Write-Error $errorMessage -ErrorId 'NativeCommandFailed' -Category OperationStopped -TargetObject $fullCommand
}
if ($output -is [array] -and $output.Count -gt 1) {
return $output -join "`n"
} else {
return $output
}
} catch {
throw
}
}

$currentBranch = (Run git rev-parse --abbrev-ref HEAD).Trim()
$defaultBranch = (Run git remote show origin | Select-String 'HEAD branch:' | ForEach-Object { $_.ToString().Split(':')[1].Trim() })
Write-Output "Current branch: $currentBranch"
Write-Output "Default branch: $defaultBranch"

Run git fetch origin
Run git checkout $defaultBranch
Run git pull origin $defaultBranch

$timeStamp = Get-Date -Format 'yyyyMMdd-HHmmss'
if ($currentBranch -eq $defaultBranch) {
# Running on main/default branch - create new branch
$targetBranch = "auto-update-font-$timeStamp"
Write-Output "Running on default branch. Creating new branch: $targetBranch"
Run 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"
Run git checkout $targetBranch
# Merge latest changes from default branch
Run git merge origin/$defaultBranch
Connect-GitHubApp -Organization 'PSModule' -Default
$repo = Get-GitHubRepository -Owner 'PSModule' -Name 'NerdFonts'

LogGroup 'Checkout' {
$currentBranch = (Run git rev-parse --abbrev-ref HEAD).Trim()
$defaultBranch = $repo.DefaultBranch

Write-Output "Current branch: $currentBranch"
Write-Output "Default branch: $defaultBranch"
Run git fetch origin
Run git checkout $defaultBranch
Run git pull origin $defaultBranch

$timeStamp = Get-Date -Format 'yyyyMMdd-HHmmss'
if ($currentBranch -eq $defaultBranch) {
# Running on main/default branch - create new branch
$targetBranch = "auto-update-$timeStamp"
Write-Output "Running on default branch. Creating new branch: $targetBranch"
Run 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"
Run git checkout $targetBranch
# Merge latest changes from default branch
Run git merge origin/$defaultBranch
}
}

LogGroup 'Latest Fonts' {
$release = Get-GitHubRelease -Owner ryanoasis -Repository nerd-fonts
LogGroup 'Getting latest fonts' {
$fonts = @()
$release = Get-GitHubRelease -Owner ryanoasis -Repository nerd-fonts
$fontAssets = $release | Get-GitHubReleaseAsset | Where-Object { $_.Name -like '*.zip' }

foreach ($fontArchive in $fontAssets) {
Expand All @@ -65,38 +75,53 @@ 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'
$null = New-Item -Path $filePath -ItemType File -Force
$fonts | ConvertTo-Json | Set-Content -Path $filePath -Force
}

$parentFolder = Split-Path -Path $PSScriptRoot -Parent
$filePath = Join-Path -Path $parentFolder -ChildPath 'src\FontsData.json'
$null = New-Item -Path $filePath -ItemType File -Force
$fonts | ConvertTo-Json | Set-Content -Path $filePath -Force

$changes = Run git status --porcelain
if ([string]::IsNullOrWhiteSpace($changes)) {
Write-Output 'No changes detected.'
Write-Output 'No updates available.'
Write-GitHubNotice 'No updates available.'
return
}
LogGroup 'Get changes' {
Run git add .
Run git commit -m 'Update FontsData.json'
Write-Output 'Changes in this commit:'
$changes = Run git diff HEAD~1 HEAD -- src/FontsData.json
Write-Output $changes
Set-GitHubStepSummary @"
## Changes available

Run git add .
Run git commit -m "Update-FontsData via script on $timeStamp"
Write-Output 'Changes in this commit:'
Run 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
Run git push origin $targetBranch
Write-Output "Changes committed and pushed to existing branch: $targetBranch"
} else {
# Push new branch and create PR
Run git push --set-upstream origin $targetBranch

Run 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"
<details><summary>Details</summary>
<p>

``````diff
$changes
``````

</p>
</details>
"@

}

LogGroup 'Process changes' {
if ($targetBranch -eq $currentBranch -and $currentBranch -ne $defaultBranch) {
Run git push origin $targetBranch
Write-Output "Changes committed and pushed to existing branch: $targetBranch"
} else {
Run git push --set-upstream origin $targetBranch

Run gh pr create `
--base $defaultBranch `
--head $targetBranch `
--title "Auto-Update $timeStamp" `
--body 'This PR updates FontsData.json with the latest metadata.'

Write-Output "Changes detected and PR opened for branch: $targetBranch"
}
}
Loading