Skip to content

Commit

Permalink
[NEW-FEATURE] Support central package management in ixc (#224)
Browse files Browse the repository at this point in the history
* Create draft PR for #223

* adds support for Central Package Management

---------

Co-authored-by: PTKu <PTKu@users.noreply.github.com>
Co-authored-by: Peter <61538034+PTKu@users.noreply.github.com>
  • Loading branch information
3 people authored Sep 15, 2023
1 parent 51496fe commit 3893f7a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ private IEnumerable<ISyntaxTree> GetReferences()

private void CompileProjectReferences(IEnumerable<IReference> referencedDependencies)
{
foreach (var ixProjectReference in AxProject.IxReferences)
foreach (var ixProjectReference in AxProject.AXSharpReferences)
{
string apaxFolder = ixProjectReference.AxProjectFolder == null
? referencedDependencies
Expand Down
8 changes: 7 additions & 1 deletion src/AXSharp.compiler/src/AXSharp.Compiler/AxProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public AxProject(string axProjectFolder, string[] sourceFiles)
/// <summary>
/// Gets paths of this project's references to other ix projects.
/// </summary>
public IEnumerable<AXSharpConfig> IxReferences
public IEnumerable<AXSharpConfig> AXSharpReferences
{
get
{
Expand All @@ -99,6 +99,12 @@ public IEnumerable<AXSharpConfig> IxReferences
.Select(p => Directory.EnumerateFiles(p.LinkTarget ?? p.FullName, AXSharpConfig.CONFIG_FILE_NAME, SearchOption.TopDirectoryOnly))
.SelectMany(p => p).Select(c => AXSharpConfig.RetrieveIxConfig(c));

if (retVal.Count() == 0)
{
Log.Logger.Information("Retrieving possible project references from .apax packages did not produce results. " +
"If you have referenced AX# projects, the packages must be previously installed by 'apax install'");
}

return retVal;
}
}
Expand Down
63 changes: 59 additions & 4 deletions src/AXSharp.compiler/src/AXSharp.Cs.Compiler/PackageReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,40 @@ public class PackageReference : IPackageReference
private static readonly string NugetDir =
SettingsUtility.GetGlobalPackagesFolder(Settings.LoadDefaultSettings(null));

private static IEnumerable<(string? include, string? version)> GetVersionFromCentralPackageManagement(string csprojFile)
{
var scFile = new FileInfo(csprojFile);
string? currentDirectory = scFile.DirectoryName;
string targetFile = null;

// Search for the file in the directory tree upstream
while (currentDirectory != null)
{
var potentialFile = Path.Combine(currentDirectory, "Directory.Packages.props");
if (File.Exists(potentialFile))
{
targetFile = potentialFile;
break;
}

currentDirectory = Directory.GetParent(currentDirectory)?.FullName;
}

if (targetFile == null)
{
return new List<(string?, string?)>();
}

XDocument xdoc = XDocument.Load(targetFile);
var packageElements = xdoc.Descendants().Where(e => e.Name == "PackageVersion" || e.Name == "GlobalPackageReference");

return packageElements.Select(pv =>
(
pv.Attribute("Include")?.Value,
pv.Attribute("Version")?.Value
));
}

/// <summary>
/// Creates new instance of <see cref="PackageReference" />
/// </summary>
Expand All @@ -32,16 +66,37 @@ public static PackageReference CreateFromReferenceNode(XElement packageReference
try
{
include = (packageReferenceNode.Attribute(XName.Get(nameof(Include)))?.Value ?? packageReferenceNode.Attribute(XName.Get("Update"))?.Value)!;
var version = packageReferenceNode.Attribute(XName.Get(nameof(Version)))!.Value;
var version = packageReferenceNode.Attribute(XName.Get(nameof(Version)))?.Value ??
packageReferenceNode.Attribute(XName.Get("VersionOverride"))?.Value;

version = version ??
GetVersionFromCentralPackageManagement(projectFile).FirstOrDefault(p => p.include == include).version;



if (include == null)
{
throw new FailedToRetrievePackageReferenceException(
$"We were unable to determine package id of one of the packages referenced in project {projectFile}\n" +
$"The package id must be in 'Include' or 'Update' attribute of 'PackageReference' element." +
$"Make sure your csproj file is valid", null);
}

if (version == null)
{
throw new FailedToRetrievePackageReferenceException(
$"We were unable to determine version of the package '{include}' referenced in project {projectFile}\n" +
$"Make sure you have the version defined either in the project file or in central package management file 'Directory.Packages.props'\n" +
$"upstream in your projects' directory structure.", null);
}

var referencePath = PackageReferenceNugetPath(include, version);

return new PackageReference(referencePath, include, version);
}
catch (Exception ex)
{
throw new FailedToRetrievePackageReferenceException(
$"Could not parse 'Name' or 'Version' of the package '{include}' reference in project {projectFile}",
ex);
throw new FailedToRetrievePackageReferenceException($"Failed to retrieve package reference {ex.Message}", ex);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/AXSharp.compiler/src/ixc/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
},
"integration_plc": {
"commandName": "Project",
"workingDirectory": "c:\\W\\Develop\\gh\\ix-ax\\axsharp\\src\\sanbox\\integration\\ix-integration-plc\\"
"workingDirectory": "c:\\W\\Develop\\gh\\ix-ax\\axopen\\src\\integrations\\ctrl\\"
},
"ixc-simple-template": {
"commandName": "Project",
"workingDirectory": "C:\\W\\Develop\\gh\\ix-ax\\ix.framework\\src\\templates.simple\\ctrl\\"
"workingDirectory": "C:\\W\\Develop\\gh\\ix-ax\\axopen\\src\\templates.simple\\ctrl\\"
},
"ixc-template-ref": {
"commandName": "Project",
Expand Down

0 comments on commit 3893f7a

Please sign in to comment.