Roslyn - save edited document to physical solution - roslyn

I'm trying to iterate through all documents, change some code and then rewrite analyzed document with new Syntax. The only problem I have it's that i don't know how to save changed document via workspace. After my code execution nothing happens.
var manager = new AnalyzerManager(slnPath);
foreach (var project in manager.Projects.Values)
{
var workspace = project.GetWorkspace();
var sln = workspace.CurrentSolution;
foreach (var msBuildProject in sln.Projects)
{
foreach (var document in msBuildProject.Documents)
{
var tree = await document.GetSyntaxTreeAsync();
var root = tree.GetRoot();
var compilation = await msBuildProject.GetCompilationAsync();
var walker = new CustomRewriter(compilation, tree);
var newRoot = walker.Visit(root);
if (!walker.Edited)
continue;
var editor = await DocumentEditor.CreateAsync(document);
editor.ReplaceNode(root, newRoot);
var newDocument = editor.GetChangedDocument();
workspace.TryApplyChanges(newDocument.Project.Solution);
break;
}
}
}
EDIT: Workspace is fetched with help of the Buildalyzer package.

Buildalyzer uses the adhoc workspace which does not persist changes. You want to use the MSBuildWorkspace.
You should have a project file that looks something like this
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net48</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Build.Locator" Version="1.2.6" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="2.9.8" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.3.1" />
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic.Workspaces" Version="3.3.1" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="3.3.1" />
</ItemGroup>
</Project>
And then this is how I would load your solution and run your rewriter
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Build.Locator;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.MSBuild;
class Program {
static async Task Main(string[] args) {
// Attempt to set the version of MSBuild.
var visualStudioInstances = MSBuildLocator.QueryVisualStudioInstances().ToArray();
var instance = visualStudioInstances.Length == 1
// If there is only one instance of MSBuild on this machine, set that as the one to use.
? visualStudioInstances[0]
// Handle selecting the version of MSBuild you want to use.
: SelectVisualStudioInstance(visualStudioInstances);
Console.WriteLine($"Using MSBuild at '{instance.MSBuildPath}' to load projects.");
// NOTE: Be sure to register an instance with the MSBuildLocator
// before calling MSBuildWorkspace.Create()
// otherwise, MSBuildWorkspace won't MEF compose.
MSBuildLocator.RegisterInstance(instance);
using (var workspace = MSBuildWorkspace.Create()) {
// Print message for WorkspaceFailed event to help diagnosing project load failures.
workspace.WorkspaceFailed += (o, e) => Console.WriteLine(e.Diagnostic.Message);
var solutionPath = args[0];
Console.WriteLine($"Loading solution '{solutionPath}'");
// Attach progress reporter so we print projects as they are loaded.
var solution = await workspace.OpenSolutionAsync(solutionPath, new ConsoleProgressReporter());
Console.WriteLine($"Finished loading solution '{solutionPath}'");
// Run your custome re-writer on the loaded solution
foreach (var msBuildProject in solution.Projects) {
foreach (var document in msBuildProject.Documents) {
var tree = await document.GetSyntaxTreeAsync();
var root = tree.GetRoot();
var compilation = await msBuildProject.GetCompilationAsync();
var walker = new CustomRewriter(compilation, tree);
var newRoot = walker.Visit(root);
if (!walker.Edited)
continue;
var editor = await DocumentEditor.CreateAsync(document);
editor.ReplaceNode(root, newRoot);
var newDocument = editor.GetChangedDocument();
workspace.TryApplyChanges(newDocument.Project.Solution);
break;
}
}
}
}
private static VisualStudioInstance SelectVisualStudioInstance(VisualStudioInstance[] visualStudioInstances) {
Console.WriteLine("Multiple installs of MSBuild detected please select one:");
for (int i = 0; i < visualStudioInstances.Length; i++) {
Console.WriteLine($"Instance {i + 1}");
Console.WriteLine($" Name: {visualStudioInstances[i].Name}");
Console.WriteLine($" Version: {visualStudioInstances[i].Version}");
Console.WriteLine($" MSBuild Path: {visualStudioInstances[i].MSBuildPath}");
}
while (true) {
var userResponse = Console.ReadLine();
if (int.TryParse(userResponse, out int instanceNumber) &&
instanceNumber > 0 &&
instanceNumber <= visualStudioInstances.Length) {
return visualStudioInstances[instanceNumber - 1];
}
Console.WriteLine("Input not accepted, try again.");
}
}
private class ConsoleProgressReporter : IProgress<ProjectLoadProgress> {
public void Report(ProjectLoadProgress loadProgress) {
var projectDisplay = Path.GetFileName(loadProgress.FilePath);
if (loadProgress.TargetFramework != null) {
projectDisplay += $" ({loadProgress.TargetFramework})";
}
Console.WriteLine($"{loadProgress.Operation,-15} {loadProgress.ElapsedTime,-15:m\\:ss\\.fffffff} {projectDisplay}");
}
}
}

Related

How to unit test a repository and mock db with moq

I am onboarding alone on an existing project that do not have any unit test. My first goal before any refactoring is to cover 100% of the code. I would like to avoid any regression.
I have read how do I mock sqlconnection or should I refactor the code? but my case as you can see below is quite different cause I need to do more than stub simply the sqlConnection. Basically, I need to mock the db. I would like to know the best approach to achieve it.
(By the way, I do not want to use any ORM such as Entity).
Below the code of the repository :
public class HotelRepository : IHotelRepository
{
private readonly IDbConnection _dbConnection;
private readonly ILogService _loggerService;
public HotelRepository(IDbConnection dbConnection, ILogService loggerService)
{
_dbConnection = dbConnection;
_loggerService = loggerService;
}
public HotelDo GetByRid(string rid)
{
return Find(rid).FirstOrDefault();
}
public List<HotelDo> Find(string text)
{
try
{
_dbConnection.Open();
var items = new List<HotelDo>();
using (var command = _dbConnection.CreateCommand())
{
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "dbo.HotelSearchByRidOrName";
command.Parameters.Add(new SqlParameter("#Text", text));
using (var reader = command.ExecuteReader(CommandBehavior.CloseConnection))
{
while (reader.Read())
{
items.Add(new HotelDo()
{
Name = SqlExtension.ReaderToStringConverter(reader["Name"]),
Id = SqlExtension.ReaderToIntConverter(reader["Id"]),
Rid = SqlExtension.ReaderToStringConverter(reader["RIDHotel"]),
IdPms = SqlExtension.ReaderToNullableIntConverter(reader["IdPms"]),
LinkResaWeb = SqlExtension.ReaderToStringConverter(reader["LinkResaWeb"]),
LinkPms = SqlExtension.ReaderToStringConverter(reader["LinkPms"]),
IdBrand = SqlExtension.ReaderToNullableIntConverter(reader["IdBrand"]) ?? 0,
IsOnline = SqlExtension.ReaderToBoolConverter(reader["IsOnline"]) ?? false,
CodeCountry = SqlExtension.ReaderToStringConverter(reader["CodeCountry"])
});
}
}
}
return items;
}
catch (Exception e)
{
var errorMessage = $"HotelRepository Find, text {text} ";
_loggerService.Trace(LogSeverity.Error, errorMessage, e);
throw new DalException() { Source = errorMessage, };
}
finally
{
_dbConnection.Close();
}
}
public List<HotelDo> GetAll()
{
try
{
_dbConnection.Open();
var items = new List<HotelDo>();
using (var command = _dbConnection.CreateCommand())
{
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "dbo.HotelGetAll";
using (var reader = command.ExecuteReader(CommandBehavior.CloseConnection))
{
while (reader.Read())
{
bool.TryParse(reader["IsOnline"].ToString(), out var isOnline);
items.Add(new HotelDo()
{
Id = SqlExtension.ReaderToIntConverter(reader["Id"]),
Rid = SqlExtension.ReaderToStringConverter(reader["RIDHotel"]),
Name = SqlExtension.ReaderToStringConverter(reader["Name"]),
CodeCountry = SqlExtension.ReaderToStringConverter(reader["CodeCountry"]),
LinkPms = SqlExtension.ReaderToStringConverter(reader["LinkPms"]),
IdPms = SqlExtension.ReaderToNullableIntConverter(reader["IdPms"]),
IdBrand = SqlExtension.ReaderToNullableIntConverter(reader["IdBrand"]) ?? 0,
LinkResaWeb = SqlExtension.ReaderToStringConverter(reader["LinkResaWeb"]),
IsOnline = isOnline
});
}
}
}
return items;
}
catch (Exception e)
{
var errorMessage = $"HotelRepository GetAllHotels";
_loggerService.Trace(LogSeverity.Error, errorMessage, e);
throw new DalException() { Source = errorMessage, };
}
finally
{
_dbConnection.Close();
}
}
}
Thank you for your help
So based on what you've got I've set up a test frame for you to follow. I've removed less important components for breviety.
Before you you jump in I just want to give my 2 cents, if you don't know how to do this sort of thing you seem to be more or less in a junior position or less experiance with C# and also found your self in a more or less mature company that doesn't care about the development deparment, since an uncovered project just get's thrown your way says you don't have alot of resources to go about to imrpove the code base.
Test only the things that have business value (can you put a price on the piece of logic if it brakes)
Delivering stuff faster will make you look better as a programmer (noone in the business gives a damn about test covarage)
Study hard, get your experiance, good reputation and don't be afraid to get the hell out of there as soon as you start getting bored.
Main
void Main()
{
var idIndex = 0;
var ids = new string[] { "1", "2" };
var mockDataReader = new Mock<IDataReader>();
mockDataReader.SetupSequence(x => x.Read()).Returns(true).Returns(true).Returns(false);
mockDataReader.SetupGet(x => x["Id"]).Returns(() => ids[idIndex]).Callback(() => idIndex++);
var mockParameters = new Mock<IDataParameterCollection>();
var mockCommand = new Mock<IDbCommand>();
mockCommand.SetupGet(x => x.Parameters).Returns(mockParameters.Object);
mockCommand.Setup(x => x.ExecuteReader(CommandBehavior.CloseConnection)).Returns(mockDataReader.Object);
var mockConnection = new Mock<IDbConnection>();
mockConnection.Setup(x => x.CreateCommand()).Returns(mockCommand.Object);
var repo = new HotelRepository(mockConnection.Object);
var result = repo.Find("search");
Assert.Equal("1", result[0].Id);
Assert.Equal("2", result[1].Id);
}
Repository
public class HotelRepository
{
private readonly IDbConnection _dbConnection;
public HotelRepository(IDbConnection dbConnection)
{
_dbConnection = dbConnection;
}
public List<Pony> Find(string text)
{
_dbConnection.Open();
var items = new List<Pony>();
using (var command = _dbConnection.CreateCommand())
{
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "dbo.HotelSearchByRidOrName";
command.Parameters.Add(new SqlParameter("#Text", text));
using (var reader = command.ExecuteReader(CommandBehavior.CloseConnection))
{
while (reader.Read())
{
items.Add(new Pony
{
Id = reader["Id"].ToString()
});
}
}
}
return items;
}
}
Just a dumb o'l pony
public class Pony {
public string Id { get; set; }
}

Compile to module with Roslyn

I need to compile any C# or VB.NET project to .NetModule. I have following sample code which emits DLLs, Need some help to modify following to get .NetModules out from .csproj
Thanks in advance.
// Required Microsoft.CodeAnalysis 1.3.0
class Program
{
static void Main(string[] args)
{
try
{
//Please copy provide a path to a .csproj
CompileProject(#"C:\WebGoat\WebGoat.NET.csproj", #"C:\tempout").Wait();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
async static Task<string> CompileProject(string projectFilePath, string outputFolderPath)
{
using (var workspace = MSBuildWorkspace.Create())
{
var project = workspace.OpenProjectAsync(projectFilePath).Result;
await Emit(project, outputFolderPath);
return Path.GetFileName(project.OutputFilePath);
}
}
async static Task Emit(Project project, string outputFolderPath)
{
Directory.CreateDirectory(outputFolderPath);
var compilation = await project.GetCompilationAsync();
var outputFilePath = Path.Combine(outputFolderPath, Path.GetFileName(project.OutputFilePath));
var pdbFilePath = Path.ChangeExtension(outputFilePath, "pdb");
var compilationStatus = compilation.Emit(outputFilePath, pdbPath: pdbFilePath);
if (!compilationStatus.Success)
{
Console.WriteLine("Failed.");
}
else
{
Console.WriteLine("Pass.");
}
}
I believe you're looking for CompilationOptions.OutputKind and specifically OutputKind.NetModule.
Something similar to the following should work:
var project = workspace.OpenProjectAsync(projectFilePath).Result;
var options = project.CompilationOptions;
var netModuleOptions = options.WithOutputKind(OutputKind.NetModule);
var projectWithOptions = project.WithCompilationOptions(netModuleOptions);
Now you should be able to get a compilation and emit it as you normally would.
Following fixed the issue
var project = workspace.OpenProjectAsync(projectFilePath).Result;
var options = project.CompilationOptions;
options = options.WithOutputKind(OutputKind.NetModule).WithPlatform(Platform.AnyCpu);
project = project.WithCompilationOptions(options);
var moduleCompilation = await project.GetCompilationAsync();

wso2 carbon how to fix Cannot read property 'getTenantDomain' of undefined at getGadgetLocation?

I'm currently working in wso2 carbon. I have developed one gadget and successfully integrated as well but gadget-util.js file showing an error of
Uncaught TypeError: Cannot read property 'getTenantDomain' of undefined
at getGadgetLocation
my code is gadget-util.js
var getGadgetLocation = function (callback) {
var gadgetLocation = "/portal/store/carbon.super/fs/gadget/circle_d3";
var PATH_SEPERATOR = "/";
if (gadgetLocation.search("store") != -1)
{
wso2.gadgets.identity.getTenantDomain(function (tenantDomain) {
var gadgetPath = gadgetLocation.split(PATH_SEPERATOR);
var modifiedPath = '';
for (var i = 1; i < gadgetPath.length; i++) {
if (i === 3) {
modifiedPath = modifiedPath.concat(PATH_SEPERATOR, tenantDomain);
} else {
modifiedPath = modifiedPath.concat(PATH_SEPERATOR, gadgetPath[i])
}
}
callback(modifiedPath);
});
} else {
callback(gadgetLocation)
}
callback(gadgetLocation);
}
please help me
You need to require identity feature[1] in gadget.xml
<Require feature="wso2-gadgets-identity" />
e.g https://github.com/wso2/product-das/blob/master/modules/distribution/src/repository/conf/template-manager/gadget-templates/numberchart/gadget.xml#L5
[1] http://mail.wso2.org/mailarchive/dev/2016-August/066568.html

Mocking ASP.NET MVC Controller properties

I have a MVC controller that loads a resource file and uses Server.MapPath to get the path to the file. I want to mock out the Server property in the controller object using Microsoft Fakes framework (I know how to do this using other frameworks).
Here's the code:
[HttpGet]
public ActionResult GeneratePdf(string reportId)
{
var template = LoadTemplate(reportId);
var document = pdfWriter.Write(GetReportModel(reportId), template);
return File(document, MediaTypeNames.Application.Pdf);
}
private byte[] LoadTemplate(string reportId)
{
var templatePath = Server.MapPath(string.Format("~/ReportTemplates/{0}.docx", reportId));
using(var templateContent = System.IO.File.OpenText(templatePath))
{
return Encoding.Default.GetBytes(templateContent.ReadToEnd());
}
}
The part I'm trying to mock out is the "Server.MapPath" method.
As of Visual Studio 2012 Update 1, you can detour the Controller.Server property using Stubs.
With the following .Fakes file in your test project:
<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/" Diagnostic="true">
<Assembly Name="System.Web" Version="4.0.0.0"/>
<StubGeneration>
<Clear/>
<Add FullName="System.Web.HttpContextBase!"/>
<Add FullName="System.Web.HttpServerUtilityBase!"/>
</StubGeneration>
<ShimGeneration>
<Clear/>
</ShimGeneration>
</Fakes>
You can write the a unit test like this:
[TestMethod]
public void TestMethod1()
{
var target = new TestController();
var serverStub = new StubHttpServerUtilityBase();
serverStub.MapPathString = (path) => path.Replace("~", string.Empty).Replace("/", #"\");
var contextStub = new StubHttpContextBase();
contextStub.ServerGet = () => serverStub;
target.ControllerContext = new ControllerContext();
target.ControllerContext.HttpContext = contextStub;
var result = (FilePathResult) target.Index();
Assert.AreEqual(#"\Content\Test.txt", result.FileName);
}
With the upcoming Update 2, you will also be able to detour Controller.Server property directly using Shims. Here is the additional .Fakes file you will need with this approach.
<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/" Diagnostic="true">
<Assembly Name="System.Web.Mvc" Version="4.0.0.0"/>
<StubGeneration>
<Clear/>
</StubGeneration>
<ShimGeneration>
<Clear/>
<Add FullName="System.Web.Mvc.Controller!"/>
</ShimGeneration>
</Fakes>
And here is the test:
[TestMethod]
public void TestMethod2()
{
using (ShimsContext.Create())
{
var target = new TestController();
var serverStub = new StubHttpServerUtilityBase();
serverStub.MapPathString = (path) => path.Replace("~", string.Empty).Replace("/", #"\");
var controllerShim = new ShimController(target);
controllerShim.ServerGet = () => serverStub;
var result = (FilePathResult)target.Index();
Assert.AreEqual(#"\Content\Test.txt", result.FileName);
}
}
Please note that this approach does not work in the current version (Update 1), due to limitations in Fakes runtime related to assemblies, like System.Web.Mvc that allow partially trusted callers. If you try to run the second test today, you will get a VerificationException.

How do I get the output directories from the last build?

Let's say I've got a solution with one or more projects, and I've just kicked off a build using the following method:
_dte.Solution.SolutionBuild.Build(true); // EnvDTE.DTE
How can I get the output paths for each project that just built? For example...
c:\MySolution\Project1\Bin\x86\Release\
c:\MySolution\Project2\Bin\Debug
Please don't tell me this is the only way...
// dte is my wrapper; dte.Dte is EnvDte.DTE
var ctxs = dte.Dte.Solution.SolutionBuild.ActiveConfiguration
.SolutionContexts.OfType<SolutionContext>()
.Where(x => x.ShouldBuild == true);
var temp = new List<string>(); // output filenames
// oh shi
foreach (var ctx in ctxs)
{
// sorry, you'll have to OfType<Project>() on Projects (dte is my wrapper)
// find my Project from the build context based on its name. Vomit.
var project = dte.Projects.First(x => x.FullName.EndsWith(ctx.ProjectName));
// Combine the project's path (FullName == path???) with the
// OutputPath of the active configuration of that project
var dir = System.IO.Path.Combine(
project.FullName,
project.ConfigurationManager.ActiveConfiguration
.Properties.Item("OutputPath").Value.ToString());
// and combine it with the OutputFilename to get the assembly
// or skip this and grab all files in the output directory
var filename = System.IO.Path.Combine(
dir,
project.ConfigurationManager.ActiveConfiguration
.Properties.Item("OutputFilename").Value.ToString());
temp.Add(filename);
}
This makes me want to retch.
You can get to the output folder(s) by traversing the file names in the Built output group of each project in EnvDTE:
var outputFolders = new HashSet<string>();
var builtGroup = project.ConfigurationManager.ActiveConfiguration.OutputGroups.OfType <EnvDTE.OutputGroup>().First(x => x.CanonicalName == "Built");
foreach (var strUri in ((object[])builtGroup.FileURLs).OfType<string>())
{
var uri = new Uri(strUri, UriKind.Absolute);
var filePath = uri.LocalPath;
var folderPath = Path.GetDirectoryName(filePath);
outputFolders.Add(folderPath.ToLower());
}