Example #1
0
 private static LockFileTargetLibrary FindLibraryInLockFile(LockFile lockFile, PackageId id)
 {
     return(lockFile
            ?.Targets?.SingleOrDefault(t => t.RuntimeIdentifier != null)
            ?.Libraries?.SingleOrDefault(l =>
                                         string.Compare(l.Name, id.ToString(), StringComparison.OrdinalIgnoreCase) == 0));
 }
Example #2
0
 private string GetCacheFile(PackageId packageId)
 {
     return(_cacheVersionedDirectory.WithFile(packageId.ToString()).Value);
 }
Example #3
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: () => {
                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: rootConfigDirectory,
                        additionalFeeds: additionalFeeds);

                    try
                    {
                        _projectRestorer.Restore(
                            tempProject,
                            nugetConfig,
                            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(_store, 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);
                }
            }));
        }