Skip to content

Commit b35b1d3

Browse files
committed
🐛 fix: minor fix Dev-Drive module
1 parent e3137fc commit b35b1d3

File tree

3 files changed

+136
-134
lines changed

3 files changed

+136
-134
lines changed
Lines changed: 136 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,175 @@
1-
# Source: https://github.com/ran-dall/Dev-Drive/blob/main/SetupDevDrivePackageCache.ps1
1+
function Get-DevDrive {
2+
$devDrives = Get-Volume | Where-Object { $_.FileSystemType -eq 'ReFS' -and $_.DriveType -eq 'Fixed' }
3+
$devDriveLetters = @()
4+
5+
foreach ($drive in $devDrives) {
6+
$driveLetter = "$($drive.DriveLetter):"
7+
$devDriveLetters += $driveLetter
8+
}
9+
10+
if ($devDriveLetters.Count -eq 0) {
11+
Write-Output "No Dev Drive found on the system."
12+
return $null
13+
}
14+
elseif ($devDriveLetters.Count -eq 1) {
15+
return $devDriveLetters[0]
16+
}
17+
else {
18+
Write-Host "Multiple Dev Drives found:"
19+
for ($i = 0; $i -lt $devDriveLetters.Count; $i++) {
20+
Write-Host "[$i] $($devDriveLetters[$i])"
21+
}
22+
$selection = Read-Host "Please select the drive you want to configure by entering the corresponding number"
23+
if ($selection -match '^\d+$' -and [int]$selection -lt $devDriveLetters.Count) {
24+
return $devDriveLetters[$selection]
25+
}
26+
else {
27+
Write-Output "Invalid selection. Exiting script."
28+
return $null
29+
}
30+
}
31+
}
232

3-
function Set-DevDriveEnvironments {
33+
function Write-Success {
34+
param (
35+
[string]$Entry1,
36+
[string]$Entry2,
37+
[string]$Entry3,
38+
[string]$Text
39+
)
40+
Write-Host "$Entry1 " -ForegroundColor "Green" -NoNewline
41+
Write-Host ": " -ForegroundColor "DarkGray" -NoNewline
42+
Write-Host "$Entry2 " -ForegroundColor "Yellow" -NoNewline
43+
Write-Host "$Text"
44+
}
45+
46+
function Write-Error {
47+
param (
48+
[string]$Entry1,
49+
[string]$Entry2,
50+
[string]$Text
51+
)
52+
Write-Host "$Entry1" -ForegroundColor "Red" -NoNewline
53+
Write-Host ": " -ForegroundColor "DarkGray" -NoNewline
54+
Write-Host "$Entry2 " -ForegroundColor "DarkYellow" -NoNewline
55+
Write-Host "$Text"
56+
}
57+
58+
function New-DirectoryIfNotExist {
59+
param ([string]$Path)
60+
if (!(Test-Path -PathType Container -Path $Path)) {
61+
New-Item -Path $Path -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null
62+
if ($LASTEXITCODE -eq 0) {
63+
Write-Success -Entry1 "Directory" -Entry2 "$Path" -Text "created successfully."
64+
}
65+
else {
66+
Write-Error -Entry1 "Directory" -Entry2 "$Path" -Text "failed to create."
67+
}
68+
}
69+
else {
70+
Write-Error -Entry1 "Directory" -Entry2 "$Path" -Text "already exists. Skipping..."
71+
}
72+
}
73+
74+
function Set-EnvironmentVariableIfNotExist {
75+
param (
76+
[string]$Name,
77+
[string]$Value
78+
)
79+
if (!([System.Environment]::GetEnvironmentVariable("$Name"))) {
80+
[System.Environment]::SetEnvironmentVariable("$Name", "$Value", "User")
81+
if ($LASTEXITCODE -eq 0) {
82+
Write-Success -Entry1 "Environment Variable" -Entry2 "$Name ==> $Value" -Text "was set."
83+
}
84+
else {
85+
Write-Error -Entry1 "Environment Variable" -Entry2 "$Name ==> $Value" -Text "failed to set."
86+
}
87+
}
88+
else {
89+
Write-Error -Entry1 "Environment Variable" -Entry2 "$Name ==> $Value" -Text "already set. Skipping..."
90+
}
91+
}
492

5-
. "$PSScriptRoot\private\DevDrive\Get-DevDrive.ps1"
6-
. "$PSScriptRoot\private\DevDrive\Set-DevDriveEnvironmentVariables.ps1"
93+
function Move-CacheContents {
94+
param (
95+
[string]$ContentPath,
96+
[string]$Destination
97+
)
98+
if (Test-Path -PathType Container -Path $ContentPath) {
99+
Move-Item -Path "$ContentPath\*" -Destination "$Destination" -Force
100+
Remove-Item -Path $ContentPath -Recurse -Force -ErrorAction SilentlyContinue
101+
if ($LASTEXITCODE -eq 0) {
102+
Write-Success -Entry1 "Contents" -Entry2 "$ContentPath ==> $Destination" -Text "moved."
103+
}
104+
else {
105+
Write-Error -Entry1 "Contents" -Entry2 "$ContentPath ==> $Destination" -Text "failed to moved."
106+
}
107+
}
108+
else {
109+
Write-Error -Entry1 "Contents" -Entry2 "$ContentPath ==> $Destination" -Text "already moved / no content to move."
110+
}
111+
}
7112

113+
function Set-DevDriveEnvironments {
8114
$devDrive = Get-DevDrive
9115
$packagePath = "$devDrive\packages"
10116
$cacheSettings = @(
11-
# TODO: Add/Remove your DevDrive's package cache settings here:
12-
@{ Command = "npm"; Name = "npm_config_cache"; Value = "$packagePath\npm"; SourcePaths = @("$env:APPDATA\npm-cache", "$env:LOCALAPPDATA\npm-cache") },
13-
@{ Command = "yarn"; Name = "YARN_CACHE_FOLDER"; Value = "$packagePath\npm"; SourcePaths = @("$env:LOCALAPPDATA\Yarn\Cache") },
14-
@{ Command = "pnpm"; Name = "PNPM_HOME"; Value = "$packagePath\pnpm"; SourcePaths = @("$env:LOCALAPPDATA\pnpm\store") },
15-
@{ Command = "nuget"; Name = "NUGET_PACKAGES"; Value = "$packagePath\nuget\packages"; SourcePaths = @("$env:USERPROFILE\.nuget\packages") },
16-
@{ Command = "vcpkg"; Name = "VCPKG_DEFAULT_BINARY_CACHE"; Value = "$packagePath\vcpkg"; SourcePaths = @("$env:LOCALAPPDATA\vcpkg\archives", "$env:APPDATA\vcpkg\archives") },
17-
@{ Command = "pip"; Name = "PIP_CACHE_DIR"; Value = "$packagePath\pip"; SourcePaths = @("$env:LOCALAPPDATA\pip\Cache") },
18-
@{ Command = "pipx"; Name = "PIPX_HOME"; Value = "$packagePath\pipx"; SourcePaths = @("$env:USERPROFILE\pipx") },
19-
@{ Command = "cargo"; Name = "CARGO_HOME"; Value = "$packagePath\cargo"; SourcePaths = @("$env:USERPROFILE\.cargo") },
20-
@{ Command = "rustup"; Name = "RUSTUP_HOME"; Value = "$packagePath\rustup"; SourcePaths = @("$env:USERPROFILE\.rustup") },
21-
@{ Command = "gradle"; Name = "GRADLE_USER_HOME"; Value = "$packagePath\gradle"; SourcePaths = @("$env:USERPROFILE\.gradle") }
117+
@{ Command = "npm"; Value = "npm_config_cache"; ValuePath = "$packagePath\npm"; SourcePaths = @("$env:APPDATA\npm-cache". "$env:LOCALAPPDATA\npm-cache") },
118+
@{ Command = "yarn"; Value = "YARN_CACHE_FOLDER"; ValuePath = "$packagePath\npm"; SourcePaths = @("$env:LOCALAPPDATA\Yarn\Cache") },
119+
@{ Command = "pnpm"; Value = "PNPM_HOME"; ValuePath = "$packagePath\pnpm"; SourcePaths = @("$env:LOCALAPPDATA\pnpm\store") },
120+
@{ Command = "pip"; Value = "PIP_CACHE_DIR"; ValuePath = "$packagePath\pip"; SourcePaths = @("$env:USERPROFILE\pipx") },
121+
@{ Command = "cargo"; Value = "CARGO_HOME"; ValuePath = "$packagePath\cargo"; SourcePaths = @("$env:USERPROFILE\.cargo") },
122+
@{ Command = "rustup"; Value = "RUSTUP_HOME"; ValuePath = "$packagePath\rustup"; SourcePaths = @("$env:USERPROFILE\.rustup") },
123+
@{ Command = "gradle"; Value = "GRADLE_USER_HOME"; ValuePath = "$packagePath\gradle"; SourcePaths = @("$env:USERPROFILE\.gradle") },
124+
@{ Command = "nuget"; Value = "NUGET_PACKAGES"; ValuePath = "$packagePath\nuget\packages"; SourcePaths = @("$env:USERPROFILE\.nuget\packages") },
125+
@{ Command = "vcpkg"; Value = "VCPKG_DEFAULT_BINARY_CACHE"; ValuePath = "$packagePath\vcpkg"; SourcePaths = @("$env:LOCALAPPDATA\vcpkg\archives", "$env:APPDATA\vcpkg\archives") }
22126
)
23127

24128
New-DirectoryIfNotExist -Path $packagePath
25129

26130
foreach ($setting in $cacheSettings) {
27131
$command = $setting.Command
28-
$packagePath = $setting.Value
29-
$envName = $setting.Name
30-
$sourcePath = $setting.SourcePaths
132+
$name = $setting.Value
133+
$value = $setting.ValuePath
134+
$sources = $setting.Paths
135+
31136
if (Get-Command $command -ErrorAction SilentlyContinue) {
32137
# Create a directory if it doesn't exist
33-
New-DirectoryIfNotExist -Path $packagePath
34-
# Set an environment variable
35-
Set-EnvironmentVariableIfNotExist -Name $envName -Value $packagePath
36-
138+
New-DirectoryIfNotExist -Path $value
139+
# Set an environment variable
140+
Set-EnvironmentVariableIfNotExist -Name $name -Value $value
37141
# Move contents from the old directory to new directory
38-
foreach ($path in $sourcePath) {
39-
Move-CacheContents -ContentPath $path -Destination $packagePath
142+
foreach ($source in $sources) {
143+
Move-CacheContents -ContentPath $source -Destination $value
40144
}
41145
}
42146
}
43147

44-
# Extra pipx
148+
# Extras
149+
# pipx
45150
if (Get-Command pipx -ErrorAction SilentlyContinue) {
46151
$extraPipxSettings = @(
47-
@{Name = "PIPX_BIN_DIR"; Value = "$packagePath\pipx\bin" }
48-
@{Name = "PIPX_MAN_DIR"; Value = "$packagePath\pipx\man" }
49-
)
50-
foreach ($setting in $extraPipxSettings) {
51-
Set-EnvironmentVariableIfNotExist -Name $($setting.Name) -Value $($setting.Value)
52-
}
53-
}
54-
55-
# Extra NuGet
56-
if (Get-Command nuget -ErrorAction SilentlyContinue) {
57-
$nugetDir = "$packagePath\nuget"
58-
New-DirectoryIfNotExist -Path $nugetDir
59-
60-
$extraNuGetSettings = @(
61-
@{ Name = "NUGET_HTTP_CACHE_PATH"; Value = "$nugetDir\v3-cache"; SourcePath = "$env:LOCALAPPDATA\NuGet\v3-cache" },
62-
@{ Name = "NUGET_PLUGINS_CACHE_PATH"; Value = "$nugetDir\plugins-cache"; SourcePath = "$env:LOCALAPPDATA\NuGet\plugins-cache" }
152+
@{ Value = "PIPX_BIN_DIR"; ValuePath = "$packagePath\pipx\bin" },
153+
@{ Value = "PIPX_MAN_DIR"; ValuePath = "$packagePath\pipx\man" }
63154
)
64-
foreach ($setting in $extraNuGetSettings) {
65-
New-DirectoryIfNotExist -Path $($setting.Value)
66-
Set-EnvironmentVariableIfNotExist -Name $($setting.Name) -Value $($setting.Value)
67-
Move-CacheContents -ContentPath $($setting.SourcePath) -Destination $($setting.Value)
155+
foreach ($pipxSetting in $extraPipxSettings) {
156+
Set-EnvironmentVariableIfNotExist -Name $($pipxSetting.Value) -Value $($pipxSetting.ValuePath)
68157
}
69158
}
70159

71-
# Maven Settings
160+
# maven
72161
if (Get-Command mvn -ErrorAction SilentlyContinue) {
73162
$mavenRepoLocal = "$packagePath\maven"
74163
$mavenOpts = [System.Environment]::GetEnvironmentVariable('MAVEN_OPTS', [System.EnvironmentVariableTarget]::User)
75164
$escapedMavenRepoLocal = [regex]::Escape($mavenRepoLocal)
76165
$mavenPath = "$Env:USERPROFILE\.m2\repository"
77-
166+
78167
New-DirectoryIfNotExist -Path $mavenRepoLocal
79168

80169
if ($mavenOpts -notmatch "-Dmaven\.repo\.local=$escapedMavenRepoLocal") {
81170
$newMavenOpts = "-Dmaven.repo.local=$mavenRepoLocal $mavenOpts"
82171
Set-EnvironmentVariableIfNotExist -Name "MAVEN_OPTS" -Value $newMavenOpts
83172
}
84-
85173
Move-CacheContents -ContentPath $mavenPath -Destination $mavenRepoLocal
86174
}
87175
}

dotposh/Modules/private/DevDrive/Get-DevDrive.ps1

Lines changed: 0 additions & 31 deletions
This file was deleted.

dotposh/Modules/private/DevDrive/Set-DevDriveEnvironmentVariables.ps1

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)