Skip to content

Commit 37db98e

Browse files
🩹 [Patch]: Refactor Update-FontsData.ps1 (#44)
## Description This pull request refactors the `Update-FontsData.ps1` script to improve readability, simplify command execution, and enhance functionality. The most significant changes include replacing `Invoke-NativeCommand` with the `Run` alias, restructuring the command execution logic, and refining the behavior for detecting and committing changes. ### Command Execution Refactor - Replaced `Invoke-NativeCommand` with the `Run` alias. This change affects all Git-related operations, such as fetching, checking out branches, merging, and pushing changes. - Updated parameter handling for the `$Command` argument to treat the first element as the command and subsequent elements as arguments, simplifying the way commands are constructed and executed. ### Improved Change Detection and Commit Workflow - Enhanced the logic for detecting changes by reversing the condition to immediately exit if no changes are detected, reducing unnecessary processing. - Simplified the diff and commit process by removing redundant checks and ensuring staged changes are handled consistently. ### Minor Adjustments - Adjusted branch naming conventions for auto-generated branches to improve clarity (e.g., changed `auto-font-update-$timeStamp` to `auto-update-font-$timeStamp`). - Removed unnecessary logging blocks to streamline the script output. ## Type of change <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] 📖 [Docs] - [ ] 🪲 [Fix] - [x] 🩹 [Patch] - [ ] ⚠️ [Security fix] - [ ] 🚀 [Feature] - [ ] 🌟 [Breaking change] ## Checklist <!-- Use the check-boxes [x] on the options that are relevant. --> - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas --------- Co-authored-by: github-actions <[email protected]>
1 parent 322526e commit 37db98e

File tree

1 file changed

+49
-75
lines changed

1 file changed

+49
-75
lines changed

scripts/Update-FontsData.ps1

Lines changed: 49 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,18 @@
77
[CmdletBinding()]
88
param (
99
# The command to execute
10-
[Parameter(Mandatory, Position = 0)]
11-
[string]$Command,
12-
13-
# The arguments to pass to the command
1410
[Parameter(ValueFromRemainingArguments)]
15-
[string[]]$Arguments
11+
[string[]]$Command
1612
)
17-
18-
Write-Debug "Command: $Command"
19-
Write-Debug "Arguments: $($Arguments -join ', ')"
20-
$fullCommand = "$Command $($Arguments -join ' ')"
13+
$cmd = $Command[0]
14+
$arguments = $Command[1..$Command.Length]
15+
Write-Debug "Command: $cmd"
16+
Write-Debug "Arguments: $($arguments -join ', ')"
17+
$fullCommand = "$cmd $($arguments -join ' ')"
2118

2219
try {
2320
Write-Verbose "Executing: $fullCommand"
24-
& $Command @Arguments
21+
& $cmd @arguments
2522
if ($LASTEXITCODE -ne 0) {
2623
$errorMessage = "Command failed with exit code $LASTEXITCODE`: $fullCommand"
2724
Write-Error $errorMessage -ErrorId 'NativeCommandFailed' -Category OperationStopped -TargetObject $fullCommand
@@ -31,49 +28,42 @@
3128
}
3229
}
3330

34-
# Get the current branch and default branch information
35-
$currentBranch = (Invoke-NativeCommand git rev-parse --abbrev-ref HEAD).Trim()
36-
$defaultBranch = (Invoke-NativeCommand git remote show origin | Select-String 'HEAD branch:' | ForEach-Object { $_.ToString().Split(':')[1].Trim() })
37-
31+
$currentBranch = (Run git rev-parse --abbrev-ref HEAD).Trim()
32+
$defaultBranch = (Run git remote show origin | Select-String 'HEAD branch:' | ForEach-Object { $_.ToString().Split(':')[1].Trim() })
3833
Write-Output "Current branch: $currentBranch"
3934
Write-Output "Default branch: $defaultBranch"
4035

41-
# Fetch latest changes from remote
42-
Invoke-NativeCommand git fetch origin
43-
44-
# Get the head branch (latest default branch)
45-
Invoke-NativeCommand git checkout $defaultBranch
46-
Invoke-NativeCommand git pull origin $defaultBranch
36+
Run git fetch origin
37+
Run git checkout $defaultBranch
38+
Run git pull origin $defaultBranch
4739

4840
$timeStamp = Get-Date -Format 'yyyyMMdd-HHmmss'
49-
50-
# Determine target branch based on current context
5141
if ($currentBranch -eq $defaultBranch) {
5242
# Running on main/default branch - create new branch
53-
$targetBranch = "auto-font-update-$timeStamp"
43+
$targetBranch = "auto-update-font-$timeStamp"
5444
Write-Output "Running on default branch. Creating new branch: $targetBranch"
55-
Invoke-NativeCommand git checkout -b $targetBranch
45+
Run git checkout -b $targetBranch
5646
} else {
5747
# Running on another branch (e.g., workflow_dispatch) - use current branch
5848
$targetBranch = $currentBranch
5949
Write-Output "Running on feature branch. Using existing branch: $targetBranch"
60-
Invoke-NativeCommand git checkout $targetBranch
50+
Run git checkout $targetBranch
6151
# Merge latest changes from default branch
62-
Invoke-NativeCommand git merge origin/$defaultBranch
52+
Run git merge origin/$defaultBranch
6353
}
6454

65-
$release = Get-GitHubRelease -Owner ryanoasis -Repository nerd-fonts
66-
$fonts = @()
67-
$fontAssets = $release | Get-GitHubReleaseAsset | Where-Object { $_.Name -like '*.zip' }
68-
69-
foreach ($fontArchive in $fontAssets) {
70-
$fonts += [PSCustomObject]@{
71-
Name = $fontArchive.Name.Split('.')[0]
72-
URL = $fontArchive.Url
55+
LogGroup 'Latest Fonts' {
56+
$release = Get-GitHubRelease -Owner ryanoasis -Repository nerd-fonts
57+
$fonts = @()
58+
$fontAssets = $release | Get-GitHubReleaseAsset | Where-Object { $_.Name -like '*.zip' }
59+
60+
foreach ($fontArchive in $fontAssets) {
61+
$fonts += [PSCustomObject]@{
62+
Name = $fontArchive.Name.Split('.')[0]
63+
URL = $fontArchive.Url
64+
}
7365
}
74-
}
7566

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

@@ -82,47 +72,31 @@ $filePath = Join-Path -Path $parentFolder -ChildPath 'src\FontsData.json'
8272
$null = New-Item -Path $filePath -ItemType File -Force
8373
$fonts | ConvertTo-Json | Set-Content -Path $filePath -Force
8474

85-
$changes = Invoke-NativeCommand git status --porcelain
86-
if (-not [string]::IsNullOrWhiteSpace($changes)) {
87-
Write-Output 'Changes detected:'
88-
Write-Output $changes
89-
90-
# Show what will be committed
91-
Write-Output 'Diff of changes to be committed:'
92-
Invoke-NativeCommand git diff --cached HEAD -- src/FontsData.json 2>$null
93-
if ($LASTEXITCODE -ne 0) {
94-
# If --cached doesn't work (no staged changes), show unstaged diff
95-
Invoke-NativeCommand git diff HEAD -- src/FontsData.json
96-
}
97-
98-
Invoke-NativeCommand git add .
99-
Invoke-NativeCommand git commit -m "Update-FontsData via script on $timeStamp"
100-
101-
# Show the commit that was just created
102-
Write-Output 'Commit created:'
103-
Invoke-NativeCommand git log -1 --oneline
75+
$changes = Run git status --porcelain
76+
if ([string]::IsNullOrWhiteSpace($changes)) {
77+
Write-Output 'No changes detected.'
78+
return
79+
}
10480

105-
# Show diff between HEAD and previous commit
106-
Write-Output 'Changes in this commit:'
107-
Invoke-NativeCommand git diff HEAD~1 HEAD -- src/FontsData.json
81+
Run git add .
82+
Run git commit -m "Update-FontsData via script on $timeStamp"
83+
Write-Output 'Changes in this commit:'
84+
Run git diff HEAD~1 HEAD -- src/FontsData.json
10885

109-
# Push behavior depends on branch type
110-
if ($targetBranch -eq $currentBranch -and $currentBranch -ne $defaultBranch) {
111-
# Push to existing branch
112-
Invoke-NativeCommand git push origin $targetBranch
113-
Write-Output "Changes committed and pushed to existing branch: $targetBranch"
114-
} else {
115-
# Push new branch and create PR
116-
Invoke-NativeCommand git push --set-upstream origin $targetBranch
86+
# Push behavior depends on branch type
87+
if ($targetBranch -eq $currentBranch -and $currentBranch -ne $defaultBranch) {
88+
# Push to existing branch
89+
Run git push origin $targetBranch
90+
Write-Output "Changes committed and pushed to existing branch: $targetBranch"
91+
} else {
92+
# Push new branch and create PR
93+
Run git push --set-upstream origin $targetBranch
11794

118-
Invoke-NativeCommand gh pr create `
119-
--base $defaultBranch `
120-
--head $targetBranch `
121-
--title "Auto-Update: NerdFonts Data ($timeStamp)" `
122-
--body 'This PR updates FontsData.json with the latest NerdFonts metadata.'
95+
Run gh pr create `
96+
--base $defaultBranch `
97+
--head $targetBranch `
98+
--title "Auto-Update: NerdFonts Data ($timeStamp)" `
99+
--body 'This PR updates FontsData.json with the latest NerdFonts metadata.'
123100

124-
Write-Output "Changes detected and PR opened for branch: $targetBranch"
125-
}
126-
} else {
127-
Write-Output 'No changes to commit.'
101+
Write-Output "Changes detected and PR opened for branch: $targetBranch"
128102
}

0 commit comments

Comments
 (0)