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

Fix Maven cache move #3898

Merged
merged 3 commits into from
Oct 1, 2024
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
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);
Copy link
Contributor

@dhoehna dhoehna Sep 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if -1 is returned? #ByDesign

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code won't execute if index is -1 here. So we are good.

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);
Copy link
Contributor

@dhoehna dhoehna Sep 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment says Update... but the method name is UpdateOrCreate please update the comment. #ByDesign

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It updates or creates the subkey, not the file itself.

}
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;
Copy link
Contributor

@dhoehna dhoehna Sep 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: Consider making -Dmaven.repo.local = a const string in a common location. #WontFix

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
Loading