-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathLauncherBuilder.cs
122 lines (103 loc) · 4.2 KB
/
LauncherBuilder.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.IO;
using Microsoft.Build.Shared;
using Microsoft.Build.Shared.FileSystem;
using Microsoft.Build.Tasks.Deployment.Bootstrapper;
#nullable disable
namespace Microsoft.Build.Tasks.Deployment.ManifestUtilities
{
/// <summary>
/// Adds Launcher and updates its resource
/// </summary>
public class LauncherBuilder
{
private const string LAUNCHER_RESOURCENAME = "FILENAME";
private const int LAUNCHER_RESOURCE_TABLE = 50;
private BuildResults _results;
public LauncherBuilder(string launcherPath)
{
LauncherPath = launcherPath;
}
/// <summary>
/// Specifies the location of the required Launcher files.
/// </summary>
/// <value>Path to Launcher files.</value>
public string LauncherPath { get; set; }
public BuildResults Build(string filename, string outputPath)
{
string launcherFilename = Path.GetFileName(LauncherPath);
_results = new BuildResults();
try
{
if (filename == null)
{
_results.AddMessage(BuildMessage.CreateMessage(BuildMessageSeverity.Error, "GenerateLauncher.InvalidInput"));
return _results;
}
if (String.IsNullOrEmpty(outputPath))
{
_results.AddMessage(BuildMessage.CreateMessage(BuildMessageSeverity.Error, "GenerateLauncher.NoOutputPath"));
return _results;
}
// Copy setup.bin to the output directory
string strOutputExe = System.IO.Path.Combine(outputPath, launcherFilename);
if (!CopyLauncherToOutputDirectory(strOutputExe))
{
// Appropriate messages should have been stuffed into the results already
return _results;
}
var resourceUpdater = new ResourceUpdater();
resourceUpdater.AddStringResource(LAUNCHER_RESOURCE_TABLE, LAUNCHER_RESOURCENAME, filename);
if (!resourceUpdater.UpdateResources(strOutputExe, _results))
{
return _results;
}
_results.SetKeyFile(launcherFilename);
_results.BuildSucceeded();
}
catch (Exception ex)
{
_results.AddMessage(BuildMessage.CreateMessage(BuildMessageSeverity.Error, "GenerateLauncher.General", ex.Message));
}
return _results;
}
private bool CopyLauncherToOutputDirectory(string strOutputExe)
{
if (!FileSystems.Default.FileExists(LauncherPath))
{
_results.AddMessage(BuildMessage.CreateMessage(BuildMessageSeverity.Error, "GenerateLauncher.MissingLauncherExe", LauncherPath));
return false;
}
try
{
EnsureFolderExists(Path.GetDirectoryName(strOutputExe));
File.Copy(LauncherPath, strOutputExe, true);
ClearReadOnlyAttribute(strOutputExe);
}
catch (Exception ex) when (ExceptionHandling.IsIoRelatedException(ex))
{
_results.AddMessage(BuildMessage.CreateMessage(BuildMessageSeverity.Error, "GenerateLauncher.CopyError", LauncherPath, strOutputExe, ex.Message));
return false;
}
return true;
}
private static void EnsureFolderExists(string strFolderPath)
{
if (!FileSystems.Default.DirectoryExists(strFolderPath))
{
Directory.CreateDirectory(strFolderPath);
}
}
private static void ClearReadOnlyAttribute(string strFileName)
{
FileAttributes attribs = File.GetAttributes(strFileName);
if ((attribs & FileAttributes.ReadOnly) != 0)
{
attribs &= (~FileAttributes.ReadOnly);
File.SetAttributes(strFileName, attribs);
}
}
}
}