-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.ps1
148 lines (126 loc) · 4.49 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
# setup script
# This script is used to setup the workspace for WarpDL.
#
# Requires: git, powershell 5.0 or higher
#Requires -Version 5.0
[CmdletBinding()]
param (
[Parameter(Mandatory = $false)]
[string]$OrgUrl = 'https://github.com/warpdl',
[Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[string]$WarpLibPath = 'warplib',
[Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[string]$WarpCorePath = 'warpdl'
)
# This function gets called whenever the specified directory by user already
# exists, so we ask them what to do exactly.
function Show-ExistingSetupOptions {
Write-Host "Select an option from the following:" -ForegroundColor "Green"
Write-Host "1 - Setup a new one with some other name"
Write-Host "2 - Delete existing and setup a new one"
Write-Host "3 - Git pull in the existing directory"
Write-Host "4 - Exit program"
Write-Host "Default: 3" -ForegroundColor "Gray"
return (Read-Host "Enter your option")
}
# This function will clone a certain repository from the specified URL to the
# specified path.
function Invoke-GitClone {
param (
[Parameter(Mandatory = $true)]
[string]$Url,
[Parameter(Mandatory = $false)]
[string]$Path
)
Write-Verbose "Cloning from '$Url' to '$Path'..."
$gitOutput = (& git clone $Url $Path --progress 2>&1)
if ($null -eq $gitOutput) {
throw 'Invalid empty output received from git clone!'
}
$doneOutput = $gitOutput | Where-Object { $_.ToString().Contains("done.") }
if ($null -eq $doneOutput -or $doneOutput.Count -eq 0) {
$gitOutput | Write-Host
return
}
Write-Host "Successfully cloned from '$Url' to '$Path'!"
}
# This function will simply run git pull command (in the current pwd).
# It does not accept any input parameters, nor does it care about the output.
function Invoke-GitPull {
Write-Verbose "Running git pull"
& git pull 2>&1
}
# This function just simply calls Invoke-GitClone inside of its body.
# The only difference is that RepoPath parameter here is mandatory, so if
# user doesn't specify it, it will prompt them to enter its value.
function Invoke-AskAndClone {
param (
[Parameter(Mandatory = $true)]
[string]$RepoPath,
[Parameter(Mandatory = $true)]
[string]$RepoUrl
)
return Invoke-GitClone -Url $RepoUrl -Path $RepoPath
}
# This function should be called when the target directory that has been chosen
# by user already exists. It will ask them what to do exactly.
function Get-DirExistSelectOption {
param (
[Parameter(Mandatory = $true)]
[string]$DirPath
)
$dirName = Split-Path -Path $DirPath -Leaf
$local:repo = "$OrgUrl/$dirName"
Write-Host "Directory $DirPath already exists!"
switch ((Show-ExistingSetupOptions)) {
1 {
Invoke-AskAndClone -RepoPath (Read-Host "Enter alternative name") -RepoUrl $local:repo
}
2 {
Write-Verbose "Deleting previous $DirPath directory..."
Remove-Item -Path $DirPath -Force -Recurse
Invoke-GitClone -Url $local:repo -Path $DirPath
}
{[string]::IsNullOrEmpty($_) -or $_ -eq 3} {
$local:originalPWD = $PWD
Set-Location -Path $DirPath
Invoke-GitPull
Set-Location -Path $local:originalPWD
}
4 {
Write-Host "Exiting!"
exit 0
}
Default {}
}
}
# This function invokes the setup steps necessary for a certain repository.
function Invoke-SetupRepo {
param (
[Parameter(Mandatory = $true)]
[string]$DirPath,
[Parameter(Mandatory = $true)]
[string]$SetupName
)
Write-Verbose "Setting up $SetupName"
if ([System.IO.Directory]::Exists($DirPath)) {
Get-DirExistSelectOption -DirPath $DirPath
}
else {
Invoke-GitClone -Url "$OrgUrl/$(Split-Path -Path $DirPath -Leaf)" -Path $DirPath
}
}
Write-Host "WarpDL Workspace Utility" -ForegroundColor Green
Start-Sleep -Milliseconds 600
Invoke-SetupRepo -SetupName "WarpLib" -DirPath $WarpLibPath
Start-Sleep -Milliseconds 600
Invoke-SetupRepo -SetupName "Warp Core" -DirPath $WarpCorePath
Start-Sleep -Milliseconds 600
Write-Verbose "Downloaded all the required repositories!"
Write-Verbose "Creating a go workspace..."
& go work init 2>&1
Write-Verbose "Adding all the required repositories to workspace..."
& go work use $WarpCorePath $WarpLibPath 2>&1
Write-Verbose "Successfully setup workspace!"