Skip to content

Commit

Permalink
fix(source-generator): handling of relativePath arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
jwfx authored and matkoch committed Aug 14, 2023
1 parent b1a7d34 commit 797a073
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions source/Nuke.SourceGenerators/StronglyTypedSolutionGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,24 @@ public void Execute(GeneratorExecutionContext context)
var compilation = context.Compilation;
var allTypes = compilation.Assembly.GlobalNamespace.GetAllTypes();
var members = allTypes.SelectMany(x => x.GetMembers())
.Where(x => x is IPropertySymbol || x is IFieldSymbol)
.Where(x => x is IPropertySymbol or IFieldSymbol)
.Select(x => (Member: x, AttributeData: x.GetAttributeData("global::Nuke.Common.ProjectModel.SolutionAttribute")))
.Where(x => x.AttributeData?.NamedArguments.SingleOrDefault(x => x.Key == "GenerateProjects").Value.Value as bool? ?? false)
.Select(x => x.Member).ToList();
.ToList();

if (members.Count == 0)
return;

var rootDirectory = GetRootDirectoryFrom(compilation);
var compilationUnit = CompilationUnit()
.AddUsings(UsingDirective(IdentifierName("Nuke.Common.ProjectModel")));

foreach (var member in members)
foreach (var (member, attributeData) in members)
{
var solutionFile = GetSolutionFileFromParametersFile(rootDirectory, member.Name);
var solutionFile = attributeData.ConstructorArguments.FirstOrDefault().Value is string { Length: > 0 } relativeSlnPath
? rootDirectory / relativeSlnPath
: GetSolutionFileFromParametersFile(rootDirectory, member.Name);

var solution = SolutionSerializer.DeserializeFromFile<Solution>(solutionFile);

var classDeclaration = GetSolutionFolderDeclaration(member.Name, solution.SolutionFolders, solution.Projects, isSolution: true);
Expand Down Expand Up @@ -104,13 +108,13 @@ MemberDeclarationSyntax GetSolutionFolderProperty(string name)
.ToArray<MemberDeclarationSyntax>());
}

private static string GetSolutionFileFromParametersFile(AbsolutePath rootDirectory, string memberName)
private static AbsolutePath GetSolutionFileFromParametersFile(AbsolutePath rootDirectory, string memberName)
{
var parametersFile = Constants.GetDefaultParametersFile(rootDirectory);
Assert.FileExists(parametersFile);
var jobject = JObject.Parse(File.ReadAllText(parametersFile));
var solutionRelativePath = jobject[memberName].NotNull($"Property '{memberName}' does not exist in '{parametersFile}'.").Value<string>();
return Path.Combine(rootDirectory, solutionRelativePath.NotNull());
return rootDirectory / solutionRelativePath.NotNull();
}

private static AbsolutePath GetRootDirectoryFrom(Compilation compilation)
Expand Down

0 comments on commit 797a073

Please sign in to comment.