Skip to content

Instantly share code, notes, and snippets.

@ran-dall
Created June 25, 2024 21:45
Show Gist options
  • Save ran-dall/1c3a565e0269d84bb467070200abc50f to your computer and use it in GitHub Desktop.
Save ran-dall/1c3a565e0269d84bb467070200abc50f to your computer and use it in GitHub Desktop.
This PowerShell script is designed to set up various development tool caches on a specified Dev Drive (e.g., D:\packages). It ensures that each tool's cache directory is properly created, sets global environment variables to point to these new directories, and moves any existing cache contents to the new locations. Additionally, the script check…
# Define the Dev Drive base path
$DevDrive = "D:\packages"
# Function to create a directory if it doesn't exist
function Ensure-Directory {
param (
[string]$Path
)
if (-not (Test-Path -Path $Path)) {
New-Item -Path $Path -ItemType Directory -Force
Write-Output "Created directory: $Path"
} else {
Write-Output "Directory already exists: $Path"
}
}
# Function to set a global environment variable with verbose output
function Set-GlobalEnvironmentVariable {
param (
[string]$Name,
[string]$Value
)
$currentValue = [System.Environment]::GetEnvironmentVariable($Name, [System.EnvironmentVariableTarget]::Machine)
if ($currentValue -eq $Value) {
Write-Output "Environment variable '$Name' is already set to '$Value'. No further setup is required."
} else {
try {
$output = setx /M $Name $Value
if ($output -match "SUCCESS: Specified value was saved.") {
Write-Output "SUCCESS: Environment variable '$Name' was set to '$Value'."
} else {
Write-Output "ERROR: Could not set environment variable '$Name'. Message: $output"
}
} catch {
Write-Output "ERROR: Access to the registry path is denied for environment variable '$Name'."
}
}
}
# Function to move contents from one directory to another
function Move-CacheContents {
param (
[string]$SourcePath,
[string]$DestinationPath
)
if (Test-Path -Path $SourcePath) {
Move-Item -Path "$SourcePath\*" -Destination $DestinationPath -Force
Remove-Item -Path $SourcePath -Recurse -Force
Write-Output "Moved contents from '$SourcePath' to '$DestinationPath' and removed the old directory."
} else {
Write-Output "No contents to move from '$SourcePath'."
}
}
# Create necessary directories
$directories = @(
"$DevDrive\npm",
"$DevDrive\$env:USERNAME\.nuget\packages",
"$DevDrive\vcpkg",
"$DevDrive\pip",
"$DevDrive\cargo",
"$DevDrive\maven",
"$DevDrive\gradle"
)
Write-Output "### Directory Setup ###"
foreach ($dir in $directories) {
Ensure-Directory -Path $dir
}
Write-Output ""
# Environment variable setup and cache moving
$cacheSettings = @(
@{ Name = "npm_config_cache"; Value = "$DevDrive\npm"; SourcePaths = @("$env:APPDATA\npm-cache", "$env:LOCALAPPDATA\npm-cache") },
@{ Name = "NUGET_PACKAGES"; Value = "$DevDrive\$env:USERNAME\.nuget\packages"; SourcePaths = @("$env:USERPROFILE\.nuget\packages") },
@{ Name = "VCPKG_DEFAULT_BINARY_CACHE"; Value = "$DevDrive\vcpkg"; SourcePaths = @("$env:LOCALAPPDATA\vcpkg\archives", "$env:APPDATA\vcpkg\archives") },
@{ Name = "PIP_CACHE_DIR"; Value = "$DevDrive\pip"; SourcePaths = @("$env:LOCALAPPDATA\pip\Cache") },
@{ Name = "CARGO_HOME"; Value = "$DevDrive\cargo"; SourcePaths = @("$env:USERPROFILE\.cargo") },
@{ Name = "MAVEN_OPTS"; Value = "-Dmaven.repo.local=$DevDrive\maven %MAVEN_OPTS%"; SourcePaths = @("$env:USERPROFILE\.m2") },
@{ Name = "GRADLE_USER_HOME"; Value = "$DevDrive\gradle"; SourcePaths = @("$env:USERPROFILE\.gradle") }
)
foreach ($setting in $cacheSettings) {
Write-Output "### Setting up $($setting.Name) ###"
Set-GlobalEnvironmentVariable -Name $setting.Name -Value $setting.Value
foreach ($source in $setting.SourcePaths) {
Move-CacheContents -SourcePath $source -DestinationPath $setting.Value
}
Write-Output ""
}
Write-Output "### Setup Complete ###"
Write-Output "Package cache on Dev Drive was set successfully."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment