From bc99df6a49231ad3ab8eced554fff76033550aa1 Mon Sep 17 00:00:00 2001 From: Muhammad Danish Date: Wed, 23 Aug 2023 21:51:20 +0500 Subject: [PATCH] Add support for removing temporary files (#427) --- doc/settings.md | 25 +++++++++++++++- src/WingetCreateCLI/Common.cs | 27 +++++++++++++++++ src/WingetCreateCLI/Models/SettingsModel.cs | 22 +++++++++++++- src/WingetCreateCLI/Program.cs | 10 +++++++ .../Schemas/settings.schema.0.1.json | 20 +++++++++++++ src/WingetCreateCLI/UserSettings.cs | 29 +++++++++++++++++++ .../UnitTests/SettingsCommandTests.cs | 14 ++++++--- 7 files changed, 141 insertions(+), 6 deletions(-) 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 result) diff --git a/src/WingetCreateCLI/Schemas/settings.schema.0.1.json b/src/WingetCreateCLI/Schemas/settings.schema.0.1.json index 239ba024..13d63433 100644 --- a/src/WingetCreateCLI/Schemas/settings.schema.0.1.json +++ b/src/WingetCreateCLI/Schemas/settings.schema.0.1.json @@ -15,6 +15,24 @@ }, "additionalProperties": false }, + "CleanUp": { + "description": "Controls the clean up interval of installer cache and logs", + "type": "object", + "properties": { + "intervalInDays": { + "description": "Controls the interval in days for clean up of old files and folders", + "type": "integer", + "default": 7, + "minimum": 1 + }, + "disable" : { + "description": "Controls whether clean up is disabled", + "type": "boolean", + "default": false + } + }, + "additionalProperties": false + }, "WindowsPackageManagerRepository": { "description": "Windows Package Manager Repository settings", "type": "object", @@ -41,10 +59,12 @@ "default": "https://aka.ms/wingetcreate-settings.schema.0.1.json" }, "Telemetry": { "$ref": "#/definitions/Telemetry" }, + "CleanUp": { "$ref": "#/definitions/CleanUp" }, "WindowsPackageManagerRepository": { "$ref": "#/definitions/WindowsPackageManagerRepository" } }, "required": [ "Telemetry", + "CleanUp", "WindowsPackageManagerRepository" ], "additionalProperties": false diff --git a/src/WingetCreateCLI/UserSettings.cs b/src/WingetCreateCLI/UserSettings.cs index ae26be6f..908b4016 100644 --- a/src/WingetCreateCLI/UserSettings.cs +++ b/src/WingetCreateCLI/UserSettings.cs @@ -49,6 +49,34 @@ public static bool TelemetryDisabled } } + /// + /// Gets or sets a value indicating whether to disable clean up. + /// + public static bool CleanUpDisabled + { + get => Settings.CleanUp.Disable; + + set + { + Settings.CleanUp.Disable = value; + SaveSettings(); + } + } + + /// + /// Gets or sets a value indicating the interval in days to clean up old files and directories. + /// + public static int CleanUpDays + { + get => Settings.CleanUp.IntervalInDays; + + set + { + Settings.CleanUp.IntervalInDays = value; + SaveSettings(); + } + } + /// /// Gets or sets the owner of the winget-pkgs repository. /// @@ -168,6 +196,7 @@ private static void LoadSettings() Settings = new SettingsManifest { Telemetry = new Models.Settings.Telemetry(), + CleanUp = new CleanUp(), WindowsPackageManagerRepository = new WindowsPackageManagerRepository(), }; } diff --git a/src/WingetCreateTests/WingetCreateTests/UnitTests/SettingsCommandTests.cs b/src/WingetCreateTests/WingetCreateTests/UnitTests/SettingsCommandTests.cs index 2bd0b551..51e7b659 100644 --- a/src/WingetCreateTests/WingetCreateTests/UnitTests/SettingsCommandTests.cs +++ b/src/WingetCreateTests/WingetCreateTests/UnitTests/SettingsCommandTests.cs @@ -39,7 +39,7 @@ public void SetUp() } /// - /// TearDown method that resets the winget-pkts repo owner and name to their default settings. + /// TearDown method that resets the winget-pkgs repo owner and name to their default settings. /// [TearDown] public void TearDown() @@ -94,14 +94,20 @@ public void VerifyEmptySettingsFile() [Test] public void VerifySavingTelemetrySettings() { - bool isDisabled = UserSettings.TelemetryDisabled; + bool isTelemetryDisabled = UserSettings.TelemetryDisabled; + bool isCleanUpDisabled = UserSettings.CleanUpDisabled; + int cleanUpDays = 30; string testRepoOwner = "testRepoOwner"; string testRepoName = "testRepoName"; - UserSettings.TelemetryDisabled = !isDisabled; + UserSettings.TelemetryDisabled = !isTelemetryDisabled; + UserSettings.CleanUpDisabled = !isCleanUpDisabled; + UserSettings.CleanUpDays = cleanUpDays; UserSettings.WindowsPackageManagerRepositoryOwner = testRepoOwner; UserSettings.WindowsPackageManagerRepositoryName = testRepoName; UserSettings.ParseJsonFile(UserSettings.SettingsJsonPath, out SettingsManifest manifest); - Assert.IsTrue(manifest.Telemetry.Disable == !isDisabled, "Changed Telemetry setting was not reflected in the settings file."); + Assert.IsTrue(manifest.Telemetry.Disable == !isTelemetryDisabled, "Changed Telemetry setting was not reflected in the settings file."); + Assert.IsTrue(manifest.CleanUp.Disable == !isCleanUpDisabled, "Changed CleanUp.Disable setting was not reflected in the settings file."); + Assert.IsTrue(manifest.CleanUp.IntervalInDays == cleanUpDays, "Changed CleanUp.IntervalInDays setting was not reflected in the settings file."); Assert.IsTrue(manifest.WindowsPackageManagerRepository.Owner == testRepoOwner, "Changed WindowsPackageManagerRepository.Owner setting was not reflected in the settings file."); Assert.IsTrue(manifest.WindowsPackageManagerRepository.Name == testRepoName, "Changed WindowsPackageManagerRepository.Name setting was not reflected in the settings file."); }