Beispiel #1
0
 public static void EnsureLog(LogLevel logLevel)
 {
     // Set up logging.
     // For tests this will already be set.
     if (Log == null)
     {
         Log = new CommandOutputLogger(logLevel);
     }
 }
Beispiel #2
0
        public static void Register(
            CommandLineApplication cmdApp,
            CommandOutputLogger log)
        {
            cmdApp.Command("restore", (Action<CommandLineApplication>)(restore =>
            {
                restore.Description = "Restores packages for a project and writes a lock file.";
                restore.HelpOption(XPlatUtility.HelpOption);

                var sources = restore.Option(
                    "-s|--source <source>",
                    "Specifies a NuGet package source to use during the restore.",
                    CommandOptionType.MultipleValue);

                var packagesDirectory = restore.Option(
                    "--packages <packagesDirectory>",
                    "Directory to install packages in.",
                    CommandOptionType.SingleValue);

                var disableParallel = restore.Option(
                    "--disable-parallel",
                   "Disables restoring multiple projects in parallel.",
                    CommandOptionType.NoValue);

                var fallBack = restore.Option(
                    "-f|--fallbacksource <FEED>",
                    "A list of packages sources to use as a fallback.",
                    CommandOptionType.MultipleValue);

                var runtime = restore.Option(
                    "--runtime <RID>",
                    "List of runtime identifiers to restore for.",
                    CommandOptionType.MultipleValue);

                var configFile = restore.Option(
                    "--configfile <file>",
                   "The NuGet configuration file to use.",
                    CommandOptionType.SingleValue);

                var noCache = restore.Option(
                    "--no-cache",
                    "Do not cache packages and http requests.",
                    CommandOptionType.NoValue);

                var verbosity = restore.Option(
                    XPlatUtility.VerbosityOption,
                    "The verbosity of logging to use. Allowed values: Debug, Verbose, Information, Minimal, Warning, Error.",
                    CommandOptionType.SingleValue);

                var argRoot = restore.Argument(
                    "[root]",
                    "List of projects and project folders to restore. Each value can be: a path to a project.json or global.json file, or a folder to recursively search for project.json files.",
                    multipleValues: true);

                restore.OnExecute(async () =>
                {
                    var logLevel = XPlatUtility.GetLogLevel(verbosity);
                    log.SetLogLevel(logLevel);

                    using (var cacheContext = new SourceCacheContext())
                    {
                        cacheContext.NoCache = noCache.HasValue();
                        var providerCache = new RestoreCommandProvidersCache();

                        // Ordered request providers
                        var providers = new List<IRestoreRequestProvider>();
                        providers.Add(new MSBuildP2PRestoreRequestProvider(providerCache));
                        providers.Add(new ProjectJsonRestoreRequestProvider(providerCache));

                        var restoreContext = new RestoreArgs()
                        {
                            CacheContext = cacheContext,
                            LockFileVersion = LockFileFormat.Version,
                            ConfigFileName = configFile.HasValue() ? configFile.Value() : null,
                            DisableParallel = true,
                            GlobalPackagesFolder = packagesDirectory.HasValue() ? packagesDirectory.Value() : null,
                            Inputs = new List<string>(argRoot.Values),
                            Log = log,
                            MachineWideSettings = new CommandLineXPlatMachineWideSetting(),
                            RequestProviders = providers,
                            Sources = sources.Values,
                            FallbackSources = fallBack.Values,
                            CachingSourceProvider = _sourceProvider
                        };

                        restoreContext.Runtimes.UnionWith(runtime.Values);

                        var restoreSummaries = await RestoreRunner.Run(restoreContext);

                        // Summary
                        RestoreSummary.Log(log, restoreSummaries, logLevel < LogLevel.Minimal);

                        return restoreSummaries.All(x => x.Success) ? 0 : 1;
                    }
                });
            }));
        }