Debug() public abstract method

public abstract Debug ( string messageText ) : bool
messageText string
return bool
        /// <summary>
        /// A delegate used for finding the package.
        /// </summary>
        /// <param name="path">File repository path</param>
        /// <param name="request"></param>
        /// <returns></returns>
        private static IPackage OpenPackage(string path, Request request)
        {
            request.Debug(Resources.Messages.DebugInfoCallMethod3, "LocalPackageRepository", "OpenPackage", path);

            if (!File.Exists(path))
            {
                request.Warning(Resources.Messages.FileNotFound, path, "LocalPackageRepository::OpenPackage");
                return(null);
            }

            //deal with .nupkg
            if (string.Equals(Path.GetExtension(path), NuGetConstant.PackageExtension, StringComparison.OrdinalIgnoreCase))
            {
                PackageBase package;
                try {
                    package = ProcessZipPackage(path);
                } catch (Exception ex) {
                    request.Verbose(ex.Message);
                    throw;
                }

                // Set the last modified date on the package
                package.Published = FileUtility.GetLastModified(path);

                // We assume local files in the local repository are all latest version
                package.IsAbsoluteLatestVersion = true;
                package.IsLatestVersion         = true;
                package.FullFilePath            = path;

                return(package);
            }

            return(null);
        }
        /// <summary>
        /// Find the package via the given uri query.
        /// </summary>
        /// <param name="query">A full Uri. A sample Uri looks like "http://www.nuget.org/api/v2/FindPackagesById()?id='Jquery'" </param>
        /// <param name="request">An object passed in from the PackageManagement that contains functions that can be used to interact with its Provider</param> 
        /// <returns>Package objects</returns>
        internal static IEnumerable<PackageBase> FindPackage(string query, Request request) {
            request.Debug(Messages.DebugInfoCallMethod, "NuGetClient", "FindPackage");

            request.Verbose(Messages.SearchingRepository, query, "");

            return HttpClientPackageRepository.SendRequest(query, request);
        }
        /// <summary>
        /// Finding the packages in the file repository
        /// </summary>
        /// <param name="openPackage">Delegate function which is actually finding a package</param>
        /// <param name="packageId">Package Id</param>
        /// <param name="packagePaths">File repository path</param>
        /// <param name="request"></param>
        /// <returns></returns>
        private static IEnumerable <IPackage> GetPackages(Func <string, Request, IPackage> openPackage,
                                                          string packageId,
                                                          IEnumerable <string> packagePaths,
                                                          Request request)
        {
            request.Debug(Resources.Messages.DebugInfoCallMethod3, "LocalPackageRepository", "GetPackages", packageId);

            foreach (var path in packagePaths)
            {
                IPackage package = null;
                try {
                    package = GetPackage(openPackage, path, request);
                } catch (InvalidOperationException ex) {
                    // ignore error for unzipped packages (nuspec files).
                    if (!string.Equals(NuGetConstant.ManifestExtension, Path.GetExtension(path), StringComparison.OrdinalIgnoreCase))
                    {
                        request.Verbose(ex.Message);
                        throw;
                    }
                }

                if (package != null && package.Id.Equals(packageId, StringComparison.OrdinalIgnoreCase))
                {
                    yield return(package);
                }
            }
        }
        /// <summary>
        /// Finding the packages in the file repository
        /// </summary>
        /// <param name="openPackage">Delegate function which is actually finding a package</param>
        /// <param name="packageId">Package Id</param>
        /// <param name="packagePaths">File repository path</param>
        /// <param name="request"></param>
        /// <returns></returns>
        private static IEnumerable<IPackage> GetPackages(Func<string, Request, IPackage> openPackage, 
            string packageId, 
            IEnumerable<string> packagePaths, 
            Request request) 
        {
            request.Debug(Resources.Messages.DebugInfoCallMethod3, "LocalPackageRepository", "GetPackages", packageId);

            foreach (var path in packagePaths) 
            {
                IPackage package = null;
                try {
                    package = GetPackage(openPackage, path, request);
                } catch (InvalidOperationException ex) {
                    // ignore error for unzipped packages (nuspec files).
                    if (!string.Equals(NuGetConstant.ManifestExtension, Path.GetExtension(path), StringComparison.OrdinalIgnoreCase)) {
                        request.Verbose(ex.Message);
                        throw;
                    }
                }

                if (package != null && package.Id.Equals(packageId, StringComparison.OrdinalIgnoreCase)) {
                    yield return package;
                }
            }
        }
        /// <summary>
        /// Returns a collection of strings to the client advertizing features this provider supports.
        /// </summary>
        /// <param name="request">An object passed in from the PackageManagement that contains functions that can be used to interact with its Provider</param>
        public void GetFeatures(Request request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, "GetFeatures");

            request.Yield(Features);
        }
        /// <summary>
        /// Find-Package based the given Id
        /// </summary>
        /// <param name="openPackage">Delegate function which is actually finding a package</param>
        /// <param name="packageId">Package version</param>
        /// <param name="request"></param>
        /// <returns></returns>
        private IEnumerable <IPackage> FindPackagesById(Func <string, Request, IPackage> openPackage, string packageId, Request request)
        {
            request.Debug(Resources.Messages.DebugInfoCallMethod3, "LocalPackageRepository", "FindPackagesById", packageId);

            // get packages in .nupkg or .nuspec files
            // add "*" to avoid parsing the version (id: packageName+Version, e.g. jQuery.1.10.0)
            return(GetPackages(
                       openPackage,
                       packageId,
                       GetPackageFiles(packageId + "*" + NuGetConstant.PackageExtension),
                       request).Union(
                       GetPackages(
                           openPackage,
                           packageId,
                           GetPackageFiles(packageId + "*" + NuGetConstant.ManifestExtension),
                           request)));
        }
Esempio n. 7
0
        /// <summary>
        /// Returns a collection of strings to the client advertizing features this provider supports.
        /// </summary>
        /// <param name="request">An object passed in from the PackageManagement that contains functions that can be used to interact with its Provider</param> 
        public void GetFeatures(Request request)
        {
            if (request == null){
                throw new ArgumentNullException("request");
            }

            request.Debug(Resources.Messages.DebugInfoCallMethod, PackageProviderName, "GetFeatures");

            request.Yield(Features);
        }
        /// <summary>
        /// Find-Package based the given Id
        /// </summary>
        /// <param name="openPackage">Delegate function which is actually finding a package</param>
        /// <param name="packageId">Package version</param>
        /// <param name="request"></param>
        /// <returns></returns>
        private IEnumerable<IPackage> FindPackagesById(Func<string, Request, IPackage> openPackage, string packageId, Request request)
        {
            request.Debug(Resources.Messages.DebugInfoCallMethod3, "LocalPackageRepository", "FindPackagesById", packageId);

            // get packages in .nupkg or .nuspec files
            // add "*" to avoid parsing the version (id: packageName+Version, e.g. jQuery.1.10.0)
            return GetPackages(
                openPackage,
                packageId,
                GetPackageFiles(packageId + "*" + NuGetConstant.PackageExtension),
                request).Union(
                    GetPackages(
                        openPackage,
                        packageId,
                        GetPackageFiles(packageId + "*" + NuGetConstant.ManifestExtension),
                        request));
        }
        /// <summary>
        /// A delegate used for finding the package.
        /// </summary>
        /// <param name="path">File repository path</param>
        /// <param name="request"></param>
        /// <returns></returns>
        private static IPackage OpenPackage(string path, Request request)
        {
            request.Debug(Resources.Messages.DebugInfoCallMethod3, "LocalPackageRepository", "OpenPackage", path);

            if (!File.Exists(path)) {
                request.Warning(Resources.Messages.FileNotFound, path,"LocalPackageRepository::OpenPackage");
                return null;
            }

            //deal with .nupkg
            if (string.Equals(Path.GetExtension(path), NuGetConstant.PackageExtension, StringComparison.OrdinalIgnoreCase)) {
                PackageBase package;
                try {
                    package = ProcessZipPackage(path);
                } catch (Exception ex) {
                    request.Verbose(ex.Message);
                    throw;
                }

                // Set the last modified date on the package
                package.Published = FileUtility.GetLastModified(path);

                // We assume local files in the local repository are all latest version
                package.IsAbsoluteLatestVersion = true;
                package.IsLatestVersion = true;
                package.FullFilePath = path;

                return package;
            }

            return null;
        }
        /// <summary>
        /// Send an initial request to download data from the server.
        /// From the initial request, we may change the host of subsequent calls (if a redirection happens in this initial request)
        /// Also, if the initial request sends us less data than the amount we request, then we do not
        /// need to issue more requests
        /// </summary>
        /// <param name="query"></param>
        /// <param name="startPoint"></param>
        /// <param name="bufferSize"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        internal static Stream InitialDownloadDataToStream(UriBuilder query, int startPoint, int bufferSize, Request request)
        {
            var uri = String.Format(CultureInfo.CurrentCulture, query.Uri.ToString(), startPoint, bufferSize);
            request.Debug(Messages.DownloadingPackage, uri);

            var client = GetHttpClient(request);

            var response = PathUtility.GetHttpResponse(client, uri, request);

            // Check that response was successful or write error
            if (response == null || !response.IsSuccessStatusCode)
            {
                request.Warning(Resources.Messages.CouldNotGetResponseFromQuery, uri);
                return null;
            }

            // Read response and write out a stream
            var stream = GetStreamBasedOnEncoding(response);

            request.Debug(Messages.CompletedDownload, uri);

            // If the host from the response is different, change the host of the original query
            if (!String.Equals(response.RequestMessage.RequestUri.Host, query.Host, StringComparison.OrdinalIgnoreCase))
            {
                query.Host = response.RequestMessage.RequestUri.Host;
            }

            return stream;
        }
        /// <summary>
        /// Download data from remote via uri query.
        /// </summary>
        /// <param name="query">Uri query</param>
        /// <param name="request">An object passed in from the PackageManagement platform that contains APIs that can be used to interact with it </param>   
        /// <returns></returns>
        internal static Stream DownloadDataToStream(string query, Request request)
        {
            request.Debug(Messages.DownloadingPackage, query);

            var client = GetHttpClient(request);

            var response = PathUtility.GetHttpResponse(client, query, request);

            // Check that response was successful or throw exception
            if (response == null || !response.IsSuccessStatusCode)
            {
                request.Warning(Resources.Messages.CouldNotGetResponseFromQuery, query);
                return null;
            }

            // Read response and write out a stream
            var stream = GetStreamBasedOnEncoding(response);

            request.Debug(Messages.CompletedDownload, query);

            return stream;
        }
        /// <summary>
        /// Find-Package
        /// </summary>
        /// <param name="packageId">package Id</param>
        /// <param name="version">package version</param>
        /// <param name="request"></param>
        /// <returns></returns>
        public IPackage FindPackage(string packageId, SemanticVersion version, Request request) 
        {
            if (string.IsNullOrWhiteSpace(packageId)) {
                return null;
            }

            request.Debug(Messages.DebugInfoCallMethod3, "HttpClientPackageRepository", "FindPackage", packageId);

            var query = packageId.MakeFindPackageByIdQuery(_nugetFindPackageIdQueryFormat);          

            var packages = NuGetClient.FindPackage(query, request);

            //Usually versions has a limited number, ToArray should be ok. 
            var versions = version.GetComparableVersionStrings().ToArray();

            //Will only enumerate oackages once
            return packages.FirstOrDefault(package => packageId.Equals(package.Id, StringComparison.OrdinalIgnoreCase) && versions.Contains(package.Version,StringComparer.OrdinalIgnoreCase));  
        }
        /// <summary>
        /// Send the request to the server with buffer size to account for the case where there are more data
        /// that we need to fetch
        /// </summary>
        /// <param name="query"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        public static IEnumerable<PackageBase> SendRequest(string query, Request request)
        {
            const int bufferSize = 40;
            // number of threads sending the requests
            const int numberOfSenders = 4;

            var startPoint = 0;
            var tasks = new List<Task<Stream>>();

            bool stopSending = false;
            object stopLock = new Object();

            // Send one request first

            // this initial query is of the form http://www.nuget.org/api/v2/FindPackagesById()?id='jquery'&$skip={0}&$top={1}
            UriBuilder initialQuery = new UriBuilder(query.InsertSkipAndTop());

            // Send out an initial request
            using (Stream stream = NuGetClient.InitialDownloadDataToStream(initialQuery, startPoint, bufferSize, request))
            {
                if (stream == null)
                {
                    yield break;
                }

                XDocument document = XmlUtility.LoadSafe(stream, ignoreWhiteSpace: true);

                var entries = document.Root.ElementsNoNamespace("entry").ToList();

                // If the initial request has less entries than the buffer size, return it because there won't be any more data
                if (entries.Count < bufferSize)
                {
                    request.Debug(Messages.PackagesReceived, entries.Count);
                    stopSending = true;
                }

                foreach (XElement entry in entries)
                {
                    var package = new PackageBase();

                    PackageUtility.ReadEntryElement(ref package, entry);
                    yield return package;
                }
            }

            if (stopSending || request.IsCanceled)
            {
                yield break;
            }

            // To avoid more redirection (for example, if the initial query is nuget.org, it will be changed to www.nuget.org

            query = initialQuery.Uri.ToString();

            // Sending the initial requests
            for (var i = 0; i < numberOfSenders; i++)
            {
                // Update the start point to fetch the packages
                startPoint += bufferSize;

                // Get the query
                var newQuery = string.Format(query, startPoint, bufferSize);

                // Send it 
                tasks.Add(Task.Factory.StartNew(() =>
                {
                    Stream items = NuGetClient.DownloadDataToStream(newQuery, request);
                    return items;
                }));
            }

            //Wait for the responses, parse the data, and send to the user
            while (tasks.Count > 0)
            {

                //Cast because the compiler warning: Co-variant array conversion from Task[] to Task[] can cause run-time exception on write operation.
                var index = Task.WaitAny(tasks.Cast<Task>().ToArray());

                using (Stream stream = tasks[index].Result)
                {
                    if (stream == null)
                    {
                        yield break;
                    }

                    XDocument document = XmlUtility.LoadSafe(stream, ignoreWhiteSpace: true);

                    var entries = document.Root.ElementsNoNamespace("entry").ToList();

                    if (entries.Count < bufferSize)
                    {
                        request.Debug(Messages.PackagesReceived, entries.Count);
                        lock (stopLock)
                        {
                            stopSending = true;
                        }
                    }

                    foreach (XElement entry in entries)
                    {
                        var package = new PackageBase();

                        PackageUtility.ReadEntryElement(ref package, entry);
                        yield return package;
                    }
                }

                // checks whether we should stop sending requests
                if (!stopSending && !request.IsCanceled)
                {
                    // Make sure nobody else is updating the startPoint
                    lock (stopLock)
                    {
                        // update the startPoint
                        startPoint += bufferSize;
                    }
                    // Make a new request with the new startPoint
                    var newQuery = string.Format(query, startPoint, bufferSize);

                    //Keep sending a request 
                    tasks[index] = (Task.Factory.StartNew(searchQuery =>
                    {
                        var items = NuGetClient.DownloadDataToStream(searchQuery.ToStringSafe(), request);
                        return items;
                    }, newQuery));

                }
                else
                {
                    if (request.IsCanceled)
                    {
                        request.Warning(Messages.RequestCanceled, "HttpClientPackageRepository", "SendRequest");
                        //stop sending request to the remote server
                        stopSending = true;
                    }

                    tasks.RemoveAt(index);
                }
            }

        }
        /// <summary>
        /// Find-Package bases on the given package Id
        /// </summary>
        /// <param name="packageId">Package Id</param>
        /// <param name="request"></param>
        /// <returns></returns>
        public IEnumerable<IPackage> FindPackagesById(string packageId, Request request){

            request.Debug(Messages.DebugInfoCallMethod3, "HttpClientPackageRepository", "FindPackagesById", packageId);

            var query = packageId.MakeFindPackageByIdQuery(_nugetFindPackageIdQueryFormat);

            //request.Verbose(query.ToString());

            return NuGetClient.FindPackage(query, request);
        }