Skip to content

Commit

Permalink
Add support for removing temporary files (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdanish-kh committed Aug 23, 2023
1 parent a403833 commit bc99df6
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 6 deletions.
25 changes: 24 additions & 1 deletion doc/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -36,4 +60,3 @@ The `name` setting specifies the name of the targeted GitHub repository. By defa
"name": "winget-pkgs"
}
```

27 changes: 27 additions & 0 deletions src/WingetCreateCLI/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,33 @@ public static class Common
/// </summary>
public static string LocalAppStatePath => AppStatePathLazy.Value;

/// <summary>
/// Cleans up files and folders in a specified directory that are older than the specified number of days.
/// </summary>
/// <param name="cleanUpDirectory">Directory to clean up.</param>
/// <param name="cleanUpDays">The number of days that determine the age of files to be considered for cleanup.</param>
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();
Expand Down
22 changes: 21 additions & 1 deletion src/WingetCreateCLI/Models/SettingsModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ public partial class Telemetry
public bool Disable { get; set; } = false;


}

/// <summary>Controls the clean up interval of installer cache and logs</summary>
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.4.3.0 (Newtonsoft.Json v11.0.0.0)")]
public partial class CleanUp
{
/// <summary>Controls the interval in days for clean up of old files and folders</summary>
[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;

/// <summary>Controls whether clean up is disabled</summary>
[Newtonsoft.Json.JsonProperty("disable", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public bool Disable { get; set; } = false;


}

/// <summary>Windows Package Manager Repository settings</summary>
Expand Down Expand Up @@ -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();


}
}
}
10 changes: 10 additions & 0 deletions src/WingetCreateCLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Microsoft.WingetCreateCLI
{
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using CommandLine;
Expand All @@ -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;

/// <summary>
Expand Down Expand Up @@ -105,6 +107,14 @@ private static async Task<int> 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<object> result)
Expand Down
20 changes: 20 additions & 0 deletions src/WingetCreateCLI/Schemas/settings.schema.0.1.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand Down
29 changes: 29 additions & 0 deletions src/WingetCreateCLI/UserSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,34 @@ public static bool TelemetryDisabled
}
}

/// <summary>
/// Gets or sets a value indicating whether to disable clean up.
/// </summary>
public static bool CleanUpDisabled
{
get => Settings.CleanUp.Disable;

set
{
Settings.CleanUp.Disable = value;
SaveSettings();
}
}

/// <summary>
/// Gets or sets a value indicating the interval in days to clean up old files and directories.
/// </summary>
public static int CleanUpDays
{
get => Settings.CleanUp.IntervalInDays;

set
{
Settings.CleanUp.IntervalInDays = value;
SaveSettings();
}
}

/// <summary>
/// Gets or sets the owner of the winget-pkgs repository.
/// </summary>
Expand Down Expand Up @@ -168,6 +196,7 @@ private static void LoadSettings()
Settings = new SettingsManifest
{
Telemetry = new Models.Settings.Telemetry(),
CleanUp = new CleanUp(),
WindowsPackageManagerRepository = new WindowsPackageManagerRepository(),
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void SetUp()
}

/// <summary>
/// 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.
/// </summary>
[TearDown]
public void TearDown()
Expand Down Expand Up @@ -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.");
}
Expand Down

0 comments on commit bc99df6

Please sign in to comment.