Skip to content

Commit

Permalink
.NET
Browse files Browse the repository at this point in the history
  • Loading branch information
ObiWanLansi committed Dec 2, 2023
1 parent e066d3d commit 5e4a635
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 44 deletions.
2 changes: 1 addition & 1 deletion GitKeeper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<StartupObject>GitKeeper.GitKeeper</StartupObject>
<AssemblyName>gk</AssemblyName>
</PropertyGroup>
Expand Down
84 changes: 41 additions & 43 deletions Src/GitKeeper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,72 @@
using System.Collections.Generic;
using System.IO;

namespace GitKeeper;


namespace GitKeeper
static class GitKeeper
{
static class GitKeeper
{
private static readonly string GITKEEP = ".gitkeep";
private static readonly string GITKEEP = ".gitkeep";

private static readonly HashSet<string> IGNORE = new() { "bin", "obj", "packages", "build" };
private static readonly HashSet<string> IGNORE = ["bin", "obj", "packages", "build", "__pycache__"];

//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------


private static bool IgnoreDirectory(DirectoryInfo di) => IGNORE.Contains(di.Name.ToLower());
private static bool IgnoreDirectory(DirectoryInfo di) => IGNORE.Contains(di.Name.ToLower());


private static bool ContainsGitKeep(FileSystemInfo[] childs)
private static bool ContainsGitKeep(FileSystemInfo[] childs)
{
foreach (FileSystemInfo fi in childs)
{
foreach (FileSystemInfo fi in childs)
if (fi.Name.Equals(GITKEEP))
{
if (fi.Name.Equals(GITKEEP))
{
return true;
}
return true;
}

return false;
}

return false;
}

private static void ScanDirectory(DirectoryInfo diCurrent)
{
Console.Out.WriteLineAsync($"Scanning {diCurrent.FullName}");

FileSystemInfo[] childs = diCurrent.GetFileSystemInfos();

if (childs.Length == 0)
{
Console.Out.WriteLineAsync($" The directory '{diCurrent.FullName}' contains no files or subfolders, i'll create an {GITKEEP}");
File.WriteAllBytes($"{diCurrent.FullName}\\{GITKEEP}", new byte[] { });
return;
}
private static void ScanDirectory(DirectoryInfo diCurrent)
{
Console.Out.WriteLineAsync($"Scanning {diCurrent.FullName}");

foreach (DirectoryInfo diChild in diCurrent.GetDirectories())
{
if (diChild.Name[0] == '.')
{
continue;
}
FileSystemInfo[] childs = diCurrent.GetFileSystemInfos();

if (IgnoreDirectory(diChild))
{
continue;
}
if (childs.Length == 0)
{
Console.Out.WriteLineAsync($" The directory '{diCurrent.FullName}' contains no files or subfolders, i'll create an {GITKEEP}");
File.WriteAllBytes($"{diCurrent.FullName}\\{GITKEEP}", Array.Empty<byte>());
return;
}

ScanDirectory(diChild);
foreach (DirectoryInfo diChild in diCurrent.GetDirectories())
{
if (diChild.Name[0] == '.')
{
continue;
}

if (ContainsGitKeep(childs) && childs.Length > 1)
if (IgnoreDirectory(diChild))
{
Console.Out.WriteLineAsync($" The directory '{diCurrent.FullName}' contains an obsolete {GITKEEP}");
File.Delete($"{diCurrent.FullName}\\{GITKEEP}");
continue;
}

ScanDirectory(diChild);
}

//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if (ContainsGitKeep(childs) && childs.Length > 1)
{
Console.Out.WriteLineAsync($" The directory '{diCurrent.FullName}' contains an obsolete {GITKEEP}");
File.Delete($"{diCurrent.FullName}\\{GITKEEP}");
}
}

//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

static void Main(string[] args) => ScanDirectory(new DirectoryInfo(Environment.CurrentDirectory));
}

static void Main(string[] args) => ScanDirectory(new DirectoryInfo(Environment.CurrentDirectory));
}

0 comments on commit 5e4a635

Please sign in to comment.