Ejemplo n.º 1
0
        public IToolPackage InstallPackage(PackageId packageId,
                                           VersionRange versionRange         = null,
                                           string targetFramework            = null,
                                           FilePath?nugetConfig              = null,
                                           DirectoryPath?rootConfigDirectory = null,
                                           string[] additionalFeeds          = null,
                                           string verbosity = null)
        {
            var    packageRootDirectory = _store.GetRootPackageDirectory(packageId);
            string rollbackDirectory    = null;

            return(TransactionalAction.Run <IToolPackage>(
                       action: () => {
                var stageDirectory = _store.GetRandomStagingDirectory();
                _fileSystem.Directory.CreateDirectory(stageDirectory.Value);
                rollbackDirectory = stageDirectory.Value;

                var tempProject = new FilePath(Path.Combine(stageDirectory.Value, ProjectFileName));

                // Write a fake project with the requested package id, version, and framework
                _fileSystem.File.WriteAllText(
                    tempProject.Value,
                    $"{packageId};{versionRange?.ToString("S", new VersionRangeFormatter()) ?? "*"};{targetFramework};{stageDirectory.Value}");

                // Perform a restore on the fake project
                _projectRestorer.Restore(
                    tempProject,
                    nugetConfig,
                    verbosity);

                if (_installCallback != null)
                {
                    _installCallback();
                }

                var version = _store.GetStagedPackageVersion(stageDirectory, packageId);
                var packageDirectory = _store.GetPackageDirectory(packageId, version);
                if (_fileSystem.Directory.Exists(packageDirectory.Value))
                {
                    throw new ToolPackageException(
                        string.Format(
                            CommonLocalizableStrings.ToolPackageConflictPackageId,
                            packageId,
                            version.ToNormalizedString()));
                }

                _fileSystem.Directory.CreateDirectory(packageRootDirectory.Value);
                _fileSystem.Directory.Move(stageDirectory.Value, packageDirectory.Value);
                rollbackDirectory = packageDirectory.Value;

                IEnumerable <string> warnings = null;
                _warningsMap.TryGetValue(packageId, out warnings);

                IReadOnlyList <FilePath> packedShims = null;
                _packagedShimsMap.TryGetValue(packageId, out packedShims);

                return new ToolPackageMock(_fileSystem, packageId, version, packageDirectory, warnings: warnings, packagedShims: packedShims);
            },
                       rollback: () => {
                if (rollbackDirectory != null && _fileSystem.Directory.Exists(rollbackDirectory))
                {
                    _fileSystem.Directory.Delete(rollbackDirectory, true);
                }
                if (_fileSystem.Directory.Exists(packageRootDirectory.Value) &&
                    !_fileSystem.Directory.EnumerateFileSystemEntries(packageRootDirectory.Value).Any())
                {
                    _fileSystem.Directory.Delete(packageRootDirectory.Value, false);
                }
            }));
        }
Ejemplo n.º 2
0
        public IToolPackage InstallPackage(
            PackageLocation packageLocation,
            PackageId packageId,
            VersionRange versionRange = null,
            string targetFramework    = null,
            string verbosity          = null)
        {
            var    packageRootDirectory = _store.GetRootPackageDirectory(packageId);
            string rollbackDirectory    = null;

            return(TransactionalAction.Run <IToolPackage>(
                       action: () => {
                try
                {
                    var stageDirectory = _store.GetRandomStagingDirectory();
                    Directory.CreateDirectory(stageDirectory.Value);
                    rollbackDirectory = stageDirectory.Value;

                    var tempProject = CreateTempProject(
                        packageId: packageId,
                        versionRange: versionRange,
                        targetFramework: targetFramework ?? BundledTargetFramework.GetTargetFrameworkMoniker(),
                        restoreDirectory: stageDirectory,
                        assetJsonOutputDirectory: stageDirectory,
                        rootConfigDirectory: packageLocation.RootConfigDirectory,
                        additionalFeeds: packageLocation.AdditionalFeeds);

                    try
                    {
                        _projectRestorer.Restore(
                            tempProject,
                            packageLocation,
                            verbosity: verbosity);
                    }
                    finally
                    {
                        File.Delete(tempProject.Value);
                    }

                    var version = _store.GetStagedPackageVersion(stageDirectory, packageId);
                    var packageDirectory = _store.GetPackageDirectory(packageId, version);
                    if (Directory.Exists(packageDirectory.Value))
                    {
                        throw new ToolPackageException(
                            string.Format(
                                CommonLocalizableStrings.ToolPackageConflictPackageId,
                                packageId,
                                version.ToNormalizedString()));
                    }

                    Directory.CreateDirectory(packageRootDirectory.Value);
                    FileAccessRetrier.RetryOnMoveAccessFailure(() => Directory.Move(stageDirectory.Value, packageDirectory.Value));
                    rollbackDirectory = packageDirectory.Value;

                    return new ToolPackageInstance(packageId, version, packageDirectory);
                }
                catch (Exception ex) when(ex is UnauthorizedAccessException || ex is IOException)
                {
                    throw new ToolPackageException(
                        string.Format(
                            CommonLocalizableStrings.FailedToInstallToolPackage,
                            packageId,
                            ex.Message),
                        ex);
                }
            },
                       rollback: () => {
                if (!string.IsNullOrEmpty(rollbackDirectory) && Directory.Exists(rollbackDirectory))
                {
                    Directory.Delete(rollbackDirectory, true);
                }

                // Delete the root if it is empty
                if (Directory.Exists(packageRootDirectory.Value) &&
                    !Directory.EnumerateFileSystemEntries(packageRootDirectory.Value).Any())
                {
                    Directory.Delete(packageRootDirectory.Value, false);
                }
            }));
        }