-
Notifications
You must be signed in to change notification settings - Fork 16
/
DeploymentWindow.cs
409 lines (354 loc) · 15.9 KB
/
DeploymentWindow.cs
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
using System.Threading.Tasks;
using UnityEditor;
using UnityEngine;
namespace AmazonGameLift.Editor
{
internal class DeploymentWindow : EditorWindow
{
private const float WindowWidthPixels = 600f;
#if UNITY_2019_1_OR_NEWER
private const float WindowIdleHeightNoFormPixels = 375f;
#else
private const float WindowIdleHeightNoFormPixels = 350f;
#endif
private const float TopMarginPixels = 18f;
private const float LeftMarginPixels = 15f;
private const float RightMarginPixels = 13f;
private const float LabelWidthPixels = 185f;
private const float VerticalSpacingPixels = 5f;
private const float SpinnerSizePixels = 23f;
private HyperLinkButton _cfnConsoleLinkButton;
private HyperLinkButton _deploymentHelpLinkButton;
private HyperLinkButton _scenarioHelpLinkButton;
private StatusLabel _statusLabel;
private DeploymentSettings _model;
private int _previousScenarioIndex = -1;
private bool? _previousBootstrapped;
private float _bootstrapWarningHeight;
private float _scenarioHeight;
private bool _isSizeDirty;
private TextProvider _textProvider;
private string _labelSelectionMode;
private string _labelCurrentStack;
private string _labelGameName;
private string _labelBuildFolderPath;
private string _labelBuildFilePath;
private string _labelRegion;
private string _tooltipRegion;
private string _labelBucket;
private string _tooltipBucket;
private string _labelScenarioHelp;
private string _labelStartButton;
private string _labelCancelButton;
private string _labelDefaultFolderName;
private string _labelApiGateway;
private string _labelCognitoClientId;
private string _labelDeploymentCosts;
private string _labelBootstrapWarning;
private string _titleServerFileDialog;
private string _titleServerFolderDialog;
private StackUpdateModelFactory _stackUpdateModelFactory;
private ImageSequenceDrawer _spinnerDrawer;
private ControlDrawer _controlDrawer;
private string _labelScenarioPath;
private bool IsFormDisabled => _model == null || _model.IsDeploymentRunning;
private void SetUp()
{
_stackUpdateModelFactory = new StackUpdateModelFactory(new ChangeSetUrlFormatter());
_textProvider = TextProviderFactory.Create();
_labelSelectionMode = _textProvider.Get(Strings.LabelDeploymentSelectionMode);
_labelCurrentStack = _textProvider.Get(Strings.LabelDeploymentCurrentStack);
_labelGameName = _textProvider.Get(Strings.LabelDeploymentGameName);
_labelBuildFilePath = _textProvider.Get(Strings.LabelDeploymentBuildFilePath);
_labelBuildFolderPath = _textProvider.Get(Strings.LabelDeploymentBuildFolderPath);
_labelRegion = _textProvider.Get(Strings.LabelDeploymentRegion);
_tooltipRegion = _textProvider.Get(Strings.TooltipDeploymentRegion);
_labelBucket = _textProvider.Get(Strings.LabelDeploymentBucket);
_tooltipBucket = _textProvider.Get(Strings.TooltipDeploymentBucket);
_labelScenarioPath = _textProvider.Get(Strings.LabelDeploymentScenarioPath);
_labelScenarioHelp = _textProvider.Get(Strings.LabelDeploymentScenarioHelp);
_labelStartButton = _textProvider.Get(Strings.LabelDeploymentStartButton);
_labelCancelButton = _textProvider.Get(Strings.LabelDeploymentCancelButton);
_labelDefaultFolderName = _textProvider.Get(Strings.LabelDefaultFolderName);
_labelApiGateway = _textProvider.Get(Strings.LabelDeploymentApiGateway);
_labelCognitoClientId = _textProvider.Get(Strings.LabelDeploymentCognitoClientId);
_labelDeploymentCosts = _textProvider.Get(Strings.LabelDeploymentCosts);
_labelBootstrapWarning = _textProvider.Get(Strings.LabelDeploymentBootstrapWarning);
_titleServerFileDialog = _textProvider.Get(Strings.TitleDeploymentServerFileDialog);
_titleServerFolderDialog = _textProvider.Get(Strings.TitleDeploymentServerFolderDialog);
titleContent = new GUIContent(_textProvider.Get(Strings.TitleDeployment));
_model = DeploymentSettingsFactory.Create();
string deploymentHelpLabelText = _textProvider.Get(Strings.LabelDeploymentHelp);
_deploymentHelpLinkButton = new HyperLinkButton(deploymentHelpLabelText, Urls.AwsHelpDeployment, ResourceUtility.GetHyperLinkStyle());
_scenarioHelpLinkButton = new HyperLinkButton(_labelScenarioHelp, string.Empty, ResourceUtility.GetHyperLinkStyle());
_statusLabel = new StatusLabel();
string goToCloudFormationConsoleLabelText = _textProvider.Get(Strings.LabelDeploymentCloudFormationConsole);
_cfnConsoleLinkButton = new HyperLinkButton(goToCloudFormationConsoleLabelText, Urls.AwsCloudFormationConsole, ResourceUtility.GetHyperLinkStyle());
SetWindowSize(WindowIdleHeightNoFormPixels);
_model.Refresh();
_model.Restore();
_spinnerDrawer = SpinnerDrawerFactory.Create(size: SpinnerSizePixels);
_controlDrawer = ControlDrawerFactory.Create();
_ = _model.WaitForCurrentDeployment();
}
private void OnEnable()
{
SetUp();
_model.CurrentStackInfoChanged += OnCurrentStackInfoChanged;
_model.Status.Changed += OnStatusChanged;
Settings.SharedInstance.AnySettingChanged += OnAnySettingChanged;
}
private void OnDisable()
{
Settings.SharedInstance.AnySettingChanged -= OnAnySettingChanged;
_model.Save();
_model.CancelWaitingForDeployment();
_model.Status.Changed -= OnStatusChanged;
_model.CurrentStackInfoChanged -= OnCurrentStackInfoChanged;
}
private void OnInspectorUpdate()
{
if (_spinnerDrawer.IsRunning)
{
Repaint();
}
}
private void OnGUI()
{
EditorGUIUtility.labelWidth = LabelWidthPixels;
float scenarioHeight;
using (new EditorGUILayout.HorizontalScope())
{
GUILayout.Space(LeftMarginPixels);
using (new EditorGUILayout.VerticalScope())
{
GUILayout.Space(TopMarginPixels);
EditorGUILayout.HelpBox(_labelDeploymentCosts, MessageType.Warning);
if (!_model.IsBootstrapped)
{
EditorGUILayout.HelpBox(_labelBootstrapWarning, MessageType.Warning);
}
GUILayout.Space(VerticalSpacingPixels);
using (new EditorGUI.DisabledScope(IsFormDisabled))
{
scenarioHeight = DrawScenarioSelection();
DrawSeparator();
scenarioHeight += DrawScenarioForm();
}
using (new EditorGUI.DisabledScope(false))
{
DrawSeparator();
GUILayout.Label(_labelCurrentStack);
GUILayout.Space(VerticalSpacingPixels);
using (new EditorGUILayout.HorizontalScope())
{
DrawInfo(_model.CurrentStackInfo.Status);
if (_model.IsDeploymentRunning)
{
_spinnerDrawer.Draw();
}
}
if (_model.IsDeploymentRunning)
{
GUILayout.Space(EditorGUIUtility.singleLineHeight - SpinnerSizePixels);
}
GUILayout.Space(VerticalSpacingPixels);
DrawInfo(_model.CurrentStackInfo.Details, GUILayout.Height(55));
DrawStackOutput(_labelCognitoClientId, _model.CurrentStackInfo.UserPoolClientId);
DrawStackOutput(_labelApiGateway, _model.CurrentStackInfo.ApiGatewayEndpoint);
GUILayout.Space(VerticalSpacingPixels);
DrawLink(_model.HasCurrentStack ? _cfnConsoleLinkButton : _deploymentHelpLinkButton);
GUILayout.Space(VerticalSpacingPixels);
DrawButtons();
if (_model.Status.IsDisplayed)
{
_statusLabel.Draw(_model.Status.Message, _model.Status.Type);
}
}
}
GUILayout.Space(RightMarginPixels);
}
UpdateWindowSize(scenarioHeight);
UpdateSpinnerDrawer();
}
private void UpdateSpinnerDrawer()
{
if (_model.IsDeploymentRunning)
{
_spinnerDrawer.Start();
}
else
{
_spinnerDrawer.Stop();
}
}
private void DrawStackOutput(string label, string output)
{
if (output != null)
{
_controlDrawer.DrawReadOnlyText(label, output);
}
else
{
GUILayout.Space(.5f * VerticalSpacingPixels);
GUILayout.Space(EditorGUIUtility.singleLineHeight);
}
}
private float DrawScenarioForm()
{
_model.GameName = DrawTextField(_labelGameName, _model.GameName);
using (new EditorGUI.DisabledScope(!_model.IsBuildRequired))
{
_model.BuildFolderPath = _controlDrawer.DrawFolderPathField(_labelBuildFolderPath,
_model.BuildFolderPath, _labelDefaultFolderName, _titleServerFolderDialog);
_model.BuildFilePath = _controlDrawer.DrawFilePathField(_labelBuildFilePath, _model.BuildFilePath, "*", _titleServerFileDialog);
}
GUILayout.Space(VerticalSpacingPixels);
_controlDrawer.DrawReadOnlyText(_labelRegion, _model.CurrentRegion, _tooltipRegion);
_controlDrawer.DrawReadOnlyText(_labelBucket, _model.CurrentBucketName, _tooltipBucket);
const int fieldCount = 4;
return fieldCount * (VerticalSpacingPixels + EditorGUIUtility.singleLineHeight);
}
private void UpdateWindowSize(float scenarioHeight)
{
if (Event.current.type == EventType.Repaint)
{
UpdateIfScenarioChanged(scenarioHeight);
UpdateIfBootstrapStateChanged();
}
if (_isSizeDirty)
{
_isSizeDirty = false;
SetWindowSize(WindowIdleHeightNoFormPixels + _scenarioHeight + _bootstrapWarningHeight);
}
}
private void UpdateIfScenarioChanged(float scenarioHeight)
{
if (_model.ScenarioIndex == _previousScenarioIndex)
{
return;
}
_previousScenarioIndex = _model.ScenarioIndex;
_scenarioHeight = scenarioHeight;
_isSizeDirty = true;
_scenarioHelpLinkButton = new HyperLinkButton(_labelScenarioHelp, _model.ScenarioHelpUrl,
ResourceUtility.GetHyperLinkStyle());
}
private void UpdateIfBootstrapStateChanged()
{
if (_model.IsBootstrapped == _previousBootstrapped)
{
return;
}
_previousBootstrapped = _model.IsBootstrapped;
_bootstrapWarningHeight = _model.IsBootstrapped ? 0f : 40f;
_isSizeDirty = true;
}
#region Form controls
private float DrawScenarioSelection()
{
_model.ScenarioIndex = EditorGUILayout.Popup(_labelSelectionMode, _model.ScenarioIndex, _model.AllScenarios);
GUILayout.Space(VerticalSpacingPixels);
float pathHeight = _controlDrawer.DrawReadOnlyTextWrapped(_labelScenarioPath, _model.ScenarioPath);
GUILayout.Space(2 * VerticalSpacingPixels);
Rect descriptionRect = DrawInfo(_model.ScenarioDescription);
float height = 2 * VerticalSpacingPixels + descriptionRect.height + pathHeight - EditorGUIUtility.singleLineHeight;
if (!string.IsNullOrEmpty(_model.ScenarioHelpUrl))
{
GUILayout.Space(VerticalSpacingPixels);
DrawLink(_scenarioHelpLinkButton);
height += VerticalSpacingPixels + EditorGUIUtility.singleLineHeight;
}
return height;
}
private void DrawButtons()
{
using (new EditorGUILayout.HorizontalScope())
{
bool disabled = !_model.CanCancel;
using (new EditorGUI.DisabledScope(disabled))
{
if (GUILayout.Button(_labelCancelButton) && ConfirmCancel())
{
_model.CancelDeployment();
}
}
using (new EditorGUI.DisabledScope(!_model.CanDeploy))
{
if (GUILayout.Button(_labelStartButton))
{
_model.StartDeployment(ConfirmChangeSet)
.ContinueWith(task =>
{
if (task.IsFaulted)
{
Debug.LogException(task.Exception);
}
});
}
}
}
}
#endregion
private bool ConfirmCancel()
{
return EditorUtility.DisplayDialog(_textProvider.Get(Strings.TitleDeploymentCancelDialog),
_textProvider.Get(Strings.LabelDeploymentCancelDialogBody),
_textProvider.Get(Strings.LabelDeploymentCancelDialogOkButton),
_textProvider.Get(Strings.LabelDeploymentCancelDialogCancelButton));
}
private Rect DrawInfo(string text, params GUILayoutOption[] options)
{
var scope = new EditorGUILayout.HorizontalScope(options);
using (scope)
{
GUILayout.Space(5f);
EditorGUILayout.LabelField(text, ResourceUtility.GetInfoLabelStyle());
}
return scope.rect;
}
private void DrawLink(HyperLinkButton linkButton)
{
using (new EditorGUI.DisabledScope(false))
{
using (new EditorGUILayout.HorizontalScope())
{
GUILayout.Space(5f);
linkButton.Draw();
}
}
}
private string DrawTextField(string label, string value)
{
return EditorGUILayout.TextField(label, value);
}
private void DrawSeparator()
{
EditorGUILayout.LabelField(string.Empty, GUI.skin.horizontalSlider);
}
private void SetWindowSize(float height)
{
this.SetConstantSize(new Vector2(x: WindowWidthPixels, y: height));
}
private Task<bool> ConfirmChangeSet(ConfirmChangesRequest request)
{
StackUpdateDialog dialog = GetWindow<StackUpdateDialog>();
return dialog.SetUp(_stackUpdateModelFactory.Create(request));
}
private void OnCurrentStackInfoChanged()
{
Repaint();
}
private void OnStatusChanged()
{
Repaint();
}
private void OnAnySettingChanged()
{
_model.Refresh();
Repaint();
}
}
}