Skip to content

Commit

Permalink
Fix Maven cache move (#3898)
Browse files Browse the repository at this point in the history
* Fix Maven cache move

* Updated settings.xml for Maven cache move

* Addressed PR comments
  • Loading branch information
SohamDas2021 authored Oct 1, 2024
1 parent 13a8cc7 commit 9654c0e
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,8 @@ public void UpdateListViewModelList()
{
CacheName = "Maven cache (Java)",
EnvironmentVariable = "MAVEN_OPTS",
CacheDirectory = new List<string> { Path.Join(_userProfilePath, ".m2") },
ExampleSubDirectory = Path.Join(PackagesStr, "m2"),
CacheDirectory = new List<string> { Path.Join(_userProfilePath, ".m2", "repository") },
ExampleSubDirectory = Path.Join(PackagesStr, "m2", "repository"),
},
new DevDriveCacheData
{
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9654c0e

Please sign in to comment.