Example #1
0
        public static void GenerateStubScript(string scriptPath, IEnumerable <object> stubs)
        {
            Ensure.That(nameof(stubs)).IsNotNull(stubs);

            var stubWriters = stubs.Select(s => AotStubWriterProvider.instance.GetDecorator(s)).ToHashSet();

            var unit = new CodeCompileUnit();

            unit.StartDirectives.Add(new CodePragmaWarningDirective(CodePragmaWarningSetting.Disable, new[] { 219 }));             // Disable unused variable warning

            var @namespace = new CodeNamespace("Ludiq.Generated.Aot");

            unit.Namespaces.Add(@namespace);

            var @class = new CodeClassTypeDeclaration(CodeMemberModifiers.Public, "AotStubs");

            @class.CustomAttributes.Add(new CodeAttributeDeclaration(Code.TypeRef(typeof(PreserveAttribute))));

            @namespace.Types.Add(@class);

            var usedMethodNames = new HashSet <string>();

            foreach (var stubWriter in stubWriters.OrderBy(sw => sw.stubMethodComment))
            {
                if (stubWriter.skip)
                {
                    continue;
                }

                var methodName = stubWriter.stubMethodName;

                var i = 0;

                while (usedMethodNames.Contains(methodName))
                {
                    methodName = stubWriter.stubMethodName + "_" + i++;
                }

                usedMethodNames.Add(methodName);

                var method = new CodeMethodMember(CodeMemberModifiers.Public | CodeMemberModifiers.Static, Code.TypeRef(typeof(void)), methodName, Enumerable.Empty <CodeParameterDeclaration>(), stubWriter.GetStubStatements().ToArray());
                method.CustomAttributes.Add(new CodeAttributeDeclaration(Code.TypeRef(typeof(PreserveAttribute), true)));
                method.Comments.Add(new CodeComment(stubWriter.stubMethodComment));

                @class.Members.Add(method);
            }

            PathUtility.CreateDirectoryIfNeeded(LudiqCore.Paths.transientGenerated);

            VersionControlUtility.Unlock(scriptPath);

            if (File.Exists(scriptPath))
            {
                File.Delete(scriptPath);
            }

            using (var scriptWriter = new StreamWriter(scriptPath))
            {
                CodeGenerator.GenerateCodeFromCompileUnit(unit, new TextCodeWriter(scriptWriter), new CodeGeneratorOptions(indentString: "\t"));
            }
        }
Example #2
0
 public StatementsGenerator(MethodDefinition methodDef, Graph graph)
 {
     this.graph = graph;
     this.method = new CodeMethodMember(methodDef);
 }