Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for removing temporary files #427

Merged
merged 7 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
mdanish-kh marked this conversation as resolved.
Show resolved Hide resolved
},
```

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