Try improving and simplifying script

This commit is contained in:
Zach Latta
2025-03-13 16:11:25 -04:00
parent fbdd29440b
commit d857561797

View File

@@ -1,51 +1,45 @@
try {
# Create config file
New-Item -Path $env:USERPROFILE\.wakatime.cfg -Force
Set-Content -Path $env:USERPROFILE\.wakatime.cfg -Value @"
# Create config file with API settings
$configPath = "$env:USERPROFILE\.wakatime.cfg"
@"
[settings]
api_url = $env:HACKATIME_API_URL
api_key = $env:HACKATIME_API_KEY
"@
"@ | Out-File -FilePath $configPath -Force -Encoding utf8
Write-Host "Config file created at $configPath"
Write-Host "Config file created at $env:USERPROFILE\.wakatime.cfg"
# Read values from config to verify
if (-not (Test-Path $env:USERPROFILE\.wakatime.cfg)) {
Write-Error "Config file not found"
throw "Config file not found"
# Verify config was created successfully
if (Test-Path $configPath) {
$config = Get-Content $configPath
$apiUrl = ($config | Select-String "api_url").ToString().Split('=')[1].Trim()
$apiKey = ($config | Select-String "api_key").ToString().Split('=')[1].Trim()
# Display verification info
Write-Host "API URL: $apiUrl"
Write-Host ("API Key: " + $apiKey.Substring(0,4) + "..." + $apiKey.Substring($apiKey.Length-4)) # Show first/last 4 chars
# Send test heartbeat
Write-Host "Sending test heartbeat..."
$time = [Math]::Floor([decimal](Get-Date(Get-Date).ToUniversalTime()-uformat '%s'))
$heartbeat = @{
type = 'file'
time = $time
entity = 'test.txt'
language = 'Text'
}
$response = Invoke-RestMethod -Uri "$apiUrl/users/current/heartbeats" `
-Method Post `
-Headers @{Authorization="Bearer $apiKey"} `
-ContentType 'application/json' `
-Body "[$($heartbeat | ConvertTo-Json)]"
Write-Host "Test heartbeat sent successfully"
} else {
throw "Failed to create config file"
}
$config = Get-Content $env:USERPROFILE\.wakatime.cfg
$apiUrl = $config | Select-String "api_url" | ForEach-Object { $_.ToString().Split('=')[1].Trim() }
$apiKey = $config | Select-String "api_key" | ForEach-Object { $_.ToString().Split('=')[1].Trim() }
if (-not $apiUrl -or -not $apiKey) {
Write-Error "Could not read api_url or api_key from config"
throw "Could not read api_url or api_key from config"
}
Write-Host "Successfully read config:"
Write-Host "API URL: $apiUrl"
Write-Host ("API Key: " + $apiKey.Substring(0,8) + "...") # Show only first 8 chars for security
# Send test heartbeat using values from config
Write-Host "Sending test heartbeat..."
$time = [Math]::Floor([decimal](Get-Date(Get-Date).ToUniversalTime()-uformat '%s'))
$heartbeat = @{
type = 'file'
time = $time
entity = 'test.txt'
language = 'Text'
}
$body = "[$($heartbeat | ConvertTo-Json)]"
$response = Invoke-WebRequest -Uri "$apiUrl/users/current/heartbeats" `
-Method Post `
-Headers @{Authorization="Bearer $apiKey"} `
-ContentType 'application/json' `
-Body $body
Write-Host "Test heartbeat sent successfully"
} catch {
Write-Host "----------------------------------------"
Write-Host "ERROR: An error occurred during setup:" -ForegroundColor Red
@@ -53,10 +47,7 @@ api_key = $env:HACKATIME_API_KEY
Write-Host "----------------------------------------"
}
finally {
# This will always execute, even if errors occur
Write-Host "`nSetup process completed. Review any errors above."
Write-Host "Press any key to exit..."
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
# Additional fallback to keep window open in all cases
Start-Sleep -Seconds 60
}