Skip to content

Commit

Permalink
Made GetJunkYards SonarCube compliance
Browse files Browse the repository at this point in the history
Updated README.md
Updated .gitignore to exclude sonar files
  • Loading branch information
MahmudX committed Oct 18, 2024
1 parent c93fd42 commit 65cb9c0
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 64 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -482,3 +482,7 @@ $RECYCLE.BIN/

# Vim temporary swap files
*.swp

# sonar analysis
.sonarlint
.sonarqube
35 changes: 15 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,34 @@ Crow is a lightweight command-line tool designed to help developers reclaim valu
6. Run `crow help` to verify the installation.

### Caution

To set the execution policy, you may need to run the shell as an admin.

## Usage
## Example Usage

```sh
--lf <file> --in <directory> ... --rm <file> ...
crow --lf *csproj --in C:\Users\YourUserName\source\repos --rm bin obj
```

## Example

```sh
crow --lf *csproj --in C:\Users\YourUserName\source\repos --rm bin obj
crow --lf package.json --in C:\Users\YourUserName\jsWorks --rm node_modules
```

```sh
crow --lf package.json --in C:\Users\YourUserName\jsWorks --rm node_modules
crow --lf package.json --in C:\Users\YourUserName\jsWorks --rm node_modules --ig AppData
```

> Make sure crow is in your PATH
### Flags

- `--lf`
Files or directories to look for. Must be a single input. Supports Regex.

- `--in`
Directories to scan. Supports multiple input.

- `--rm`
Files or directories to remove. Supports multiple input. Supports Regex.

- `--force`
Removes files without asking for confirmation. Use this at your own risk.
|Flag|Description|Remark|
|---|---|---|
|`--lf`|Files or directories to look for|Required|
|`--in`|Directories to scan|Required|
|`--rm`|Files or directories to remove|Required|
|`--ig`|Directories to ignore|Optional|
|`--force`|Removes files without asking for confirmation|Optional|

### Misc

Expand All @@ -60,6 +56,5 @@ crow --lf package.json --in C:\Users\YourUserName\jsWorks --rm node_modules

### Note

1. All flags except `--force` are required.
2. Flags are case insensitive.
3. Using `--force` is not recommended.
1. Flags are case insensitive.
2. Using `--force` is not recommended.
109 changes: 65 additions & 44 deletions src/Crow/Helpers/JunkYardScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ public static class JunkYardScanner
/// <returns>A leaky enumerable collection of JunkYard objects found in the specified directories.</returns>
public static IEnumerable<JunkYard> ScanJunkYards(this Configuration config)
{
foreach (var path in config.LookIns)
var validPaths = config.LookIns
.Where(p => !string.IsNullOrEmpty(p))
.Select(p => new DirectoryInfo(p));

foreach (var directoryInfo in validPaths)
{
if (string.IsNullOrEmpty(path))
continue;

var directoryInfo = new DirectoryInfo(path);

foreach (var junk in directoryInfo.GetJunkYards(config))
{
yield return junk;
Expand Down Expand Up @@ -56,60 +55,82 @@ private static IEnumerable<JunkYard> GetJunkYards(
Configuration config
)
{
if (
directoryInfo is null
|| !directoryInfo.Exists
|| config.Ignores.Contains(directoryInfo.FullName)
)
if (ShouldSkipDirectory(directoryInfo, config))
yield break;

FileInfo[] files;
DirectoryInfo[] directories;
try
{
files = directoryInfo.GetFiles();
directories = directoryInfo.GetDirectories();
}
catch
{
if (TryGetDirectoryContents(directoryInfo!, out var files, out var directories))
yield break;
}

if (files.Length == 0 && directories.Length == 0)
if (IsDirectoryEmpty(files, directories))
yield break;

var isJunkFolder = false;
if (CheckForJunkFiles(files, directories, config, out var junkYards))
{
foreach (var junkYard in junkYards)
{
yield return junkYard;
}

// Search through all the files to find target directories or files where
// certain identifiers exists
yield break;
}

foreach (var file in files)
foreach (var junkYard1 in SearchSubDirectories(config, directories))
{
if (!Sherlock.MatchesPattern(config.LookFor, file.Name.AsSpan(), MatchType.Win32))
continue;
isJunkFolder = true;
yield return junkYard1;
}
}

foreach (var target in config.RemoveCandidates)
{
foreach (var junkYard in FindJunkYards(directories, files, target))
{
yield return junkYard;
}
}
private static bool CheckForJunkFiles(
FileInfo[] files, DirectoryInfo[] directories, Configuration config, out IEnumerable<JunkYard> junkYards)
{
var validFiles = files.Where(f => Sherlock.MatchesPattern(config.LookFor, f.Name.AsSpan(), MatchType.Win32))
.ToList();

break;
if (validFiles.Count == 0)
{
junkYards = [];
return false;
}

if (isJunkFolder)
yield break;
junkYards = config.RemoveCandidates.SelectMany(
target => FindJunkYards(directories, files, target)
);
return true;
}

private static IEnumerable<JunkYard> SearchSubDirectories(Configuration config, DirectoryInfo[] directories)
{
return directories.SelectMany(directory => directory.GetJunkYards(config));
}

foreach (var directory in directories)
private static bool TryGetDirectoryContents(DirectoryInfo directoryInfo, out FileInfo[] files,
out DirectoryInfo[] directories)
{
try
{
foreach (var junk in directory.GetJunkYards(config))
{
yield return junk;
}
files = directoryInfo.GetFiles();
directories = directoryInfo.GetDirectories();
}
catch
{
files = [];
directories = [];
return true;
}

return false;
}

private static bool IsDirectoryEmpty(FileInfo[] files, DirectoryInfo[] directories)
{
return files.Length == 0 && directories.Length == 0;
}

private static bool ShouldSkipDirectory(DirectoryInfo? directoryInfo, Configuration config)
{
return directoryInfo is null
|| !directoryInfo.Exists
|| config.Ignores.Contains(directoryInfo.FullName);
}

private static IEnumerable<JunkYard> FindJunkYards(
Expand Down

0 comments on commit 65cb9c0

Please sign in to comment.