Skip to content

Commit

Permalink
Use AdhocWorkspace instead of MSBuildWorkspace (#1987)
Browse files Browse the repository at this point in the history
* update Versions.props to preview 8.

* Use Document.Name for filepath

Co-authored-by: deepchoudhery <decho@microsoft.com>
  • Loading branch information
zahalzel and deepchoudhery authored Aug 16, 2022
1 parent ef571b9 commit f2c6b26
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public async Task AddAuthCodeAsync()
}

// Initialize CodeAnalysis.Project wrapper
CodeAnalysis.Project project = await CodeAnalysisHelper.LoadCodeAnalysisProjectAsync(_toolOptions.ProjectFilePath, _files);
CodeAnalysis.Project project = CodeAnalysisHelper.LoadCodeAnalysisProject(_toolOptions.ProjectFilePath, _files);
if (project is null)
{
return;
Expand Down Expand Up @@ -228,8 +228,8 @@ internal async Task ModifyCsFile(CodeFile file, CodeAnalysis.Project project, Co
file.FileName = await ProjectModifierHelper.GetStartupClass(project.Documents.ToList()) ?? file.FileName;
}

var fileDoc = project.Documents.Where(d => d.Name.Equals(file.FileName)).FirstOrDefault();
if (fileDoc is null || string.IsNullOrEmpty(fileDoc.FilePath))
var fileDoc = project.Documents.Where(d => d.Name.EndsWith(file.FileName)).FirstOrDefault();
if (fileDoc is null || string.IsNullOrEmpty(fileDoc.Name))
{
return;
}
Expand All @@ -246,7 +246,7 @@ internal async Task ModifyCsFile(CodeFile file, CodeAnalysis.Project project, Co
if (modifiedRoot != null)
{
documentEditor.ReplaceNode(documentEditor.OriginalRoot, modifiedRoot);
await documentBuilder.WriteToClassFileAsync(fileDoc.FilePath);
await documentBuilder.WriteToClassFileAsync(fileDoc.Name);
_output.AppendLine($"Modified {file.FileName}"); // TODO strings.
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.MSBuild;
using System.IO;
using Microsoft.CodeAnalysis;

namespace Microsoft.DotNet.Scaffolding.Shared.CodeModifier
{
//static class helper for CodeAnalysis
public static class CodeAnalysisHelper
{
//helps create a CodeAnalysis.Project with project files given a project path.
public static async Task<CodeAnalysis.Project> LoadCodeAnalysisProjectAsync(
public static CodeAnalysis.Project LoadCodeAnalysisProject(
string projectFilePath,
IEnumerable<string> files)
{
var workspace = MSBuildWorkspace.Create();
var project = await workspace.OpenProjectAsync(projectFilePath);
var workspace = new AdhocWorkspace();
var project = workspace.AddProject(Path.GetFileName(projectFilePath), "C#");
var projectWithFiles = project.WithAllSourceFiles(files);
project = projectWithFiles ?? project;
return project;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
using Microsoft.DotNet.MSIdentity.Shared;
using Microsoft.DotNet.Scaffolding.Shared.CodeModifier;
using Microsoft.DotNet.Scaffolding.Shared.CodeModifier.CodeChange;

Expand Down Expand Up @@ -497,10 +496,7 @@ internal static async Task<Document> ModifyDocumentText(Document fileDoc, IEnume
internal static async Task<string> UpdateDocument(Document document)
{
var classFileTxt = await document.GetTextAsync();

// Note: For files without .cs extension, document.Name is the full filepath
var filePath = document.Name.EndsWith(".cs") ? document.FilePath : document.Name;
File.WriteAllText(filePath, classFileTxt.ToString(), new UTF8Encoding(false));
File.WriteAllText(document.Name, classFileTxt.ToString(), new UTF8Encoding(false));

return $"Modified {document.Name}.\n"; // todo strings.
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Collections.Generic;
using Microsoft.DotNet.Scaffolding.Shared.CodeModifier;
using Xunit;

namespace Microsoft.DotNet.Scaffolding.Shared.Tests
{
public class CodeAnalysisHelperTests
{
[Fact]
public void LoadCodeAnalysisProjectTest()
{
var files = new List<string>();
var projectPath = string.Empty; // TODO test project
var project = CodeAnalysisHelper.LoadCodeAnalysisProject(projectPath, files);
Assert.NotNull(project);
}
}
}

0 comments on commit f2c6b26

Please sign in to comment.