-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathentrypoint.ps1
More file actions
120 lines (94 loc) · 4.03 KB
/
entrypoint.ps1
File metadata and controls
120 lines (94 loc) · 4.03 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
<#
.SYNOPSIS
Deploy an Application to Discord
.DESCRIPTION
Deploy an application to Discord using Discord Dispatch
#>
[CmdletBinding()]
param (
[Parameter()]
[string]$BranchName = $env:BranchName,
[Parameter()]
[string]$ConfigPath = $env:ConfigPath,
[Parameter()]
[string]$BuildPath = $env:BuildPath,
[Parameter()]
[string]$ApplicationId = $env:ApplicationId,
[Parameter()]
[string]$BotToken = $env:BotToken,
[Parameter()]
[bool]$DrmWrap = [bool]::parse($env:DrmWrap) ?? $false,
[Parameter()]
[string]$ExecutableName = $env:ExecutableName
)
#Requires -Version 7.0
# Set preference to Stop if errors are experienced
$ErrorActionPreference = 'Stop'
# Static variables
$CredentialsFilePath = "/Dispatch/credentials.json"
$DispatchProfilePath = "~/.dispatch"
$CredentialsFileTarget = "$DispatchProfilePath/credentials.json"
# Functions
function GetBranchId([string]$ApplicationId, [string]$BranchName)
{
# Dispatch command to get list of branches for provided ApplicationId
$Branches = $(& /Dispatch/dispatch branch list $ApplicationId)
if ($LASTEXITCODE -ne 0) { Write-Error $Branches }
# Obtain the BranchId for matching branches
$BranchId = Select-String -InputObject $Branches -Pattern "\d+(?=\s*\|\s*$BranchName)" -AllMatches
# Select the BranchId. If there are more than one matches, return exception
!($BranchId.Matches.Count -gt 1) ? $($BranchId = ($BranchId.Matches | Select-Object -First 1).Value) : $(return Write-Error "There were $($BranchId.Matches.Count) branches with the name '$BranchName' detected!")
# Return the BranchId variable
return $BranchId
}
trap
{
Write-Host "Error: $_"
exit 1
}
try
{
# Create Dispatch Profile directory
New-Item -ItemType Directory -Path $DispatchProfilePath | Out-Null && Write-Host "Created directory $DispatchProfilePath"
# Transform Credentials file and save to Dispatch Profile directory
$CredentialsFile = Get-Content $CredentialsFilePath -Raw | ConvertFrom-Json
$CredentialsFile.BotCredentials.application_id = $ApplicationId
$CredentialsFile.BotCredentials.token = $BotToken
$CredentialsFile | ConvertTo-Json | Set-Content $CredentialsFileTarget
Write-Host "Imported credentials to $CredentialsFileTarget"
# Get the BranchId using the GetBranchId function
$BranchId = GetBranchId -ApplicationId $ApplicationId -BranchName $BranchName
if (!$BranchId)
{
# No BranchId was returned, commence creating the branch
Write-Host "Branch $BranchName does not exist; Creating.."
# Dispatch command to create the branch
$Command = $(& /Dispatch/dispatch branch create $ApplicationId $BranchName)
if ($LASTEXITCODE -ne 0) { Write-Error $Command }
# Get the newly created branch
$BranchId = GetBranchId -ApplicationId $ApplicationId -BranchName $BranchName
# Check that the branch was actually retrieved, else return exception
if (!$BranchId) { Write-Error "Attempted to create branch $BranchName, but it still could not be found!" }
Write-Host "Branch $BranchName [$BranchId] created."
}
if ($DrmWrap)
{
# Validate that an executable name was provided, else return exception
if (!$ExecutableName) { Write-Error "DrmWrap was requested but no ExecutableName was provided!" }
# Define the path to the executable
$ExecutablePath = "$BuildPath/$ExecutableName"
# Apply DRM to the executable
$Command = $(& /Dispatch/dispatch build drm-wrap $ApplicationId $ExecutablePath)
if ($LASTEXITCODE -ne 0) { Write-Error $Command }
Write-Host "DRM has been applied to $ExecutablePath"
}
# Deploy the application to Discord with Dispatch
Write-Host "Using config ($ConfigPath) for $BranchName [$BranchId] to build ($BuildPath).."
$Command = $(& /Dispatch/dispatch build push $BranchId $ConfigPath $BuildPath -p)
if ($LASTEXITCODE -ne 0) { Write-Error $Command }
Write-Host "Discord deploy completed."
}
catch
{
throw
}