From 54afb892d36ff117650741953d713cd657dde248 Mon Sep 17 00:00:00 2001 From: Soham Das Date: Tue, 24 Sep 2024 12:50:33 -0700 Subject: [PATCH 1/3] Fix Maven cache move --- .../OptimizeDevDriveDialogViewModel.cs | 8 +++++ .../ViewModels/DevDriveInsightsViewModel.cs | 34 +++++++++++++++++-- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/tools/Customization/DevHome.Customization/ViewModels/DevDriveInsights/OptimizeDevDriveDialogViewModel.cs b/tools/Customization/DevHome.Customization/ViewModels/DevDriveInsights/OptimizeDevDriveDialogViewModel.cs index 7b45288b6d..0a7723237b 100644 --- a/tools/Customization/DevHome.Customization/ViewModels/DevDriveInsights/OptimizeDevDriveDialogViewModel.cs +++ b/tools/Customization/DevHome.Customization/ViewModels/DevDriveInsights/OptimizeDevDriveDialogViewModel.cs @@ -224,6 +224,14 @@ private void SetEnvironmentVariable(string variableName, string value) { try { + if (string.Equals(variableName, "MAVEN_OPTS", StringComparison.OrdinalIgnoreCase)) + { + var existingValue = Environment.GetEnvironmentVariable(variableName, EnvironmentVariableTarget.User); + var newValue = (!string.IsNullOrEmpty(existingValue) ? existingValue + " " : string.Empty) + "-Dmaven.repo.local = " + value; + Environment.SetEnvironmentVariable(variableName, newValue, EnvironmentVariableTarget.User); + return; + } + Environment.SetEnvironmentVariable(variableName, value, EnvironmentVariableTarget.User); if (string.Equals(variableName, "CARGO_HOME", StringComparison.OrdinalIgnoreCase)) diff --git a/tools/Customization/DevHome.Customization/ViewModels/DevDriveInsightsViewModel.cs b/tools/Customization/DevHome.Customization/ViewModels/DevDriveInsightsViewModel.cs index 8d4419c52b..ba52c1c610 100644 --- a/tools/Customization/DevHome.Customization/ViewModels/DevDriveInsightsViewModel.cs +++ b/tools/Customization/DevHome.Customization/ViewModels/DevDriveInsightsViewModel.cs @@ -320,8 +320,8 @@ public void UpdateListViewModelList() { CacheName = "Maven cache (Java)", EnvironmentVariable = "MAVEN_OPTS", - CacheDirectory = new List { Path.Join(_userProfilePath, ".m2") }, - ExampleSubDirectory = Path.Join(PackagesStr, "m2"), + CacheDirectory = new List { Path.Join(_userProfilePath, ".m2", "repository") }, + ExampleSubDirectory = Path.Join(PackagesStr, "m2", "repository"), }, new DevDriveCacheData { @@ -406,12 +406,40 @@ public void UpdateOptimizerListViewModelList() DevDriveOptimizerLoadingCompleted = true; } + private string GetMovedCacheLocationForMaven(string input) + { + var searchString = "-Dmaven.repo.local = "; + int startIndex = input.IndexOf(searchString, StringComparison.OrdinalIgnoreCase); + if (startIndex == -1) + { + return string.Empty; // search substring not found + } + + startIndex += searchString.Length; + int endIndex = input.IndexOf(' ', startIndex); + if (endIndex == -1) + { + endIndex = input.Length; // No space found, take till end of string + } + + return input.Substring(startIndex, endIndex - startIndex); + } + public void UpdateOptimizedListViewModelList() { foreach (var cache in _cacheInfo) { - // We retrieve the cache location from environment variable, because if the cache might have already moved. + // We retrieve the cache location from environment variable, because the cache might have already moved. var movedCacheLocation = Environment.GetEnvironmentVariable(cache.EnvironmentVariable!, EnvironmentVariableTarget.User); + + // Note that for Maven cache, the environment variable is in the format "-Dmaven.repo.local = E:\packages\m2\repository" + // So we have to extract the cache location accordingly + if (string.Equals(cache.EnvironmentVariable!, "MAVEN_OPTS", StringComparison.OrdinalIgnoreCase) && !string.IsNullOrEmpty(movedCacheLocation)) + { + var movedCacheLocationForMaven = GetMovedCacheLocationForMaven(movedCacheLocation); + movedCacheLocation = movedCacheLocationForMaven; + } + if (!string.IsNullOrEmpty(movedCacheLocation) && CacheInDevDrive(movedCacheLocation)) { // Cache already in dev drive, show the "Optimized" card From 3dceb4d1b5716224421fa7f7d5f92591633fce78 Mon Sep 17 00:00:00 2001 From: Soham Das Date: Mon, 30 Sep 2024 13:46:45 -0700 Subject: [PATCH 2/3] Updated settings.xml for Maven cache move --- .../OptimizeDevDriveDialogViewModel.cs | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/tools/Customization/DevHome.Customization/ViewModels/DevDriveInsights/OptimizeDevDriveDialogViewModel.cs b/tools/Customization/DevHome.Customization/ViewModels/DevDriveInsights/OptimizeDevDriveDialogViewModel.cs index 0a7723237b..b77585b9ae 100644 --- a/tools/Customization/DevHome.Customization/ViewModels/DevDriveInsights/OptimizeDevDriveDialogViewModel.cs +++ b/tools/Customization/DevHome.Customization/ViewModels/DevDriveInsights/OptimizeDevDriveDialogViewModel.cs @@ -3,8 +3,11 @@ using System; using System.Collections.Generic; +using System.Data.SqlTypes; using System.IO; using System.Threading.Tasks; +using System.Xml; +using System.Xml.Linq; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Messaging; @@ -220,6 +223,73 @@ private void UpdatePathEnvironmentVariable(string value) } } + private void UpdateOrCreateSubkey(string xmlFilePath, string key, string subkey, string newValue) + { + XmlDocument doc = new XmlDocument(); + doc.Load(xmlFilePath); + + XmlNode? keyNode = doc.SelectSingleNode($"{key}"); + if (keyNode != null) + { + XmlNode? subkeyNode = keyNode.SelectSingleNode(subkey); + if (subkeyNode != null) + { + // Update the subkey value + subkeyNode.InnerText = newValue; + } + else + { + // Create the subkey with the new value + XmlElement newSubkey = doc.CreateElement(subkey); + newSubkey.InnerText = newValue; + keyNode.AppendChild(newSubkey); + } + } + else + { + // Create the key and subkey with the new value + XmlElement newKey = doc.CreateElement(key); + XmlElement newSubkey = doc.CreateElement(subkey); + newSubkey.InnerText = newValue; + newKey.AppendChild(newSubkey); + if (doc.DocumentElement != null) + { + doc.DocumentElement.AppendChild(newKey); + } + } + + doc.Save(xmlFilePath); + } + + private void UpdateSettingsXML(string localRepositoryLocation) + { + int index = ExistingCacheLocation.IndexOf("repository", StringComparison.OrdinalIgnoreCase); + var settingsXMLPath = string.Concat(this.ExistingCacheLocation.AsSpan(0, index), "settings.xml"); + var key = "settings"; + var subkey = "localRepository"; + + if (File.Exists(settingsXMLPath)) + { + // Update settings.xml with the new repository path + UpdateOrCreateSubkey(settingsXMLPath, key, subkey, localRepositoryLocation); + } + else + { + // Create the root element + XElement root = new XElement(key); + + // Add child element + root.Add(new XElement(subkey, localRepositoryLocation)); + + // Save the XML document to the specified file path + XDocument doc = new XDocument(root); + if (index >= 0) + { + doc.Save(settingsXMLPath); + } + } + } + private void SetEnvironmentVariable(string variableName, string value) { try @@ -229,6 +299,7 @@ private void SetEnvironmentVariable(string variableName, string value) var existingValue = Environment.GetEnvironmentVariable(variableName, EnvironmentVariableTarget.User); var newValue = (!string.IsNullOrEmpty(existingValue) ? existingValue + " " : string.Empty) + "-Dmaven.repo.local = " + value; Environment.SetEnvironmentVariable(variableName, newValue, EnvironmentVariableTarget.User); + UpdateSettingsXML(value); return; } From 23c6c5b998df157eff228848cbd6ccc1f847dd6e Mon Sep 17 00:00:00 2001 From: Soham Das Date: Tue, 1 Oct 2024 10:55:09 -0700 Subject: [PATCH 3/3] Addressed PR comments --- .../DevDriveInsights/OptimizeDevDriveDialogViewModel.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/Customization/DevHome.Customization/ViewModels/DevDriveInsights/OptimizeDevDriveDialogViewModel.cs b/tools/Customization/DevHome.Customization/ViewModels/DevDriveInsights/OptimizeDevDriveDialogViewModel.cs index b77585b9ae..2a00d80b2c 100644 --- a/tools/Customization/DevHome.Customization/ViewModels/DevDriveInsights/OptimizeDevDriveDialogViewModel.cs +++ b/tools/Customization/DevHome.Customization/ViewModels/DevDriveInsights/OptimizeDevDriveDialogViewModel.cs @@ -264,7 +264,7 @@ private void UpdateOrCreateSubkey(string xmlFilePath, string key, string subkey, private void UpdateSettingsXML(string localRepositoryLocation) { int index = ExistingCacheLocation.IndexOf("repository", StringComparison.OrdinalIgnoreCase); - var settingsXMLPath = string.Concat(this.ExistingCacheLocation.AsSpan(0, index), "settings.xml"); + var settingsXMLPath = string.Concat(ExistingCacheLocation.AsSpan(0, index), "settings.xml"); var key = "settings"; var subkey = "localRepository";