Example #1
0
        protected override int DoExecute(ITaskContextInternal context)
        {
            var findPackageTask = new FindNuGetPackageInUserRepositoryTask(_packageId);

            findPackageTask.Execute(context);

            if (findPackageTask.PackageVersion != null && _packageVersion != null &&
                findPackageTask.PackageVersion > _packageVersion)
            {
                PackageDirectory = findPackageTask.PackageDirectory;
                return(0);
            }

            if (findPackageTask.PackageDirectory != null)
            {
                PackageDirectory = findPackageTask.PackageDirectory;
                return(0);
            }

            var task = new NuGetCmdLineTask("install")
                       .WithArguments(_packageId)
                       .WithArguments($"-Source {PackageSource}")
                       .WithArguments("-NonInteractive")
                       .WithArguments($"-OutputDirectory {NuGetPackagesCacheDir}");

            if (_packageVersion != null)
            {
                task.WithArguments($"-Version {_packageVersion}");
            }

            if (ConfigFile != null)
            {
                task.WithArguments($"-ConfigFile {ConfigFile}");
            }

            if (Verbosity.HasValue)
            {
                task.Verbosity = Verbosity.Value;
            }

            task.Execute(context);

            findPackageTask.Execute(context);
            PackageDirectory = findPackageTask.PackageDirectory;

            context.LogError(
                PackageDirectory == null
                    ? $"Something is wrong, after downloading it the NuGet package '{_packageId}' still could not be found."
                    : "Package downloaded to '{packageDirectory}'");

            return(0);
        }
        protected override int DoExecute(ITaskContextInternal context)
        {
            FullPath packagesDir = new FullPath(context.Properties.Get(BuildProps.ProductRootDir, "."));

            packagesDir = packagesDir.CombineWith(context.Properties.Get <string>(DotNetBuildProps.BuildDir));

            FileFullPath destNuspecFile = packagesDir.AddFileName("{0}.nuspec", _packageId);

            DoLogInfo($"Preparing the {destNuspecFile} file");

            new ReplaceTokensTask(_nuspecFileName)
            .Replace("version", context.Properties.GetBuildVersion().BuildVersionWithQuality(3))
            .UseToken("$")
            .ToDestination(destNuspecFile.ToString())
            .ExecuteVoid(context);

            // package it
            DoLogInfo("Creating a NuGet package file");
            string           nugetWorkingDir = destNuspecFile.Directory.ToString();
            NuGetCmdLineTask nugetTask       = new NuGetCmdLineTask("pack", nugetWorkingDir)
            {
                Verbosity = NuGetCmdLineTask.NuGetVerbosity.Detailed
            };

            nugetTask.WithArguments(destNuspecFile.FileName);

            if (BasePath != null)
            {
                nugetTask.WithArguments("-BasePath", BasePath);
            }

            nugetTask.ExecuteVoid(context);

            string nupkgFileName = string.Format(
                CultureInfo.InvariantCulture,
                "{0}.{1}.nupkg",
                _packageId,
                context.Properties.GetBuildVersion().BuildVersionWithQuality(3));

            DoLogInfo($"NuGet package file {nupkgFileName} created");

            // do not push new packages from a local build
            if (context.BuildServers().IsLocalBuild&& _skipPushOnLocalBuild)
            {
                context.LogInfo("pushing package on local build is disabled in build script...Skiping.");
                return(1);
            }

            if (_apiKeyFunc == null)
            {
                throw new InvalidOperationException("NuGet API key was not provided");
            }

            string apiKey = _apiKeyFunc(context);

            if (apiKey == null)
            {
                return(1);
            }

            // publish the package file
            DoLogInfo("Pushing the NuGet package to the repository");

            nugetTask = new NuGetCmdLineTask("push", nugetWorkingDir)
            {
                Verbosity = NuGetCmdLineTask.NuGetVerbosity.Detailed,
                ApiKey    = apiKey
            };

            if (_nuGetServerUrl != null)
            {
                nugetTask.WithArguments("-Source", _nuGetServerUrl);
            }

            nugetTask
            .WithArguments(nupkgFileName)
            .ExecuteVoid(context);

            return(0);
        }