forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateAppHost.cs
114 lines (96 loc) · 4.28 KB
/
CreateAppHost.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
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.IO;
using System.Threading;
using Microsoft.Build.Framework;
using Microsoft.NET.HostModel;
using Microsoft.NET.HostModel.AppHost;
namespace Microsoft.NET.Build.Tasks
{
/// <summary>
/// Creates the runtime host to be used for an application.
/// This embeds the application DLL path into the apphost and performs additional customizations as requested.
/// </summary>
public class CreateAppHost : TaskBase
{
/// <summary>
/// The number of additional retries to attempt for creating the apphost.
/// <summary>
/// <remarks>
/// The default is no retries because internally the `HostWriter` attempts to retry
/// on different I/O operations. Users can optionally retry at the task level if desired.
/// </remarks>
public const int DefaultRetries = 0;
/// The default delay, in milliseconds, for each retry attempt for creating the apphost.
/// </summary>
public const int DefaultRetryDelayMilliseconds = 1000;
[Required]
public string AppHostSourcePath { get; set; }
[Required]
public string AppHostDestinationPath { get; set; }
[Required]
public string AppBinaryName { get; set; }
[Required]
public string IntermediateAssembly { get; set; }
public bool WindowsGraphicalUserInterface { get; set; }
public int Retries { get; set; } = DefaultRetries;
public int RetryDelayMilliseconds { get; set; } = DefaultRetryDelayMilliseconds;
protected override void ExecuteCore()
{
try
{
var isGUI = WindowsGraphicalUserInterface;
var resourcesAssembly = IntermediateAssembly;
if (!ResourceUpdater.IsSupportedOS())
{
if (isGUI)
{
Log.LogWarning(Strings.AppHostCustomizationRequiresWindowsHostWarning);
}
isGUI = false;
resourcesAssembly = null;
}
int attempts = 0;
while (true)
{
try
{
HostWriter.CreateAppHost(appHostSourceFilePath: AppHostSourcePath,
appHostDestinationFilePath: AppHostDestinationPath,
appBinaryFilePath: AppBinaryName,
windowsGraphicalUserInterface: isGUI,
assemblyToCopyResorcesFrom: resourcesAssembly);
return;
}
catch (Exception ex) when (ex is IOException ||
ex is UnauthorizedAccessException ||
// Note: replace this when https://github.com/dotnet/core-setup/issues/7516 is fixed
ex.GetType().Name == "HResultException")
{
if (Retries < 0 || attempts == Retries) {
throw;
}
++attempts;
Log.LogWarning(
string.Format(Strings.AppHostCreationFailedWithRetry,
attempts,
Retries + 1,
ex.Message));
if (RetryDelayMilliseconds > 0) {
Thread.Sleep(RetryDelayMilliseconds);
}
}
}
}
catch (AppNameTooLongException ex)
{
throw new BuildErrorException(Strings.FileNameIsTooLong, ex.LongName);
}
catch (PlaceHolderNotFoundInAppHostException ex)
{
throw new BuildErrorException(Strings.AppHostHasBeenModified, AppHostSourcePath, BitConverter.ToString(ex.MissingPattern));
}
}
}
}