static int Main(string[] args)
        {
            IReadOnlyList <string> compile = Array.Empty <string>();
            IReadOnlyList <string> refs    = Array.Empty <string>();
            IReadOnlyList <string> generatorSearchPaths = Array.Empty <string>();
            string generatedCompileItemFile             = null;
            string outputDirectory = null;

            ArgumentSyntax.Parse(args, syntax =>
            {
                syntax.DefineOptionList("r|reference", ref refs, "Paths to assemblies being referenced");
                syntax.DefineOptionList("generatorSearchPath", ref generatorSearchPaths, "Paths to folders that may contain generator assemblies");
                syntax.DefineOption("out", ref outputDirectory, true, "The directory to write generated source files to");
                syntax.DefineOption("generatedFilesList", ref generatedCompileItemFile, "The path to the file to create with a list of generated source files");
                syntax.DefineParameterList("compile", ref compile, "Source files included in compilation");
            });

            if (!compile.Any())
            {
                Console.Error.WriteLine("No source files are specified.");
                return(1);
            }

            if (outputDirectory == null)
            {
                Console.Error.WriteLine("The output directory must be specified.");
                return(2);
            }

            var generator = new CompilationGenerator
            {
                Compile       = compile,
                ReferencePath = refs,
                GeneratorAssemblySearchPaths = generatorSearchPaths,
                IntermediateOutputDirectory  = outputDirectory,
            };

            var progress = new Progress <Diagnostic>(OnDiagnosticProgress);

            try
            {
                generator.Generate(progress);
            }
            catch
            {
                return(3);
            }

            if (generatedCompileItemFile != null)
            {
                File.WriteAllLines(generatedCompileItemFile, generator.GeneratedFiles);
            }

            foreach (var file in generator.GeneratedFiles)
            {
                Console.WriteLine(file);
            }

            return(0);
        }
Example #2
0
        static int Main(string[] args)
        {
            IReadOnlyList <string> compile              = Array.Empty <string>();
            IReadOnlyList <string> refs                 = Array.Empty <string>();
            IReadOnlyList <string> preprocessorSymbols  = Array.Empty <string>();
            IReadOnlyList <string> generatorSearchPaths = Array.Empty <string>();
            string generatedCompileItemFile             = null;
            string outputDirectory = null;
            string projectDir      = null;
            bool   version         = false;

            ArgumentSyntax.Parse(args, syntax =>
            {
                syntax.DefineOption("version", ref version, "Show version of this tool (and exits).");
                syntax.DefineOptionList("r|reference", ref refs, "Paths to assemblies being referenced");
                syntax.DefineOptionList("d|define", ref preprocessorSymbols, "Preprocessor symbols");
                syntax.DefineOptionList("generatorSearchPath", ref generatorSearchPaths, "Paths to folders that may contain generator assemblies");
                syntax.DefineOption("out", ref outputDirectory, true, "The directory to write generated source files to");
                syntax.DefineOption("projectDir", ref projectDir, true, "The absolute path of the directory where the project file is located");
                syntax.DefineOption("generatedFilesList", ref generatedCompileItemFile, "The path to the file to create with a list of generated source files");
                syntax.DefineParameterList("compile", ref compile, "Source files included in compilation");
            });

            if (version)
            {
                Console.WriteLine(ThisAssembly.AssemblyInformationalVersion);
                return(0);
            }
            if (!compile.Any())
            {
                Console.Error.WriteLine("No source files are specified.");
                return(1);
            }

            if (outputDirectory == null)
            {
                Console.Error.WriteLine("The output directory must be specified.");
                return(2);
            }

            var generator = new CompilationGenerator
            {
                ProjectDirectory             = projectDir,
                Compile                      = Sanitize(compile),
                ReferencePath                = Sanitize(refs),
                PreprocessorSymbols          = preprocessorSymbols,
                GeneratorAssemblySearchPaths = Sanitize(generatorSearchPaths),
                IntermediateOutputDirectory  = outputDirectory,
            };

            var progress = new Progress <Diagnostic>(OnDiagnosticProgress);

            try
            {
                generator.Generate(progress);
            }
            catch (Exception e)
            {
                Console.Error.WriteLine($"{e.GetType().Name}: {e.Message}");
                Console.Error.WriteLine(e.ToString());
                return(3);
            }

            if (generatedCompileItemFile != null)
            {
                File.WriteAllLines(generatedCompileItemFile, generator.GeneratedFiles);
            }

            foreach (var file in generator.GeneratedFiles)
            {
                Logger.Info(file);
            }

            return(0);
        }
        private static async Task <int> Core(string[] args, CancellationToken cancellationToken)
        {
            IReadOnlyList <string> compile             = Array.Empty <string>();
            IReadOnlyList <string> refs                = Array.Empty <string>();
            IReadOnlyList <string> preprocessorSymbols = Array.Empty <string>();
            IReadOnlyList <string> plugins             = Array.Empty <string>();
            IReadOnlyList <string> buildPropertiesList = Array.Empty <string>();
            string?generatedCompileItemFile            = null;
            string?outputDirectory = null;
            string?projectDir      = null;
            bool   version         = false;
            Dictionary <string, string> buildProperties = new Dictionary <string, string>();

            ArgumentSyntax.Parse(args, syntax =>
            {
                syntax.DefineOption("version", ref version, "Show version of this tool (and exit).");
                syntax.DefineOptionList("r|reference", ref refs, "Paths to assemblies being referenced");
                syntax.DefineOptionList("d|define", ref preprocessorSymbols, "Preprocessor symbols");
                syntax.DefineOptionList("plugin", ref plugins, "Paths to generator plugin assemblies");
                syntax.DefineOptionList("buildProperty", ref buildPropertiesList, false, "MSBuild properties to expose to generators");
                syntax.DefineOption("out", ref outputDirectory, true, "The directory to write generated source files to");
                syntax.DefineOption("projectDir", ref projectDir, true, "The absolute path of the directory where the project file is located");
                syntax.DefineOption("generatedFilesList", ref generatedCompileItemFile, "The path to the file to create with a list of generated source files");
                syntax.DefineParameterList("compile", ref compile, "Source files included in compilation");
            });

            if (version)
            {
                Console.WriteLine(ThisAssembly.AssemblyInformationalVersion);
                return(0);
            }

            if (!compile.Any())
            {
                Console.Error.WriteLine("No source files are specified.");
                return(1);
            }

            if (outputDirectory == null)
            {
                Console.Error.WriteLine("The output directory must be specified.");
                return(2);
            }

            if (projectDir == null)
            {
                Console.Error.WriteLine("The project directory must be specified.");
                return(3);
            }

            foreach (var prop in buildPropertiesList)
            {
                var i = prop.IndexOf("=");

                if (i <= 0)
                {
                    continue;
                }

                var key   = prop.Substring(0, i);
                var value = prop.Substring(i + 1);
                buildProperties[key] = value;
            }

            var fileLastModifiedPath = Path.Combine(Path.GetDirectoryName(generatedCompileItemFile), "fileLastModified.json");

            var generator = new CompilationGenerator(projectDir, Sanitize(compile), Sanitize(refs), preprocessorSymbols, Sanitize(plugins), outputDirectory, fileLastModifiedPath, Sanitize(buildProperties));

            var progress = new Progress <Diagnostic>(OnDiagnosticProgress);

            try
            {
                await generator.GenerateAsync(progress, cancellationToken);
            }
            catch (Exception e)
            {
                Console.Error.WriteLine($"{e.GetType().Name}: {e.Message}");
                Console.Error.WriteLine(e.ToString());
                return(3);
            }

            if (generatedCompileItemFile != null)
            {
                await File.WriteAllLinesAsync(generatedCompileItemFile, generator.GeneratedFiles, cancellationToken);
            }

            foreach (var file in generator.GeneratedFiles)
            {
                Logger.Info(file);
            }

            return(0);
        }