Can roslyn be used to compile code in Windows 8 Apps ?

Topics: APIs, General
Oct 21, 2014 at 11:27 AM
Hi,
I want to know whether I can use Roslyn to compile windows 8 apps ? and If can please let me know how ? Any help would be appreciated ?
Developer
Oct 21, 2014 at 6:56 PM
If you are using Visual Studio "14" CTP 4 then everything you compile uses the roslyn compiler, including Windows 8 apps. Is there a scenario other than Visual Studio where you want to compile Windows 8 apps?
Oct 22, 2014 at 4:21 AM
Thank you very much for the information. Actually I am quite new to the Roslyn and I am sorry if I am being super dumb, Is it possible for me to go ahead and compile source taken from a text input and generate the output( file )? Any guidance would be highly appreciated.

Thanks again
Developer
Oct 22, 2014 at 7:09 PM
Its certainly possible :) We have several apis you could use. Here is a quick example. Let me know which scenario you are interested in.
using System.IO;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.MSBuild;
using Microsoft.CodeAnalysis.Text;

class Program
{
    private static readonly string code = @"
using System;

class HelloWorld
{
    static void Main(string[] args)
    {
        Console.WriteLine(""Hello World!"");
    }
}";

    static void Main(string[] args)
    {
        CompileFromString(code, "HelloWorld.exe");
        CompileFromFile(@"Path\To\HelloWorld.cs", "HelloWorld.exe");
        CompileFromProject(@"Path\To\HelloWorld.csproj", "HelloWorld.exe");
    }

    static void CompileFromString(string code, string outputPath)
    {
        var sourceText = SourceText.From(code);
        var tree = CSharpSyntaxTree.ParseText(sourceText);
        var references = new[] { MetadataReference.CreateFromAssembly(typeof(object).Assembly) };
        var options = new CSharpCompilationOptions(OutputKind.ConsoleApplication);
        var compilation = CSharpCompilation.Create("HelloWorld", new[] { tree }, references, options);
        var outputFileStream = File.Create(outputPath);
        compilation.Emit(outputFileStream);
    }

    static void CompileFromFile(string sourceCodeFilePath, string outputPath)
    {
        var inputFileStream = File.OpenRead(sourceCodeFilePath);
        var sourceText = SourceText.From(inputFileStream);
        var tree = CSharpSyntaxTree.ParseText(sourceText);
        var references = new[] { MetadataReference.CreateFromAssembly(typeof(object).Assembly) };
        var options = new CSharpCompilationOptions(OutputKind.ConsoleApplication);
        var compilation = CSharpCompilation.Create("HelloWorld", new[] { tree }, references, options);
        var outputFileStream = File.Create(outputPath);
        compilation.Emit(outputFileStream);
    }

    static void CompileFromProject(string projectFilePath, string outputPath)
    {
        var workspace = MSBuildWorkspace.Create();
        var project = workspace.OpenProjectAsync(projectFilePath).GetAwaiter().GetResult();
        var compilation = project.GetCompilationAsync().GetAwaiter().GetResult();
        var outputFileStream = File.Create(outputPath);
        compilation.Emit(outputFileStream);
    }

}
Marked as answer by roshan605 on 10/22/2014 at 9:10 PM
Oct 23, 2014 at 5:10 AM
Edited Oct 23, 2014 at 5:13 AM
Thank you .. !! you are amazing ! And how is it possible to derive intellisense features aswell ?
Developer
Oct 23, 2014 at 6:38 AM
We don't have an API that exactly duplicates intellisense, but we do have some that are similar.
LookupBaseMembers
LookupLabels
LookupNamespacesAndTypes
LookupStaticMembers


Here's quick contrived example:
using System;
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.MSBuild;

class Program
{
    static void Main(string[] args)
    {
        var namespacesAndTypesInScope = GetNamespacesAndTypesInScope(@"Path\To\HelloWorld.csproj").GetAwaiter().GetResult();
        Console.WriteLine(
            string.Join(
                Environment.NewLine, 
                namespacesAndTypesInScope.Select(x => x.ToDisplayString())));
    }

    static async Task<ImmutableArray<ISymbol>> GetNamespacesAndTypesInScope(string projectFilePath)
    {
        // load the given project
        var workspace = MSBuildWorkspace.Create();
        var project = await workspace.OpenProjectAsync(projectFilePath);

        // grab the first method declared in the first document
        var document = project.Documents.First();
        var rootNode = await document.GetSyntaxRootAsync();
        var firstMethodInDocument = (from syntaxNode in rootNode.DescendantNodes()
                                     where syntaxNode.IsKind(SyntaxKind.MethodDeclaration)
                                     select syntaxNode as MethodDeclarationSyntax)
                                     .First();

        // grab the first statement inside the method body
        var firstStatementInMethod = firstMethodInDocument.Body.ChildNodes().First();

        // get a list of all namespaces and types in scope at the statements location
        var semanticModel = await document.GetSemanticModelAsync();
        return semanticModel.LookupNamespacesAndTypes(firstStatementInMethod.Span.Start);
    }

}