Exemplo n.º 1
0
            public override async Task <bool> OnPackageInstallingAsync(PackageIdentity package, NuGetFramework projectFramework, PackageReaderBase packageReader, string installPath)
            {
                var entryPoint            = package.Id + BonsaiExtension;
                var nearestFrameworkGroup = packageReader.GetContentItems().GetNearest(Owner.bootstrapperFramework);
                var executablePackage     = nearestFrameworkGroup?.Items.Any(file => PathUtility.GetRelativePath(ContentFolder, file) == entryPoint);

                if (executablePackage.GetValueOrDefault())
                {
                    var packageFolder = Path.GetDirectoryName(packageReader.GetNuspecFile());
                    var resolver      = new VersionFolderPathResolver(packageFolder, isLowercase: false);
                    var nupkgFileName = resolver.GetPackageFileName(package.Id, package.Version);
                    var nupkgFilePath = Path.Combine(packageFolder, nupkgFileName);
                    var localPackage  = Owner.galleryRepository.GetLocalPackage(package);
                    if (localPackage == null)
                    {
                        var targetFilePath = Path.Combine(Owner.galleryRepository.PackageSource.Source, Path.GetFileName(nupkgFileName));
                        PathUtility.EnsureParentDirectory(targetFilePath);
                        File.Copy(nupkgFilePath, targetFilePath);
                    }
                    return(false);
                }
                else
                {
                    var pivots = OverlayHelper.FindPivots(packageReader, installPath).ToArray();
                    if (pivots.Length > 0)
                    {
                        PathUtility.EnsureParentDirectory(Path.Combine(installPath, package.Id));
                        var overlayVersion = OverlayHelper.FindOverlayVersion(packageReader);
                        var overlayManager = OverlayHelper.CreateOverlayManager(Owner.packageManager, installPath);
                        overlayManager.Logger = Owner.packageManager.Logger;
                        try
                        {
                            foreach (var pivot in pivots)
                            {
                                var pivotIdentity = new PackageIdentity(pivot, overlayVersion);
                                var pivotPackage  = await overlayManager.InstallPackageAsync(pivotIdentity, projectFramework, ignoreDependencies : true, CancellationToken.None);

                                if (pivotPackage == null)
                                {
                                    throw new InvalidOperationException(string.Format("The package '{0}' could not be found.", pivot));
                                }
                            }
                        }
                        catch
                        {
                            foreach (var pivot in pivots)
                            {
                                var pivotIdentity = new PackageIdentity(pivot, overlayVersion);
                                await overlayManager.UninstallPackageAsync(pivotIdentity, projectFramework, removeDependencies : false, CancellationToken.None);
                            }
                            throw;
                        }
                    }

                    return(true);
                }
            }
Exemplo n.º 2
0
        public static string InstallExecutablePackage(PackageIdentity package, NuGetFramework projectFramework, PackageReaderBase packageReader, string targetPath)
        {
            var targetId                = Path.GetFileName(targetPath);
            var targetEntryPoint        = targetId + Constants.BonsaiExtension;
            var targetEntryPointLayout  = targetEntryPoint + Constants.LayoutExtension;
            var packageEntryPoint       = package.Id + Constants.BonsaiExtension;
            var packageEntryPointLayout = packageEntryPoint + Constants.LayoutExtension;

            var nearestFrameworkGroup = packageReader.GetContentItems().GetNearest(projectFramework);

            if (nearestFrameworkGroup != null)
            {
                foreach (var file in nearestFrameworkGroup.Items)
                {
                    var effectivePath = PathUtility.GetRelativePath(ContentFolder, file);
                    if (effectivePath == packageEntryPoint)
                    {
                        effectivePath = targetEntryPoint;
                    }
                    else if (effectivePath == packageEntryPointLayout)
                    {
                        effectivePath = targetEntryPointLayout;
                    }
                    effectivePath = Path.Combine(targetPath, effectivePath);
                    PathUtility.EnsureParentDirectory(effectivePath);

                    using (var stream = packageReader.GetStream(file))
                        using (var targetStream = File.Create(effectivePath))
                        {
                            stream.CopyTo(targetStream);
                        }
                }
            }

            var effectiveEntryPoint = Path.Combine(targetPath, targetEntryPoint);

            if (!File.Exists(effectiveEntryPoint))
            {
                var message = string.Format(Resources.MissingWorkflowEntryPoint, targetEntryPoint);
                throw new InvalidOperationException(message);
            }

            var manifestFile = packageReader.GetNuspecFile();
            var metadataPath = Path.Combine(targetPath, targetId + NuGetConstants.ManifestExtension);

            using (var manifestStream = packageReader.GetStream(manifestFile))
                using (var manifestTargetStream = File.Create(metadataPath))
                {
                    var manifest = Manifest.ReadFrom(manifestStream, validateSchema: true);
                    manifest.Save(manifestTargetStream);
                }

            return(effectiveEntryPoint);
        }