From 85f92378f7b7542c9723972de23dcb20d1346423 Mon Sep 17 00:00:00 2001 From: Muhammad Danish Date: Sat, 12 Aug 2023 00:29:50 +0500 Subject: [PATCH 1/4] Add support for removing temporary files --- doc/settings.md | 25 ++++++++++++++- src/WingetCreateCLI/Common.cs | 31 +++++++++++++++++++ src/WingetCreateCLI/Models/SettingsModel.cs | 22 ++++++++++++- src/WingetCreateCLI/Program.cs | 7 +++++ .../Schemas/settings.schema.0.1.json | 20 ++++++++++++ src/WingetCreateCLI/UserSettings.cs | 29 +++++++++++++++++ .../UnitTests/SettingsCommandTests.cs | 14 ++++++--- 7 files changed, 142 insertions(+), 6 deletions(-) diff --git a/doc/settings.md b/doc/settings.md index 1b0b7d13..ec3c28cc 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..796bb8e3 100644 --- a/src/WingetCreateCLI/Common.cs +++ b/src/WingetCreateCLI/Common.cs @@ -28,6 +28,37 @@ public static class Common /// public static string LocalAppStatePath => AppStatePathLazy.Value; + /// + /// Initiates the clean up process for the given directory. + /// + /// Directory to clean up. + public static void BeginCleanUp(string cleanUpDirectory) + { + if (UserSettings.CleanUpDisabled) + { + return; + } + + var logDirectory = new DirectoryInfo(cleanUpDirectory); + var files = logDirectory.GetFiles(); + foreach (var file in files) + { + if (file.CreationTime < DateTime.Now.AddDays(-UserSettings.CleanUpDays)) + { + file.Delete(); + } + } + + var directories = logDirectory.GetDirectories(); + foreach (var directory in directories) + { + if (directory.CreationTime < DateTime.Now.AddDays(-UserSettings.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 50755212..affb0482 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; /// @@ -96,6 +98,11 @@ private static async Task Main(string[] args) Logger.Error(ex.ToString()); return 1; } + finally + { + Common.BeginCleanUp(PackageParser.InstallerDownloadPath); + Common.BeginCleanUp(Path.Combine(Common.LocalAppStatePath, "DiagOutputDir")); + } } 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."); } From 913dcea548964b119dd7f017848e4368b38631cc Mon Sep 17 00:00:00 2001 From: Muhammad Danish Date: Mon, 14 Aug 2023 20:49:33 +0500 Subject: [PATCH 2/4] formatting fix --- doc/settings.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/settings.md b/doc/settings.md index ec3c28cc..b0a22b8b 100644 --- a/doc/settings.md +++ b/doc/settings.md @@ -21,7 +21,7 @@ If set to true, the `telemetry.disable` setting will prevent any event from bein ## 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. +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 From d5ecb5ab494358d0f0bc91cc66e412955fffe00a Mon Sep 17 00:00:00 2001 From: Muhammad Danish Date: Wed, 23 Aug 2023 21:21:23 +0500 Subject: [PATCH 3/4] address PR feedback --- src/WingetCreateCLI/Common.cs | 14 +++++--------- src/WingetCreateCLI/Program.cs | 7 +++++-- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/WingetCreateCLI/Common.cs b/src/WingetCreateCLI/Common.cs index 796bb8e3..7aac3d01 100644 --- a/src/WingetCreateCLI/Common.cs +++ b/src/WingetCreateCLI/Common.cs @@ -29,21 +29,17 @@ public static class Common public static string LocalAppStatePath => AppStatePathLazy.Value; /// - /// Initiates the clean up process for the given directory. + /// Cleans up files and folders in a specified directory that are older than the specified number of days. /// /// Directory to clean up. - public static void BeginCleanUp(string cleanUpDirectory) + /// Number of days to keep files. + public static void CleanUpFilesOlderThan(string cleanUpDirectory, int cleanUpDays) { - if (UserSettings.CleanUpDisabled) - { - return; - } - var logDirectory = new DirectoryInfo(cleanUpDirectory); var files = logDirectory.GetFiles(); foreach (var file in files) { - if (file.CreationTime < DateTime.Now.AddDays(-UserSettings.CleanUpDays)) + if (file.CreationTime < DateTime.Now.AddDays(-cleanUpDays)) { file.Delete(); } @@ -52,7 +48,7 @@ public static void BeginCleanUp(string cleanUpDirectory) var directories = logDirectory.GetDirectories(); foreach (var directory in directories) { - if (directory.CreationTime < DateTime.Now.AddDays(-UserSettings.CleanUpDays)) + if (directory.CreationTime < DateTime.Now.AddDays(-cleanUpDays)) { directory.Delete(true); } diff --git a/src/WingetCreateCLI/Program.cs b/src/WingetCreateCLI/Program.cs index affb0482..ab626c16 100644 --- a/src/WingetCreateCLI/Program.cs +++ b/src/WingetCreateCLI/Program.cs @@ -100,8 +100,11 @@ private static async Task Main(string[] args) } finally { - Common.BeginCleanUp(PackageParser.InstallerDownloadPath); - Common.BeginCleanUp(Path.Combine(Common.LocalAppStatePath, "DiagOutputDir")); + if (!UserSettings.CleanUpDisabled) + { + Common.CleanUpFilesOlderThan(PackageParser.InstallerDownloadPath, UserSettings.CleanUpDays); + Common.CleanUpFilesOlderThan(Path.Combine(Common.LocalAppStatePath, "DiagOutputDir"), UserSettings.CleanUpDays); + } } } From 43730cdfc86cc98fe86ec1934b128283c4f3315d Mon Sep 17 00:00:00 2001 From: Muhammad Danish Date: Wed, 23 Aug 2023 21:25:54 +0500 Subject: [PATCH 4/4] description --- src/WingetCreateCLI/Common.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/WingetCreateCLI/Common.cs b/src/WingetCreateCLI/Common.cs index 7aac3d01..2cd7b5c2 100644 --- a/src/WingetCreateCLI/Common.cs +++ b/src/WingetCreateCLI/Common.cs @@ -32,7 +32,7 @@ public static class Common /// Cleans up files and folders in a specified directory that are older than the specified number of days. /// /// Directory to clean up. - /// Number of days to keep files. + /// 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);