|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.IO; |
| 8 | +using System.Reflection; |
| 9 | + |
| 10 | +namespace Microsoft.Extensions.Hosting.IntegrationTesting |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Parameters to control application deployment. |
| 14 | + /// </summary> |
| 15 | + public class DeploymentParameters |
| 16 | + { |
| 17 | + public DeploymentParameters() |
| 18 | + { |
| 19 | + var configAttribute = Assembly.GetCallingAssembly().GetCustomAttribute<AssemblyConfigurationAttribute>(); |
| 20 | + if (configAttribute != null && !string.IsNullOrEmpty(configAttribute.Configuration)) |
| 21 | + { |
| 22 | + Configuration = configAttribute.Configuration; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + public DeploymentParameters(TestVariant variant) |
| 27 | + { |
| 28 | + var configAttribute = Assembly.GetCallingAssembly().GetCustomAttribute<AssemblyConfigurationAttribute>(); |
| 29 | + if (configAttribute != null && !string.IsNullOrEmpty(configAttribute.Configuration)) |
| 30 | + { |
| 31 | + Configuration = configAttribute.Configuration; |
| 32 | + } |
| 33 | + |
| 34 | + TargetFramework = variant.Tfm; |
| 35 | + ApplicationType = variant.ApplicationType; |
| 36 | + RuntimeArchitecture = variant.Architecture; |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Creates an instance of <see cref="DeploymentParameters"/>. |
| 41 | + /// </summary> |
| 42 | + /// <param name="applicationPath">Source code location of the target location to be deployed.</param> |
| 43 | + /// <param name="runtimeFlavor">Flavor of the clr to run against.</param> |
| 44 | + /// <param name="runtimeArchitecture">Architecture of the runtime to be used.</param> |
| 45 | + public DeploymentParameters( |
| 46 | + string applicationPath, |
| 47 | + RuntimeFlavor runtimeFlavor, |
| 48 | + RuntimeArchitecture runtimeArchitecture) |
| 49 | + { |
| 50 | + if (string.IsNullOrEmpty(applicationPath)) |
| 51 | + { |
| 52 | + throw new ArgumentException("Value cannot be null.", nameof(applicationPath)); |
| 53 | + } |
| 54 | + |
| 55 | + if (!Directory.Exists(applicationPath)) |
| 56 | + { |
| 57 | + throw new DirectoryNotFoundException(string.Format("Application path {0} does not exist.", applicationPath)); |
| 58 | + } |
| 59 | + |
| 60 | + ApplicationPath = applicationPath; |
| 61 | + ApplicationName = new DirectoryInfo(ApplicationPath).Name; |
| 62 | + RuntimeFlavor = runtimeFlavor; |
| 63 | + |
| 64 | + var configAttribute = Assembly.GetCallingAssembly().GetCustomAttribute<AssemblyConfigurationAttribute>(); |
| 65 | + if (configAttribute != null && !string.IsNullOrEmpty(configAttribute.Configuration)) |
| 66 | + { |
| 67 | + Configuration = configAttribute.Configuration; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public DeploymentParameters(DeploymentParameters parameters) |
| 72 | + { |
| 73 | + foreach (var propertyInfo in typeof(DeploymentParameters).GetProperties()) |
| 74 | + { |
| 75 | + if (propertyInfo.CanWrite) |
| 76 | + { |
| 77 | + propertyInfo.SetValue(this, propertyInfo.GetValue(parameters)); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + foreach (var kvp in parameters.EnvironmentVariables) |
| 82 | + { |
| 83 | + EnvironmentVariables.Add(kvp); |
| 84 | + } |
| 85 | + |
| 86 | + foreach (var kvp in parameters.PublishEnvironmentVariables) |
| 87 | + { |
| 88 | + PublishEnvironmentVariables.Add(kvp); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public ApplicationPublisher ApplicationPublisher { get; set; } |
| 93 | + |
| 94 | + public RuntimeFlavor RuntimeFlavor { get; set; } |
| 95 | + |
| 96 | + public RuntimeArchitecture RuntimeArchitecture { get; set; } = RuntimeArchitecture.x64; |
| 97 | + |
| 98 | + public string EnvironmentName { get; set; } |
| 99 | + |
| 100 | + public string ApplicationPath { get; set; } |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// Gets or sets the name of the application. This is used to execute the application when deployed. |
| 104 | + /// Defaults to the file name of <see cref="ApplicationPath"/>. |
| 105 | + /// </summary> |
| 106 | + public string ApplicationName { get; set; } |
| 107 | + |
| 108 | + public string TargetFramework { get; set; } |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// Configuration under which to build (ex: Release or Debug) |
| 112 | + /// </summary> |
| 113 | + public string Configuration { get; set; } = "Debug"; |
| 114 | + |
| 115 | + /// <summary> |
| 116 | + /// Space separated command line arguments to be passed to dotnet-publish |
| 117 | + /// </summary> |
| 118 | + public string AdditionalPublishParameters { get; set; } |
| 119 | + |
| 120 | + /// <summary> |
| 121 | + /// To publish the application before deployment. |
| 122 | + /// </summary> |
| 123 | + public bool PublishApplicationBeforeDeployment { get; set; } |
| 124 | + |
| 125 | + public bool PreservePublishedApplicationForDebugging { get; set; } = false; |
| 126 | + |
| 127 | + public bool StatusMessagesEnabled { get; set; } = true; |
| 128 | + |
| 129 | + public ApplicationType ApplicationType { get; set; } |
| 130 | + |
| 131 | + public string PublishedApplicationRootPath { get; set; } |
| 132 | + |
| 133 | + /// <summary> |
| 134 | + /// Environment variables to be set before starting the host. |
| 135 | + /// Not applicable for IIS Scenarios. |
| 136 | + /// </summary> |
| 137 | + public IDictionary<string, string> EnvironmentVariables { get; } = new Dictionary<string, string>(); |
| 138 | + |
| 139 | + /// <summary> |
| 140 | + /// Environment variables used when invoking dotnet publish. |
| 141 | + /// </summary> |
| 142 | + public IDictionary<string, string> PublishEnvironmentVariables { get; } = new Dictionary<string, string>(); |
| 143 | + |
| 144 | + /// <summary> |
| 145 | + /// For any application level cleanup to be invoked after performing host cleanup. |
| 146 | + /// </summary> |
| 147 | + public Action<DeploymentParameters> UserAdditionalCleanup { get; set; } |
| 148 | + |
| 149 | + public override string ToString() |
| 150 | + { |
| 151 | + return string.Format( |
| 152 | + "[Variation] :: Runtime={0}, Arch={1}, Publish={2}", |
| 153 | + RuntimeFlavor, |
| 154 | + RuntimeArchitecture, |
| 155 | + PublishApplicationBeforeDeployment); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments