Skip to content

Commit

Permalink
241 new feature explore possible nuget package installation during ix…
Browse files Browse the repository at this point in the history
…c run from apax package (#244)

* Create draft PR for #241

* [wip] installs project and package dependencies from apax.yml into twin companion project

* fixes an issue with ixr where wrong default cli parameter was set

* fixes an issue when existing package reference was not found in csproj file

* perf improvent by limiting search of apax files in dir structure

* asp

---------

Co-authored-by: PTKu <PTKu@users.noreply.github.com>
  • Loading branch information
PTKu and PTKu authored Oct 5, 2023
1 parent 854308a commit 2df3b77
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 8 deletions.
62 changes: 57 additions & 5 deletions src/AXSharp.compiler/src/AXSharp.Compiler/AxProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,61 @@ private class InstalledDependencies
/// </summary>
public IEnumerable<object> AXSharpReferences => GetProjectDependencies();

private static string GetStartDirectory(string givenDirectory, int levelsUp)
{
try
{
// Move 'levelsUp' safely
DirectoryInfo dirInfo = new DirectoryInfo(givenDirectory);

for (int i = 0; i < levelsUp; i++)
{
if (dirInfo.Parent != null)
{
dirInfo = dirInfo.Parent;
}
else
{
return dirInfo.FullName; // Return root if we hit it before moving the desired levels up
}
}

return dirInfo.FullName;
}
catch
{
return null; // return null if any error occurs
}
}

private static IEnumerable<string> SearchForApaxFiles(string directory, int currentDepth, int maxDepth)
{
var apaxFilesList = new List<string>();

if (currentDepth > maxDepth) return apaxFilesList;

try
{
apaxFilesList.AddRange(Directory.GetFiles(directory, "apax.yml"));

foreach (var subDir in Directory.GetDirectories(directory))
{
// Exclude '.apax' directories
if (Path.GetFileName(subDir) != ".apax")
{
apaxFilesList.AddRange(SearchForApaxFiles(subDir, currentDepth + 1, maxDepth));
}
}
}
catch (UnauthorizedAccessException)
{
// Handle permissions issues, if any
Console.WriteLine($"Access denied to: {directory}");
}

return apaxFilesList;
}

private IEnumerable<object> GetProjectDependencies()
{
var dependencies = ProjectInfo.Dependencies ?? new Dictionary<string, string>();
Expand All @@ -118,14 +173,11 @@ private IEnumerable<object> GetProjectDependencies()
ApaxFile = new FileInfo(Path.Combine(p, "apax.yml"))
}).ToList();


nearByProjects ??= Directory.EnumerateFiles(
Path.GetFullPath(Path.Combine(this.ProjectFolder, "../../..")),
"apax.yml", SearchOption.AllDirectories)
nearByProjects = SearchForApaxFiles(GetStartDirectory(this.ProjectFolder, 2), 0, 2)
.Select(p => new FileInfo(p))
.Where(p => !p.Directory.FullName.Contains(".apax"))
.Select(a => new NearByProjects() { Apax = Apax.TryCreateApaxDto(a.FullName), ApaxFile = a })
.ToList();
.ToList(); ;

var projectDependencies = new List<object>();

Expand Down
11 changes: 8 additions & 3 deletions src/AXSharp.compiler/src/AXSharp.Cs.Compiler/CsProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using NuGet.Packaging;
using NuGet.Versioning;
using Polly;
using Serilog.Core;

namespace AXSharp.Compiler;

Expand Down Expand Up @@ -216,18 +217,21 @@ private static bool ProjectReferenceExists(string mainProjectPath, string refere
var xDocument = XDocument.Load(mainProjectPath);

// Using XPath to search for the ProjectReference with a specific Include path
var projectReferenceElements = xDocument.XPathSelectElements($"//ProjectReference[@Include='{referenceProjectPath}']");

return projectReferenceElements.Any();
var projectReferenceElements = xDocument.XPathSelectElements($"//ProjectReference[@Include='{referenceProjectPath}']").Any();
var any = xDocument.Descendants("ProjectReference").Any(p => p.Attribute("Include")?.Value == referenceProjectPath);
return projectReferenceElements || any;
}

private static void AddProjectReference(string mainProjectPath, string referenceProjectPath)
{
if (ProjectReferenceExists(mainProjectPath, referenceProjectPath))
return;

Log.Logger.Information($"Adding project reference '{referenceProjectPath}' to '{mainProjectPath}'");

using (var process = new Process())
{
process.StartInfo.WorkingDirectory = new FileInfo(mainProjectPath).DirectoryName;
process.StartInfo.FileName = "dotnet";
process.StartInfo.Arguments = $"add \"{mainProjectPath}\" reference \"{referenceProjectPath}\"";
process.StartInfo.RedirectStandardOutput = true;
Expand Down Expand Up @@ -272,6 +276,7 @@ private static void AddNuGetPackageReference(string projectPath, string packageN

using (var process = new Process())
{
process.StartInfo.WorkingDirectory = new FileInfo(projectPath).DirectoryName;
process.StartInfo.FileName = "dotnet";
process.StartInfo.Arguments = $"add \"{projectPath}\" package {packageName}" + (string.IsNullOrEmpty(version) ? "" : $" --version {version}");
process.StartInfo.RedirectStandardOutput = true;
Expand Down

0 comments on commit 2df3b77

Please sign in to comment.