Skip to content

Commit

Permalink
Merge pull request #378 from Inxton/377-bugixc-does-not-compile-the-d…
Browse files Browse the repository at this point in the history
…eps-when-run-with-args-outside-of-dependants-folder

[BUG]ixc does not compile the deps. when run with args outside of dependants folder.
  • Loading branch information
PTKu authored Jan 28, 2025
2 parents c8345f0 + eb7acbf commit a732929
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 69 deletions.
38 changes: 35 additions & 3 deletions src/AXSharp.compiler/src/AXSharp.Compiler/AXSharpConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,35 @@ public string AxProjectFolder
set => _axProjectFolder = value;
}


private static string VerifyRelativePath(string baseFolder, string path)
{
if (string.IsNullOrWhiteSpace(baseFolder))
{
throw new ArgumentException("Base folder cannot be null or empty.", nameof(baseFolder));
}

if (string.IsNullOrWhiteSpace(path))
{
throw new ArgumentException("Path cannot be null or empty.", nameof(path));
}

// Ensure baseFolder is absolute
baseFolder = Path.GetFullPath(baseFolder);

// Check whether the path is already absolute
if (Path.IsPathRooted(path))
{
Log.Logger.Fatal($"Path in AXSharpConfig is absolute '{path}'. Change it to path relative to AX project folder path.");
throw new ArgumentException($"Path cannot be absolute '{path}'.", nameof(path));
}
else
{
// Convert it to an absolute path based on baseFolder
return Path.GetFullPath(Path.Combine(baseFolder, path));
}
}

/// <summary>
/// Gets updated or creates default config for given AX project.
/// </summary>
Expand Down Expand Up @@ -143,16 +172,19 @@ public static AXSharpConfig RetrieveAXSharpConfig(string ixConfigFilePath)
if (config != null)
{
var fi = new FileInfo(ixConfigFilePath);
if (fi.DirectoryName != null) config.AxProjectFolder = fi.DirectoryName;
if (fi.DirectoryName != null)
{
config.AxProjectFolder = fi.DirectoryName;
config.OutputProjectFolder = VerifyRelativePath(fi.DirectoryName, config.OutputProjectFolder);
}
}

return config;
}
catch (Exception ex)
{
throw new FailedToReadIxConfigurationFileException($"Unable to process '{ixConfigFilePath}'", ex);
}

}
}

private static void OverridesFromCli(ICompilerOptions fromConfig, ICompilerOptions? newCompilerOptions)
Expand Down
16 changes: 11 additions & 5 deletions src/AXSharp.compiler/src/AXSharp.Compiler/AXSharpProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ public AXSharpProject(AxProject axProject, IEnumerable<Type> builderTypes, Type
throw new InvalidOperationException("Output project folder must be set in the AXSharp.config.json file.");
OutputFolder = Path.GetFullPath(Path.Combine(AxProject.ProjectFolder, CompilerOptions.OutputProjectFolder));

if (!string.IsNullOrEmpty(CompilerOptions.ProjectFile))
{
ProjectFile = Path.Combine(OutputFolder, CompilerOptions.ProjectFile);
}
//if (!string.IsNullOrEmpty(CompilerOptions.ProjectFile))
//{
// ProjectFile = Path.Combine(OutputFolder, CompilerOptions.ProjectFile);
//}
}

if (cliCompilerOptions != null) UseBaseSymbol = cliCompilerOptions.UseBase;
Expand Down Expand Up @@ -252,8 +252,9 @@ private static string EnsureFolder(string folder)
public IEnumerable<ISyntaxTree> GetReferences()
{
TargetProject.InstallAXSharpDependencies(AxProject.AXSharpReferences);

var referencedDependencies = TargetProject.LoadReferences();


if (!this.CompilerOptions.SkipDependencyCompilation)
{
Expand All @@ -265,9 +266,13 @@ public IEnumerable<ISyntaxTree> GetReferences()
.Select(p => p.MetadataPath)
.Select(p => JsonConvert.DeserializeObject<IEnumerable<string>>(File.ReadAllText(p)));


dependencyMetadata.ToList().ForEach(p => Log.Logger.Debug($"Dependency metadata: \n {string.Join(";", p)}"));

var refParseTrees = dependencyMetadata.SelectMany(p => p)
.Select(s => STParser.ParseTextAsync(new StringText(s)).Result);


return refParseTrees;
}

Expand All @@ -277,6 +282,7 @@ private void CompileProjectReferences(IEnumerable<IReference> referencedDependen
{
foreach (var ixProjectReference in AxProject.AXSharpReferences.OfType<AXSharpConfig>())
{
Log.Logger.Verbose($"Starting compilation of project reference '{ixProjectReference.AxProjectFolder}' into '{ixProjectReference.OutputProjectFolder}'.");

if (compiled.Contains(ixProjectReference.AxProjectFolder))
{
Expand Down
6 changes: 5 additions & 1 deletion src/AXSharp.compiler/src/AXSharp.Compiler/AxProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ private IEnumerable<object> GetProjectDependencies()
if (File.Exists(pathAXSharpConfig))
{
projectDependencies.Add((AXSharpConfig.RetrieveAXSharpConfig(pathAXSharpConfig)));
Log.Logger.Verbose($"Project reference for '{pathAXSharpConfig}' considered.");
}
}
}
Expand All @@ -276,7 +277,10 @@ private IEnumerable<object> GetProjectDependencies()
var packageFile =
Path.Combine(dependencyWithCompanion.ApaxFile.Directory.FullName, "package.json");
if(File.Exists(packageFile))
{
projectDependencies.Add(dependencyWithCompanion.Companion);
Log.Logger.Verbose($"Package reference for '{dependencyWithCompanion.ApaxFile.FullName}' considered.");
}
}
}

Expand All @@ -285,7 +289,7 @@ private IEnumerable<object> GetProjectDependencies()
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 projectDependencies;
}

Expand Down
73 changes: 68 additions & 5 deletions src/AXSharp.compiler/src/AXSharp.Compiler/Logger/CompilerLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using Serilog;
using Serilog.Core;
using Serilog.Events;

namespace AXSharp.Compiler;

Expand All @@ -15,15 +16,77 @@ namespace AXSharp.Compiler;
/// </summary>
public static class Log
{
static Log()
private static Logger logger;

/// <summary>
/// Configures the logger.
/// </summary>
/// <param name="logLevel"></param>
public static void ConfigureLogger(LogEventLevel logLevel)
{
Logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateLogger();
switch(logLevel)
{
case LogEventLevel.Verbose:
Logger = new LoggerConfiguration()
.WriteTo.Console()
.MinimumLevel.Verbose()
.CreateLogger();
break;
case LogEventLevel.Debug:
Logger = new LoggerConfiguration()
.WriteTo.Console()
.MinimumLevel.Debug()
.CreateLogger();
break;
case LogEventLevel.Information:
Logger = new LoggerConfiguration()
.WriteTo.Console()
.MinimumLevel.Information()
.CreateLogger();
break;
case LogEventLevel.Warning:
Logger = new LoggerConfiguration()
.WriteTo.Console()
.MinimumLevel.Warning()
.CreateLogger();
break;
case LogEventLevel.Error:
Logger = new LoggerConfiguration()
.WriteTo.Console()
.MinimumLevel.Error()
.CreateLogger();
break;
case LogEventLevel.Fatal:
Logger = new LoggerConfiguration()
.WriteTo.Console()
.MinimumLevel.Fatal()
.CreateLogger();
break;
default:
Logger = new LoggerConfiguration()
.WriteTo.Console()
.MinimumLevel.Information()
.CreateLogger();
break;
}


}

/// <summary>
/// Gets the logger.
/// </summary>
public static Logger Logger { get; }
public static Logger Logger
{
get
{
if (logger == null)
{
ConfigureLogger(LogEventLevel.Information);
}

return logger;
}
private set => logger = value;
}
}
20 changes: 13 additions & 7 deletions src/AXSharp.compiler/src/AXSharp.Cs.Compiler/CsProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public CsProject(AXSharpProject AXSharpProject)
/// <summary>
/// Gets associated IxProject file.
/// </summary>
public string IxProjectFile
private string CsProjectFile
{
get
{
Expand All @@ -51,7 +51,7 @@ public string IxProjectFile
$"{MakeValidFileName(AxSharpProject.AxProject.ProjectInfo.Name)}.csproj");
}

return AxSharpProject.ProjectFile;
return Path.Combine(AxSharpProject.OutputFolder, AxSharpProject.ProjectFile);
}
}

Expand Down Expand Up @@ -276,6 +276,7 @@ private static void AddProjectReference(string mainProjectPath, string reference
process.BeginErrorReadLine();

process.WaitForExit();
process.WaitForExit();
}
}

Expand Down Expand Up @@ -321,6 +322,7 @@ private static void AddNuGetPackageReference(string projectPath, string packageN
process.BeginErrorReadLine();

process.WaitForExit();
process.WaitForExit();
}
}

Expand All @@ -343,7 +345,7 @@ public void InstallAXSharpDependencies(IEnumerable<object> dependencies)
{
throw new Exception("Missing dependency file.");
}

foreach (var dependency in dependencies)
{

Expand All @@ -357,7 +359,7 @@ public void InstallAXSharpDependencies(IEnumerable<object> dependencies)
AddProjectReference(dependent, GetRelativePath(dependent, projectPath));
break;
}
}
}
}
}

Expand All @@ -367,14 +369,18 @@ public void InstallAXSharpDependencies(IEnumerable<object> dependencies)
/// <returns>List of references.</returns>
public IEnumerable<IReference> LoadReferences()
{
var directDependencies = GetDirectDependencies(IxProjectFile).ToList();
var directDependencies = GetDirectDependencies(CsProjectFile).ToList();

var referenceDependencies = new List<IReference>();

foreach (var dependency in directDependencies)
GetReferenceDependencies(dependency, referenceDependencies);

return DistinctBy(referenceDependencies, p => p.ReferencePath);
var distinct = DistinctBy(referenceDependencies, p => p.ReferencePath);

distinct.ToList().ForEach(p => Log.Logger.Debug($"Dependency metadata {this.AxSharpProject.AxProject.ProjectFolder}: \n {string.Join(";", p)}"));

return distinct;
}

private static string FileDirectory(string path)
Expand All @@ -386,7 +392,7 @@ private static IEnumerable<IReference> GetDirectDependencies(string projectFile)
{
var projectPath = projectFile;

if (!File.Exists(projectPath)) return new List<IReference>();
if (!File.Exists(projectPath)) throw new FileNotFoundException(projectFile);

try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private static bool IsToBeOmitted(this IStorageDeclaration fieldDeclaration, ISo
public static bool IsEligibleForTranspile(this IFieldDeclaration fieldDeclaration, ISourceBuilder sourceBuilder)
{
var type = fieldDeclaration.Type;
return !(type is IReferenceTypeDeclaration)
var isEligible = !(type is IReferenceTypeDeclaration)
&&
fieldDeclaration.IsAvailableForComm(sourceBuilder)
&&
Expand All @@ -91,6 +91,13 @@ type is IStructuredTypeDeclaration ||
type is INamedValueTypeDeclaration ||
sourceBuilder.Compilation.GetSemanticTree().Types.Any(p =>
p.FullyQualifiedName == type.FullyQualifiedName));

if(!isEligible)
{
Log.Logger.Debug($"Field '{fieldDeclaration.Name}' of type '{fieldDeclaration.Type.FullyQualifiedName}' is not eligible for transpile");
}

return isEligible;
}

/// <summary>
Expand All @@ -102,7 +109,7 @@ type is INamedValueTypeDeclaration ||
public static bool IsEligibleForTranspile(this IVariableDeclaration variableDeclaration, ISourceBuilder sourceBuilder)
{
var type = variableDeclaration.Type;
return !(type is IReferenceTypeDeclaration)
var isEligible = !(type is IReferenceTypeDeclaration)
&&
variableDeclaration.IsAvailableForComm(sourceBuilder)
&&
Expand All @@ -112,6 +119,13 @@ type is IStructuredTypeDeclaration ||
type is INamedValueTypeDeclaration ||
sourceBuilder.Compilation.GetSemanticTree().Types.Any(p =>
p.FullyQualifiedName == type.FullyQualifiedName));

if (!isEligible)
{
Log.Logger.Debug($"Variable '{variableDeclaration.Name}' of type '{variableDeclaration.Type.FullyQualifiedName}' is not eligible for transpile");
}

return isEligible;
}


Expand Down
5 changes: 5 additions & 0 deletions src/AXSharp.compiler/src/ixc/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using CommandLine;
using AXSharp.Compiler;
using Serilog.Events;

namespace ixc;

Expand Down Expand Up @@ -45,5 +46,9 @@ internal class Options : ICompilerOptions
[Option('t', "target-platform-moniker", Required = false, Default = "ax",
HelpText = "Instructs the compiler to adjust for target platform differences. Possible values 'ax', 'tia'")]
public string TargetPlatfromMoniker { get; set; }

[Option('v', "verbosity", Required = false, Default = LogEventLevel.Information,
HelpText = "Level of compiler output. Possible options Verbose, Debug, Information, Warning, Error, Fatal")]
public LogEventLevel Versbosity { get; set; }
}

8 changes: 4 additions & 4 deletions src/AXSharp.compiler/src/ixc/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ public static void Main(string[] args)
{
var recoverCurrentDirectory = Environment.CurrentDirectory;
try
{
//string json = JsonSerializer.Serialize(o);

//Console.WriteLine(json);
{
Log.ConfigureLogger(o.Versbosity);

Log.Logger.Verbose(JsonSerializer.Serialize(o));

Project = GenerateIxProject(o);
}
catch (Exception e)
Expand Down
Loading

0 comments on commit a732929

Please sign in to comment.