| ofs | hex dump | ascii |
|---|
| 0000 | 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 04 3d 00 00 02 c8 08 06 00 00 00 7c 7b 8b | .PNG........IHDR...=.........|{. |
| 0020 | e8 00 00 00 01 73 52 47 42 00 ae ce 1c e9 00 00 00 04 67 41 4d 41 00 00 b1 8f 0b fc 61 05 00 00 | .....sRGB.........gAMA......a... |
| 0040 | 00 09 70 48 59 73 00 00 0e c3 00 00 0e c3 01 c7 6f a8 64 00 00 00 18 74 45 58 74 53 6f 66 74 77 | ..pHYs..........o.d....tEXtSoftw |
| 0060 | 61 72 65 00 70 61 69 6e 74 2e 6e 65 74 20 34 2e 30 2e 39 6c 33 7e 4e 00 00 ff 81 49 44 41 54 78 | are.paint.net.4.0.9l3~N....IDATx |
| 0080 | 5e ec fd 0b f0 25 67 7a de 87 8d 13 72 99 58 66 a4 98 5c 4a 32 43 5a 52 64 89 92 48 8a 97 dd e5 | ^....%gz....r.Xf..\J2CZRd..H.... |
| 00a0 | 6d 49 49 94 44 49 36 53 b2 ab 52 2a 59 b2 75 49 4a b2 14 59 72 54 ba c5 0b 2c 15 45 76 ec c4 2e | mII.DI6S..R*Y.uIJ..YrT...,.Ev... |
| 00c0 | 57 e2 c8 72 25 b6 93 2a 9b 8c 68 96 a8 24 8a 2a fe f3 a2 5d 2c b0 58 dc 2f 33 c0 dc 07 0b 2c 30 | W..r%..*..h..$.*...],.X./3....,0 |
| 00e0 | 00 66 31 18 ec 0d c0 2e 06 18 4c be a7 f7 3c ff 79 fe ef bc 5f 77 9f 3e 7d ce e9 ee f3 4c d5 af | .f1.......L...<.y..._w.>}....L.. |
| 0100 | fa fd de f7 fd be af cf f9 f7 39 a7 df 67 be ee 3e 75 74 74 64 fa f3 cf 18 63 8c 31 c6 18 63 8c | ..........9..g..>uttd....c.1..c. |
| 0120 | 31 7b 24 ab 55 4d 85 d4 69 d2 03 6b 5d fe 7b c6 18 63 8c 31 c6 18 63 cc 1a 64 b5 e5 3a 64 f5 ed | 1{$.UM..i..k].{..c.1..c..d..:d.. |
| 0140 | 41 93 3a 0f 90 ec 60 e9 43 76 90 fe f7 8d 31 c6 18 63 8c 31 c6 98 81 64 75 26 c8 6a d2 36 b2 da | A.:...`.Cv....1..c.1...du&.j.6.. |
| 0160 | f7 e0 48 9d 07 40 3c 10 b4 ad f0 e0 ca 0e 44 f0 0d c2 37 1a 63 8c 31 c6 18 63 8c 31 23 a1 f5 26 | ..H..@<.......D...7.c.1..c.1#..& |
| 0180 | 88 f5 68 97 18 92 d5 ba 5a 17 1f 04 a9 f3 00 88 7f 78 50 13 38 32 41 e3 9b 02 ff 83 15 ff 43 63 | ..h.....Z........xP.82A.......Cc |
| 01a0 | 8c 31 c6 18 63 8c 31 66 43 58 63 c6 da f3 03 05 d6 a5 6d 62 48 56 f3 82 ac 3e 5e 34 a9 73 a1 64 | .1..c.1fCXc.......mbHV...>^4.s.d |
| 01c0 | 7f 70 10 85 0e 8a 1c 38 98 00 45 0d 1e 7c ff 6c e1 d7 ac f8 e7 56 7c f3 8a ff 91 31 c6 18 63 8c | .p.....8..E..|.l.....V|....1..c. |
| 01e0 | 31 c6 18 b3 21 ac 31 59 73 b2 06 45 3d aa c2 08 ea 55 8a 20 14 42 b4 be e5 7f ee 67 42 48 56 37 | 1...!.1Ys..E=....U...B.....gBHV7 |
| 0200 | 2f 8e d4 b9 10 e2 1f 94 f0 0f ce 83 80 07 47 14 38 70 30 51 e0 c0 41 86 03 ef d7 16 7e 9d f0 3f | /.............G.8p0Q..A.....~..? |
| 0220 | 2e fc f3 2b be a5 f0 ad c6 18 63 8c 31 c6 18 63 cc 86 a0 be 64 ad 89 ba 93 35 28 6a 52 8a 22 14 | ...+......c.1..c....d....5(jR.". |
| 0240 | 43 28 82 64 42 88 8a 20 99 f0 41 b2 9a 7a 11 a4 ce 99 93 fd 01 41 26 74 c4 55 1c 99 c0 41 61 03 | C(.dB.....A..z.......A&t.U...Aa. |
| 0260 | 07 de b7 ad f8 0d 2b 7e 63 e1 5f 58 f1 ed 85 ff 49 e0 3b 8c 31 c6 18 63 8c 31 c6 98 1e 68 2d 89 | ......+~c._X....I.;.1..c.1...h-. |
| 0280 | fa 92 b5 26 ea 4e d4 9f bf be 80 7a f4 83 05 8a 22 14 44 b8 42 04 f5 2c 57 83 64 2b 41 28 7e d4 | ...&.N.....z....".D.B..,W.d+A(~. |
| 02a0 | 04 90 ac c6 9e 35 a9 73 a6 c4 3f 16 ff 90 14 3b a0 70 65 ab 39 28 72 00 15 38 70 20 e1 a0 c2 c1 | .....5.s..?....;.pe.9(r..8p..... |
| 02c0 | 85 03 8d 22 c6 77 16 fe c5 c2 6f 5e f1 5b 0a ff d3 c2 6f 2d fc 4b c2 6f 2b fc f6 0a df 65 8c 31 | ...".w....o^.[....o-.K.o+....e.1 |
| 02e0 | c6 18 63 8c 31 e6 e0 c8 ea 43 d4 8e 04 b5 24 6a 4b 80 3a 13 f5 26 ea ce df 54 40 1d 8a 7a 14 b5 | ..c.1....C....$jK.:..&...T@..z.. |
| 0300 | a9 8a 21 14 42 50 c7 a2 9e d5 d5 20 5c 09 42 01 84 f7 04 e9 5a 01 92 d5 dc b3 24 75 ce 10 fd e3 | ..!.BP......\.B.....Z.....$u.... |
| 0320 | 50 e4 a0 d0 41 45 0b 7f e0 28 74 70 25 07 57 71 70 f5 06 54 35 08 1c 38 a8 28 6a e0 e0 c3 01 f9 | P...AE...(tp%.Wqp..T5..8.(j..... |
| 0340 | 3b 56 fc ae c2 77 17 be 67 c5 ef 5e f1 7d 2b be bf f0 03 c6 18 63 8c 31 c6 18 63 4c 0b a8 1d 09 | ;V...w..g..^.}+......c.1..cL.... |
| 0360 | eb 4a c0 5a 13 75 27 ea 4f d6 a2 a8 4b 51 9f a2 4e 85 18 42 21 04 75 2c ea 59 5d 0d 82 7a 17 22 | .J.Z.u'.O...KQ..N..B!.u,.Y]..z." |
| 0380 | 88 5e 0a a3 2b 40 54 fc d0 ba 5a c9 6a f0 d9 90 3a 67 42 f6 c7 50 c1 83 42 47 b6 aa 23 0a 1d 50 | .^..+@T...Z.j...:gB..P..BG..#..P |
| 03a0 | c9 20 72 e0 80 89 02 07 0e 2e 8a 1a 14 32 3e 54 f8 70 e1 23 85 1f 5e f1 23 85 1f 15 7e 4c f8 a8 | ..r..........2>T.p.#..^.#...~L.. |
| 03c0 | 31 c6 18 63 8c 31 c6 18 13 d0 7a 11 36 eb 49 d4 97 a8 33 51 73 02 d4 9f 3f 58 40 3d 8a ff 64 ff | 1..c.1....z.6.I...3Qs...?X@=..d. |
| 03e0 | de 02 c4 90 df 59 e0 0a 12 d4 b1 f8 4f 7b fc e7 3d 44 10 08 20 b8 7a 01 02 08 56 80 f0 32 18 5e | .....Y......O{..=D....z...V..2.^ |
| 0400 | 02 13 2f 7f 81 00 92 ad fa 20 59 5d 3e 79 52 e7 c4 e1 9b 1d ff 00 14 3c f0 c7 c2 92 1d 8a 1d ba | ../.......Y]>yR........<........ |
| 0420 | aa 83 97 ae e0 0f 0f a1 03 8a 18 96 0a 61 e9 10 45 0e 1c 38 14 38 70 50 51 d8 a0 90 81 83 f1 27 | .............a..E..8.8pPQ......' |
| 0440 | 0a bf a7 f0 7b 0b bf af f0 93 85 3f 50 f8 83 81 9f 32 c6 18 63 8c 31 c6 18 63 5a 88 75 24 6a cb | ....{......?P....2..c.1..cZ.u$j. |
| 0460 | df 5f 40 9d 89 7a 13 75 27 ea cf 1f 2f a0 1e 45 6d 4a 41 04 ff 21 4f 21 04 ff 59 8f ff b4 87 08 | ._@..z.u'.../..EmJA..!O!..Y..... |
| 0480 | 12 05 10 d4 bf f8 4f 7f bd 04 06 97 bf f0 fe 1f 7a e9 0b ea ea 9a f8 11 eb f3 c9 93 3a 37 e1 ab | ......O.........z...........:7.. |
| 04a0 | 5f fd ea 46 bc fd f6 db a7 de 78 e3 8d 53 af bf fe fa 31 5f f8 c2 17 1a 3f e2 65 0e 7d c3 e3 a5 | _..F......x..S....1_....?.e.}... |
| 04c0 | 2c 35 b1 03 cb 7a f0 07 e6 bd 39 b8 a2 03 42 07 94 31 1c 1c 38 48 54 e4 f8 b1 27 9f 7c f2 5f 3f | ,5...z....9...B..1..8HT...'.|._? |
| 04e0 | 7d fa f4 df 78 e2 89 27 fe 0f 8f 3e fa e8 7f 74 ee dc b9 ff d3 f3 cf 3f ff f3 85 4f 7d ee 73 9f | }...x..'...>...t.......?...O}.s. |
| 0500 | 3b fd e2 8b 2f 5e 7d e9 a5 97 6e 5e bd 7a f5 36 29 ed 13 94 9c e3 6d 1b 9a df e6 d3 58 6c ab af | ;.../^}...n^.z.6).....m.....Xl.. |
| 0520 | ab 5d cb 51 7f 16 db 16 6d 73 d7 88 39 b5 7e f4 6b 4c db 31 96 e5 44 7f f4 d1 5f 8b b5 11 fb 75 | .].Q....ms..9.~.kL.1..D..._....u |
| 0540 | 8d d1 16 5f 67 fe 5a 2e fc 31 d6 67 dc be 73 33 8f f3 68 bf da 18 99 bf 96 db 45 9c 8f 6d b5 db | ..._g.Z..1.g..s3..h.......E..m.. |
| 0560 | 88 39 b5 7e 5d eusing Ionic.Zip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Tango.BL;
using Tango.Core;
using Tango.Core.DB;
using Tango.Core.DI;
using Tango.MachineStudio.Common.Web;
using Tango.SQLExaminer;
using Tango.Transport.Web;
using Tango.Core.ExtensionMethods;
using Tango.PPC.Common.Publish;
using Tango.Settings;
using Tango.Core.Components;
using System.Text.RegularExpressions;
namespace Tango.MachineStudio.Common.Tup
{
public class TupFileBuilder : ExtendedObject
{
public event EventHandler<TupFileBuilderProgressEventArgs> Progress;
public Task Build(String serialNumber, String filePath)
{
return Task.Factory.StartNew(() =>
{
String tempDbName = "Tango_TUP";
var tempPackageFolder = TemporaryManager.CreateFolder();
String tempBackupFolder = "C:\\MachineStudioTUP";
String tempBackupFile = Path.Combine(tempBackupFolder, tempDbName + ".bak");
var tempZipFile = TemporaryManager.CreateImaginaryFile();
DbManager dbManager = null;
LogManager.Log("Generating tup file...");
LogManager.Log($"Tup file: '{filePath}.'");
LogManager.Log($"Temporary db name: '{tempDbName}'.");
LogManager.Log($"Temporary package folder: '{tempPackageFolder}'.");
LogManager.Log($"Temporary db backup folder: '{tempBackupFolder}'.");
LogManager.Log($"Temporary db backup file: '{tempBackupFile}'.");
LogManager.Log($"Temporary zip file: '{tempZipFile}'.");
try
{
LogManager.Log("Initializing...");
OnProgress("Initializing...");
Core.DataSource localDataSource = new Core.DataSource()
{
Address = "localhost\\SQLEXPRESS",
IntegratedSecurity = true,
Type = DataSourceType.SQLServer,
Catalog = null,
};
try
{
LogManager.Log($"Trying to connect via SQLEXPRESS:\n{localDataSource.ToJsonString()}");
dbManager = DbManager.FromDataSource(localDataSource);
}
catch (Exception ex)
{
try
{
LogManager.Log(ex, "Could not connect using SQLEXPRESS. Trying local DB...");
CmdCommand command = new CmdCommand("sqllocaldb", "start \"MSSQLLocalDB\"");
var result = command.Run().Result;
command = new CmdCommand("sqllocaldb", "info \"MSSQLLocalDB\"");
result = command.Run().Result;
String pattern = "np:.+";
Regex reg = new Regex(pattern);
var match = reg.Match(result.StandardOutput);
String address = match.ToString();
if (address.Contains("np:"))
{
localDataSource.Address = address;
address = address.Trim().Replace("\r", "");
}
else
{
throw new ArgumentException("Could not parse LocalDB address string.");
}
LogManager.Log($"Trying to connect via LocalDB:\n{localDataSource.ToJsonString()}");
dbManager = DbManager.FromDataSource(localDataSource);
}
catch (Exception x)
{
LogManager.Log(x, "Could not find any database service for this operation.");
throw x;
}
}
OnProgress("Downloading latest PPC version...");
LogManager.Log("Connecting to machine service...");
MachineStudioWebClient client = TangoIOC.Default.GetInstance<MachineStudioWebClient>();
LogManager.Log("Requesting latest PPC version from machine service...");
var response = client.DownloadLatestPPCVersion(new DownloadLatestPPCVersionRequest() { SerialNumber = serialNumber }).Result;
LogManager.Log($"Machine service response:\n{response.ToJsonString()}");
var remoteDataSource = response.DataSource;
using (AutoFileDownloader downloader = new AutoFileDownloader(response.BlobAddress, response.CdnAddress, tempZipFile))
{
downloader.Progress += (x, e) =>
{
OnProgress($"Downloading latest PPC version '{response.Version}'...", false, e.Current, e.Total);
};
downloader.ResolveMode().GetAwaiter().GetResult();
LogManager.Log($"Downloading latest PPC version from: '{downloader.Address}'");
downloader.Download().Wait();
}
LogManager.Log("Extracting PPC version package...");
OnProgress("Extracting PPC package...");
using (ZipFile zip = new ZipFile(tempZipFile))
{
int currentEntry = 0;
zip.ExtractProgress += (x, args) =>
{
if (args.EventType == ZipProgressEventType.Extracting_AfterExtractEntry)
{
OnProgress("Extracting PPC package...", false, currentEntry++, zip.Entries.Count);
}
};
zip.ExtractAll(tempPackageFolder);
}
OnProgress("Extracting version information...");
LogManager.Log("Extracting publish information...");
PublishInfo publishInfo = PublishInfo.FromJson(File.ReadAllText(Path.Combine(tempPackageFolder, "version.json")));
LogManager.Log($"Publish Information:\n{publishInfo}");
LogManager.Log("Modifying publish information to custom tup file...");
publishInfo.IsMachineTupPackage = true;
publishInfo.MachineSerialNumber = serialNumber;
publishInfo.MachineDeploymentSlot = SettingsManager.Default.GetOrCreate<MachineStudioSettings>().DeploymentSlot;
OnProgress("Creating temporary database...");
LogManager.Log($"Creating temporary db backup directory '{tempBackupFolder}'");
Directory.CreateDirectory(tempBackupFolder);
LogManager.Log($"Creating new database: '{tempDbName}'");
//Create temp db
dbManager.Create(tempDbName, Path.Combine(tempBackupFolder, tempDbName + ".mdf"));
OnProgress("Generating database snapshot...");
LogManager.Log("Starting database synchronization...");
Thread.Sleep(2000);
localDataSource.Catalog = tempDbName;
ExaminerSequenceConfigurationRunner runner = new ExaminerSequenceConfigurationRunner(
Path.Combine(tempPackageFolder, "Provision Scripts", "config.xml"),
Path.Combine(tempPackageFolder, "Provision Scripts"),
remoteDataSource,
localDataSource,
serialNumber);
runner.ScriptExecuting += (x, item) =>
{
LogManager.Log($"Executing script '{item.FileName}'...");
OnProgress($"{item.Name}...");
};
runner.Log += (x, log) =>
{
LogManager.Log(log);
};
runner.Run().GetAwaiter().GetResult();
OnProgress("Generating database snapshot...");
if (File.Exists(tempBackupFile))
{
LogManager.Log($"Deleting file '{tempBackupFile}'");
File.Delete(tempBackupFile);
}
LogManager.Log($"Generating backup for '{tempDbName}' to '{tempBackupFile}'...");
dbManager.Backup(tempDbName, tempBackupFile);
OnProgress("Injecting database snapshot to PPC package...");
using (ZipFile zip = new ZipFile(tempZipFile))
{
LogManager.Log($"Injecting file '{tempBackupFile}' to original package at '{tempZipFile}'...");
zip.AddFile(tempBackupFile, "/");
LogManager.Log($"Injecting modified publish information...");
zip.UpdateEntry("version.json", publishInfo.ToJson());
zip.Save();
}
LogManager.Log($"Copying '{tempZipFile}' to '{filePath}'...");
File.Copy(tempZipFile, filePath, true);
OnProgress("Completed", false, 100, 100);
LogManager.Log("TUP file generation completed successfully.");
}
catch (Exception ex)
{
LogManager.Log(ex, "TUP file generation failed.");
OnProgress("Failed", false, 0, 100);
throw ex;
}
finally
{
LogManager.Log($"Removing '{tempZipFile}'.");
tempZipFile.Delete();
LogManager.Log($"Removing '{tempPackageFolder}'.");
tempPackageFolder.Delete();
try
{
LogManager.Log($"Removing database '{tempDbName}'.");
dbManager.SetOffline(tempDbName);
dbManager.SetOnline(tempDbName);
dbManager.Delete(tempDbName);
dbManager.Dispose();
}
catch (Exception ex)
{
LogManager.Log(ex, $"Error removing temp database '{tempDbName}'.");
}
try
{
LogManager.Log($"Removing '{tempBackupFolder}'.");
Directory.Delete(tempBackupFolder, true);
}
catch (Exception ex)
{
LogManager.Log(ex, $"Error removing folder '{tempBackupFolder}'.");
}
}
});
}
public async Task<String> GetLatestPPCVersion(String serialNumber)
{
MachineStudioWebClient client = TangoIOC.Default.GetInstance<MachineStudioWebClient>();
var response = await client.DownloadLatestPPCVersion(new DownloadLatestPPCVersionRequest() { SerialNumber = serialNumber });
return response.Version;
}
private void OnProgress(String message, bool isIntermediate = true, double progress = 0, double total = 100)
{
Progress?.Invoke(this, new TupFileBuilderProgressEventArgs()
{
Message = message,
IsIntermediate = isIntermediate,
Progress = progress,
Total = total,
});
}
}
}
|