public static void TelemetryPostOperation(bool succeess, string str)
 {
     if (ToolConsole.IsMarkupEnabled && !String.IsNullOrWhiteSpace(str))
     {
         ToolConsole.WriteLine(str, (succeess ? LogTag.TraceSuccess : LogTag.TraceFailure), ToolConsole.Space);
     }
 }
Exemple #2
0
 internal static void WriteHelp()
 {
     if (ToolConsole.Verbosity > Verbosity.Silent && ToolConsole.ToolModeLevel != OperationalContext.Infrastructure)
     {
         ToolConsole.WriteLine(HelpGenerator.GenerateHelpText(), LogTag.Important);
     }
 }
 public static void TelemetryPostFault(Exception ex)
 {
     if (ToolConsole.IsMarkupEnabled)
     {
         var str = Utils.GetExceptionMessage(ex, includeStackTrace: true);
         ToolConsole.WriteLine(str, LogTag.TraceException, ToolConsole.Space);
     }
 }
Exemple #4
0
        internal static void WriteHeaderIf(bool condition)
        {
            if (condition && ToolConsole.Verbosity > Verbosity.Minimal && ToolConsole.ToolModeLevel != OperationalContext.Infrastructure)
            {
                ToolConsole.WriteLine(string.Format(SR.LogoFormat, Tool.ToolName, Tool.PackageVersion, SR.Microsoft_Copyright_CommandLine_Logo), LogTag.Important);

                if (AppInsightsTelemetryClient.IsUserOptedIn)
                {
                    ToolConsole.WriteLine(SR.TelemetryEnabled, LogTag.Information);
                }
            }
        }
        // While enabling logging markup option, the tool will append specific prefix string to each console output
        // This feature is designed for console redirection to get better UI experience also telemetry data collection

        private static void TelemetryAddProperty(string propName, string propValue)
        {
            if (!String.IsNullOrWhiteSpace(propName))
            {
                if (propValue == null)
                {
                    propValue = "<null>";
                }
                else if (propValue.Trim() == string.Empty)
                {
                    propValue = "<empty>";
                }

                ToolConsole.WriteLine(propName + LogTag.TraceProperty + propValue, LogTag.TraceProperty, ToolConsole.Space);
            }
        }
Exemple #6
0
        private void LogMessage(string message, LogTag logTag, bool logToUI = false)
        {
            if (this.EnableTracing || logToUI)
            {
                message = message?.Trim();

                if (!string.IsNullOrEmpty(message))
                {
                    if (logTag == LogTag.Error)
                    {
                        ToolConsole.WriteError(message, isTrace: !logToUI);
                    }
                    else if (logTag == LogTag.Warning)
                    {
                        ToolConsole.WriteWarning(message, isTrace: !logToUI);
                    }
                    else
                    {
                        ToolConsole.WriteLine(message, logToUI ? LogTag.Information : LogTag.LogMessage);
                    }
                }
            }
        }
Exemple #7
0
        internal static async Task <int> MainAsync(string[] args, ILogger logger, CancellationToken cancellationToken)
        {
            int result = -1;
            CommandProcessorOptions options = null;

            WaitForDebugger();

            try
            {
                options = await CommandProcessorOptions.ParseArgumentsAsync(args, logger, cancellationToken);

                ValidateUICulture(options);

                if (options.NoTelemetry == true)
                {
                    AppInsightsTelemetryClient.IsUserOptedIn = false;
                }

                ToolConsole.Init(options);
                ToolConsole.WriteHeaderIf(options.NoLogo != true);

                ThrowOnValidationErrors(options);

                if (options.Help == true)
                {
                    ToolConsole.WriteHelp();
                    result = (int)ToolExitCode.Success;
                }
                else
                {
                    // Show early warnings
                    var earlyWarnings = options.Warnings;
                    foreach (string warning in earlyWarnings.Distinct())
                    {
                        ToolConsole.WriteWarning(warning);
                    }

                    var operationMessage = (options.IsUpdateOperation ? "Update" : "Add") + " web service reference operation started!";
                    using (var safeLogger = await SafeLogger.WriteStartOperationAsync(options.Logger, operationMessage).ConfigureAwait(false))
                    {
                        await options.ResolveAsync(cancellationToken).ConfigureAwait(false);

                        ThrowOnValidationErrors(options);

                        options.ProviderId = Tool.ToolName;
                        options.Version    = Tool.PackageVersion;

                        foreach (string warning in options.Warnings.Distinct().Except(earlyWarnings))
                        {
                            ToolConsole.WriteWarning(warning);
                        }

                        if (options.RequiresBoostrapping)
                        {
                            using (var bootstrapper = new SvcutilBootstrapper(options.CloneAs <SvcutilOptions>()))
                            {
                                await options.SetupBootstrappingDirectoryAsync(options.Logger, cancellationToken).ConfigureAwait(false);

                                var bootstrapResult = await bootstrapper.BoostrapSvcutilAsync(options.KeepBootstrapDir, options.Logger, cancellationToken).ConfigureAwait(false);

                                ToolConsole.WriteLine(bootstrapResult.OutputText);
                                result = bootstrapResult.ExitCode;
                            }
                        }
                        else
                        {
                            result = (int) await RunAsync(options, cancellationToken).ConfigureAwait(false);
                        }

                        if (IsSuccess(result))
                        {
                            if (!File.Exists(options.OutputFile.FullName))
                            {
                                await safeLogger.WriteMessageAsync("The process completed successfully but no proxy file was found!", logToUI : false);

                                throw new ToolArgumentException(SR.ErrUnexpectedError);
                            }
                            else
                            {
                                await GenerateParamsFileAsync(options, options.Logger, cancellationToken);

                                if (CanAddProjectReferences(options) && !await AddProjectReferencesAsync(options.Project, options, cancellationToken).ConfigureAwait(false))
                                {
                                    result = (int)ToolExitCode.InputError;
                                }
                            }

                            // clean up only on success to allow to troubleshoot any problems on failure.
                            options?.Cleanup();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                result = await ProcessExceptionAsync(e, options);
            }
            finally
            {
                try
                {
                    // Don't log telemetry if we're running from bootstrapper or connected service.
                    if (options?.ToolContext <= OperationalContext.Global)
                    {
                        var properties = new Dictionary <string, string>()
                        {
                            { "IsUpdate", (options?.IsUpdateOperation).ToString() },
                            { "TargetFramework", options?.TargetFramework?.FullName },
                            { "Parameters", options?.ToTelemetryString() },
                            { "ExitCode", result.ToString() },
                            { "RequiresBootstrapping", options?.RequiresBoostrapping.ToString() },
                        };

                        var telemetryClient = await AppInsightsTelemetryClient.GetInstanceAsync(cancellationToken).ConfigureAwait(false);

                        telemetryClient.TrackEvent("ToolRun", properties);
                    }
                }
                catch
                {
                }
            }

            return(result);
        }
Exemple #8
0
        internal static async Task <ToolExitCode> RunAsync(CommandProcessorOptions options, CancellationToken cancellationToken)
        {
            ImportModule importModule  = null;
            var          credsProvicer = new CmdCredentialsProvider();

            ServiceDescriptor serviceDescriptor = options.Inputs.Count == 1 ?
                                                  new ServiceDescriptor(options.Inputs[0].ToString(), credsProvicer, credsProvicer, credsProvicer) :
                                                  new ServiceDescriptor(options.Inputs.Select(i => i.ToString()).ToList(), credsProvicer, credsProvicer, credsProvicer);

            // When in Infrastructure mode (WCF CS) it is assumed the metadata docs have been downloaded and passed in as wsdl files.
            if (options.ToolContext != OperationalContext.Infrastructure)
            {
                if (serviceDescriptor.MetadataUrl != null)
                {
                    ToolConsole.WriteLine(string.Format(SR.RetreivingMetadataMsgFormat, serviceDescriptor.MetadataUrl.AbsoluteUri));
                }
                else
                {
                    var displayUri = serviceDescriptor.MetadataFiles.Count() == 1 ? serviceDescriptor.MetadataFiles.First().LocalPath : SR.WsdlOrSchemaFilesMsg;
                    ToolConsole.WriteLine(string.Format(SR.ReadingMetadataMessageFormat, displayUri));
                }
            }

            using (await SafeLogger.WriteStartOperationAsync(options.Logger, "Importing metadata ...").ConfigureAwait(false))
            {
                try
                {
                    await serviceDescriptor.ImportMetadataAsync(
                        (wi) => importModule = new ImportModule(options, serviceDescriptor, wi),
                        (sd) => importModule.BeforeImportMetadata(sd),
                        (sd) => importModule.AfterImportMetadata(sd),
                        cancellationToken).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    if (Utils.IsFatalOrUnexpected(ex))
                    {
                        throw;
                    }
                    var exception = new ToolInputException(Utils.GetExceptionMessage(ex), ex);
                    if (serviceDescriptor.MetadataUrl != null)
                    {
                        exception = new ToolMexException(exception, serviceDescriptor.MetadataUrl);
                    }
                    throw exception;
                }
            }

            using (await SafeLogger.WriteStartOperationAsync(options.Logger, "Processing Code DOM ...").ConfigureAwait(false))
            {
                ToolConsole.WriteLine(SR.GeneratingFiles);

                CodeSerializer codeSerializer = new CodeSerializer(options, serviceDescriptor.MetadataDocuments);
                var            filePath       = codeSerializer.Save(importModule.CodeCompileUnit);

                // When in Infrastructure mode (WCF CS) it is assumed the output file path have been provided so no need to display it.
                ToolConsole.WriteLineIf(options.ToolContext != OperationalContext.Infrastructure, filePath, LogTag.Important);
            }

            return(ToolConsole.ExitCode);
        }