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
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);
}
}
}