Skip to content
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
3 changes: 2 additions & 1 deletion src/OmniSharp.MSBuild/ProjectFile/ItemNames.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
namespace OmniSharp.MSBuild.ProjectFile
namespace OmniSharp.MSBuild.ProjectFile
{
internal static class ItemNames
{
public const string Analyzer = nameof(Analyzer);
public const string AdditionalFiles = nameof(AdditionalFiles);
public const string Compile = nameof(Compile);
public const string PackageReference = nameof(PackageReference);
public const string ProjectReference = nameof(ProjectReference);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
Expand Down Expand Up @@ -47,6 +47,7 @@ private class ProjectData
public ImmutableArray<string> References { get; }
public ImmutableArray<PackageReference> PackageReferences { get; }
public ImmutableArray<string> Analyzers { get; }
public ImmutableArray<string> AdditionalFiles { get; }
public RuleSet RuleSet { get; }
public ImmutableDictionary<string, string> ReferenceAliases { get; }
public bool TreatWarningsAsErrors { get; }
Expand All @@ -63,6 +64,7 @@ private ProjectData()
References = ImmutableArray<string>.Empty;
PackageReferences = ImmutableArray<PackageReference>.Empty;
Analyzers = ImmutableArray<string>.Empty;
AdditionalFiles = ImmutableArray<string>.Empty;
ReferenceAliases = ImmutableDictionary<string, string>.Empty;
}

Expand Down Expand Up @@ -135,6 +137,7 @@ private ProjectData(
ImmutableArray<string> references,
ImmutableArray<PackageReference> packageReferences,
ImmutableArray<string> analyzers,
ImmutableArray<string> additionalFiles,
bool treatWarningsAsErrors,
RuleSet ruleset,
ImmutableDictionary<string, string> referenceAliases)
Expand All @@ -147,6 +150,7 @@ private ProjectData(
References = references.EmptyIfDefault();
PackageReferences = packageReferences.EmptyIfDefault();
Analyzers = analyzers.EmptyIfDefault();
AdditionalFiles = additionalFiles.EmptyIfDefault();
ReferenceAliases = referenceAliases;
}

Expand Down Expand Up @@ -266,13 +270,14 @@ public static ProjectData Create(MSB.Execution.ProjectInstance projectInstance)

var packageReferences = GetPackageReferences(projectInstance.GetItems(ItemNames.PackageReference));
var analyzers = GetFullPaths(projectInstance.GetItems(ItemNames.Analyzer));
var additionalFiles = GetFullPaths(projectInstance.GetItems(ItemNames.AdditionalFiles));

return new ProjectData(guid, name,
assemblyName, targetPath, outputPath, intermediateOutputPath, projectAssetsFile,
configuration, platform, targetFramework, targetFrameworks,
outputKind, languageVersion, nullableContextOptions, allowUnsafeCode, documentationFile, preprocessorSymbolNames, suppressedDiagnosticIds,
signAssembly, assemblyOriginatorKeyFile,
sourceFiles, projectReferences, references.ToImmutable(), packageReferences, analyzers, treatWarningsAsErrors, ruleset, referenceAliases.ToImmutableDictionary());
sourceFiles, projectReferences, references.ToImmutable(), packageReferences, analyzers, additionalFiles, treatWarningsAsErrors, ruleset, referenceAliases.ToImmutableDictionary());
}

private static RuleSet ResolveRulesetIfAny(MSB.Execution.ProjectInstance projectInstance)
Expand Down
1 change: 1 addition & 0 deletions src/OmniSharp.MSBuild/ProjectFile/ProjectFileInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ internal partial class ProjectFileInfo
public ImmutableArray<string> ProjectReferences => _data.ProjectReferences;
public ImmutableArray<PackageReference> PackageReferences => _data.PackageReferences;
public ImmutableArray<string> Analyzers => _data.Analyzers;
public ImmutableArray<string> AdditionalFiles => _data.AdditionalFiles;
public ImmutableDictionary<string, string> ReferenceAliases => _data.ReferenceAliases;
public bool TreatWarningsAsErrors => _data.TreatWarningsAsErrors;
public ProjectIdInfo ProjectIdInfo { get; }
Expand Down
20 changes: 19 additions & 1 deletion src/OmniSharp.MSBuild/ProjectManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
Expand Down Expand Up @@ -435,6 +435,7 @@ private void UpdateProject(string projectFilePath)
UpdateProjectReferences(project, projectFileInfo.ProjectReferences);
UpdateReferences(project, projectFileInfo.ProjectReferences, projectFileInfo.References);
UpdateAnalyzerReferences(projectFileInfo, project);
UpdateAdditionalFiles(project, projectFileInfo.AdditionalFiles);

_workspace.TryPromoteMiscellaneousDocumentsToProject(project);
_workspace.UpdateDiagnosticOptionsForProject(project.Id, projectFileInfo.GetDiagnosticOptions());
Expand All @@ -449,6 +450,23 @@ private void UpdateAnalyzerReferences(ProjectFileInfo projectFileInfo, Project p
_workspace.SetAnalyzerReferences(project.Id, analyzerFileReferences);
}

private void UpdateAdditionalFiles(Project project, IList<string> additionalFiles)
{
var currentAdditionalDocuments = project.AdditionalDocuments;
foreach (var document in currentAdditionalDocuments)
{
_workspace.RemoveAdditionalDocument(document.Id);
}

foreach (var file in additionalFiles)
{
if (File.Exists(file))
{
_workspace.AddAdditionalDocument(project.Id, file);
}
}
}

private void UpdateSourceFiles(Project project, IList<string> sourceFiles)
{
// Remove transient documents from list of current documents, to assure proper new documents are added.
Expand Down
15 changes: 14 additions & 1 deletion src/OmniSharp.Roslyn/OmniSharpWorkspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public DocumentId AddDocument(ProjectId projectId, string filePath, SourceCodeKi
public DocumentId AddDocument(DocumentId documentId, ProjectId projectId, string filePath, SourceCodeKind sourceCodeKind = SourceCodeKind.Regular)
{
var loader = new OmniSharpTextLoader(filePath);
var documentInfo = DocumentInfo.Create(documentId, filePath, filePath: filePath, loader: loader, sourceCodeKind: sourceCodeKind);
var documentInfo = DocumentInfo.Create(documentId, Path.GetFileName(filePath), filePath: filePath, loader: loader, sourceCodeKind: sourceCodeKind);

this.AddDocument(documentInfo);

Expand Down Expand Up @@ -414,5 +414,18 @@ public void SetAnalyzerReferences(ProjectId id, ImmutableArray<AnalyzerFileRefer
base.OnAnalyzerReferenceRemoved(id, toRemove);
}
}

public void AddAdditionalDocument(ProjectId projectId, string filePath)
{
var documentId = DocumentId.CreateNewId(projectId);
var loader = new OmniSharpTextLoader(filePath);
var documentInfo = DocumentInfo.Create(documentId, Path.GetFileName(filePath), filePath: filePath, loader: loader);
OnAdditionalDocumentAdded(documentInfo);
}

public void RemoveAdditionalDocument(DocumentId documentId)
{
OnAdditionalDocumentRemoved(documentId);
}
}
}
12 changes: 12 additions & 0 deletions test-assets/test-projects/ProjectWithAdditionalFiles/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<LangVersion>7.1</LangVersion>
</PropertyGroup>
<ItemGroup>
<AdditionalFiles Include="dummy0.txt" />
<AdditionalFiles Include="dummy1.txt" />
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="dummy2.txt" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<LangVersion>7.1</LangVersion>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<LangVersion>7.1</LangVersion>
</PropertyGroup>
<ItemGroup>
<AdditionalFiles Include="dummy0.txt" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Dummy0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Dummy1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Dummy2
64 changes: 64 additions & 0 deletions tests/OmniSharp.MSBuild.Tests/ProjectWithAdditionalFilesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using TestUtility;
using Xunit;
using Xunit.Abstractions;

namespace OmniSharp.MSBuild.Tests
{
public class ProjectWithAdditionalFilesTests : AbstractMSBuildTestFixture
{
public ProjectWithAdditionalFilesTests(ITestOutputHelper output)
: base(output)
{
}

[Fact]
public async Task WhenProjectIsNoAdditionalFiles()
{
using (var testProject = await TestAssets.Instance.GetTestProjectAsync("ProjectWithAdditionalFiles"))
using (var host = CreateMSBuildTestHost(testProject.Directory))
{
var project = host.Workspace.CurrentSolution.Projects.Where(x => x.AssemblyName == "ProjectWithNoAdditionalFiles").Single();
Assert.Empty(project.AdditionalDocuments);
}
}

[Fact]
public async Task WhenProjectIsSingleAdditionalFiles()
{
using (var testProject = await TestAssets.Instance.GetTestProjectAsync("ProjectWithAdditionalFiles"))
using (var host = CreateMSBuildTestHost(testProject.Directory))
{
var project = host.Workspace.CurrentSolution.Projects.Where(x => x.AssemblyName == "ProjectWithSingleAdditionalFiles").Single();
Assert.Single(project.AdditionalDocuments);

var doc = project.AdditionalDocuments.Single();
Assert.Equal("dummy0.txt", doc.Name);
var text = await doc.GetTextAsync();
Assert.Equal("Dummy0", text.ToString());
}
}

[Fact]
public async Task WhenProjectIsMultiAdditionalFiles()
{
using (var testProject = await TestAssets.Instance.GetTestProjectAsync("ProjectWithAdditionalFiles"))
using (var host = CreateMSBuildTestHost(testProject.Directory))
{
var project = host.Workspace.CurrentSolution.Projects.Where(x => x.AssemblyName == "ProjectWithMultiAdditionalFiles").Single();
var additionalDocuments = project.AdditionalDocuments.ToList();
Assert.Equal(3, additionalDocuments.Count());

for (int i = 0; i < 3; i++)
{
var doc = additionalDocuments[i];
Assert.Equal(string.Format("dummy{0}.txt", i), doc.Name);
var text = await additionalDocuments[i].GetTextAsync();
Assert.Equal(string.Format("Dummy{0}", i), text.ToString());
}
}
}
}
}