Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 86 additions & 22 deletions build-tools/prep/ReplaceFileContents.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -32,41 +32,105 @@ public override bool Execute ()
}
}

if (File.Exists (DestinationFile.ItemSpec))
File.Delete (DestinationFile.ItemSpec);
try
{
// Validate source file exists
if (!File.Exists (SourceFile.ItemSpec))
{
Log.LogError ($"Source file not found: {SourceFile.ItemSpec}");
Comment on lines +35 to +40
return false;
}

string[] replacements;
if (!String.IsNullOrEmpty (ReplacementFilePath))
replacements = File.ReadAllLines (ReplacementFilePath);
else
replacements = Replacements;
var r = GetReplacementInfo (replacements);
using (var i = File.OpenText (SourceFile.ItemSpec))
using (var o = File.CreateText (DestinationFile.ItemSpec)) {
string line;
while ((line = i.ReadLine ()) != null) {
foreach (var e in r) {
line = line.Replace (e.Key, e.Value);
// Delete destination if it exists
if (File.Exists (DestinationFile.ItemSpec))
{
try
{
File.Delete (DestinationFile.ItemSpec);
}
catch (Exception ex)
{
Log.LogError ($"Failed to delete destination file '{DestinationFile.ItemSpec}': {ex.Message}");
return false;
}
}

// Get replacement pairs
string[] replacements;
if (!String.IsNullOrEmpty (ReplacementFilePath))
{
if (!File.Exists (ReplacementFilePath))
{
Log.LogError ($"Replacement file not found: {ReplacementFilePath}");
return false;
}
replacements = File.ReadAllLines (ReplacementFilePath);
}
else
{
replacements = Replacements;
}

var r = GetReplacementInfo (replacements);

// Process file with error handling
using (var i = File.OpenText (SourceFile.ItemSpec))
using (var o = File.CreateText (DestinationFile.ItemSpec))
{
string line;
while ((line = i.ReadLine ()) != null)
{
foreach (var e in r)
{
line = line.Replace (e.Key, e.Value);
}
o.WriteLine (line);
}
o.WriteLine (line);
}
}

return !Log.HasLoggedErrors;
return !Log.HasLoggedErrors;
}
catch (Exception ex)
{
Log.LogError ($"Task {nameof (ReplaceFileContents)} failed: {ex.Message}");
return false;
}
}

static readonly char[] Separator = new [] { '=' };

static Dictionary<string, string> GetReplacementInfo (string[] replacements)
{
var r = new Dictionary<string, string> (replacements?.Length ?? 0);
if (replacements == null)
var r = new Dictionary<string, string> (replacements?.Length ?? 0);
if (replacements == null || replacements.Length == 0)
return r;
foreach (var e in replacements) {

foreach (var e in replacements)
{
if (string.IsNullOrEmpty (e))
continue;

var kvp = e.Split (Separator, 2, StringSplitOptions.RemoveEmptyEntries);
r.Add (kvp [0], kvp.Length > 1 ? kvp [1] : "");

// Validate we have a key
if (kvp.Length == 0)
{
continue; // Skip malformed lines
}

string key = kvp[0];
string value = kvp.Length > 1 ? kvp[1] : "";

// Warn if key already exists (duplicate replacement)
if (r.ContainsKey (key))
{
Log.LogWarning ($"Duplicate replacement key: '{key}'");
r[key] = value; // Override with latest
}
else
{
r.Add (key, value);
}
}
return r;
}
Expand Down