Пример #1
0
        private async Task<PreflightResult> _performPreflight(RefSpec refSpec, CancellationToken token)
        // requires refSpec.Source != null
        // ensures result != null
        {
            if (refSpec.ErrorMessage != null)
                return new PreflightResult { ErrorMessage = refSpec.ErrorMessage };

            token.ThrowIfCancellationRequested();
            _trace.TraceEvent(TraceEventType.Information, 0, "Preflight parsing of {0} requested.", refSpec);

            // Make sure refSpec has a re-usable source (will have to support both preflight and actual compilation)
            var source = refSpec.Source;
            if (source == null)
                throw new ArgumentException(Resources.SelfAssemblingPlan_RefSpecMustHaveSource, "refSpec");
            var reportedPath = _getPath(source);
            if (source.IsSingleUse)
                source = await source.CacheInMemoryAsync();

            token.ThrowIfCancellationRequested();

            // Perform preflight parse
            var eng = new Engine { ExecutionProhibited = true };
            var app = new Application();
            var ldr =
                new Loader(new LoaderOptions(eng, app)
                               {
                                   // Important: Have preflight flag set
                                   PreflightModeEnabled = true,
                                   ReconstructSymbols = false,
                                   RegisterCommands = false,
                                   StoreSourceInformation = false,
                               });

            TextReader sourceReader;
            if (!source.TryOpen(out sourceReader))
            {
                var errorResult = new PreflightResult
                                      {
                                          ErrorMessage =
                                              "Failed to open " + refSpec + " for preflight parsing."
                                      };
                return errorResult;
            }
            ldr.LoadFromReader(sourceReader, refSpec.ResolvedPath != null ? refSpec.ResolvedPath.ToString() : null);

            // Extract preflight information
            ModuleName theModuleName;
            if (!ModuleName.TryParse(app.Meta[Module.NameKey], out theModuleName))
                theModuleName = app.Module.Name;

            MetaEntry noStdLibEntry;
            var result = new PreflightResult
            {
                ModuleName = theModuleName,
                SuppressStandardLibrary =
                    app.Meta.TryGetValue(Module.NoStandardLibraryKey, out noStdLibEntry) && noStdLibEntry.Switch,
                Path = reportedPath
            };

            result.References.AddRange(
                app.Meta[Module.ReferencesKey].List.Where(entry => !entry.Equals(new MetaEntry("")))
                    .Select(_parseRefSpec));
            _trace.TraceEvent(TraceEventType.Verbose, 0, "Preflight parsing of {0} finished.", refSpec);
            return result;
        }