Пример #1
0
        internal static int ProcessArgs(string[] args, ITelemetry telemetryClient = null)
        {
            // CommandLineApplication is a bit restrictive, so we parse things ourselves here. Individual apps should use CLA.

            var success = true;
            var command = string.Empty;
            var lastArg = 0;
            var cliFallbackFolderPathCalculator = new CliFallbackFolderPathCalculator();

            using (INuGetCacheSentinel nugetCacheSentinel = new NuGetCacheSentinel(cliFallbackFolderPathCalculator))
                using (IFirstTimeUseNoticeSentinel disposableFirstTimeUseNoticeSentinel =
                           new FirstTimeUseNoticeSentinel(cliFallbackFolderPathCalculator))
                {
                    IFirstTimeUseNoticeSentinel firstTimeUseNoticeSentinel = disposableFirstTimeUseNoticeSentinel;
                    for (; lastArg < args.Length; lastArg++)
                    {
                        if (IsArg(args[lastArg], "d", "diagnostics"))
                        {
                            Environment.SetEnvironmentVariable(CommandContext.Variables.Verbose, bool.TrueString);
                            CommandContext.SetVerbose(true);
                        }
                        else if (IsArg(args[lastArg], "version"))
                        {
                            PrintVersion();
                            return(0);
                        }
                        else if (IsArg(args[lastArg], "info"))
                        {
                            PrintInfo();
                            return(0);
                        }
                        else if (IsArg(args[lastArg], "h", "help") ||
                                 args[lastArg] == "-?" ||
                                 args[lastArg] == "/?")
                        {
                            HelpCommand.PrintHelp();
                            return(0);
                        }
                        else if (args[lastArg].StartsWith("-"))
                        {
                            Reporter.Error.WriteLine($"Unknown option: {args[lastArg]}");
                            success = false;
                        }
                        else
                        {
                            // It's the command, and we're done!
                            command = args[lastArg];

                            if (IsDotnetBeingInvokedFromNativeInstaller(command))
                            {
                                firstTimeUseNoticeSentinel = new NoOpFirstTimeUseNoticeSentinel();
                            }

                            ConfigureDotNetForFirstTimeUse(
                                nugetCacheSentinel,
                                firstTimeUseNoticeSentinel,
                                cliFallbackFolderPathCalculator);

                            break;
                        }
                    }
                    if (!success)
                    {
                        HelpCommand.PrintHelp();
                        return(1);
                    }

                    if (telemetryClient == null)
                    {
                        telemetryClient = new Telemetry.Telemetry(firstTimeUseNoticeSentinel);
                    }
                    TelemetryEventEntry.Subscribe(telemetryClient.TrackEvent);
                    TelemetryEventEntry.TelemetryFilter = new TelemetryFilter();
                }

            IEnumerable <string> appArgs =
                (lastArg + 1) >= args.Length
                ? Enumerable.Empty <string>()
                : args.Skip(lastArg + 1).ToArray();

            if (CommandContext.IsVerbose())
            {
                Console.WriteLine($"Telemetry is: {(telemetryClient.Enabled ? "Enabled" : "Disabled")}");
            }

            if (string.IsNullOrEmpty(command))
            {
                command = "help";
            }

            TelemetryEventEntry.TrackEvent(command, null, null);

            int exitCode;

            if (BuiltInCommandsCatalog.Commands.TryGetValue(command, out var builtIn))
            {
                TelemetryEventEntry.SendFiltered(Parser.Instance.ParseFrom($"dotnet {command}", appArgs.ToArray()));
                exitCode = builtIn.Command(appArgs.ToArray());
            }
            else
            {
                CommandResult result = Command.Create(
                    "dotnet-" + command,
                    appArgs,
                    FrameworkConstants.CommonFrameworks.NetStandardApp15)
                                       .Execute();
                exitCode = result.ExitCode;
            }
            return(exitCode);
        }
Пример #2
0
        public static Command CreateCommandForScript(Project project, string scriptCommandLine, Func <string, string> getVariable)
        {
            // Preserve quotation marks around arguments since command is about to be passed to a shell. May need
            // the quotes to ensure the shell groups arguments correctly.
            var scriptArguments = CommandGrammar.Process(
                scriptCommandLine,
                GetScriptVariable(project, getVariable),
                preserveSurroundingQuotes: true);

            // Ensure the array won't be empty and the elements won't be null or empty strings.
            scriptArguments = scriptArguments.Where(argument => !string.IsNullOrEmpty(argument)).ToArray();
            if (scriptArguments.Length == 0)
            {
                return(null);
            }

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                // Only forward slashes are used in script blocks. Replace with backslashes to correctly
                // locate the script. The directory separator is platform-specific.
                scriptArguments[0] = scriptArguments[0].Replace(
                    Path.AltDirectorySeparatorChar,
                    Path.DirectorySeparatorChar);

                // Command-lines on Windows are executed via "cmd /S /C" in order to support batch files, &&,
                // built-in commands like echo, et cetera. /S allows quoting the command as well as the arguments.
                // ComSpec is Windows-specific, and contains the full path to cmd.exe
                var comSpec = Environment.GetEnvironmentVariable("ComSpec");
                if (!string.IsNullOrEmpty(comSpec))
                {
                    scriptArguments =
                        new[] { comSpec, "/S", "/C", "\"" }
                    .Concat(scriptArguments)
                    .Concat(new[] { "\"" })
                    .ToArray();
                }
            }
            else
            {
                // Special-case a script name that, perhaps with added .sh, matches an existing file.
                var surroundWithQuotes = false;
                var scriptCandidate    = scriptArguments[0];
                if (scriptCandidate.StartsWith("\"", StringComparison.Ordinal) &&
                    scriptCandidate.EndsWith("\"", StringComparison.Ordinal))
                {
                    // Strip surrounding quotes; they were required in project.json to keep the script name
                    // together but confuse File.Exists() e.g. "My Script", lacking ./ prefix and .sh suffix.
                    surroundWithQuotes = true;
                    scriptCandidate    = scriptCandidate.Substring(1, scriptCandidate.Length - 2);
                }

                if (!scriptCandidate.EndsWith(".sh", StringComparison.Ordinal))
                {
                    scriptCandidate = scriptCandidate + ".sh";
                }

                if (File.Exists(Path.Combine(project.ProjectDirectory, scriptCandidate)))
                {
                    // scriptCandidate may be a path relative to the project root. If so, likely will not be found
                    // in the $PATH; add ./ to let bash know where to look.
                    var prefix = Path.IsPathRooted(scriptCandidate) ? string.Empty : "./";
                    var quote  = surroundWithQuotes ? "\"" : string.Empty;
                    scriptArguments[0] = $"{ quote }{ prefix }{ scriptCandidate }{ quote }";
                }

                // Always use /usr/bin/env bash -c in order to support redirection and so on; similar to Windows case.
                // Unlike Windows, must escape quotation marks within the newly-quoted string.
                scriptArguments = new[] { "/usr/bin/env", "bash", "-c", "\"" }
                .Concat(scriptArguments.Select(argument => argument.Replace("\"", "\\\"")))
                .Concat(new[] { "\"" })
                .ToArray();
            }

            return(Command.Create(scriptArguments.FirstOrDefault(), string.Join(" ", scriptArguments.Skip(1)))
                   .WorkingDirectory(project.ProjectDirectory));
        }
Пример #3
0
        internal static int ProcessArgs(string[] args, ITelemetry telemetryClient = null)
        {
            // CommandLineApplication is a bit restrictive, so we parse things ourselves here. Individual apps should use CLA.

            var success = true;
            var command = string.Empty;
            var lastArg = 0;
            TopLevelCommandParserResult topLevelCommandParserResult = TopLevelCommandParserResult.Empty;

            using (INuGetCacheSentinel nugetCacheSentinel = new NuGetCacheSentinel())
                using (IFirstTimeUseNoticeSentinel disposableFirstTimeUseNoticeSentinel =
                           new FirstTimeUseNoticeSentinel())
                {
                    IFirstTimeUseNoticeSentinel firstTimeUseNoticeSentinel = disposableFirstTimeUseNoticeSentinel;
                    IAspNetCertificateSentinel  aspNetCertificateSentinel  = new AspNetCertificateSentinel();
                    IFileSentinel toolPathSentinel = new FileSentinel(
                        new FilePath(
                            Path.Combine(
                                CliFolderPathCalculator.DotnetUserProfileFolderPath,
                                ToolPathSentinelFileName)));

                    for (; lastArg < args.Length; lastArg++)
                    {
                        if (IsArg(args[lastArg], "d", "diagnostics"))
                        {
                            Environment.SetEnvironmentVariable(CommandContext.Variables.Verbose, bool.TrueString);
                            CommandContext.SetVerbose(true);
                        }
                        else if (IsArg(args[lastArg], "version"))
                        {
                            PrintVersion();
                            return(0);
                        }
                        else if (IsArg(args[lastArg], "info"))
                        {
                            PrintInfo();
                            return(0);
                        }
                        else if (IsArg(args[lastArg], "h", "help") ||
                                 args[lastArg] == "-?" ||
                                 args[lastArg] == "/?")
                        {
                            HelpCommand.PrintHelp();
                            return(0);
                        }
                        else if (args[lastArg].StartsWith("-", StringComparison.OrdinalIgnoreCase))
                        {
                            Reporter.Error.WriteLine($"Unknown option: {args[lastArg]}");
                            success = false;
                        }
                        else
                        {
                            // It's the command, and we're done!
                            command = args[lastArg];
                            if (string.IsNullOrEmpty(command))
                            {
                                command = "help";
                            }

                            var environmentProvider = new EnvironmentProvider();

                            bool generateAspNetCertificate =
                                environmentProvider.GetEnvironmentVariableAsBool("DOTNET_GENERATE_ASPNET_CERTIFICATE", true);
                            bool printTelemetryMessage =
                                environmentProvider.GetEnvironmentVariableAsBool("DOTNET_PRINT_TELEMETRY_MESSAGE", true);
                            bool skipFirstRunExperience =
                                environmentProvider.GetEnvironmentVariableAsBool("DOTNET_SKIP_FIRST_TIME_EXPERIENCE", false);

                            ReportDotnetHomeUsage(environmentProvider);

                            topLevelCommandParserResult = new TopLevelCommandParserResult(command);
                            var hasSuperUserAccess = false;
                            if (IsDotnetBeingInvokedFromNativeInstaller(topLevelCommandParserResult))
                            {
                                aspNetCertificateSentinel  = new NoOpAspNetCertificateSentinel();
                                firstTimeUseNoticeSentinel = new NoOpFirstTimeUseNoticeSentinel();
                                toolPathSentinel           = new NoOpFileSentinel(exists: false);
                                hasSuperUserAccess         = true;

                                // When running through a native installer, we want the cache expansion to happen, so
                                // we need to override this.
                                skipFirstRunExperience = false;
                            }

                            var dotnetFirstRunConfiguration = new DotnetFirstRunConfiguration(
                                generateAspNetCertificate,
                                printTelemetryMessage,
                                skipFirstRunExperience);

                            ConfigureDotNetForFirstTimeUse(
                                nugetCacheSentinel,
                                firstTimeUseNoticeSentinel,
                                aspNetCertificateSentinel,
                                toolPathSentinel,
                                hasSuperUserAccess,
                                dotnetFirstRunConfiguration,
                                environmentProvider);

                            break;
                        }
                    }
                    if (!success)
                    {
                        HelpCommand.PrintHelp();
                        return(1);
                    }

                    if (telemetryClient == null)
                    {
                        telemetryClient = new Telemetry.Telemetry(firstTimeUseNoticeSentinel);
                    }
                    TelemetryEventEntry.Subscribe(telemetryClient.TrackEvent);
                    TelemetryEventEntry.TelemetryFilter = new TelemetryFilter(Sha256Hasher.HashWithNormalizedCasing);
                }

            IEnumerable <string> appArgs =
                (lastArg + 1) >= args.Length
                ? Enumerable.Empty <string>()
                : args.Skip(lastArg + 1).ToArray();

            if (CommandContext.IsVerbose())
            {
                Console.WriteLine($"Telemetry is: {(telemetryClient.Enabled ? "Enabled" : "Disabled")}");
            }

            TelemetryEventEntry.SendFiltered(topLevelCommandParserResult);

            int exitCode;

            if (BuiltInCommandsCatalog.Commands.TryGetValue(topLevelCommandParserResult.Command, out var builtIn))
            {
                var parseResult = Parser.Instance.ParseFrom($"dotnet {topLevelCommandParserResult.Command}", appArgs.ToArray());
                if (!parseResult.Errors.Any())
                {
                    TelemetryEventEntry.SendFiltered(parseResult);
                }

                exitCode = builtIn.Command(appArgs.ToArray());
            }
            else
            {
                CommandResult result = Command.Create(
                    "dotnet-" + topLevelCommandParserResult.Command,
                    appArgs,
                    FrameworkConstants.CommonFrameworks.NetStandardApp15)
                                       .Execute();
                exitCode = result.ExitCode;
            }
            return(exitCode);
        }
Пример #4
0
        internal static int ProcessArgs(string[] args, ITelemetry telemetryClient = null)
        {
            // CommandLineApplication is a bit restrictive, so we parse things ourselves here. Individual apps should use CLA.

            bool?verbose = null;
            var  success = true;
            var  command = string.Empty;
            var  lastArg = 0;

            using (INuGetCacheSentinel nugetCacheSentinel = new NuGetCacheSentinel())
            {
                for (; lastArg < args.Length; lastArg++)
                {
                    if (IsArg(args[lastArg], "d", "diagnostics"))
                    {
                        verbose = true;
                    }
                    else if (IsArg(args[lastArg], "version"))
                    {
                        PrintVersion();
                        return(0);
                    }
                    else if (IsArg(args[lastArg], "info"))
                    {
                        PrintInfo();
                        return(0);
                    }
                    else if (IsArg(args[lastArg], "h", "help") ||
                             args[lastArg] == "-?" ||
                             args[lastArg] == "/?")
                    {
                        HelpCommand.PrintHelp();
                        return(0);
                    }
                    else if (args[lastArg].StartsWith("-"))
                    {
                        Reporter.Error.WriteLine($"Unknown option: {args[lastArg]}");
                        success = false;
                    }
                    else
                    {
                        ConfigureDotNetForFirstTimeUse(nugetCacheSentinel);

                        // It's the command, and we're done!
                        command = args[lastArg];
                        break;
                    }
                }
                if (!success)
                {
                    HelpCommand.PrintHelp();
                    return(1);
                }

                if (telemetryClient == null)
                {
                    telemetryClient = new Telemetry(nugetCacheSentinel);
                }
            }

            var appArgs = (lastArg + 1) >= args.Length ? Enumerable.Empty <string>() : args.Skip(lastArg + 1).ToArray();

            if (verbose.HasValue)
            {
                Environment.SetEnvironmentVariable(CommandContext.Variables.Verbose, verbose.ToString());
                Console.WriteLine($"Telemetry is: {(telemetryClient.Enabled ? "Enabled" : "Disabled")}");
            }

            if (string.IsNullOrEmpty(command))
            {
                command = "help";
            }

            telemetryClient.TrackEvent(command, null, null);

            int exitCode;
            BuiltInCommandMetadata builtIn;

            if (BuiltInCommandsCatalog.Commands.TryGetValue(command, out builtIn))
            {
                exitCode = builtIn.Command(appArgs.ToArray());
            }
            else
            {
                CommandResult result = Command.Create(
                    "dotnet-" + command,
                    appArgs,
                    FrameworkConstants.CommonFrameworks.NetStandardApp15)
                                       .Execute();
                exitCode = result.ExitCode;
            }

            return(exitCode);
        }