-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUpdate-FontsData.ps1
More file actions
128 lines (107 loc) · 4.62 KB
/
Copy pathUpdate-FontsData.ps1
File metadata and controls
128 lines (107 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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,
# 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
$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"
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
}
$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
}
$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 = 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
# 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.'
}