This repository has been archived by the owner on Feb 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 344
/
chocolateysetup.psm1
212 lines (176 loc) · 8.75 KB
/
chocolateysetup.psm1
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
$thisScriptFolder = (Split-Path -parent $MyInvocation.MyCommand.Definition)
$chocInstallVariableName = "ChocolateyInstall"
$sysDrive = $env:SystemDrive
$tempDir = $env:TEMP
function Initialize-Chocolatey {
<#
.DESCRIPTION
This will initialize the Chocolatey tool by
a) setting up the "nugetPath" (the location where all chocolatey nuget packages will be installed)
b) Installs chocolatey into the "nugetPath"
c) Instals .net 4.0 if needed
d) Adds chocolaty to the PATH environment variable so you have access to the chocolatey|cinst commands.
.PARAMETER NuGetPath
Allows you to override the default path of (C:\Chocolatey\) by specifying a directory chocolaty will install nuget packages.
.EXAMPLE
C:\PS> Initialize-Chocolatey
Installs chocolatey into the default C:\Chocolatey\ directory.
.EXAMPLE
C:\PS> Initialize-Chocolatey -nugetPath "D:\ChocolateyInstalledNuGets\"
Installs chocolatey into the custom directory D:\ChocolateyInstalledNuGets\
#>
param(
[Parameter(Mandatory=$false)][string]$chocolateyPath = ''
)
$installModule = Join-Path $thisScriptFolder 'chocolateyInstall\helpers\chocolateyInstaller.psm1'
Import-Module $installModule -Force
if ($chocolateyPath -eq '') {
$programData = [Environment]::GetFolderPath("CommonApplicationData")
$chocolateyPath = Join-Path "$programData" 'chocolatey'
}
#if we have an already environment variable path, use it.
$alreadyInitializedNugetPath = Get-ChocolateyInstallFolder
if($alreadyInitializedNugetPath -and $alreadyInitializedNugetPath -ne $chocolateyPath){
$chocolateyPath = $alreadyInitializedNugetPath
}
else {
Set-ChocolateyInstallFolder $chocolateyPath
}
Create-DirectoryIfNotExists $chocolateyPath
#set up variables to add
$chocolateyExePath = Join-Path $chocolateyPath 'bin'
$chocolateyLibPath = Join-Path $chocolateyPath 'lib'
$chocolateyInstallPath = Join-Path $chocolateyPath 'chocolateyinstall'
if ($tempDir -eq $null) {
$tempDir = Join-Path $chocolateyPath 'temp'
Create-DirectoryIfNotExists $tempDir
}
$yourPkgPath = [System.IO.Path]::Combine($chocolateyLibPath,"yourPackageName")
@"
We are setting up the Chocolatey repository for NuGet packages that should be at the machine level. Think executables/application packages, not library packages.
That is what Chocolatey NuGet goodness is for. The repository is set up at `'$chocolateyPath`'.
The packages themselves go to `'$chocolateyLibPath`' (i.e. $yourPkgPath).
A shim file for the command line goes to `'$chocolateyExePath`' and points to an executable in `'$yourPkgPath`'.
Creating Chocolatey NuGet folders if they do not already exist.
"@ | Write-Host
#create the base structure if it doesn't exist
Create-DirectoryIfNotExists $chocolateyExePath
Create-DirectoryIfNotExists $chocolateyLibPath
Create-DirectoryIfNotExists $chocolateyInstallPath
Install-ChocolateyFiles $chocolateyPath
$chocolateyExePathVariable = $chocolateyExePath.ToLower().Replace($chocolateyPath.ToLower(), "%DIR%..\").Replace("\\","\")
Install-ChocolateyBinFiles $chocolateyInstallPath $chocolateyExePath
Initialize-ChocolateyPath $chocolateyExePath $chocolateyExePathVariable
Process-ChocolateyBinFiles $chocolateyExePath $chocolateyExePathVariable
Install-DotNet4IfMissing $chocolateyInstallPath
$realModule = Join-Path $chocolateyInstallPath "helpers\chocolateyInstaller.psm1"
Import-Module "$realModule" -Force
@"
Chocolatey is now ready.
You can call chocolatey from anywhere, command line or powershell by typing choco.
Run choco /? for a list of functions.
You may need to shut down and restart powershell and/or consoles first prior to using chocolatey.
"@ | write-host
}
function Set-ChocolateyInstallFolder {
param(
[string]$folder
)
$environmentTarget = [System.EnvironmentVariableTarget]::User
if (Test-ProcessAdminRights) {
Write-Debug "Administrator installing so using Machine environment variable target instead of User."
$environmentTarget = [System.EnvironmentVariableTarget]::Machine
}
Write-Host "Creating $chocInstallVariableName as an Environment variable (targeting `'$environmentTarget`') and setting it to `'$folder`'"
Install-ChocolateyEnvironmentVariable -variableName "$chocInstallVariableName" -variableValue "$folder" -variableType $environmentTarget
}
function Get-ChocolateyInstallFolder(){
[Environment]::GetEnvironmentVariable($chocInstallVariableName)
}
function Create-DirectoryIfNotExists($folderName){
if (![System.IO.Directory]::Exists($folderName)) {[System.IO.Directory]::CreateDirectory($folderName) | Out-Null}
}
function Install-ChocolateyBinFiles {
param(
[string] $chocolateyInstallPath,
[string] $chocolateyExePath
)
$redirectsPath = Join-Path $chocolateyInstallPath 'redirects'
$exeFiles = Get-ChildItem "$redirectsPath" -include @("*.exe","*.cmd") -recurse
foreach ($exeFile in $exeFiles) {
$exeFilePath = $exeFile.FullName
$exeFileName = [System.IO.Path]::GetFileName("$exeFilePath")
$binFilePath = Join-Path $chocolateyExePath $exeFileName
$binFilePathRename = $binFilePath + '.old'
$batchFilePath = $binFilePath.Replace(".exe",".bat")
$bashFilePath = $binFilePath.Replace(".exe","")
if (Test-Path ($batchFilePath)) {Remove-Item $batchFilePath -force}
if (Test-Path ($bashFilePath)) {Remove-Item $bashFilePath -force}
if (Test-Path ($binFilePathRename)) {Remove-Item $binFilePathRename -force}
if (Test-Path ($binFilePath)) {Move-Item -path $binFilePath -destination $binFilePathRename -force}
Copy-Item -path $exeFilePath -destination $binFilePath -force
$commandShortcut = [System.IO.Path]::GetFileNameWithoutExtension("$exeFilePath")
Write-Debug "Added command $commandShortcut"
}
}
function Install-ChocolateyFiles {
param(
[string]$chocolateyPath
)
$chocInstallFolder = Join-Path $thisScriptFolder "chocolateyInstall"
Write-Host "Copying the contents of `'$chocInstallFolder`' to `'$chocolateyPath`'."
if(test-path "$chocolateyPath\chocolateyInstall\functions") {
Remove-Item "$chocolateyPath\chocolateyInstall\functions" -recurse -force
}
if(test-path "$chocolateyPath\chocolateyInstall\helpers") {
Remove-Item "$chocolateyPath\chocolateyInstall\helpers" -recurse -force
}
Copy-Item $chocInstallFolder $chocolateyPath -recurse -force
}
function Initialize-ChocolateyPath {
param(
[string]$chocolateyExePath = "$sysDrive\Chocolatey\bin",
[string]$chocolateyExePathVariable = "%$($chocInstallVariableName)%\bin"
)
$environmentTarget = [System.EnvironmentVariableTarget]::User
if (Test-ProcessAdminRights) {
Write-Debug "Administrator installing so using Machine environment variable target instead of User."
$environmentTarget = [System.EnvironmentVariableTarget]::Machine
}
Install-ChocolateyPath -pathToInstall "$chocolateyExePath" -pathType $environmentTarget
}
function Process-ChocolateyBinFiles {
param(
[string]$chocolateyExePath = "$($env:ALLUSERSPROFILE)\Chocolatey\bin",
[string]$chocolateyExePathVariable = "%$($chocInstallVariableName)%\bin"
)
$processedMarkerFile = Join-Path $chocolateyExePath '_processed.txt'
if (!(test-path $processedMarkerFile)) {
$files = get-childitem $chocolateyExePath -include *.bat -recurse
if ($files -ne $null -and $files.Count -gt 0) {
foreach ($file in $files) {
Write-Host "Processing $($file.Name) to make it portable"
$fileStream = [System.IO.File]::Open("$file", 'Open', 'Read', 'ReadWrite')
$reader = New-Object System.IO.StreamReader($fileStream)
$fileText = $reader.ReadToEnd()
$reader.Close()
$fileStream.Close()
$fileText = $fileText.ToLower().Replace("`"" + $chocolateyPath.ToLower(), "SET DIR=%~dp0%`n""%DIR%..\").Replace("\\","\")
Set-Content $file -Value $fileText -Encoding Ascii
}
}
Set-Content $processedMarkerFile -Value "$([System.DateTime]::Now.Date)" -Encoding Ascii
}
}
function Install-DotNet4IfMissing {
param(
[string]$chocolateyInstallPath
)
if([IntPtr]::Size -eq 8) {$fx="framework64"} else {$fx="framework"}
if(!(test-path "$env:windir\Microsoft.Net\$fx\v4.0.30319")) {
$NetFx4ClientUrl = 'http://download.microsoft.com/download/5/6/2/562A10F9-C9F4-4313-A044-9C94E0A8FAC8/dotNetFx40_Client_x86_x64.exe'
$NetFx4FullUrl = 'http://download.microsoft.com/download/9/5/A/95A9616B-7A37-4AF6-BC36-D6EA96C8DAAE/dotNetFx40_Full_x86_x64.exe'
Install-ChocolateyPackage "NetFx4.0" 'exe' -silentArgs "/q /norestart /repair /log `'$tempDir\NetFx4Install.log`'" -url "$NetFx4ClientUrl" -url64bit "$NetFx4ClientUrl" -validExitCodes @(0, 3010)
}
}
Export-ModuleMember -function Initialize-Chocolatey;