Пример #1
0
        public static Assembly CompileCode(this INode node, string path, SupportedLanguage language = SupportedLanguage.CSharp)
        {
            var code       = node.GenerateCode();
            var provider   = NRefactoryUtility.CreateCodeProvider(language);
            var parameters = new CompilerParameters(new[]
            {
                "mscorlib.dll",
                "System.Core.dll",
                UnityEditor.EditorApplication.applicationContentsPath + "/Managed/UnityEngine.dll",
                UnityEditor.EditorApplication.applicationContentsPath + "/Managed/UnityEditor.dll",
            }, path);

            var results = provider.CompileAssemblyFromSource(parameters, code);

            if (results.Errors.Count > 0)
            {
                var builder = new StringBuilder(results.Errors.Count + 1);
                builder.AppendLine("Compilation failed:");

                foreach (var error in results.Errors)
                {
                    builder.AppendLine(error.ToString());
                }

                throw new InvalidOperationException(builder.ToString());
            }

            return(results.CompiledAssembly);
        }
Пример #2
0
        public static void SortChildren(this INode node, bool recursive = false)
        {
            node.Children.Sort((a, b) =>
            {
                var scoreA = NRefactoryUtility.GetSortScore(a);
                var scoreB = NRefactoryUtility.GetSortScore(b);

                if (scoreA > scoreB)
                {
                    return(-1);
                }
                else if (scoreA < scoreB)
                {
                    return(1);
                }
                else
                {
                    return(0);
                }
            });

            if (recursive)
            {
                for (int i = 0; i < node.Children.Count; i++)
                {
                    node.Children[i].SortChildren(recursive);
                }
            }
        }
Пример #3
0
        public static string GenerateCode(this INode node, SupportedLanguage language = SupportedLanguage.CSharp)
        {
            var output = NRefactoryUtility.CreateOutputVisitor(language);

            node.AcceptVisitor(output, null);

            return(output.Text);
        }
Пример #4
0
        IExpressionNode[] CreateNodes(string code)
        {
            var block = NRefactoryUtility.ParseBlock(code);

            return(CreateNodes(block.Children));
        }