HelperFindReplace.UpdateTnsNames
This commit is contained in:
95
Helpers/HelperFindReplace.cs
Normal file
95
Helpers/HelperFindReplace.cs
Normal file
@ -0,0 +1,95 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace File_Folder_Helper.Helpers;
|
||||
|
||||
internal static class HelperFindReplace
|
||||
{
|
||||
|
||||
private static string? GetTnsNamesOraFile(List<string> args)
|
||||
{
|
||||
string? result = null;
|
||||
for (int i = 1; i < args.Count; i++)
|
||||
{
|
||||
if (args[i].Length == 2 && i + 1 < args.Count)
|
||||
{
|
||||
if (args[i][1] == 't')
|
||||
result = Path.GetFullPath(args[i + 1]);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static ReadOnlyCollection<(string, string)> GetFindReplace(string tnsNamesOraFile)
|
||||
{
|
||||
List<(string, string)> results = new();
|
||||
string[] segments;
|
||||
string[] lines = File.ReadAllLines(tnsNamesOraFile);
|
||||
foreach (string line in lines)
|
||||
{
|
||||
segments = line.Split('=');
|
||||
if (segments.Length < 3)
|
||||
continue;
|
||||
results.Add((segments[0].Trim(), line));
|
||||
}
|
||||
return new(results);
|
||||
}
|
||||
|
||||
private static void FindReplace(ILogger log, string[] files, ReadOnlyCollection<(string, string)> findReplace)
|
||||
{
|
||||
bool check;
|
||||
string[] lines;
|
||||
string[] segments;
|
||||
foreach (string file in files)
|
||||
{
|
||||
check = false;
|
||||
lines = File.ReadAllLines(file);
|
||||
for (int i = 0; i < lines.Length; i++)
|
||||
{
|
||||
segments = lines[i].Split('=');
|
||||
if (segments.Length < 3)
|
||||
continue;
|
||||
foreach ((string find, string replace) in findReplace)
|
||||
{
|
||||
if (!segments[0].Contains(find))
|
||||
continue;
|
||||
if (lines[i] == replace)
|
||||
continue;
|
||||
if (!check)
|
||||
check = true;
|
||||
lines[i] = replace;
|
||||
}
|
||||
}
|
||||
if (check)
|
||||
{
|
||||
log.LogInformation("{file}", file);
|
||||
File.WriteAllLines(file, lines);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static void UpdateTnsNames(ILogger log, List<string> args)
|
||||
{
|
||||
string? tnsNamesOraFile = GetTnsNamesOraFile(args);
|
||||
ReadOnlyCollection<(string, string)> findReplace;
|
||||
if (!string.IsNullOrEmpty(tnsNamesOraFile) && File.Exists(tnsNamesOraFile))
|
||||
findReplace = GetFindReplace(tnsNamesOraFile);
|
||||
else
|
||||
{
|
||||
log.LogInformation("<{tnsNamesOraFile}> doesn't exist!", tnsNamesOraFile);
|
||||
findReplace = new(Array.Empty<(string, string)>());
|
||||
}
|
||||
if (findReplace.Count == 0)
|
||||
log.LogInformation("Count == {count}", findReplace.Count);
|
||||
else
|
||||
{
|
||||
string[] files = Directory.GetFiles(args[0], "tnsNames.ora", SearchOption.AllDirectories);
|
||||
if (files.Length == 0)
|
||||
log.LogInformation("Count == {count}", findReplace.Count);
|
||||
else
|
||||
FindReplace(log, files, findReplace);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user