Beispiel #1
0
        /// <summary>
        /// Builds the compilation for the Q# code or Q# snippet and referenced assemblies defined by the given options.
        /// Generates formatted Q# code for each source file in the compilation.
        /// Returns a suitable error code if some of the source files or references could not be found or loaded, or if the Q# generation failed.
        /// Compilation errors are not reflected in the return code, but are logged using the given logger.
        /// Throws an ArgumentNullException if any of the given arguments is null.
        /// </summary>
        public static int Run(FormatOptions options, ConsoleLogger logger)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            ImmutableDictionary <Uri, string> LoadSources(SourceFileLoader loadFromDisk) =>
            options.LoadSourcesOrSnippet(logger)(loadFromDisk)
            .ToImmutableDictionary(entry => entry.Key, entry => UpdateArrayLiterals(entry.Value)); // manually replace array literals

            var loaded = new CompilationLoader(LoadSources, options.References, logger: logger);   // no rewrite steps, no generation

            if (ReturnCode.Status(loaded) == ReturnCode.UNRESOLVED_FILES)
            {
                return(ReturnCode.UNRESOLVED_FILES); // ignore compilation errors
            }

            // TODO: a lot of the formatting logic defined here and also in the routines above
            // is supposed to move into the compilation manager in order to be available for the language server to provide formatting
            var success = true;

            foreach (var file in loaded.VerifiedCompilation.SourceFiles)
            {
                var verbosity = logger.Verbosity;
                if (Options.IsCodeSnippet(file) && logger.Verbosity < DiagnosticSeverity.Information)
                {
                    logger.Verbosity = DiagnosticSeverity.Information;
                }
                if (!GenerateFormattedQsFile(loaded.VerifiedCompilation, file, options.OutputFolder, logger))
                {
                    success = false;
                }
                logger.Verbosity = verbosity;
            }
            return(success ? ReturnCode.SUCCESS : ReturnCode.CODE_GENERATION_ERRORS);
        }