Exemplo n.º 1
0
        static int Main(string[] args)
        {
            try
            {
                var arguments = Args.Parse <Arguments>(args);
                var language  = arguments.Language.GetValueOrDefault(DetermineLanguageByOutputFileName(arguments.OutputFile));
                var logSink   = arguments.Verbose ? (ILogSink)ConsoleLogSink.Instance : NullLogSink.Instance;
                var result    = XmlBasedGenerator.GenerateMocks(
                    logSink,
                    language,
                    arguments.SolutionFile,
                    arguments.ConfigurationFile);

                File.WriteAllText(arguments.OutputFile, result);
                return(0);
            }
            catch (ArgException ex)
            {
                WriteLine(OutputType.Error, ex.Message);
                WriteLine();
                WriteLine(ArgUsage.GenerateUsageFromTemplate <Arguments>().ToNormalString());
                return(-1);
            }
            catch (Exception ex)
            {
                WriteLine(OutputType.Error, "Failed to generate code: {0}", ex);
                return(-1);
            }
        }
Exemplo n.º 2
0
        public void Execute(SourceGeneratorContext context)
        {
            var xmlFiles = context
                           .AdditionalFiles
                           .Where(additionalFile => Path.GetFileName(additionalFile.Path) == "PCLMock.xml")
                           .ToList();

            if (xmlFiles.Count == 0)
            {
                ReportDiagnostic(context, "PM0001", "PM0001: No XML configuration found", "No XML configuration file named 'PCLMock.xml' was found. See the documentation for more details.", DiagnosticSeverity.Error);
                return;
            }
            else if (xmlFiles.Count > 1)
            {
                ReportDiagnostic(context, "PM0002", "PM0002: Multiple XML configurations found", "Multiple XML configuration files named 'PCLMock.xml' were found where only one is expected.", DiagnosticSeverity.Error);
                return;
            }

            var xmlFile = xmlFiles.Single();

            if (!File.Exists(xmlFile.Path))
            {
                ReportDiagnostic(context, "PM0003", "PM0003: XML configuration file not found", "XML configuration file at '{0}' was not found.", DiagnosticSeverity.Error, xmlFile.Path);
                return;
            }

            var logSink = new SourceGeneratorContextLogSink(context)
                          .WithMinimumLevel(LogLevel.Info)
                          .WithSource(typeof(MockSourceGenerator));

            try
            {
                var syntaxNodes = XmlBasedGenerator
                                  .GenerateMocks(
                    logSink,
                    Language.CSharp,
                    ImmutableList.Create(context.Compilation),
                    xmlFile.Path);
                var source = syntaxNodes
                             .Aggregate(
                    new StringBuilder(),
                    (acc, next) => acc.AppendLine(next.ToFullString()),
                    acc => acc.ToString());

                context.AddSource($"PCLMock", SourceText.From(source, Encoding.UTF8));
            }
            catch (Exception ex)
            {
                logSink.Error("Failed during mock generation: {0}", ex);
            }
        }
Exemplo n.º 3
0
        static async Task <int> Main(string[] args)
        {
            try
            {
                var arguments     = Args.Parse <Arguments>(args);
                var language      = arguments.Language.GetValueOrDefault(DetermineLanguageByOutputFileName(arguments.OutputFile));
                var logSink       = arguments.Verbose ? (ILogSink)ConsoleLogSink.Instance : NullLogSink.Instance;
                var loggerFactory = new LoggerFactory();
                loggerFactory.AddProvider(new LogSinkLoggerProvider(logSink));
                var options = new AnalyzerManagerOptions
                {
                    LoggerFactory = loggerFactory,
                };
                var manager          = new AnalyzerManager(arguments.SolutionFile, options);
                var workspace        = manager.GetWorkspace();
                var compilationTasks = workspace
                                       .CurrentSolution
                                       .Projects
                                       .Select(project => project.GetCompilationAsync());
                var compilations = await Task.WhenAll(compilationTasks);

                var syntaxNodes = XmlBasedGenerator.GenerateMocks(
                    logSink,
                    language,
                    compilations.ToImmutableList(),
                    arguments.ConfigurationFile);
                var result = syntaxNodes
                             .Aggregate(
                    new StringBuilder(),
                    (acc, next) => acc.AppendLine(next.ToFullString()).AppendLine(),
                    acc => acc.ToString());

                var log = logSink.ToString();
                File.WriteAllText(arguments.OutputFile, result);
                return(0);
            }
            catch (ArgException ex)
            {
                WriteLine(OutputType.Error, ex.Message);
                WriteLine();
                WriteLine(ArgUsage.GenerateUsageFromTemplate <Arguments>().ToNormalString());
                return(-1);
            }
            catch (Exception ex)
            {
                WriteLine(OutputType.Error, "Failed to generate code: {0}", ex);
                return(-1);
            }
        }