Skip to content
Merged
Changes from all commits
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
124 changes: 49 additions & 75 deletions scripts/Update-FontsData.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,18 @@
[CmdletBinding()]
param (
# The command to execute
[Parameter(Mandatory, Position = 0)]
[string]$Command,

# The arguments to pass to the command
[Parameter(ValueFromRemainingArguments)]
[string[]]$Arguments
[string[]]$Command
)

Write-Debug "Command: $Command"
Write-Debug "Arguments: $($Arguments -join ', ')"
$fullCommand = "$Command $($Arguments -join ' ')"
$cmd = $Command[0]
$arguments = $Command[1..$Command.Length]
Write-Debug "Command: $cmd"
Write-Debug "Arguments: $($arguments -join ', ')"
$fullCommand = "$cmd $($arguments -join ' ')"

try {
Write-Verbose "Executing: $fullCommand"
& $Command @Arguments
& $cmd @arguments
if ($LASTEXITCODE -ne 0) {
$errorMessage = "Command failed with exit code $LASTEXITCODE`: $fullCommand"
Write-Error $errorMessage -ErrorId 'NativeCommandFailed' -Category OperationStopped -TargetObject $fullCommand
Expand All @@ -31,49 +28,42 @@
}
}

# 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() })

$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"

# 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
Run git fetch origin
Run git checkout $defaultBranch
Run git pull origin $defaultBranch

$timeStamp = Get-Date -Format 'yyyyMMdd-HHmmss'

# Determine target branch based on current context
if ($currentBranch -eq $defaultBranch) {
# Running on main/default branch - create new branch
$targetBranch = "auto-font-update-$timeStamp"
$targetBranch = "auto-update-font-$timeStamp"
Write-Output "Running on default branch. Creating new branch: $targetBranch"
Invoke-NativeCommand git checkout -b $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"
Invoke-NativeCommand git checkout $targetBranch
Run git checkout $targetBranch
# Merge latest changes from default branch
Invoke-NativeCommand git merge origin/$defaultBranch
Run git merge origin/$defaultBranch
}

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

foreach ($fontArchive in $fontAssets) {
$fonts += [PSCustomObject]@{
Name = $fontArchive.Name.Split('.')[0]
URL = $fontArchive.Url
LogGroup 'Latest Fonts' {
$release = Get-GitHubRelease -Owner ryanoasis -Repository nerd-fonts
$fonts = @()
$fontAssets = $release | Get-GitHubReleaseAsset | Where-Object { $_.Name -like '*.zip' }

foreach ($fontArchive in $fontAssets) {
$fonts += [PSCustomObject]@{
Name = $fontArchive.Name.Split('.')[0]
URL = $fontArchive.Url
}
}
}

LogGroup 'Latest Fonts' {
$fonts | Sort-Object Name | Format-Table -AutoSize | Out-String
}

Expand All @@ -82,47 +72,31 @@ $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 = Invoke-NativeCommand git status --porcelain
if (-not [string]::IsNullOrWhiteSpace($changes)) {
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
$changes = Run git status --porcelain
if ([string]::IsNullOrWhiteSpace($changes)) {
Write-Output 'No changes detected.'
return
}

# Show diff between HEAD and previous commit
Write-Output 'Changes in this commit:'
Invoke-NativeCommand git diff HEAD~1 HEAD -- src/FontsData.json
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
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
# 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

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.'
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"
}
} else {
Write-Output 'No changes to commit.'
Write-Output "Changes detected and PR opened for branch: $targetBranch"
}
Loading