-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsetup.ps1
233 lines (179 loc) · 4.96 KB
/
setup.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
[CmdletBinding()]
param (
[string]
$VirtualEnvFolder = 'venv',
[bool]
$Force = $true,
[bool]
$Execute = $true
)
function Test-Command-Exists {
<#
.SYNOPSIS
Check if a command exists: whether is installed or in your $PATH environment variable
.PARAMETER Command
The to verify if exists in your environment.
.EXAMPLE
Test-Command-Exists 'python'
.INPUTS
String
.OUTPUTS
Boolean
.NOTES
Author: Felipe Ferreira
Website: http://github.com/mfdeveloper
Twitter: @mfdeveloper
#>
param (
[string]
$Command
)
try {
if(Get-Command -Name $Command){
return $true
}
} catch {
return $false
}
}
function Install-Pyenv {
<#
.SYNOPSIS
Try install the pyenv CLI, if not exists in your system
.EXAMPLE
if(Install-Pyenv) {
# Continue your script automation process
}
.OUTPUTS
Boolean
.NOTES
Author: Felipe Ferreira
Website: http://github.com/mfdeveloper
Twitter: @mfdeveloper
#>
if (-not(Test-Command-Exists 'pyenv')) {
if (-not(Test-Command-Exists 'choco')) {
Write-Error 'The chocolatey package manager is not installed. Please, install it from: https://chocolatey.org/install'
return $false
}
Invoke-Expression 'choco install pyenv-win'
$result = Invoke-Expression 'pyenv rehash'
Write-Host $result
}
$pyenvVersion = Invoke-Expression 'pyenv --version'
Write-Host "$pyenvVersion installed!"
return $true
}
function Install-Python {
<#
.SYNOPSIS
Try install the python command, and verify if the version located in .python-versions is equal of in your system
.EXAMPLE
if(Install-Python) {
# Continue your script automation process
}
.OUTPUTS
Boolean
.NOTES
Author: Felipe Ferreira
Website: http://github.com/mfdeveloper
Twitter: @mfdeveloper
#>
if (-not(Test-Command-Exists 'python')) {
$lines = Get-Content -Path .\.python-version
foreach ($version in $lines) {
Invoke-Expression -Command "pyenv install $version"
}
}
$currentVersion = Invoke-Expression 'python --version'
$currentVersion = $currentVersion.Replace("Python ", "")
$projectVersion = Invoke-Expression 'pyenv local'
if ($currentVersion -ne $projectVersion) {
Write-Warning "The version '$currentVersion' in your system is not equals to version '$projectVersion' in '.python-version' file.
Is recommended to install '$projectVersion' version, and activate it with virtualenv"
return $false
}
Write-Host "Python $currentVersion installed!"
return $true
}
function Mount-Virtual-Env {
<#
.SYNOPSIS
Create and activate the python virtualenv folder
.PARAMETER FolderName
The name of the folder to create a virtualenv. By default, the name is "venv"
.EXAMPLE
# Optionally, pass a custom folder name. In this case is "myenv"
Mount-Virtual-Env -FolderName myenv
.INPUTS
String
.OUTPUTS
Boolean
.NOTES
Author: Felipe Ferreira
Website: http://github.com/mfdeveloper
Twitter: @mfdeveloper
#>
[CmdletBinding()]
param (
[string]
$FolderName = 'venv'
)
if ($FolderName.Length -le 0) {
Write-Error "The virtualenv folder '$FolderName' is invalid"
return $false
}
if ($Force) {
Write-Host "Removing folder '$FolderName' ..."
Remove-Item ".\$FolderName" -Force
}
if ( -not(Test-Path ".\$FolderName") ) {
Write-Host 'Creating Python VIRTUAL ENVIRONMENT:'
$resultCreation = Invoke-Expression "python -m venv $FolderName"
Write-Host $resultCreation
} else {
Write-Host "VIRTUAL ENVIRONMENT '$FolderName' was found!"
}
Invoke-Expression -Command ".\$FolderName\Scripts\Activate.ps1"
return $true
}
function Install-Packages-Dependencies {
<#
.SYNOPSIS
Try install the package dependencies from requirements-dev.txt or requirements.txt file
.EXAMPLE
Install-Packages-Dependencies
.OUTPUTS
String
.NOTES
Author: Felipe Ferreira
Website: http://github.com/mfdeveloper
Twitter: @mfdeveloper
#>
Write-Host 'Installing dependencies'
$files = @('requirements-dev.txt', 'requirements.txt')
$invokedInstall = $false
foreach ($fileName in $files) {
if (Test-Path $fileName -PathType leaf) {
Invoke-Expression -Command "pip install -r .\$fileName"
$invokedInstall = $true
break
}
}
if (!$invokedInstall) {
Write-Host 'requirements.txt file was not found'
}
}
function Run {
if(Install-Pyenv) {
if(Install-Python) {
$virtualEnvCreated = Mount-Virtual-Env -FolderName $VirtualEnvFolder
if($virtualEnvCreated) {
Install-Packages-Dependencies
}
}
}
}
if ($Execute) {
Run
}