diff --git a/tools/Customization/DevHome.Customization/ViewModels/DevDriveInsights/OptimizeDevDriveDialogViewModel.cs b/tools/Customization/DevHome.Customization/ViewModels/DevDriveInsights/OptimizeDevDriveDialogViewModel.cs index 7b45288b6d..2a00d80b2c 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,10 +223,86 @@ 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(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 { + 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); + UpdateSettingsXML(value); + 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