示例#1
0
        private static string GetReadmeFilePath(NewPackageAction action)
        {
            // Get the package manager and project manager from the target
            var packageManager = action.Target.TryGetFeature <IPackageManager>();

            if (packageManager == null)
            {
                return(null);
            }

            // Get the package from the shared repository
            var package = packageManager.LocalRepository.FindPackage(
                action.PackageIdentity.Id, CoreConverters.SafeToSemanticVersion(action.PackageIdentity.Version));

            if (package != null &&
                package.GetFiles().Any(f => f.Path.Equals(ReadmeFileName, StringComparison.OrdinalIgnoreCase)))
            {
                var packageInstalledPath = packageManager.PathResolver.GetInstallPath(package);
                return(System.IO.Path.Combine(
                           packageInstalledPath,
                           ReadmeFileName));
            }

            return(null);
        }
示例#2
0
        public override Task <JObject> GetPackageMetadata(string id, Versioning.NuGetVersion version)
        {
            return(Task.Factory.StartNew(() =>
            {
                NuGetTraceSources.V2SourceRepository.Verbose("getpackage", "Getting metadata for {0} {1}", id, version);
                var semver = CoreConverters.SafeToSemanticVersion(version);
                var package = _repository.FindPackage(id, semver);

                // Sometimes, V2 APIs seem to fail to return a value for Packages(Id=,Version=) requests...
                if (package == null)
                {
                    var packages = _repository.FindPackagesById(id);
                    package = packages.FirstOrDefault(p => Equals(p.Version, semver));
                }

                // If still null, fail
                if (package == null)
                {
                    return null;
                }

                string repoRoot = null;
                IPackagePathResolver resolver = null;
                if (_lprepo != null)
                {
                    repoRoot = _lprepo.Source;
                    resolver = _lprepo.PathResolver;
                }

                return PackageJsonLd.CreatePackage(package, repoRoot, resolver);
            }));
        }
示例#3
0
        public CoreInteropPackage(JObject json)
        {
            Id      = json.Value <string>("id");
            Version = NuGetVersion.Parse(json.Value <string>("version"));
            _oldVer = CoreConverters.SafeToSemanticVersion(Version);

            Json = json;
        }
示例#4
0
        private IEnumerable <PackageIdentity> CreatePackageIdentityFromNupkgPath()
        {
            PackageIdentity identity = null;
            IPackage        package  = null;

            try
            {
                // Example: install-package2 https://az320820.vo.msecnd.net/packages/microsoft.aspnet.mvc.4.0.20505.nupkg
                if (_isHttp)
                {
                    PackageIdentity         packageIdentity = ParsePackageIdentityFromHttpSource(Id);
                    IPackageCacheRepository packageCache    = this.Projects.FirstOrDefault().TryGetFeature <IPackageCacheRepository>();
                    IPackageName            packageName     = CoreConverters.SafeToPackageName(packageIdentity);
                    SemanticVersion         packageSemVer   = CoreConverters.SafeToSemanticVersion(packageIdentity.Version);
                    Uri downloadUri = new Uri(Id);
                    PackageDownloader downloader = new PackageDownloader();

                    // Try to download the package through the cache.
                    bool success = packageCache.InvokeOnPackage(
                        packageIdentity.Id,
                        packageSemVer,
                        (targetStream) =>
                        downloader.DownloadPackage(
                            new HttpClient(downloadUri),
                            packageName,
                            targetStream));
                    if (success)
                    {
                        // Try to get it from the cache again
                        package = packageCache.FindPackage(packageIdentity.Id, packageSemVer);
                    }
                }
                else
                {
                    // Example: install-package2 c:\temp\packages\jQuery.1.10.2.nupkg
                    string fullPath = Path.GetFullPath(Id);
                    package = new OptimizedZipPackage(fullPath);
                }

                if (package != null)
                {
                    Id       = package.Id;
                    Version  = package.Version.ToString();
                    identity = new PackageIdentity(Id, NuGetVersion.Parse(Version));
                }
            }
            catch (Exception ex)
            {
                Log(MessageLevel.Error, Resources.Cmdlet_FailToParsePackages, Id, ex.Message);
            }

            return(new List <PackageIdentity>()
            {
                identity
            });
        }
示例#5
0
        public void Execute(NewPackageAction action, IExecutionContext context, CancellationToken cancelToken)
        {
            var nugetAware = action.Target.TryGetFeature <NuGetAwareProject>();

            if (nugetAware != null)
            {
                var task = nugetAware.UninstallPackage(
                    action.PackageIdentity,
                    context,
                    cancelToken);
                task.Wait();
                return;
            }

            // Get the project manager
            var projectManager = action.Target.GetRequiredFeature <IProjectManager>();

            // Get the package out of the project manager
            var package = projectManager.LocalRepository.FindPackage(
                action.PackageIdentity.Id,
                CoreConverters.SafeToSemanticVersion(action.PackageIdentity.Version));

            Debug.Assert(package != null);

            // Add the package to the project
            projectManager.Logger         = new ShimLogger(context);
            projectManager.Project.Logger = projectManager.Logger;
            projectManager.Execute(new PackageOperation(
                                       package,
                                       NuGet.PackageAction.Uninstall));

            // Run uninstall.ps1 if present
            ActionHandlerHelpers.ExecutePowerShellScriptIfPresent(
                "uninstall.ps1",
                action.Target,
                package,
                projectManager.PackageManager.PathResolver.GetInstallPath(package),
                context);
        }
示例#6
0
        public void Execute(NewPackageAction action, IExecutionContext context, CancellationToken cancelToken)
        {
            // Use the core-interop feature to execute the action
            var packageManager = action.Target.GetRequiredFeature <IPackageManager>();

            // Preconditions:
            Debug.Assert(!packageManager.LocalRepository.IsReferenced(
                             action.PackageIdentity.Id,
                             CoreConverters.SafeToSemanticVersion(action.PackageIdentity.Version)),
                         "Expected the purge operation would only be executed AFTER the package was no longer referenced!");

            // Get the package out of the project manager
            var package = packageManager.LocalRepository.FindPackage(
                action.PackageIdentity.Id,
                CoreConverters.SafeToSemanticVersion(action.PackageIdentity.Version));

            Debug.Assert(package != null);

            // Purge the package from the local repository
            packageManager.Logger = new ShimLogger(context);
            packageManager.Execute(new PackageOperation(
                                       package,
                                       NuGet.PackageAction.Uninstall));
        }
        internal static IPackage GetPackage(IPackageCacheRepository packageCache, PackageIdentity packageIdentity, Uri downloadUri)
        {
            var packageSemVer = CoreConverters.SafeToSemanticVersion(packageIdentity.Version);
            var packageName   = CoreConverters.SafeToPackageName(packageIdentity);
            var downloader    = new PackageDownloader();

            IPackage package = null;

            if (packageCache != null)
            {
                package = packageCache.FindPackage(packageName.Id, packageSemVer);
                if (package != null)
                {
                    NuGetTraceSources.ActionExecutor.Info(
                        "download/cachehit",
                        "[{0}] Download: Cache Hit!",
                        packageIdentity);
                    // Success!
                    return(package);
                }

                if (downloadUri == null)
                {
                    throw new InvalidOperationException(String.Format(
                                                            CultureInfo.CurrentCulture,
                                                            Strings.DownloadActionHandler_NoDownloadUrl,
                                                            packageIdentity));
                }

                // Try to download the package through the cache.
                bool success = packageCache.InvokeOnPackage(
                    packageIdentity.Id,
                    packageSemVer,
                    (targetStream) =>
                    downloader.DownloadPackage(
                        new HttpClient(downloadUri),
                        packageName,
                        targetStream));
                if (success)
                {
                    NuGetTraceSources.ActionExecutor.Info(
                        "download/downloadedtocache",
                        "[{0}] Download: Downloaded to cache",
                        packageName);

                    // Try to get it from the cache again
                    package = packageCache.FindPackage(packageIdentity.Id, packageSemVer);
                }
            }

            // Either:
            //  1. We failed to load the package into the cache, which can happen when
            //       access to the %LocalAppData% directory is blocked,
            //       e.g. on Windows Azure Web Site build OR
            //  B. It was purged from the cache before it could be retrieved again.
            // Regardless, the cache isn't working for us, so download it in to memory.
            if (package == null)
            {
                NuGetTraceSources.ActionExecutor.Info(
                    "download/cachefailing",
                    "[{0}] Download: Cache isn't working. Downloading to RAM",
                    packageName);

                using (var targetStream = new MemoryStream())
                {
                    downloader.DownloadPackage(
                        new HttpClient(downloadUri),
                        packageName,
                        targetStream);

                    targetStream.Seek(0, SeekOrigin.Begin);
                    package = new ZipPackage(targetStream);
                }
            }
            return(package);
        }
示例#8
0
        public void Execute(NewPackageAction action, IExecutionContext context, CancellationToken cancelToken)
        {
            var nugetAware = action.Target.TryGetFeature <NuGetAwareProject>();

            if (nugetAware != null)
            {
                // TODO: this is a hack to get the supported frameworks. Since action.Package
                // does not contain this info for now, we have to download the package to
                // get this info.
                var      packageContent = action.Package[Properties.PackageContent].ToString();
                var      downloadUri    = new Uri(packageContent);
                IPackage package;
                if (downloadUri.IsFile)
                {
                    package = new OptimizedZipPackage(packageContent);
                }
                else
                {
                    var packageCache = action.Target.GetRequiredFeature <IPackageCacheRepository>();
                    package = DownloadActionHandler.GetPackage(
                        packageCache,
                        action.PackageIdentity,
                        downloadUri);
                }
                var frameworks = package.GetSupportedFrameworks();
                var task       = nugetAware.InstallPackage(
                    action.PackageIdentity,
                    frameworks,
                    context,
                    cancelToken);
                task.Wait();
            }
            else
            {
                // TODO: PMC - Write Disclamer Texts
                // TODO: Dialog & PMC - open Readme.txt

                // Get the package manager and project manager from the target
                var packageManager = action.Target.GetRequiredFeature <IPackageManager>();
                var projectManager = action.Target.GetRequiredFeature <IProjectManager>();

                // Get the package from the shared repository
                var package = packageManager.LocalRepository.FindPackage(
                    action.PackageIdentity.Id, CoreConverters.SafeToSemanticVersion(action.PackageIdentity.Version));
                Debug.Assert(package != null); // The package had better be in the local repository!!

                // Ping the metrics service
                action.Source.RecordMetric(
                    action.ActionType,
                    action.PackageIdentity,
                    action.DependentPackage,
                    action.IsUpdate,
                    action.Target);

                // Add the package to the project
                projectManager.Logger         = new ShimLogger(context);
                projectManager.Project.Logger = projectManager.Logger;
                projectManager.Execute(new PackageOperation(
                                           package,
                                           NuGet.PackageAction.Install));

                // Run install.ps1 if present
                ActionHandlerHelpers.ExecutePowerShellScriptIfPresent(
                    "install.ps1",
                    action.Target,
                    package,
                    packageManager.PathResolver.GetInstallPath(package),
                    context);
            }
        }