Skip to content

Commit

Permalink
Make a folder delete helper (microsoft#297)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfs authored Jan 22, 2022
1 parent a25747d commit a0f6af1
Show file tree
Hide file tree
Showing 72 changed files with 180 additions and 316 deletions.
2 changes: 1 addition & 1 deletion Pipelines/core-pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ stages:
- task: UseDotNet@2
inputs:
packageType: 'sdk'
- script: 'dotnet tool install -g nbgv'
- script: 'dotnet tool update -g nbgv'
displayName: 'Install GitVersioning'
- task: PowerShell@2
displayName: Set Release Version
Expand Down
2 changes: 1 addition & 1 deletion Pipelines/templates/nbgv-set-version-steps.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
steps:
- script: 'dotnet tool install -g nbgv'
- script: 'dotnet tool update -g nbgv'
displayName: 'Install GitVersioning'
- task: PowerShell@2
displayName: Set Release Version
Expand Down
22 changes: 11 additions & 11 deletions src/OSSGadget.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.32014.148
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shared.CLI", "Shared\Shared.CLI.csproj", "{292BEE61-1C30-4EB5-A2DE-810E7118433A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "oss-download", "oss-download\oss-download.csproj", "{DEBC05E9-A5A9-4FFE-9023-1248F2DA58D9}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "oss-defog", "oss-defog\oss-defog.csproj", "{7F44BC11-2DE4-4EC7-8E07-41BD5A3D2734}"
Expand Down Expand Up @@ -40,18 +38,16 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "oss-reproducible", "oss-rep
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "oss-find-squats-lib", "oss-find-squats-lib\oss-find-squats-lib.csproj", "{182CA72B-6BB1-400E-83FC-B35558928BAB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shared.Lib", "Shared-lib\Shared.Lib.csproj", "{E87DAFE2-A060-458F-8924-C946585BF746}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shared.Lib", "Shared\Shared.Lib.csproj", "{0023A885-5255-46A0-BAF7-F419FFA00AE4}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shared.CLI", "Shared.CLI\Shared.CLI.csproj", "{66CE54D2-40AA-41CB-A487-3FE44E38BFE0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{292BEE61-1C30-4EB5-A2DE-810E7118433A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{292BEE61-1C30-4EB5-A2DE-810E7118433A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{292BEE61-1C30-4EB5-A2DE-810E7118433A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{292BEE61-1C30-4EB5-A2DE-810E7118433A}.Release|Any CPU.Build.0 = Release|Any CPU
{DEBC05E9-A5A9-4FFE-9023-1248F2DA58D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DEBC05E9-A5A9-4FFE-9023-1248F2DA58D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DEBC05E9-A5A9-4FFE-9023-1248F2DA58D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -111,10 +107,14 @@ Global
{182CA72B-6BB1-400E-83FC-B35558928BAB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{182CA72B-6BB1-400E-83FC-B35558928BAB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{182CA72B-6BB1-400E-83FC-B35558928BAB}.Release|Any CPU.Build.0 = Release|Any CPU
{E87DAFE2-A060-458F-8924-C946585BF746}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E87DAFE2-A060-458F-8924-C946585BF746}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E87DAFE2-A060-458F-8924-C946585BF746}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E87DAFE2-A060-458F-8924-C946585BF746}.Release|Any CPU.Build.0 = Release|Any CPU
{0023A885-5255-46A0-BAF7-F419FFA00AE4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0023A885-5255-46A0-BAF7-F419FFA00AE4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0023A885-5255-46A0-BAF7-F419FFA00AE4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0023A885-5255-46A0-BAF7-F419FFA00AE4}.Release|Any CPU.Build.0 = Release|Any CPU
{66CE54D2-40AA-41CB-A487-3FE44E38BFE0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{66CE54D2-40AA-41CB-A487-3FE44E38BFE0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{66CE54D2-40AA-41CB-A487-3FE44E38BFE0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{66CE54D2-40AA-41CB-A487-3FE44E38BFE0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
67 changes: 0 additions & 67 deletions src/Shared-lib/Shared-lib.csproj

This file was deleted.

30 changes: 0 additions & 30 deletions src/Shared-lib/nlog.config

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Microsoft.CST.OpenSource
{
using Microsoft.CST.OpenSource.Helpers;
using Microsoft.CST.OpenSource.PackageManagers;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -34,8 +35,15 @@ public PackageDownloader(PackageURL purl, IHttpClientFactory? httpClientFactory,
actualCaching = (doCaching && !string.IsNullOrEmpty(destinationDir) && Directory.Exists(destinationDir));

// if no destination specified, dump the package in the temp directory
destinationDirectory = string.IsNullOrEmpty(destinationDir) ?
Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()) : destinationDir;
if (string.IsNullOrEmpty(destinationDir))
{
usingTemp = true;
destinationDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
}
else
{
destinationDirectory = destinationDir;
}

packageManager = ProjectManagerFactory.CreateProjectManager(purl, httpClientFactory, destinationDirectory);
if (packageManager == null)
Expand All @@ -55,6 +63,19 @@ public PackageDownloader(PackageURL purl, IHttpClientFactory? httpClientFactory,
}
}

/// <summary>
/// Deletes the destination directory for this package downloader if no destination directory was provided to <see cref="PackageDownloader(PackageURL, IHttpClientFactory?, string?, bool)"/>
/// This can be used to clean up the temp folder that will be created when a path was not provided during creation.
/// Note that the downloader will no longer work after calling this method.
/// </summary>
public void DeleteDestinationDirectoryIfTemp()
{
if (usingTemp)
{
FileSystemHelper.RetryDeleteDirectory(destinationDirectory);
}
}

/// <summary>
/// Clears the cache directory
/// </summary>
Expand All @@ -67,7 +88,7 @@ public void ClearPackageLocalCopy()
if (Directory.Exists(packageDirectory))
{
Logger.Trace("Removing directory {0}", packageDirectory);
Directory.Delete(packageDirectory, true);
FileSystemHelper.RetryDeleteDirectory(packageDirectory);
}
}
}
Expand Down Expand Up @@ -256,9 +277,12 @@ public async Task<List<PackageURL>> GetPackageVersionsToProcess(PackageURL purl)

// should we cache/check for the cache?
private readonly bool doCache = false;
private bool usingTemp;

private string destinationDirectory { get; set; }

private bool usingTempDir;

// folders created
private List<string> downloadPaths { get; set; } = new List<string>();

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,12 @@
<PackageReference Include="System.Console" Version="4.3.1" />
</ItemGroup>

<ItemGroup>
<None Update="nlog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<None Include="..\..\LICENSE.txt" Pack="true" PackagePath="" />
<None Include="..\..\icon-128.png" Pack="true" PackagePath="" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Shared-lib\Shared.Lib.csproj" />
<ProjectReference Include="..\Shared\Shared.Lib.csproj" />
</ItemGroup>
</Project>
File renamed without changes.
File renamed without changes.
File renamed without changes.
54 changes: 54 additions & 0 deletions src/Shared/Helpers/FileSystemHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.

namespace Microsoft.CST.OpenSource.Helpers
{
using NLog;
using System;
using System.IO;
using System.Threading;

public static class FileSystemHelper
{
static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();

/// <summary>
/// Attempt to delete a directory and its contents with retries.
/// </summary>
/// <param name="path">The directory to delete.</param>
/// <param name="recursive">If true, also delete all the contents of the directory.</param>
/// <param name="attempts">Number of attempts. Minimum 1.</param>
/// <param name="millisecondsDelay">Delay between attempts. Minimum 1.</param>
/// <returns>If the directory was deleted.</returns>
public static bool RetryDeleteDirectory(string path, bool recursive = true, int attempts = 5, int millisecondsDelay = 10)
{
if (path == null)
return false;
if (attempts < 1)
attempts = 1;
if (millisecondsDelay < 1)
millisecondsDelay = 1;

for (int i = 0; i < attempts; i++)
{
try
{
if (Directory.Exists(path))
{
Directory.Delete(path, recursive);
}
if (!Directory.Exists(path))
{
return true;
}
}
catch(Exception ex) when (ex is IOException)
{
Thread.Sleep(millisecondsDelay);
Logger.Debug("Error deleting [{0}], sleeping for {1} seconds.", path, millisecondsDelay);
}
}

return false;
}
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
73 changes: 0 additions & 73 deletions src/Shared/Shared.csproj

This file was deleted.

File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/oss-characteristics/oss-characteristic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

<ItemGroup>
<ProjectReference Include="..\oss-download\oss-download.csproj" />
<ProjectReference Include="..\Shared\Shared.CLI.csproj" />
<ProjectReference Include="..\Shared.CLI\Shared.CLI.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit a0f6af1

Please sign in to comment.