-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reparent absolute path in .target files prior to packaging in Project…
…Package tests
- Loading branch information
Showing
6 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
UET/uet/Commands/Internal/ReparentAdditionalPropertiesInTargets/MinimalUnrealTargetFile.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace UET.Commands.Internal.ReparentAdditionalPropertiesInTargets | ||
{ | ||
using System.Text.Json.Serialization; | ||
|
||
internal class MinimalUnrealTargetFile | ||
{ | ||
[JsonPropertyName("AdditionalProperties")] | ||
public List<MinimalUnrealTargetFileAdditionalProperty>? AdditionalProperties { get; set; } | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...ternal/ReparentAdditionalPropertiesInTargets/MinimalUnrealTargetFileAdditionalProperty.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace UET.Commands.Internal.ReparentAdditionalPropertiesInTargets | ||
{ | ||
using System.Text.Json.Serialization; | ||
|
||
internal class MinimalUnrealTargetFileAdditionalProperty | ||
{ | ||
[JsonPropertyName("Name")] | ||
public string? Name { get; set; } | ||
|
||
[JsonPropertyName("Value")] | ||
public string? Value { get; set; } | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...nal/ReparentAdditionalPropertiesInTargets/MinimalUnrealTargetFileJsonSerializerContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace UET.Commands.Internal.ReparentAdditionalPropertiesInTargets | ||
{ | ||
using System.Text.Json.Serialization; | ||
|
||
[JsonSerializable(typeof(MinimalUnrealTargetFile))] | ||
internal partial class MinimalUnrealTargetFileJsonSerializerContext : JsonSerializerContext | ||
{ | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
...nal/ReparentAdditionalPropertiesInTargets/ReparentAdditionalPropertiesInTargetsCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
namespace UET.Commands.Internal.ReparentAdditionalPropertiesInTargets | ||
{ | ||
using Microsoft.Extensions.Logging; | ||
using System.CommandLine; | ||
using System.CommandLine.Invocation; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
|
||
internal sealed class ReparentAdditionalPropertiesInTargetsCommand | ||
{ | ||
public sealed class Options | ||
{ | ||
public Option<DirectoryInfo> ProjectDirectoryPath = new Option<DirectoryInfo>("--project-directory-path") { IsRequired = true }; | ||
} | ||
|
||
public static Command CreateReparentAdditionalPropertiesInTargetsCommand() | ||
{ | ||
var options = new Options(); | ||
var command = new Command("reparent-additional-properties-in-targets"); | ||
command.AddAllOptions(options); | ||
command.AddCommonHandler<ReparentAdditionalPropertiesInTargetsCommandInstance>(options); | ||
return command; | ||
} | ||
|
||
private sealed class ReparentAdditionalPropertiesInTargetsCommandInstance : ICommandInstance | ||
{ | ||
private readonly ILogger<ReparentAdditionalPropertiesInTargetsCommandInstance> _logger; | ||
private readonly Options _options; | ||
|
||
public ReparentAdditionalPropertiesInTargetsCommandInstance( | ||
ILogger<ReparentAdditionalPropertiesInTargetsCommandInstance> logger, | ||
Options options) | ||
{ | ||
_logger = logger; | ||
_options = options; | ||
} | ||
|
||
public async Task<int> ExecuteAsync(InvocationContext context) | ||
{ | ||
var projectDirectoryPath = context.ParseResult.GetValueForOption(_options.ProjectDirectoryPath)!; | ||
|
||
foreach (var targetFile in Directory.GetFiles( | ||
Path.Combine(projectDirectoryPath.FullName, "Binaries"), | ||
"*.target", | ||
new EnumerationOptions { RecurseSubdirectories = true })) | ||
{ | ||
_logger.LogInformation($"Discovered .target file: {targetFile}"); | ||
|
||
// Deserialize just enough that we can read the RedpointUETOriginalProjectDirectory property. | ||
var targetText = await File.ReadAllTextAsync(targetFile).ConfigureAwait(false); | ||
var json = JsonSerializer.Deserialize(targetText, MinimalUnrealTargetFileJsonSerializerContext.Default.MinimalUnrealTargetFile); | ||
var originalProjectDirectoryProperty = (json?.AdditionalProperties ?? new List<MinimalUnrealTargetFileAdditionalProperty>()) | ||
.FirstOrDefault(x => x.Name == "RedpointUETOriginalProjectDirectory"); | ||
|
||
// If we have it, just do a really simple find-and-replace on the file. We don't serialize | ||
// via JSON since then UET would need to be updated whenever Unreal changes the schema | ||
// of target files. | ||
if (originalProjectDirectoryProperty?.Value != null) | ||
{ | ||
var originalValue = originalProjectDirectoryProperty.Value.Replace("\\", "\\\\", StringComparison.OrdinalIgnoreCase).TrimEnd('\\'); | ||
var replacedValue = projectDirectoryPath.FullName.Replace("\\", "\\\\", StringComparison.OrdinalIgnoreCase).TrimEnd('\\'); | ||
|
||
_logger.LogInformation($"Reparenting absolute paths in .target file: {targetFile} (Replacing '{originalValue}' with '{replacedValue}')"); | ||
targetText = targetText.Replace(originalValue, replacedValue, StringComparison.OrdinalIgnoreCase); | ||
await File.WriteAllTextAsync(targetFile, targetText).ConfigureAwait(false); | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
} | ||
} | ||
} |