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
|
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Tango.AdvancedInstaller;
using Tango.Core;
using Tango.Core.Commands;
using Tango.FSE.BL.Web;
using Tango.FSE.Web.Messages;
using Tango.Git;
using Tango.MachineService.Gateway;
using Tango.Settings;
using Tango.SharedUI;
using Tango.Transport.Web;
namespace Tango.FSE.Publisher.UI
{
public class MainWindowVM : ViewModel
{
private FSEWebClient _client;
private String _buildFolder;
#if TS
private const String APP_EXE_NAME = "Tango.TwineRSM.UI.exe";
#else
private const String APP_EXE_NAME = "Tango.FSE.UI.exe";
#endif
private List<EnvironmentConfiguration> _environments;
public List<EnvironmentConfiguration> Environments
{
get { return _environments; }
set { _environments = value; RaisePropertyChangedAuto(); }
}
private EnvironmentConfiguration _selectedEnvironment;
public EnvironmentConfiguration SelectedEnvironment
{
get { return _selectedEnvironment; }
set { _selectedEnvironment = value; RaisePropertyChangedAuto(); OnSelectedEnvironmentChanged(); InvalidateRelayCommands(); }
}
private String _localVersion;
public String LocalVersion
{
get { return _localVersion; }
set { _localVersion = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
}
private String _remoteVersion;
public String RemoteVersion
{
get { return _remoteVersion; }
set { _remoteVersion = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
}
private String _buidConfig;
public String BuildConfig
{
get { return _buidConfig; }
set { _buidConfig = value; RaisePropertyChangedAuto(); OnBuildConfigChanged(); InvalidateRelayCommands(); }
}
private String _email;
public String Email
{
get { return _email; }
set { _email = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
}
private String _password;
public String Password
{
get { return _password; }
set { _password = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
}
private String _comments;
public String Comments
{
get { return _comments; }
set { _comments = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
}
private bool _createTag;
public bool CreateTag
{
get { return _createTag; }
set { _createTag = value; RaisePropertyChangedAuto(); }
}
private bool _autoCommitAndPush;
public bool AutoCommitAndPush
{
get { return _autoCommitAndPush; }
set { _autoCommitAndPush = value; RaisePropertyChangedAuto(); }
}
private TangoProgress<double> _progress;
public TangoProgress<double> Progress
{
get { return _progress; }
set { _progress = value; RaisePropertyChangedAuto(); }
}
public BuildVariants BuildVariant
{
get
{
#if TS
return BuildVariants.TwineRSM;
#else
return BuildVariants.FSE;
#endif
}
}
public RelayCommand PublishCommand { get; set; }
public MainWindowVM()
{
PublishCommand = new RelayCommand(Publish, CanPublish);
var settings = SettingsManager.Default.GetOrCreate<PublisherSettings>();
Email = settings.Email;
Password = settings.Password;
CreateTag = true;
AutoCommitAndPush = true;
}
private bool CanPublish()
{
try
{
return
SelectedEnvironment != null &&
Email.IsNotNullOrEmpty() &&
Password.IsNotNullOrEmpty() &&
Comments.IsNotNullOrEmpty() &&
System.Version.Parse(LocalVersion) > System.Version.Parse(RemoteVersion) &&
IsFree;
}
catch (Exception ex)
{
Debug.WriteLine(ex);
return false;
}
}
public async void Init()
{
try
{
IsFree = false;
UpdateProgress("Retrieving environments...");
using (HttpClient http = new HttpClient())
{
http.DefaultRequestHeaders.TryAddWithoutValidation("Cookie", "x-ms-routing-name=self");
GatewayClient client = new GatewayClient(ConfigurationManager.AppSettings.Get("GatewayUrl"), http);
Environments = (await client.GetEnvironmentsAsync(new EnvironmentsRequest())).Environments.OrderBy(x => x.DisplayIndex).ToList();
}
SelectedEnvironment = Environments.SingleOrDefault(x => x.Name == "DEV");
BuildConfig = "Release";
ProgressReady();
}
catch (Exception ex)
{
ShowError($"Error retrieving environments from gateway service.\n{ex.FlattenMessage()}");
}
finally
{
IsFree = true;
}
}
private void OnBuildConfigChanged()
{
UpdateProgress("Searching build path...");
String path = Path.GetFullPath($@"../../../../Build\{(BuildVariant == BuildVariants.FSE ? "FSE" : "Twine RSM")}\{BuildConfig}");
if (!Directory.Exists(path))
{
ShowError($"Could not locate {BuildVariant} build folder at '{path}'.");
}
_buildFolder = path;
try
{
String appExe = Path.Combine(path, APP_EXE_NAME);
LocalVersion = FileVersionInfo.GetVersionInfo(appExe).ProductVersion;
ProgressReady();
}
catch (Exception ex)
{
ShowError($"Could not determine the local application version.\n{ex.FlattenMessage()}");
}
}
private async Task OnSelectedEnvironmentChanged()
{
if (SelectedEnvironment != null)
{
try
{
IsFree = false;
UpdateProgress("Getting remote version...");
_client = new FSEWebClient(SelectedEnvironment.MachineServiceAddress);
RemoteVersion = (await _client.GetLatestVersion(new Web.Messages.LatestVersionRequest() { Build = BuildVariant })).Version;
ProgressReady();
}
catch (Exception ex)
{
ShowError($"Error retrieving the remote version.\n{ex.FlattenMessage()}");
}
finally
{
IsFree = true;
}
}
}
private async void Publish()
{
try
{
IsFree = false;
UpdateProgress($"Publishing {BuildVariant.ToDescription()}...");
var settings = SettingsManager.Default.GetOrCreate<PublisherSettings>();
settings.Email = Email;
settings.Password = Password;
settings.Save();
UpdateProgress($"Validating {BuildVariant.ToDescription()} installer....");
String projectPath = Path.GetFullPath($@"../../../../Advanced Installer Projects\{BuildVariant} Installer.aip");
if (!File.Exists(projectPath))
{
throw new FileNotFoundException($"Could not locate advanced installer project at '{projectPath}'.");
}
String installerName = $"{BuildVariant.ToDescription()} v{System.Version.Parse(LocalVersion).ToString(3)}.exe";
String installerPath = Path.GetFullPath($@"../../../../Build\Installers\{BuildVariant}\Release\{installerName}");
if (!Directory.Exists(Path.GetDirectoryName(installerPath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(installerPath));
}
UpdateProgress("Authenticating with machine service...");
await _client.Login(new LoginRequest()
{
Email = Email,
Password = Password,
Version = LocalVersion,
Build = BuildVariant
});
UpdateProgress("Requesting version upload...");
var uploadResponse = await _client.UploadVersion(new UploadVersionRequest()
{
Comments = Comments,
InstallerBlobName = installerName,
Version = LocalVersion,
Build = BuildVariant
});
UpdateProgress($"Building {BuildVariant.ToDescription()} installer...");
InstallerBuilder builder = new InstallerBuilder(projectPath);
await builder.Build(LocalVersion, installerPath);
if (!File.Exists(installerPath))
{
throw new FileNotFoundException($"Installer build was successful but the output installer could not be located at the specified path '{installerPath}'.");
}
UpdateProgress($"Uploading {BuildVariant.ToDescription()} installer...");
using (StorageBlobUploader uploader = new StorageBlobUploader(uploadResponse.InstallerBlobAddress, installerPath))
{
uploader.Progress += (x, e) =>
{
UpdateProgress($"Uploading {BuildVariant.ToDescription()} installer...", false, e.Current, e.Total);
};
await uploader.Upload();
}
UpdateProgress("Finalizing version publishing...");
await _client.NotifyVersionUploadCompleted(new UploadCompletedRequest()
{
Token = uploadResponse.Token,
Build = BuildVariant
});
UpdateProgress("Validating...");
await OnSelectedEnvironmentChanged();
if (LocalVersion != RemoteVersion)
{
throw new InvalidDataException("Publishing completed but the local and remote versions are different?!");
}
if (CreateTag)
{
await Task.Factory.StartNew(() =>
{
String repoPath = Path.GetFullPath("../../../../../../");
String tagVersion = System.Version.Parse(LocalVersion).ToString(3);
String tagName = $"{BuildVariant}_v{tagVersion}";
using (GitRepositoryManager git = new GitRepositoryManager(repoPath, Email, ConfigurationManager.AppSettings.Get("PAT")))
{
UpdateProgress("Checking repository changes...");
int changes = git.GetChanges().Count;
if (changes > 0)
{
if (AutoCommitAndPush)
{
UpdateProgress("Committing repository changes...");
git.Commit(tagName);
}
else
{
throw new InvalidOperationException($"There are {changes} uncommitted changes on the repository. Please commit and push all changes before creating the Tag");
}
}
UpdateProgress("Checking outgoing commits...");
int commits = git.GetOutgoingCommits().Count;
//if (commits > 0)
//{
if (AutoCommitAndPush)
{
UpdateProgress("Pushing repository changes...");
git.Sync();
}
else
{
throw new InvalidOperationException($"There are {commits} outgoing commits on the repository. Please push all commits before creating the Tag");
}
//}
git.Progress += (x, e) =>
{
UpdateProgress($"Pushing Tag '{tagName}'...", false, e.Progress.Value, e.Progress.Maximum);
};
UpdateProgress($"Creating Tag '{tagName}'...");
git.CreatePushTag(tagName, Comments, "Roy Ben Shabat");
}
});
}
Process.Start(SelectedEnvironment.MachineServiceAddress + $"/fse?buildVariant={(int)BuildVariant}");
UpdateProgress("Version published successfully!", false, 100, 100);
ShowInfo($"{BuildVariant.ToDescription()} published successfully!");
IsFree = true;
}
catch (Exception ex)
{
ShowError($"Error while trying to publish {BuildVariant.ToDescription()}\n{ex.FlattenMessage()}");
IsFree = true;
}
}
private void ShowError(String error)
{
UpdateProgress("Error!", false, 0, 100);
MessageBox.Show(error, $"{BuildVariant} Publisher", MessageBoxButton.OK, MessageBoxImage.Error);
}
private void ShowInfo(String message)
{
MessageBox.Show(message, $"{BuildVariant} Publisher", MessageBoxButton.OK, MessageBoxImage.Information);
}
private void UpdateProgress(String message, bool isIndeterminate = true, double value = 0, double max = 100)
{
Progress = new TangoProgress<double>(message, isIndeterminate, value, max);
}
private void ProgressReady()
{
UpdateProgress("Ready", false, 0, 100);
}
}
}
|