Roslyn: How to update a document - roslyn

I'm new to Visual Studio extensions. I'm developing a Menu Command extension to add a using directive to my class. So far, I could successfully create a new Document object containing the changes:
var syntaxTree = await sourceDocument.GetSyntaxTreeAsync();
var unitRoot = syntaxTree.GetCompilationUnitRoot();
var qualifiedName = SyntaxFactory.ParseName("MyApp.Utilities"); // using MayApp.Utilities
var usingDirective = SyntaxFactory.UsingDirective(qualifiedName);
unitRoot = unitRoot.AddUsings(usingDirective);
var newDocument = sourceDocument.WithSyntaxRoot(unitRoot);
The problem is it doesn't reflect the changes back to the source code (or workspace if it's correct term).
Any idea and suggestion is appreciated.

Related

Postman - Can you use the {{...}} syntax within a Test script?

I have an environment variable called "url", the value is a combination of several other variables in the same environment.
Here is the bulk environment variable definition:
scheme:http
server:localhost
port::55881
application:/
url:{{scheme}}://{{server}}{{port}}{{application}}
As you can see, url contains other variables.
This works great in the actual request (I'm using {{url}} when addressing my service), but when I try to use the same variable in my scripted tests (In the Tests tab), I'm getting the un-evaluated version.
var serviceUrl = pm.variables.get("url");
console.log(serviceUrl); //Yields {{scheme}}://{{server}}{{port}}{{application}}
Is there a way to get the evaluated value inside my tests?
Thanks!
Complete test for more insight:
var jsonData = JSON.parse(responseBody);
tests["Status code is 200"] = responseCode.code === 200;
var ordrereferanse = jsonData.Ordrereferanse;
tests.OrdreReferanse = ordrereferanse.length > 0;
//Have to do this
var scheme = pm.variables.get("scheme");
var server = pm.variables.get("server");
var port = pm.variables.get("port");
var application = pm.variables.get("application");
var api_key = pm.variables.get("api_key");
var serviceUrl = scheme + "://" + server + port + application;
//Instead of this - an environment variable defined like this "{{scheme}}://{{server}}{{port}}{{application}}"
//var serviceUrl = pm.variables.get("url");
//remaining test - go to url to verify that the resource is created and the order reference is set
var infoUrl = serviceUrl + "ordreinformasjon/" + ordrereferanse + "?format=json&api_key=" + api_key;
pm.sendRequest(infoUrl, function (err, response) {
var info = response.json();
console.log(info);
tests.OrdreInformasjonOrdreReferanse = info.OrdreReferanse === ordrereferanse;
});
This would work but I'm not sure what you're trying to achieve:
var scheme = pm.variables.get("scheme")
var server = pm.variables.get("server")
var port = pm.variables.get("port")
var application = pm.variables.get("application")
console.log(`${scheme}://${server}${port}${application}`)
That would log out http://localhost:55881/ to the console.
The {{...}} syntax doesn't work in the way that you had it in the environment file. As it's just storing everything as a string so that's why you would get that output.
You could use {{scheme}}://{{server}}{{port}}{{application}} as the URL but not in the tests using the same syntax.
UPDATE
After seeing the update to the question - Could you not combine the separate variables into a single url variable and construct the infoUrl variable in the following way:
var infoUrl = `${pm.variables.get("url")}ordreinformasjon/${ordrereferanse}?format=json&api_key=${pm.variables.get("api_key")}`
Then use a different environment file with the same url key but with a different value if you need to point the request at a staging or production URL.
I've also noticed that you're using the older tests syntax rather than the newer pm.test() syntax, that might clean up some of the code for you.

How do I get the Code Review Id in a custom section of the code review page?

I am writing a visual studio extension that has a section on the code review page. I would like access to the information about the rest of the code review page, specifically what code review is current on the page being displayed. I should be able to access the workitemId but so far I have not figured out how.
Edit
Using Cole's suggestion I have accessed the PageContent but I do not know what type I should cast the content to. Nor do I know when it will be available. I would like access both when I initialize my section, and later. Here is my code when I try to initialize the section:
public override object SectionContent
{
get
{
if (base.SectionContent == null)
{
var teamExplorerPage = this.GetService<ITeamExplorerPage>();
var pageContent = teamExplorerPage.PageContent;
this.model.CodeReviewId = pageContent;
base.SectionContent = new CodePlusTeamExplorerSectionView(this.ServiceProvider, this.model);
}
return base.SectionContent;
}
}
When I debug the code I see that a DataContext is available on the PageContent, but I do not know what type to cast the pageContent (orITeamExplorerPage) to, to gain access to it. Also the DataContext has a CodeReviewId property which seems like the value I need but it is null at this point of the Lifecycle. If I want to retrieve some additional data based on the CodeReviewId when/where is it available?
I am still trying to figure out how to get it earlier in the lifecycle of the page but from an event like a button click in the page I can retrieve the value using reflection. This seems like a bit of a hack, perhaps someone else has a better way, but here it is.
private void Button_Click(object sender, RoutedEventArgs e)
{
var teamExplorer = this.GetService<ITeamExplorer>();
var currentPage = teamExplorer.CurrentPage;
var currentContent = currentPage.PageContent;
Type type = currentContent.GetType();
PropertyInfo pi = type.GetProperty("DataContext");
var dataContext = pi.GetValue(currentContent);
Type contextTypetype = dataContext.GetType();
PropertyInfo contextTypePi = contextTypetype.GetProperty("CodeReviewId");
var codeReviewId = contextTypePi.GetValue(dataContext);
var context = new Dictionary<string, object>();
context.Add("WorkItemId", codeReviewId);
teamExplorer.NavigateToPage(new Guid(TeamExplorerPageIds.ViewCodeReview), context);
}

Using NAudio/Nlayer to mix two mp3 files

In reference to this question: NoDriver calling acmFormatSuggest on Azure
My hosting server does not allow me to install anything or register dlls. I am using Naudio to mix to mp3 files and it gave me the error NoDriver calling acmFormatSuggest.
I downloaded and installed Nlayer in my application and modified the code to look like this:
var builderBackground = new Mp3FileReader.FrameDecompressorBuilder(wf => new Mp3FrameDecompressor(wf));
var builderMessage = new Mp3FileReader.FrameDecompressorBuilder(wf => new Mp3FrameDecompressor(wf));
Mp3FileReader mpbacground = new Mp3FileReader(ThumbAudioMP3, builderBackground); Mp3FileReader mpMessage = new Mp3FileReader(stream, builderMessage);
background = WaveFormatConversionStream.CreatePcmStream(mpbacground);
message = WaveFormatConversionStream.CreatePcmStream(mpMessage);
var mixer = new WaveMixerStream32(); var messageOffsetted = new WaveOffsetStream(message, TimeSpan.FromSeconds(0), TimeSpan.Zero, TimeSpan.FromSeconds(seconds));
I get the same NoDriver calling acmFormatSuggest error in the line WaveFormatConversionStream.CreatePcmStream(...
Can someone tell me how I should be doing this? Any documentation on Nlayer?
You don't need the WaveFormatConversionStream.CreatePcmStream lines. The Mp3FileReader classes will already emit PCM.

How to get a particular folder in JSOM like GetFolder("FolderName") in CSOM?

I am trying to get a particular folder using JSOM, in order to set group permissions. There is group called NewGroup which should assigned to a folder called NewFolder in the Document Library called Shared Documents.
var clientContext = new SP.ClientContext.get_current();
var oWebsite = clientContext.get_web();
var collGroup = oWebsite.get_siteGroups();
var oGroup = collGroup.getByName("NewGroup");
var role = SP.RoleDefinitionBindingCollection.newObject(clientContext);
role.add(oWebsite.get_roleDefinitions().getByType(SP.RoleType.contributor));
var oFolder = oWebsite.getFolderByServerRelativeUrl("/ActivityTracker/Shared%20Documents/NewFolder");
oFolder.breakRoleInheritance(false, true);
oFolder.get_roleAssignments().add(oGroup, role)
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
I am unable to get the result using this code. Please correct me where I am wrong.

Roslyn Workspace API : Emit each Project inside Solution to dll

I started to wonder how to emit C# Projects using Workspace API and Compiler API from Rolsyn.
This what I get so far:
var msBuild = MSBuildWorkspace.Create();
var sln = msBuild.OpenSolutionAsync
(#"D:\User\Documents\visual studio 14\Projects\ConsoleApplicationWorkspaces"
+#"\ConsoleApplicationWorkspaces.sln").Result;
foreach (var item in sln.Projects)
{
EmitProject(item);
}
public static async void EmitProject(Project proj)
{
var c = await proj.GetCompilationAsync();
var options = new CSharpCompilationOptions
(OutputKind.DynamicallyLinkedLibrary);
c = c.WithOptions(options);
c = c.AddReferences(proj.MetadataReferences);
var result = c.Emit("my" + proj.Name + ".dll");
Console.WriteLine(r.Success);
}
This code don't work.
According to diagnostic info I didn't add references like "System.Runtime","System.Linq".
Using Workspace API I can get references I need but I guess I still I am adding them wrong.
You shouldn't need to change the options or references that come back from Project.GetCompilationAsync. You should just be able to call Emit directly.
Note that you should emit the projects in topological sort order, which you can get from Solution.GetProjectDependencyService.