Skip to content

Commit 551070d

Browse files
Merge pull request #154 from atc-net/feature/Fix-IsFileDataLengthEqual
fix: IsFileDataLengthEqual and rename it to AreFilesEqual
2 parents 3ed0f17 + fcca54a commit 551070d

File tree

4 files changed

+41
-9
lines changed

4 files changed

+41
-9
lines changed

src/Atc.CodingRules.Updater.CLI/OptionsHelper.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ public static async Task<OptionsFile> CreateDefault(
2525
return CreateDefaultOptions(projectPath);
2626
}
2727

28-
options.Mappings.ResolvePaths(new DirectoryInfo(optionsPath));
28+
options.Mappings.ResolvePaths(
29+
optionsPath.EndsWith(".json", StringComparison.CurrentCultureIgnoreCase)
30+
? new FileInfo(optionsPath).Directory!
31+
: new DirectoryInfo(optionsPath));
32+
2933
return options;
3034
}
3135

src/Atc.CodingRules.Updater/DirectoryBuildPropsHelper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static void HandleFile(
4444

4545
try
4646
{
47-
if (!file.Directory!.Exists)
47+
if (!Directory.Exists(file.Directory!.FullName))
4848
{
4949
Directory.CreateDirectory(file.Directory.FullName);
5050
}
@@ -68,7 +68,7 @@ public static void HandleFile(
6868
return;
6969
}
7070

71-
if (FileHelper.IsFileDataLengthEqual(contentGit, contentFile) &&
71+
if (FileHelper.AreFilesEqual(contentGit, contentFile) &&
7272
contentGit.Equals(contentFile, StringComparison.Ordinal))
7373
{
7474
logger.LogInformation($"{EmojisConstants.FileNotUpdated} {descriptionPart} nothing to update");
@@ -141,7 +141,7 @@ private static void UpdateFile(
141141
if (useLatestMinorNugetVersion)
142142
{
143143
var newFileContent = EnsureLatestPackageReferencesVersion(logger, fileContent, LogCategoryType.Debug);
144-
if (!FileHelper.IsFileDataLengthEqual(fileContent, newFileContent) ||
144+
if (!FileHelper.AreFilesEqual(fileContent, newFileContent) ||
145145
!fileContent.Equals(newFileContent, StringComparison.Ordinal))
146146
{
147147
fileContent = newFileContent;

src/Atc.CodingRules.Updater/EditorConfigHelper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static void HandleFile(
3737

3838
try
3939
{
40-
if (!file.Directory!.Exists)
40+
if (!Directory.Exists(file.Directory!.FullName))
4141
{
4242
Directory.CreateDirectory(file.Directory.FullName);
4343
}
@@ -67,7 +67,7 @@ public static void HandleFile(
6767

6868
try
6969
{
70-
if (FileHelper.IsFileDataLengthEqual(contentGit, contentFile))
70+
if (FileHelper.AreFilesEqual(contentGit, contentFile))
7171
{
7272
logger.LogInformation($"{EmojisConstants.FileNotUpdated} {descriptionPart} nothing to update");
7373
return;
@@ -82,7 +82,7 @@ public static void HandleFile(
8282
var contentGitBasePart = ExtractContentBasePart(contentGit);
8383
var contentFileBasePart = ExtractContentBasePart(contentFile);
8484

85-
if (FileHelper.IsFileDataLengthEqual(contentGitBasePart, contentFileBasePart))
85+
if (FileHelper.AreFilesEqual(contentGitBasePart, contentFileBasePart))
8686
{
8787
logger.LogInformation($"{EmojisConstants.FileNotUpdated} {descriptionPart} nothing to update");
8888
return;

src/Atc.CodingRules.Updater/FileHelper.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// ReSharper disable ConvertIfStatementToReturnStatement
12
namespace Atc.CodingRules.Updater;
23

34
public static class FileHelper
@@ -51,7 +52,7 @@ public static void CreateFile(
5152
logger.LogInformation($"{EmojisConstants.FileCreated} {descriptionPart} created");
5253
}
5354

54-
public static bool IsFileDataLengthEqual(
55+
public static bool AreFilesEqual(
5556
string dataA,
5657
string dataB)
5758
{
@@ -61,7 +62,34 @@ public static bool IsFileDataLengthEqual(
6162
var l1 = dataA.EnsureEnvironmentNewLines().Length;
6263
var l2 = dataB.EnsureEnvironmentNewLines().Length;
6364

64-
return l1.Equals(l2);
65+
var isSameFileLength = l1.Equals(l2);
66+
if (!isSameFileLength)
67+
{
68+
return false;
69+
}
70+
71+
var headerLinesA = dataA.ToLines().Take(10).ToList();
72+
var headerLinesB = dataB.ToLines().Take(10).ToList();
73+
74+
if (headerLinesA.Find(x => x.StartsWith("# Version", StringComparison.CurrentCultureIgnoreCase)) !=
75+
headerLinesB.Find(x => x.StartsWith("# Version", StringComparison.CurrentCultureIgnoreCase)))
76+
{
77+
return false;
78+
}
79+
80+
if (headerLinesA.Find(x => x.StartsWith("# Updated", StringComparison.CurrentCultureIgnoreCase)) !=
81+
headerLinesB.Find(x => x.StartsWith("# Updated", StringComparison.CurrentCultureIgnoreCase)))
82+
{
83+
return false;
84+
}
85+
86+
if (headerLinesA.Find(x => x.StartsWith("# Distribution", StringComparison.CurrentCultureIgnoreCase)) !=
87+
headerLinesB.Find(x => x.StartsWith("# Distribution", StringComparison.CurrentCultureIgnoreCase)))
88+
{
89+
return false;
90+
}
91+
92+
return true;
6593
}
6694

6795
public static bool ContainsEditorConfigFile(

0 commit comments

Comments
 (0)