Пример #1
0
        /// <summary>
        /// The Thrift.Net Thrift compiler. Use this to generate C# code from
        /// your Thrift IDL files.
        /// </summary>
        /// <param name="input">Specifies the path to the input file.</param>
        /// <param name="outputDirectory">
        /// Specifies the output directory. If it does not already exist it will be created.
        /// </param>
        /// <param name="console">Used to interact with the console.</param>
        /// <returns>
        /// The exit code. This can be used by scripts to determine whether the
        /// compile was successful or not.
        /// </returns>
        public static int Main(FileInfo input, DirectoryInfo outputDirectory, IConsole console)
        {
            if (!input.Exists)
            {
                console.Error.Write($"The specified input file '{input.Name}' could not be found.{Environment.NewLine}");
                return((int)ExitCode.InputFileNotFound);
            }

            if (!outputDirectory.Exists)
            {
                outputDirectory.Create();
            }

            console.Out.Write($"Starting compilation of {input.Name}{Environment.NewLine}");

            var thriftFile = FileProvider.Create(
                new DirectoryInfo(Directory.GetCurrentDirectory()),
                input,
                outputDirectory);

            using (var stream = input.OpenRead())
            {
                var result = Compiler.Compile(stream);

                OutputCompilationSummary(console, result);

                // TODO: Pull message formatting out to its own object.
                foreach (var message in result.Messages.OrderBy(message => message.LineNumber))
                {
                    console.Out.Write($"{thriftFile.RelativePath}({message.LineNumber},{message.StartPosition}-{message.EndPosition}): {message.MessageType} {message.FormattedMessageId}: {message.Message} [{input.FullName}]{Environment.NewLine}");
                }

                if (result.HasErrors)
                {
                    return((int)ExitCode.CompilationFailed);
                }

                if (result.Document.ContainsDefinitions)
                {
                    var generatedCode = DocumentGenerator.Generate(thriftFile, result.Document);

                    FileWriter.Write(thriftFile, generatedCode);
                }
            }

            return((int)ExitCode.Success);
        }