Programmatic Usage

FlexCompiler can be accessed in code. It's under the namespace FlexFramework.FlexCompiler. Your code must be under any "Editor" folder since FlexCompiler is an editor-only reference.

Build task

The quickest way to build source files is to use BuildTask. A BuildTask contains information of source files and build settings.

This example adds a "Quick Compile" menu when you right click on assets in Unity's project window:

using UnityEditor;
using FlexFramework.FlexCompiler.Compilation;
using FlexFramework.FlexCompiler.Editor;
using System;
using System.IO;
using UnityEngine;

public class MyEditor
{
    [MenuItem("Assets/Quick Compile")]
    public static void BuildSelected()
    {
        var output = EditorUtility.SaveFilePanel("Build output path", Environment.CurrentDirectory, Application.productName, "dll");
        if (string.IsNullOrEmpty(output))
        {
            return;
        }
        var task = BuildTask.Create(ScriptHelper.GetSelected());
        var compilation = task.Build(Path.GetFileNameWithoutExtension(output));

        using var stream = File.OpenWrite(output);
        var res = compilation.Emit(stream);
        GUIHelper.PrintResult(res);
    }
}

BuildTask is immutable. You cannot update its sources or settings after creation. But you can copy it with additional override:

task.Settings.languageVersion = LanguageVersion.Preview;

var newTask = BuildTask.Create(task.Sources);

var newTask = task.WithSettings(new BuildSettings());

Project

A Project is a BuildTask with additional build-time information such as assemblyName, assemblyOutput, etc. Project can be serialized and deserialized while BuildTask cannot.

Open an existing project:

using FlexFramework.FlexCompiler.Editor;
using FlexFramework.FlexCompiler.Integration;

var project = Project.Create("/path/to/project.fcxml");
var res = project.Build();
GUIHelper.PrintResult(res);

Create a new project:

using FlexFramework.FlexCompiler.Editor;
using FlexFramework.FlexCompiler.Integration;
using Microsoft.CodeAnalysis.CSharp;

var project = Project.Create();
project.AddSources(ScriptHelper.GetSelected());
project.assemblyName = "MyProject";
project.assemblyOutput = "/path/to/assembly.dll";
project.xmlOutput = "/path/to/assembly.xml";
project.pdbOutput = "/path/to/assembly.pdb";
project.buildSettings.warningLevel = 0;
project.buildSettings.languageVersion = LanguageVersion.Preview;
project.Write("/path/to/project.fcxml");

Project to task:

var task = project.CreateTask();

Build settings

BuildSettings contains information of references, parser options and other compiler settings.

var settings = task.Settings;

var settings = project.buildSettings;

project.buildSettings = new BuildSettings();
// BuildTask is immutable, but you can copy it with settings override:
var newTask = task.WithSettings(new BuildSettings());

Extensions

Extension ID can be found at List of built-in extensions. You can also see an extension's ID when hovering over it in FlexCompiler project window.

List enabled extensions of a project:

var extensions = project.ListExtensions();

Enable or disable extensions:

project.EnableExtension("extensionId");
project.DisableExtension("extensionId");

Set extension config:

project.ConfigExtension("extensionId", "extensionConfig");

Set extension order:

project.SortExtension("extensionId", 0);
Previous
Legacy Project Importer