示例#1
0
文件: Translator.cs 项目: nrnoble/h5
        protected virtual MemberResolver Preconvert(MemberResolver resolver, IH5DotJson_AssemblySettings config, CancellationToken cancellationToken)
        {
            bool needRecompile = false;
            foreach (var sourceFile in ParsedSourceFiles)
            {
                cancellationToken.ThrowIfCancellationRequested();

                Logger.ZLogTrace("Preconvert {0}", sourceFile.ParsedFile.FileName);
                var syntaxTree = sourceFile.SyntaxTree;
                var tempEmitter = new TempEmitter { AssemblyInfo = config };
                var detecter = new PreconverterDetecter(resolver, tempEmitter);
                syntaxTree.AcceptVisitor(detecter);

                if (detecter.Found)
                {
                    var fixer = new PreconverterFixer(resolver, tempEmitter);
                    var astNode = syntaxTree.AcceptVisitor(fixer);
                    syntaxTree = astNode != null ? (SyntaxTree)astNode : syntaxTree;
                    sourceFile.SyntaxTree = syntaxTree;
                    needRecompile = true;
                }
            }

            if (needRecompile)
            {
                return new MemberResolver(ParsedSourceFiles, resolver.Assemblies, AssemblyDefinition);
            }

            return resolver;
        }
示例#2
0
文件: Translator.cs 项目: nrnoble/h5
        public void Translate(CancellationToken cancellationToken)
        {
            using (new Measure(Logger, "Translating assembly"))
            {
                var config = AssemblyInfo;

                Dictionary<string, string> discoveredAssemblyPaths = null;

                if (Rebuild)
                {
                    Logger.ZLogInformation("Rebuilding assembly on path {0}", AssemblyLocation);
                    discoveredAssemblyPaths = BuildAssembly(cancellationToken);
                }
                else if (!File.Exists(AssemblyLocation))
                {
                    Logger.ZLogInformation("Building assembly on path {0}", AssemblyLocation);
                    discoveredAssemblyPaths = BuildAssembly(cancellationToken);
                }

                var references = InspectReferences(discoveredAssemblyPaths);
                References = references;

                LogProductInfo();

                if (!string.IsNullOrWhiteSpace(config.BeforeBuild))
                {
                    try
                    {
                        using (new Measure(Logger, $"Running BeforeBuild event {config.BeforeBuild}"))
                        {
                            RunEvent(config.BeforeBuild);
                        }
                    }
                    catch (Exception exc)
                    {
                        Logger.ZLogError(exc, "Error running BeforeBuild event {0}", config.BeforeBuild);
                        throw new TranslatorException($"Error: Unable to run beforeBuild event command: {exc.Message}\nStack trace:\n{exc.StackTrace}");
                    }
                }

                BuildSyntaxTree(cancellationToken);

                var resolver = new MemberResolver(ParsedSourceFiles, Emitter.ToAssemblyReferences(references), AssemblyDefinition);
                resolver = Preconvert(resolver, config, cancellationToken);

                InspectTypes(resolver, config);

                resolver.CanFreeze = true;
                var emitter = CreateEmitter(resolver, cancellationToken);

                if (!AssemblyInfo.OverflowMode.HasValue)
                {
                    AssemblyInfo.OverflowMode = OverflowMode;
                }

                emitter.Translator = this;
                emitter.AssemblyInfo = AssemblyInfo;
                emitter.References = references;
                emitter.SourceFiles = SourceFiles;
                emitter.InitialLevel = 1;

                if (AssemblyInfo.Module != null)
                {
                    AssemblyInfo.Module.Emitter = emitter;
                }

                foreach (var td in TypeInfoDefinitions)
                {
                    if (td.Value.Module != null)
                    {
                        td.Value.Module.Emitter = emitter;
                    }
                }

                SortReferences();

                AddMainOutputs(emitter.Emit());
                EmitterOutputs = emitter.Outputs;
            }
        }
示例#3
0
 public PreconverterDetecter(MemberResolver resolver, IEmitter emitter)
 {
     Resolver = resolver;
     Emitter  = emitter;
 }