Example #1
0
        public static int AddPackage(string[] args, [CanBeNull] AbsolutePath rootDirectory, [CanBeNull] AbsolutePath buildScript)
        {
            PrintInfo();
            Logging.Configure();
            Telemetry.AddPackage();
            ProjectModelTasks.Initialize();

            var packageId      = args.ElementAt(0);
            var packageVersion =
                (EnvironmentInfo.GetParameter <string>("version") ??
                 args.ElementAtOrDefault(1) ??
                 NuGetPackageResolver.GetLatestPackageVersion(packageId, includePrereleases: false).GetAwaiter().GetResult() ??
                 NuGetPackageResolver.GetGlobalInstalledPackage(packageId, version: null, packagesConfigFile: null)?.Version.ToString())
                .NotNull("packageVersion != null");

            var configuration    = GetConfiguration(buildScript, evaluate: true);
            var buildProjectFile = configuration[BUILD_PROJECT_FILE];

            Host.Information($"Installing {packageId}/{packageVersion} to {buildProjectFile} ...");
            AddOrReplacePackage(packageId, packageVersion, PACKAGE_TYPE_DOWNLOAD, buildProjectFile);
            DotNetTasks.DotNet($"restore {buildProjectFile}");

            var installedPackage = NuGetPackageResolver.GetGlobalInstalledPackage(packageId, packageVersion, packagesConfigFile: null)
                                   .NotNull("installedPackage != null");
            var hasToolsDirectory = installedPackage.Directory.GlobDirectories("tools").Any();

            if (!hasToolsDirectory)
            {
                AddOrReplacePackage(packageId, packageVersion, PACKAGE_TYPE_REFERENCE, buildProjectFile);
            }

            Host.Information($"Done installing {packageId}/{packageVersion} to {buildProjectFile}");
            return(0);
        }