-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Java.Interop.Maven] Initial commit.
- Loading branch information
Showing
22 changed files
with
7,656 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Java.Interop.Maven.Models; | ||
|
||
namespace Java.Interop.Maven; | ||
|
||
public class DefaultPomResolver : IPomResolver | ||
{ | ||
readonly Dictionary<string, Project> poms = new (); | ||
|
||
public void Register (Project project) | ||
{ | ||
poms.Add (project.ToString (), project); | ||
} | ||
|
||
public virtual Project ResolveRawProject (Artifact artifact) | ||
{ | ||
if (poms.TryGetValue (artifact.ToString (), out var project)) | ||
return project; | ||
|
||
throw new InvalidOperationException ($"No POM registered for {artifact}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
using Java.Interop.Maven.Models; | ||
|
||
namespace Java.Interop.Maven.Extensions; | ||
|
||
static class MavenNetExtensions | ||
{ | ||
public static bool HasValue ([NotNullWhen (true)] this string? str) => !string.IsNullOrEmpty (str); | ||
|
||
public static string OrEmpty (this string? str) => str ?? string.Empty; | ||
|
||
public static string GetInheritedProperty (this ResolvedDependency dependency, ResolvedProject project, Func<ResolvedDependency, string?> property) | ||
{ | ||
// Check our <dependencyManagement> section | ||
if (CheckDependencyManagementSection (project, dependency, property, out var result)) | ||
return result; | ||
|
||
// Check imported POMs | ||
foreach (var imported in project.ImportedPomProjects) { | ||
var value = GetInheritedProperty (dependency, imported, property); | ||
|
||
if (value.HasValue ()) | ||
return value; | ||
} | ||
|
||
// Check parent POM | ||
if (project.Parent is not null && !project.Parent.IsSuperPom) | ||
return GetInheritedProperty (dependency, project.Parent, property); | ||
|
||
return string.Empty; | ||
} | ||
|
||
static bool CheckImportedPoms (ResolvedDependency dependency, ResolvedProject project, Func<ResolvedDependency, string?> property, [NotNullWhen (true)] out string? result) | ||
{ | ||
result = null; | ||
|
||
foreach (var imported in project.ImportedPomProjects) { | ||
var imported_dep = imported.Resolved.DependencyManagement?.Dependencies.FirstOrDefault (x => x.ArtifactId == dependency.ArtifactId && x.GroupId == dependency.GroupId); | ||
|
||
if (imported_dep != null) { | ||
result = property (new ResolvedDependency (imported, imported_dep, true)); | ||
|
||
if (result.HasValue ()) | ||
return true; | ||
} | ||
|
||
// Recurse, as imported POMs can also import POMs | ||
if (CheckImportedPoms (dependency, imported, property, out result)) | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
static bool CheckDependencyManagementSection (ResolvedProject project, ResolvedDependency dependency, Func<ResolvedDependency, string?> property, [NotNullWhen (true)] out string? result) | ||
{ | ||
result = null; | ||
|
||
// Check <dependencyManagement> | ||
var dep_man = project.Resolved.DependencyManagement?.Dependencies.FirstOrDefault (x => x.ArtifactId == dependency.ArtifactId && x.GroupId == dependency.GroupId); | ||
|
||
if (dep_man != null) { | ||
result = property (new ResolvedDependency (project, dep_man, true)) ?? string.Empty; | ||
return result.HasValue (); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public static Artifact ToArtifact (this Dependency dependency) | ||
{ | ||
return new Artifact (dependency.GroupId.OrEmpty (), dependency.ArtifactId.OrEmpty ().OrEmpty (), dependency.Version.OrEmpty ()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using System.Xml.Linq; | ||
using Java.Interop.Maven.Models; | ||
|
||
namespace Java.Interop.Maven.Extensions; | ||
|
||
class PropertyStack | ||
{ | ||
// Why go to this trouble? | ||
// A property can be specified in both a child POM and its parent POM. | ||
// Even if the property is being consumed in the parent POM, the property in | ||
// the child POM takes precedence. | ||
readonly List<List<KeyValuePair<string, string>>> stack = new (); | ||
|
||
public void Push (ModelProperties? properties) | ||
{ | ||
// We add a new list to the stack, even if it's empty, so that the Pop works later | ||
var list = new List<KeyValuePair<string, string>> (); | ||
|
||
if (properties?.Any is Collection<XElement> props) | ||
foreach (var prop in props) | ||
list.Add (new KeyValuePair<string, string> (prop.Name.LocalName, prop.Value)); | ||
|
||
stack.Add (list); | ||
} | ||
|
||
public void Pop () | ||
{ | ||
stack.RemoveAt (stack.Count - 1); | ||
} | ||
|
||
public string Apply (string value) | ||
{ | ||
foreach (var property_set in stack) { | ||
foreach (var prop in property_set) | ||
value = value.Replace ($"${{{prop.Key}}}", prop.Value); | ||
} | ||
|
||
return value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using Java.Interop.Maven.Models; | ||
|
||
namespace Java.Interop.Maven; | ||
|
||
public interface IPomResolver | ||
{ | ||
Project ResolveRawProject (Artifact artifact); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<LangVersion>11.0</LangVersion> | ||
<Nullable>enable</Nullable> | ||
<DefineConstants>INTERNAL_NULLABLE_ATTRIBUTES</DefineConstants> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<Import Project="..\..\TargetFrameworkDependentValues.props" /> | ||
|
||
<PropertyGroup> | ||
<OutputPath>$(UtilityOutputFullPath)</OutputPath> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="..\utils\NullableAttributes.cs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.SourceLink.GitHub" PrivateAssets="All" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System; | ||
|
||
namespace Java.Interop.Maven.Models; | ||
|
||
public class Artifact | ||
{ | ||
public string GroupId { get; } | ||
|
||
public string Id { get; } | ||
|
||
public string Version { get; } | ||
|
||
public Artifact (string groupId, string artifactId, string version) | ||
{ | ||
Id = artifactId; | ||
GroupId = groupId; | ||
Version = version; | ||
} | ||
|
||
public static Artifact Parse (string value) | ||
{ | ||
var parts = value.Split (':'); | ||
|
||
if (parts.Length != 3) | ||
throw new ArgumentException ($"Invalid artifact format: {value}"); | ||
|
||
return new Artifact (parts [0], parts [1], parts [2]); | ||
} | ||
|
||
public override string ToString () => $"{GroupId}:{Id}:{Version}"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.IO; | ||
using System.Xml; | ||
using System.Xml.Serialization; | ||
using Java.Interop.Maven.Extensions; | ||
|
||
namespace Java.Interop.Maven.Models; | ||
|
||
public partial class Project | ||
{ | ||
public static Project? Parse (Stream stream) | ||
{ | ||
Project? result = null; | ||
|
||
var serializer = new XmlSerializer (typeof (Project)); | ||
|
||
using (var sr = new StreamReader (stream)) | ||
result = (Project?) serializer.Deserialize (new XmlTextReader (sr) { | ||
Namespaces = false, | ||
}); | ||
|
||
return result; | ||
} | ||
|
||
public static Project? ParseXml (string xml) | ||
{ | ||
Project? result = null; | ||
|
||
var serializer = new XmlSerializer (typeof (Project)); | ||
|
||
using (var sr = new StringReader (xml)) | ||
result = (Project?) serializer.Deserialize (new XmlTextReader (sr) { | ||
Namespaces = false, | ||
}); | ||
|
||
return result; | ||
} | ||
|
||
public bool TryGetParentPomArtifact ([NotNullWhen (true)] out Artifact? parent) | ||
{ | ||
parent = null; | ||
|
||
if (Parent is not null) { | ||
parent = new Artifact (Parent.GroupId.OrEmpty (), Parent.ArtifactId.OrEmpty (), Parent.Version.OrEmpty ()); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public override string ToString () => $"{GroupId}:{ArtifactId}:{Version}"; | ||
|
||
public string ToXml () | ||
{ | ||
var serializer = new XmlSerializer (typeof (Project)); | ||
|
||
using (var sw = new StringWriter ()) { | ||
serializer.Serialize (sw, this); | ||
return sw.ToString (); | ||
} | ||
} | ||
} |
Oops, something went wrong.