Exemplo n.º 1
0
        private void EnsurePackageFiles()
        {
            if (_files != null &&
                _expandedFolderPath != null &&
                _expandedFileSystem.DirectoryExists(_expandedFolderPath))
            {
                return;
            }

            _files = new Dictionary <string, PhysicalPackageFile>();
            _supportedFrameworks = null;

            var packageName = new PackageName(Id, Version);

            // Only use the cache for expanded folders under %temp%, or set from unit tests
            if (_expandedFileSystem == TempFileSystem || _forceUseCache)
            {
                Tuple <string, DateTimeOffset> cacheValue;
                DateTimeOffset lastModifiedTime = _fileSystem.GetLastModified(_packagePath);

                // if the cache doesn't exist, or it exists but points to a stale package,
                // then we invalidate the cache and store the new entry.
                if (!_cachedExpandedFolder.TryGetValue(packageName, out cacheValue) ||
                    cacheValue.Item2 < lastModifiedTime)
                {
                    cacheValue = Tuple.Create(GetExpandedFolderPath(), lastModifiedTime);
                    _cachedExpandedFolder[packageName] = cacheValue;
                }

                _expandedFolderPath = cacheValue.Item1;
            }
            else
            {
                _expandedFolderPath = GetExpandedFolderPath();
            }

            using (Stream stream = GetStream())
            {
                Package package = Package.Open(stream);
                // unzip files inside package
                var files = from part in package.GetParts()
                            where ZipPackage.IsPackageFile(part, package.PackageProperties.Identifier)
                            select part;

                // now copy all package's files to disk
                foreach (PackagePart file in files)
                {
                    string path     = UriUtility.GetPath(file.Uri);
                    string filePath = Path.Combine(_expandedFolderPath, path);

                    bool copyFile = true;
                    if (_expandedFileSystem.FileExists(filePath))
                    {
                        using (Stream partStream = file.GetStream(),
                               targetStream = _expandedFileSystem.OpenFile(filePath))
                        {
                            // if the target file already exists,
                            // don't copy file if the lengths are equal.
                            copyFile = partStream.Length != targetStream.Length;
                        }
                    }

                    if (copyFile)
                    {
                        using (Stream partStream = file.GetStream())
                        {
                            try
                            {
                                using (Stream targetStream = _expandedFileSystem.CreateFile(filePath))
                                {
                                    partStream.CopyTo(targetStream);
                                }
                            }
                            catch (Exception)
                            {
                                // if the file is read-only or has an access denied issue, we just ignore it
                            }
                        }
                    }

                    var packageFile = new PhysicalPackageFile
                    {
                        SourcePath = _expandedFileSystem.GetFullPath(filePath),
                        TargetPath = path
                    };

                    _files[path] = packageFile;
                }
            }
        }
 public ZipPackageFile(PackagePart part)
     : this(UriUtility.GetPath(part.Uri), part.GetStream().ToStreamFactory())
 {
 }
Exemplo n.º 3
0
        public override IEnumerable <FrameworkName> GetSupportedFrameworks()
        {
            IEnumerable <FrameworkName> fileFrameworks;
            IEnumerable <IPackageFile>  cachedFiles;

            if (_enableCaching && MemoryCache.Instance.TryGetValue(GetFilesCacheKey(), out cachedFiles))
            {
                fileFrameworks = cachedFiles.Select(c => c.TargetFramework);
            }
            else
            {
                using (Stream stream = _streamFactory())
                {
                    var package = Package.Open(stream);

                    string effectivePath;
                    fileFrameworks = from part in package.GetParts()
                                     where IsPackageFile(part, Id)
                                     select VersionUtility.ParseFrameworkNameFromFilePath(UriUtility.GetPath(part.Uri), out effectivePath);
                }
            }

            return(base.GetSupportedFrameworks()
                   .Concat(fileFrameworks)
                   .Where(f => f != null)
                   .Distinct());
        }
Exemplo n.º 4
0
 /// <summary>
 /// Returns path2 relative to path1, with given path separator
 /// </summary>
 public static string GetRelativePath(string path1, string path2, char separator)
 {
     return(UriUtility.GetPath(GetRelativeUri(path1, path2), separator));
 }
Exemplo n.º 5
0
 public ZipPackageFile(PackagePart part)
 {
     Path           = UriUtility.GetPath(part.Uri);
     _streamFactory = part.GetStream().ToStreamFactory();
 }
Exemplo n.º 6
0
        private void EnsurePackageFiles()
        {
            if ((this._files == null) || ((this._expandedFolderPath == null) || !this._expandedFileSystem.DirectoryExists(this._expandedFolderPath)))
            {
                this._files = new Dictionary <string, PhysicalPackageFile>();
                this._supportedFrameworks = null;
                PackageName key = new PackageName(base.Id, base.Version);
                if (!ReferenceEquals(this._expandedFileSystem, _tempFileSystem) && !this._forceUseCache)
                {
                    this._expandedFolderPath = this.GetExpandedFolderPath();
                }
                else
                {
                    Tuple <string, DateTimeOffset> tuple;
                    DateTimeOffset lastModified = this._fileSystem.GetLastModified(this._packagePath);
                    if (!_cachedExpandedFolder.TryGetValue(key, out tuple) || (tuple.Item2 < lastModified))
                    {
                        tuple = Tuple.Create <string, DateTimeOffset>(this.GetExpandedFolderPath(), lastModified);
                        _cachedExpandedFolder[key] = tuple;
                    }
                    this._expandedFolderPath = tuple.Item1;
                }
                using (Stream stream = this.GetStream())
                {
                    using (IEnumerator <PackagePart> enumerator = (from part in Package.Open(stream).GetParts()
                                                                   where ZipPackage.IsPackageFile(part)
                                                                   select part).GetEnumerator())
                    {
                        string path;
                        string str2;
                        goto TR_0023;
TR_000E:
                        PhysicalPackageFile file1 = new PhysicalPackageFile();
                        file1.SourcePath          = this._expandedFileSystem.GetFullPath(str2);
                        file1.TargetPath          = path;
                        this._files[path]         = file1;
TR_0023:
                        while (true)
                        {
                            if (enumerator.MoveNext())
                            {
                                PackagePart current = enumerator.Current;
                                path = UriUtility.GetPath(current.Uri);
                                str2 = Path.Combine(this._expandedFolderPath, path);
                                bool flag = true;
                                if (this._expandedFileSystem.FileExists(str2))
                                {
                                    using (Stream stream2 = current.GetStream())
                                    {
                                        using (Stream stream3 = this._expandedFileSystem.OpenFile(str2))
                                        {
                                            flag = stream2.Length != stream3.Length;
                                        }
                                    }
                                }
                                if (flag)
                                {
                                    Stream stream4 = current.GetStream();
                                    try
                                    {
                                        using (Stream stream5 = this._expandedFileSystem.CreateFile(str2))
                                        {
                                            stream4.CopyTo(stream5);
                                        }
                                    }
                                    catch (Exception)
                                    {
                                    }
                                    finally
                                    {
                                        if (stream4 != null)
                                        {
                                            stream4.Dispose();
                                        }
                                    }
                                }
                            }
                            else
                            {
                                return;
                            }
                            break;
                        }
                        goto TR_000E;
                    }
                }
            }
        }
Exemplo n.º 7
0
 /// <summary>
 /// This routine was implemented to assist with finding credentials in the settings file using the uri
 /// It appears the package uri is used to find the credentials ex) https://hostname/api/nuget/Download/packagename/versionnumber
 /// but the settings file will more than likely only have the repository uri ex) https://hostname/api/nuget
 /// This routine will attempt to find the uri in settings as is
 /// If not found, see if source Uri is base of uri
 /// </summary>
 /// <param name="uri1">base or source URI</param>
 /// <param name="uri2">full URI</param>
 /// <returns></returns>
 public static bool UriStartsWith(Uri uri1, Uri uri2)
 {
     return(UriUtility.UriEquals(uri1, uri2) || uri1.IsBaseOf(uri2));
 }
Exemplo n.º 8
0
        private void EnsurePackageFiles()
        {
            if (_files != null &&
                _expandedFolderPath != null &&
                _expandedFileSystem.DirectoryExists(_expandedFolderPath))
            {
                return;
            }

            _files = new Dictionary <string, PhysicalPackageFile>();
            _supportedFrameworks = null;

            var packageName = new PackageName(Id, Version);

            // Only use the cache for expanded folders under %temp%.
            if (_expandedFileSystem == _tempFileSystem)
            {
                _expandedFolderPath = _cachedExpandedFolder.GetOrAdd(packageName, _ => GetExpandedFolderPath());
            }
            else
            {
                _expandedFolderPath = GetExpandedFolderPath();
            }

            using (Stream stream = GetStream())
            {
                Package package = Package.Open(stream);
                // unzip files inside package
                var files = from part in package.GetParts()
                            where ZipPackage.IsPackageFile(part)
                            select part;

                // now copy all package's files to disk
                foreach (PackagePart file in files)
                {
                    string path     = UriUtility.GetPath(file.Uri);
                    string filePath = Path.Combine(_expandedFolderPath, path);

                    using (Stream partStream = file.GetStream())
                    {
                        // only copy the package part to disk if there doesn't exist
                        // a file with the same name.
                        if (!_expandedFileSystem.FileExists(filePath))
                        {
                            using (Stream targetStream = _expandedFileSystem.CreateFile(filePath))
                            {
                                partStream.CopyTo(targetStream);
                            }
                        }
                    }

                    var packageFile = new PhysicalPackageFile
                    {
                        SourcePath = _expandedFileSystem.GetFullPath(filePath),
                        TargetPath = path
                    };

                    _files[path] = packageFile;
                }
            }
        }
 public ZipPackageFile(PackagePart part)
     : base(UriUtility.GetPath(part.Uri))
 {
     Debug.Assert(part != null, "part should not be null");
     _streamFactory = () => part.GetStream();
 }
Exemplo n.º 10
0
 public ZipPackageFile(ZipEntry part)
     : this(UriUtility.GetPath(new Uri(part.FileName, UriKind.Relative)), part)
 {
 }
Exemplo n.º 11
0
        public override IEnumerable <FrameworkName> GetSupportedFrameworks()
        {
            IEnumerable <FrameworkName> fileFrameworks;
            IEnumerable <IPackageFile>  cachedFiles;

            if (_enableCaching && MemoryCache.Instance.TryGetValue(GetFilesCacheKey(), out cachedFiles))
            {
                fileFrameworks = cachedFiles.Select(c => c.TargetFramework);
            }
            else
            {
                using (Stream stream = _streamFactory())
                {
                    using (ZipFile zip = ZipFile.Read(stream))
                    {
                        string effectivePath;
                        fileFrameworks = from part in zip.Entries
                                         where IsPackageFile(new Uri(part.FileName, UriKind.Relative))
                                         select VersionUtility.ParseFrameworkNameFromFilePath(UriUtility.GetPath(new Uri(part.FileName, UriKind.Relative)), out effectivePath);
                    }
                }
            }

            return(base.GetSupportedFrameworks()
                   .Concat(fileFrameworks)
                   .Where(f => f != null)
                   .Distinct());
        }
Exemplo n.º 12
0
        private bool TryGetCredentials(Uri uri, out NetworkCredential configurationCredentials)
        {
            PackageSource source = Enumerable.FirstOrDefault <PackageSource>(this._packageSourceProvider.LoadPackageSources(), delegate(PackageSource p) {
                Uri uri;
                return(!string.IsNullOrEmpty(p.UserName) && (!string.IsNullOrEmpty(p.Password) && (Uri.TryCreate(p.Source, UriKind.Absolute, out uri) && UriUtility.UriStartsWith(uri, uri))));
            });

            if (source == null)
            {
                configurationCredentials = null;
                return(false);
            }
            configurationCredentials = new NetworkCredential(source.UserName, source.Password);
            return(true);
        }