-
Notifications
You must be signed in to change notification settings - Fork 0
/
MovePackageCaches.ps1
99 lines (88 loc) · 4.55 KB
/
MovePackageCaches.ps1
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
#
# Script to move package caches to the desired dev drive
# Assumes package caches are in their default locations, otherwise it won't recognize or move them to dev drive
#
# Dev Drive must already exist, and passed in as a parameter
# TestMode allows you to see the output of potential changes, default=True
#
param ([Parameter(Mandatory)] [string]$DevDrive,
[bool]$TestMode=$True)
class packageCache {
[string] $srcLocation
[string] $destLocation
[string] $envVar
[string] $destPath
[string] $scope
# class to handle the copy.
# src & destination location may be a parent folder, so destPath enables ability to specify the drilldown specific cache location
packageCache(
[string] $srcLocation,
[string] $destLocation,
[string] $envVar,
[string] $destPath,
[string] $scope
){
$this.srcLocation = $srcLocation
$this.destLocation = $destLocation
$this.envVar = $envVar
$this.destPath = $destPath
$this.scope = $scope
}
}
# function to copy the package cache to Dev Drive and update environment variable overrides
function updatePackageCaches {
param([System.Collections.Generic.List[packageCache]] $cacheList)
foreach($pkgCache in $cacheList) {
if (Test-Path $pkgCache.srcLocation) {
Write-Output "Package cache $($pkgCache.srcLocation) does exist"
Write-Output " Copy $($pkgCache.srcLocation) to $($pkgCache.destLocation)"
if (!($TestMode)) {
Copy-Item -Path $pkgCache.srcLocation -Destination $pkgCache.destLocation -Recurse
}
Write-Output " Setting $($pkgCache.envVar) to $($pkgCache.destPath) with scope $($pkgCache.scope)"
if (!($TestMode)) {
[Environment]::SetEnvironmentVariable($pkgCache.envVar, $pkgCache.destPath, $pkgCache.scope)
}
}
else {
Write-Output "Package cache $($pkgCache.srcLocation) does not exist"
}
}
}
#Display variables to user
$DevDrive = $DevDrive.ToUpper()
Write-Output "Setting Dev Drive to $DevDrive"
Write-Output "Setting TestMode to $TestMode"
# Validate Dev Drive exists
if (!(Test-Path $DevDrive)) {
Write-Output "Dev Drive '$DevDrive' does not exist, please create one first."
Exit
}
# Create a list of package caches for python, npm, dotnet, vcpkg, rust, maven, gradle
# add new package caches here in the future, eg: go, flutter, etc
# - spacing for readability
$packageCacheList = [System.Collections.Generic.List[packageCache]]::new()
$packageCacheList.Add([packageCache]::new("$env:LocalAppData\pip\Cache", "$DevDrive\Packages\pip", "PIP_CACHE_DIR", "$DevDrive\Packages\pip\Cache", "User"))
$packageCacheList.Add([packageCache]::new("$env:LocalAppData\npm-cache", "$DevDrive\Packages", "npm_config_cache", "$DevDrive\Packages\npm_cache", "User"))
$packageCacheList.Add([packageCache]::new("$env:UserProfile\.nuget", "$DevDrive\Packages", "NUGET_PACKAGES", "$DevDrive\Packages\.nuget\packages", "User"))
$packageCacheList.Add([packageCache]::new("$env:LocalAppData\vcpkg\archives", "$DevDrive\Packages\vcpkg", "VCPKG_DEFAULT_BINARY_CACHE", "$DevDrive\Packages\vcpkg\archives", "User"))
$packageCacheList.Add([packageCache]::new("$env:UserProfile\.cargo", "$DevDrive\Packages", "CARGO_HOME", "$DevDrive\Packages\cargo", "User"))
$packageCacheList.Add([packageCache]::new("$env:UserProfile\.m2", "$DevDrive\Packages", "MAVEN_OPTS", "-Dmaven.repo.local=$DevDrive\Packages\.m2", "User"))
$packageCacheList.Add([packageCache]::new("$env:UserProfile\.gradle", "$DevDrive\Packages", "GRADLE_USER_HOME", "$DevDrive\Packages\.gradle", "User"))
# Create packages directory on Dev Drive
# TODO - Consider:what if creating the DevDrive\Packages folder fails?
if (Test-Path -Path "$DevDrive\Packages") {
Write-Output "$DevDrive\Packages already exists"
} else {
Write-Output "$DevDrive\Packages does not exist"
if (!($TestMode)) {
Write-Output "Creating $DevDrive\Packages folder"
New-Item -Path $DevDrive\ -Name "Packages" -ItemType "directory"
}
}
# Update package caches
updatePackageCaches($packageCacheList)
# Tell user to validate and delete source location
Write-Output ""
Write-Output "Validate package caches and environment variables are correct, and then you can delete the source"
Write-Output ""