Exemple #1
0
        public int Main(string[] args)
        {
            try
            {
                var app = new CommandLineApplication
                {
                    Name               = "razor-tooling",
                    FullName           = "Microsoft Razor Tooling Utility",
                    Description        = "Resolves Razor tooling specific information.",
                    ShortVersionGetter = GetInformationalVersion,
                };
                app.HelpOption("-?|-h|--help");

                ResolveProtocolCommand.Register(app);
                ResolveTagHelpersCommand.Register(app, _assemblyLoadContext);

                app.OnExecute(() =>
                {
                    app.ShowHelp();
                    return(2);
                });

                return(app.Execute(args));
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {Environment.NewLine}{ex.Message}.");
                return(1);
            }
        }
        public static void Register(CommandLineApplication app, IAssemblyLoadContext assemblyLoadContext)
        {
            app.Command("resolve-taghelpers", config =>
            {
                config.Description = "Resolves TagHelperDescriptors in the specified assembly(s).";
                config.HelpOption("-?|-h|--help");
                var clientProtocol = config.Option(
                    "-p|--protocol",
                    "Provide client protocol version.",
                    CommandOptionType.SingleValue);
                var assemblyNames = config.Argument(
                    "[name]",
                    "Assembly name to resolve TagHelperDescriptors in.",
                    multipleValues: true);

                config.OnExecute(() =>
                {
                    var messageBroker    = new CommandMessageBroker();
                    var plugin           = new RazorPlugin(messageBroker);
                    var resolvedProtocol = ResolveProtocolCommand.ResolveProtocol(clientProtocol, plugin.Protocol);

                    plugin.Protocol = resolvedProtocol;

                    var success = true;
                    foreach (var assemblyName in assemblyNames.Values)
                    {
                        var messageData = new ResolveTagHelperDescriptorsRequestData
                        {
                            AssemblyName   = assemblyName,
                            SourceLocation = SourceLocation.Zero
                        };
                        var message = new RazorPluginRequestMessage(
                            RazorPluginMessageTypes.ResolveTagHelperDescriptors,
                            JObject.FromObject(messageData));

                        success &= plugin.ProcessMessage(JObject.FromObject(message), assemblyLoadContext);
                    }

                    var resolvedDescriptors = messageBroker.Results.SelectMany(result => result.Data.Descriptors);
                    var resolvedErrors      = messageBroker.Results.SelectMany(result => result.Data.Errors);
                    var resolvedResult      = new ResolvedTagHelperDescriptorsResult
                    {
                        Descriptors = resolvedDescriptors,
                        Errors      = resolvedErrors
                    };
                    var serializedResult = JsonConvert.SerializeObject(resolvedResult, Formatting.Indented);

                    Console.WriteLine(serializedResult);

                    return(success ? 0 : 1);
                });
            });
        }