-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathactivate.ps1
More file actions
137 lines (118 loc) · 4.45 KB
/
activate.ps1
File metadata and controls
137 lines (118 loc) · 4.45 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
<#
.SYNOPSIS
Script to check and install required Python packages, Node.js tools, add directories to PATH, and activate a virtual environment.
.DESCRIPTION
This script performs the following tasks:
1. Checks if Python is installed.
2. Checks if pip is installed.
3. Checks if Node.js and npm are installed.
4. Checks and installs specified Python packages if they are not already installed.
5. Installs changie globally via npm if not already installed.
6. Adds a specified directory to the system PATH if it is not already included.
7. Ensures the 'uv' command is available in the PATH.
8. Activates a virtual environment using 'uv'.
.NOTES
Make sure that Python, pip, Node.js, and npm are installed and available in the system PATH.
#>
# Function to check if a dependency is available
function Test-Dependancy {
param (
[string]$commandName
)
if (-not (Get-Command $commandName -ErrorAction SilentlyContinue)) {
Write-Host " $commandName is not installed or not in PATH. Please install $commandName and make sure it's available in the PATH."
exit 1
}
else {
$commandPath = (Get-Command $commandName).Path
$commandDirectory = [System.IO.Path]::GetDirectoryName($commandPath)
Write-Host "$commandName is installed in $commandPath"
Add-DirectoryToPath -directory $commandDirectory
}
}
# Function to install required packages if not already installed
function Test-And-Install-Python-Package {
param (
[string]$packageName
)
pip show $packageName -q
if ($LASTEXITCODE -ne 0) {
Write-Host "$packageName is not installed. Installing $packageName..."
try {
pip install $packageName
Write-Host "$packageName installed successfully."
}
catch {
Write-Host "Failed to install $packageName. Please check your pip installation."
exit 1
}
}
else {
Write-Host "$packageName is already installed."
}
}
# Function to install changie globally via npm if not already installed
function Test-And-Install-Changie {
if (-not (Get-Command changie -ErrorAction SilentlyContinue)) {
Write-Host "changie not found, installing globally via npm..."
try {
npm install -g changie --registry https://registry.npmjs.org/
# Add npm global bin to PATH if needed
$npmGlobalPath = npm config get prefix
if ($npmGlobalPath) {
Add-DirectoryToPath -directory $npmGlobalPath
}
# Refresh PATH for the current session
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
Test-Dependancy -commandName "changie"
}
catch {
Write-Host "Failed to install changie via npm. Please check your npm installation and connection."
exit 1
}
}
else {
Write-Host "changie is already installed."
}
}
# Function to add a directory to PATH
function Add-DirectoryToPath {
param (
[string]$directory
)
if (-not ($env:Path -split ';' | ForEach-Object { $_.Trim() } | Where-Object { $_ -eq $directory })) {
$env:Path += ";$directory"
Write-Host "Added $directory to PATH."
}
}
# Check if dependencies are installed and add directory to PATH
Test-Dependancy -commandName "python"
Test-Dependancy -commandName "pip"
Test-Dependancy -commandName "node"
Test-Dependancy -commandName "npm"
# Check and install required packages
Test-And-Install-Python-Package -packageName "uv"
Test-And-Install-Python-Package -packageName "ruff"
Test-And-Install-Changie
# uv fallback to default path if unavailable in python directory
if (-not (Get-Command uv -ErrorAction SilentlyContinue)) {
Write-Host "uv is not recognized. Attempting to add uv to PATH..."
$localBinPath = [System.IO.Path]::Combine($env:USERPROFILE, '.local', 'bin')
Add-DirectoryToPath -directory $localBinPath
Test-Dependancy -commandName "uv"
}
# Activate the environment
uv sync --python 3.11
$venvPath = ".venv\Scripts\activate.ps1"
if (Test-Path $venvPath) {
& $venvPath
Write-Host "venv activated"
}
else {
Write-Host "venv not found"
}
Write-Host ""
Write-Host "To deactivate the environment, run " -NoNewline
Write-Host "deactivate" -ForegroundColor Green