Skip to content

Commit

Permalink
[Dev Drive Insights] Move %userprofile$/.rustup and set RUSTUP_HOME a…
Browse files Browse the repository at this point in the history
…s part of optimizing Cargo cache (#3827)

* Working solution

* Addressed PR comments

* Addressed PR comments
  • Loading branch information
SohamDas2021 authored Sep 12, 2024
1 parent ca1ca04 commit 92c6eba
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ public static IServiceCollection AddWindowsCustomization(this IServiceCollection
services.AddTransient<FileExplorerPage>();

services.AddSingleton<OptimizeDevDriveDialogViewModelFactory>(sp =>
(cacheLocation, environmentVariable, exampleDevDriveLocation, existingDevDriveLetters) =>
ActivatorUtilities.CreateInstance<OptimizeDevDriveDialogViewModel>(sp, cacheLocation, environmentVariable, exampleDevDriveLocation, existingDevDriveLetters));
(cacheLocation, environmentVariable, exampleDevDriveLocation, existingDevDriveLetters, relatedEnvironmentVariablesToBeSet, relatedCacheDirectories) =>
ActivatorUtilities.CreateInstance<OptimizeDevDriveDialogViewModel>(sp, cacheLocation, environmentVariable, exampleDevDriveLocation, existingDevDriveLetters, relatedEnvironmentVariablesToBeSet, relatedCacheDirectories));

services.AddSingleton<DevDriveInsightsViewModel>();
services.AddTransient<DevDriveInsightsPage>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ public partial class DevDriveCacheData

public string? CacheName { get; set; }

public List<string>? CacheDirectory { get; set; }
public List<string>? CacheDirectory { get; set; }

public List<string>? RelatedCacheDirectories { get; set; } = new();

public List<string>? RelatedEnvironmentVariables { get; set; } = new();

public string? ExampleSubDirectory { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public partial class DevDriveOptimizerCardViewModel : ObservableObject

public string MakeTheChangeText { get; set; }

public List<string> RelatedEnvironmentVariablesToBeSet { get; set; }

public List<string> RelatedCacheDirectories { get; set; }

/// <summary>
/// User wants to optimize a dev drive.
/// </summary>
Expand All @@ -48,7 +52,9 @@ private async Task OptimizeDevDriveAsync(object sender)
ExistingCacheLocation,
EnvironmentVariableToBeSet,
ExampleLocationOnDevDrive,
ExistingDevDriveLetters);
ExistingDevDriveLetters,
RelatedEnvironmentVariablesToBeSet,
RelatedCacheDirectories);
var optimizeDevDriveDialog = new OptimizeDevDriveDialog(optimizeDevDriveViewModel);
optimizeDevDriveDialog.XamlRoot = settingsCard.XamlRoot;
optimizeDevDriveDialog.RequestedTheme = settingsCard.ActualTheme;
Expand All @@ -63,14 +69,18 @@ public DevDriveOptimizerCardViewModel(
string exampleLocationOnDevDrive,
string environmentVariableToBeSet,
List<string> existingDevDriveLetters,
bool environmentVariableHasValue)
bool environmentVariableHasValue,
List<string> relatedEnvironmentVariablesToBeSet,
List<string> relatedCacheDirectories)
{
OptimizeDevDriveDialogViewModelFactory = optimizeDevDriveDialogViewModelFactory;
ExistingDevDriveLetters = existingDevDriveLetters;
CacheToBeMoved = cacheToBeMoved;
ExistingCacheLocation = existingCacheLocation;
ExampleLocationOnDevDrive = exampleLocationOnDevDrive;
EnvironmentVariableToBeSet = environmentVariableToBeSet;
RelatedEnvironmentVariablesToBeSet = relatedEnvironmentVariablesToBeSet;
RelatedCacheDirectories = relatedCacheDirectories;
var stringResource = new StringResource("DevHome.Customization.pri", "DevHome.Customization/Resources");

if (environmentVariableHasValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,17 @@ public partial class OptimizeDevDriveDialogViewModel : ObservableObject
[ObservableProperty]
private bool _isNotDevDrive;

private List<string> RelatedEnvironmentVariablesToBeSet { get; set; } = new List<string>();

private List<string> RelatedCacheDirectories { get; set; } = new List<string>();

public OptimizeDevDriveDialogViewModel(
string existingCacheLocation,
string environmentVariableToBeSet,
string exampleDevDriveLocation,
List<string> existingDevDriveLetters)
List<string> existingDevDriveLetters,
List<string> relatedEnvironmentVariablesToBeSet,
List<string> relatedCacheDirectories)
{
var stringResource = new StringResource("DevHome.Customization.pri", "DevHome.Customization/Resources");
ExistingDevDriveLetters = existingDevDriveLetters;
Expand All @@ -75,6 +81,8 @@ public OptimizeDevDriveDialogViewModel(
IsPrimaryButtonEnabled = true;
ErrorMessage = string.Empty;
IsNotDevDrive = false;
RelatedEnvironmentVariablesToBeSet = relatedEnvironmentVariablesToBeSet;
RelatedCacheDirectories = relatedCacheDirectories;
}

[RelayCommand]
Expand Down Expand Up @@ -244,6 +252,32 @@ private bool ChosenDirectoryInDevDrive(string directoryPath)
return false;
}

private bool MoveDirectories(string sourceDirectory, string targetDirectory, List<string> relatedCacheDirectories)
{
// TODO: If in future we support some cache with multiple relatedCacheDirectories, we should consider using Parallel.ForEachAsync
foreach (var relatedCacheDirectory in relatedCacheDirectories)
{
var relatedCacheDirectoryName = Path.GetFileName(relatedCacheDirectory);
if (!MoveDirectory(relatedCacheDirectory, $@"{targetDirectory}\Related Directories\{relatedCacheDirectoryName}"))
{
return false;
}
}

return MoveDirectory(sourceDirectory, targetDirectory);
}

private void SetRelatedEnvironmentVariables(List<string> relatedEnvironmentVariablesToBeSet, List<string> relatedCacheDirectories, string directoryPath)
{
var index = 0;
foreach (var relatedEnvironmentVariableToBeSet in relatedEnvironmentVariablesToBeSet)
{
var relatedCacheDirectoryName = Path.GetFileName(relatedCacheDirectories[index]);
SetEnvironmentVariable(relatedEnvironmentVariableToBeSet, $@"{directoryPath}\Related Directories\{relatedCacheDirectoryName}");
index++;
}
}

[RelayCommand]
private void DirectoryInputConfirmed()
{
Expand All @@ -255,8 +289,9 @@ private void DirectoryInputConfirmed()
{
Task.Run(() =>
{
if (MoveDirectory(ExistingCacheLocation, directoryPath))
if (MoveDirectories(ExistingCacheLocation, directoryPath, RelatedCacheDirectories))
{
SetRelatedEnvironmentVariables(RelatedEnvironmentVariablesToBeSet, RelatedCacheDirectories, directoryPath);
SetEnvironmentVariable(EnvironmentVariableToBeSet, directoryPath);
var existingCacheLocationVetted = RemovePrivacyInfo(ExistingCacheLocation);
Log.Debug($"Moved cache from {existingCacheLocationVetted} to {directoryPath}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ public void UpdateListViewModelList()
CacheName = "Cargo cache (Rust)",
EnvironmentVariable = "CARGO_HOME",
CacheDirectory = new List<string> { Path.Join(_userProfilePath, ".cargo") },
RelatedEnvironmentVariables = new List<string> { "RUSTUP_HOME" },
RelatedCacheDirectories = new List<string> { Path.Join(_userProfilePath, ".rustup") },
ExampleSubDirectory = Path.Join(PackagesStr, "cargo"),
},
new DevDriveCacheData
Expand Down Expand Up @@ -395,7 +397,9 @@ public void UpdateOptimizerListViewModelList()
exampleDirectory!, // example location on dev drive to move cache to
cache.EnvironmentVariable!, // environmentVariableToBeSet
existingDevDriveLetters,
!string.IsNullOrEmpty(environmentVariablePath));
!string.IsNullOrEmpty(environmentVariablePath),
cache.RelatedEnvironmentVariables!,
cache.RelatedCacheDirectories!);
DevDriveOptimizerCardCollection.Add(card);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ public delegate OptimizeDevDriveDialogViewModel OptimizeDevDriveDialogViewModelF
string existingCacheLocation,
string environmentVariableToBeSet,
string exampleDevDriveLocation,
List<string> existingDevDriveLetters);
List<string> existingDevDriveLetters,
List<string> relatedEnvironmentVariablesToBeSet,
List<string> relatedCacheDirectories);

public sealed partial class OptimizeDevDriveDialog : ContentDialog
{
Expand Down

0 comments on commit 92c6eba

Please sign in to comment.