Ejemplo n.º 1
0
        public static int Main(string[] args)
        {
            var moduleName     = string.Empty;
            var outputPath     = string.Empty;
            var referencePaths = new List <string>();
            var sourcePaths    = new List <string>();
            var helpRequested  = false;

            var options = new OptionSet {
                "usage: mkc <source-paths> [options]",
                { "m=", "The name of the module", v => moduleName = v },
                { "r=", "The {path} of an assembly to reference", v => referencePaths.Add(v) },
                { "o=", "The output {path} of the assembly to create", v => outputPath = v },
                { "<>", v => sourcePaths.Add(v) },
                { "?|h|help", "Display the help", v => helpRequested = true }
            };

            options.Parse(args);

            if (helpRequested)
            {
                options.WriteOptionDescriptions(Console.Out);
                return(0);
            }

            var(filePaths, errorPaths) = GetFilePaths(sourcePaths);

            if (errorPaths.Any())
            {
                foreach (var path in errorPaths)
                {
                    Console.Error.WriteLine($"error: path '{path}' does not exist");
                }
                return(1);
            }

            if (!filePaths.Any())
            {
                Console.Error.WriteLine($"error: no source files found");
                return(1);
            }

            outputPath = InferOutputPath(outputPath, sourcePaths);
            moduleName = InferModuleName(moduleName, outputPath);

            if (!CheckReferencesOk(referencePaths))
            {
                return(1);
            }

            var diagnostics = new DiagnosticBag();
            var syntaxTrees =
                filePaths.Select(path => SourceText.Load(path))
                .Select(source => SyntaxTree.Parse(source, diagnostics))
                .ToList();

            var compilation = Compilation.Create(diagnostics, syntaxTrees);
            //var result = compilation.Evaluate();

            var emitted = compilation.Emit(moduleName, referencePaths, outputPath);

            if (emitted.Diagnostics.Any())
            {
                Console.Out.WriteDiagnostics(emitted.Diagnostics);
                return(1);
            }

            // Save?

            return(0);
        }