Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Additional files on GetWorkspace() #231

Merged
merged 4 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions src/Buildalyzer.Workspaces/AnalyzerResultExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ private static ProjectInfo GetProjectInfo(IAnalyzerResult analyzerResult, Worksp
projectReferences: GetExistingProjectReferences(analyzerResult, workspace),
metadataReferences: GetMetadataReferences(analyzerResult),
analyzerReferences: GetAnalyzerReferences(analyzerResult, workspace),
additionalDocuments: GetAdditionalDocuments(analyzerResult, projectId),
parseOptions: CreateParseOptions(analyzerResult, languageName),
compilationOptions: CreateCompilationOptions(analyzerResult, languageName));
}
Expand Down Expand Up @@ -266,17 +267,28 @@ private static IEnumerable<IProjectAnalyzer> GetReferencedAnalyzerProjects(IAnal
.Where(x => x != null)
?? Array.Empty<ProjectAnalyzer>();

private static IEnumerable<DocumentInfo> GetDocuments(IAnalyzerResult analyzerResult, ProjectId projectId) =>
analyzerResult
.SourceFiles?.Where(File.Exists)
.Select(x => DocumentInfo.Create(
DocumentId.CreateNewId(projectId),
Path.GetFileName(x),
loader: TextLoader.From(
TextAndVersion.Create(
SourceText.From(File.ReadAllText(x), Encoding.Unicode), VersionStamp.Create())),
filePath: x))
?? Array.Empty<DocumentInfo>();
private static IEnumerable<DocumentInfo> GetDocuments(IAnalyzerResult analyzerResult, ProjectId projectId)
{
string[] sourceFiles = analyzerResult.SourceFiles ?? Array.Empty<string>();
return GetDocuments(sourceFiles, projectId);
}

private static IEnumerable<DocumentInfo> GetDocuments(IEnumerable<string> files, ProjectId projectId) =>
files.Where(File.Exists)
.Select(x => DocumentInfo.Create(
DocumentId.CreateNewId(projectId),
Path.GetFileName(x),
loader: TextLoader.From(
TextAndVersion.Create(
SourceText.From(File.ReadAllText(x), Encoding.Unicode), VersionStamp.Create())),
filePath: x));

private static IEnumerable<DocumentInfo> GetAdditionalDocuments(IAnalyzerResult analyzerResult, ProjectId projectId)
{
string projectDirectory = Path.GetDirectoryName(analyzerResult.ProjectFilePath);
string[] additionalFiles = analyzerResult.AdditionalFiles ?? Array.Empty<string>();
return GetDocuments(additionalFiles.Select(x => Path.Combine(projectDirectory, x)), projectId);
}

private static IEnumerable<MetadataReference> GetMetadataReferences(IAnalyzerResult analyzerResult) =>
analyzerResult
Expand Down
14 changes: 14 additions & 0 deletions tests/Buildalyzer.Tests/Integration/SimpleProjectsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,20 @@ public void GetsAdditionalCscFiles()
additionalFiles.ShouldBe(new[] { "_Imports.razor", "Component1.razor" }, log.ToString());
}

[Test]
public void GetsAdditionalFile()
{
// Given
StringWriter log = new StringWriter();
IProjectAnalyzer analyzer = GetProjectAnalyzer(@"ProjectWithAdditionalFile\ProjectWithAdditionalFile.csproj", log);

// When
IEnumerable<string> additionalFiles = analyzer.Build().First().AdditionalFiles;

// Then
additionalFiles.ShouldBe(new[] { "message.txt" }, log.ToString());
}

private static IProjectAnalyzer GetProjectAnalyzer(string projectFile, StringWriter log)
{
IProjectAnalyzer analyzer = new AnalyzerManager(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,23 @@ public void SupportsAnalyzers()
project.AnalyzerReferences.ShouldContain(reference => reference.Display == "Microsoft.CodeQuality.Analyzers");
}

[Test]
public void SupportsAdditionalFiles()
{
// Given
SafeStringWriter log = new SafeStringWriter();
IProjectAnalyzer analyzer = GetProjectAnalyzer(@"projects\ProjectWithAdditionalFile\ProjectWithAdditionalFile.csproj", log);

// When
Workspace workspace = analyzer.GetWorkspace();
Project project = workspace.CurrentSolution.Projects.First();

// Then
string logged = log.ToString();
logged.ShouldNotContain("Workspace failed", logged);
project.AdditionalDocuments.Select(d => d.Name).ShouldBe(new[] { "message.txt" });
}

#if Is_Windows
[Test]
public void HandlesWpfCustomControlLibrary()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<AdditionalFiles Include="message.txt" />
</ItemGroup>

</Project>
3 changes: 3 additions & 0 deletions tests/projects/ProjectWithAdditionalFile/SomeClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace ProjectWithAdditionalFile;

public class SomeClass { }
1 change: 1 addition & 0 deletions tests/projects/ProjectWithAdditionalFile/message.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, World!
Loading