diff --git a/doc/settings.md b/doc/settings.md
index 1b0b7d13..b0a22b8b 100644
--- a/doc/settings.md
+++ b/doc/settings.md
@@ -19,6 +19,30 @@ See [details on telemetry](https://github.com/microsoft/winget-create#datateleme
If set to true, the `telemetry.disable` setting will prevent any event from being written by the program.
+## CleanUp
+
+The `CleanUp` settings determine whether Winget-Create will handle the removal of temporary files (installer cache and logs) generated during the manifest creation process. These settings provide control over the decision to remove files or not and the frequency at which this clean up occurs.
+
+### disable
+
+```json
+ "CleanUp": {
+ "disable": true
+ },
+```
+
+If set to true, the `CleanUp.disable` setting will prevent any temporary files from being removed by the program.
+
+### intervalInDays
+
+```json
+ "CleanUp": {
+ "intervalInDays": 7
+ },
+```
+
+The `intervalInDays` setting specifies how often Winget-Create will remove temporary files. By default, this is set to 7 days.
+
## WindowsPackageManagerRepository
The `WindowsPackageManagerRepository` setting specifies which repository Winget-Create targets. By default, this setting targets the main [`microsoft/winget-pkgs`](https://github.com/microsoft/winget-pkgs) repository but can be changed to target a forked copy of the main repository like a [test](https://github.com/microsoft/winget-pkgs-submission-test) or private production repository.
@@ -36,4 +60,3 @@ The `name` setting specifies the name of the targeted GitHub repository. By defa
"name": "winget-pkgs"
}
```
-
diff --git a/src/WingetCreateCLI/Common.cs b/src/WingetCreateCLI/Common.cs
index b3ba6c5a..2cd7b5c2 100644
--- a/src/WingetCreateCLI/Common.cs
+++ b/src/WingetCreateCLI/Common.cs
@@ -28,6 +28,33 @@ public static class Common
///
public static string LocalAppStatePath => AppStatePathLazy.Value;
+ ///
+ /// Cleans up files and folders in a specified directory that are older than the specified number of days.
+ ///
+ /// Directory to clean up.
+ /// The number of days that determine the age of files to be considered for cleanup.
+ public static void CleanUpFilesOlderThan(string cleanUpDirectory, int cleanUpDays)
+ {
+ var logDirectory = new DirectoryInfo(cleanUpDirectory);
+ var files = logDirectory.GetFiles();
+ foreach (var file in files)
+ {
+ if (file.CreationTime < DateTime.Now.AddDays(-cleanUpDays))
+ {
+ file.Delete();
+ }
+ }
+
+ var directories = logDirectory.GetDirectories();
+ foreach (var directory in directories)
+ {
+ if (directory.CreationTime < DateTime.Now.AddDays(-cleanUpDays))
+ {
+ directory.Delete(true);
+ }
+ }
+ }
+
private static bool IsRunningAsUwp()
{
DesktopBridge.Helpers helpers = new DesktopBridge.Helpers();
diff --git a/src/WingetCreateCLI/Models/SettingsModel.cs b/src/WingetCreateCLI/Models/SettingsModel.cs
index a4d2845f..b1a0643f 100644
--- a/src/WingetCreateCLI/Models/SettingsModel.cs
+++ b/src/WingetCreateCLI/Models/SettingsModel.cs
@@ -17,6 +17,22 @@ public partial class Telemetry
public bool Disable { get; set; } = false;
+ }
+
+ /// Controls the clean up interval of installer cache and logs
+ [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.4.3.0 (Newtonsoft.Json v11.0.0.0)")]
+ public partial class CleanUp
+ {
+ /// Controls the interval in days for clean up of old files and folders
+ [Newtonsoft.Json.JsonProperty("intervalInDays", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
+ [System.ComponentModel.DataAnnotations.Range(1, int.MaxValue)]
+ public int IntervalInDays { get; set; } = 7;
+
+ /// Controls whether clean up is disabled
+ [Newtonsoft.Json.JsonProperty("disable", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
+ public bool Disable { get; set; } = false;
+
+
}
/// Windows Package Manager Repository settings
@@ -45,10 +61,14 @@ public partial class SettingsManifest
[System.ComponentModel.DataAnnotations.Required]
public Telemetry Telemetry { get; set; } = new Telemetry();
+ [Newtonsoft.Json.JsonProperty("CleanUp", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
+ [System.ComponentModel.DataAnnotations.Required]
+ public CleanUp CleanUp { get; set; } = new CleanUp();
+
[Newtonsoft.Json.JsonProperty("WindowsPackageManagerRepository", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
[System.ComponentModel.DataAnnotations.Required]
public WindowsPackageManagerRepository WindowsPackageManagerRepository { get; set; } = new WindowsPackageManagerRepository();
}
-}
\ No newline at end of file
+}
diff --git a/src/WingetCreateCLI/Program.cs b/src/WingetCreateCLI/Program.cs
index 70b04b4f..33f83e03 100644
--- a/src/WingetCreateCLI/Program.cs
+++ b/src/WingetCreateCLI/Program.cs
@@ -4,6 +4,7 @@
namespace Microsoft.WingetCreateCLI
{
using System;
+ using System.IO;
using System.Linq;
using System.Threading.Tasks;
using CommandLine;
@@ -13,6 +14,7 @@ namespace Microsoft.WingetCreateCLI
using Microsoft.WingetCreateCLI.Properties;
using Microsoft.WingetCreateCLI.Telemetry;
using Microsoft.WingetCreateCLI.Telemetry.Events;
+ using Microsoft.WingetCreateCore;
using Microsoft.WingetCreateCore.Common;
///
@@ -105,6 +107,14 @@ private static async Task Main(string[] args)
Logger.Error(ex.ToString());
return 1;
}
+ finally
+ {
+ if (!UserSettings.CleanUpDisabled)
+ {
+ Common.CleanUpFilesOlderThan(PackageParser.InstallerDownloadPath, UserSettings.CleanUpDays);
+ Common.CleanUpFilesOlderThan(Path.Combine(Common.LocalAppStatePath, "DiagOutputDir"), UserSettings.CleanUpDays);
+ }
+ }
}
private static void DisplayHelp(NotParsed