Example #1
0
        private static PackageRequestResult RequestPackage(string repoPath, string packageName, string version, string localFile, string packagesFolder)
        {
            var  result     = new PackageRequestResult();
            bool hasVersion = !string.IsNullOrWhiteSpace(version);

            try
            {
                if (Directory.Exists(repoPath))
                {
                    string name = null;
                    if (hasVersion)
                    {
                        name = packageName + "." + version;
                    }
                    else
                    {
                        var packages    = Directory.GetFiles(repoPath, "*.nupkg", SearchOption.TopDirectoryOnly);
                        var escapedName = packageName.Replace(".", @"\.");

                        string pattern = "\\A" + escapedName + "\\.\\d+(\\.\\d+){2}\\z";

                        // Prerelease-aware pattern
                        if (Program.EnablePrerelease)
                        {
                            pattern = "\\A" + escapedName + "\\.\\d+(\\.\\d+){2}(|-.*)\\z";
                        }

                        version = null;
                        InformationalVersion latestVersion = null;

                        foreach (var p in packages)
                        {
                            var pName = Path.GetFileNameWithoutExtension(p);
                            if (Regex.IsMatch(pName, pattern, RegexOptions.IgnoreCase))
                            {
                                var pVersion = new InformationalVersion(pName.Substring(packageName.Length + 1));

                                if (latestVersion == null)
                                {
                                    latestVersion = pVersion;
                                }
                                else if (pVersion > latestVersion)
                                {
                                    latestVersion = pVersion;
                                }
                            }
                        }

                        if (latestVersion == null)
                        {
                            return(result);
                        }

                        BridgeVersion.ValidatePackageVersion(packageName, repoPath, latestVersion);

                        name = packageName + "." + latestVersion;
                    }

                    name = name[0].ToString().ToUpper() + name.Substring(1);
                    var packageFolder = Path.Combine(packagesFolder, name);

                    result.Folder = packageFolder;
                    if (Directory.Exists(packageFolder))
                    {
                        result.Exists  = true;
                        result.Success = true;
                    }
                    else
                    {
                        var packageFile = Path.Combine(repoPath, name + ".nupkg");

                        if (File.Exists(packageFile))
                        {
                            File.Copy(packageFile, localFile, true);
                            result.Success = true;
                        }
                        else
                        {
                            result.Success = false;
                        }
                    }
                }
                else
                {
                    var isFile = true;

                    try
                    {
                        var uri = new Uri(repoPath);
                        isFile = uri.IsFile;
                    }
                    catch (Exception)
                    {
                    }

                    if (!isFile)
                    {
                        if (!repoPath.EndsWith("/"))
                        {
                            repoPath += "/";
                        }

                        string uri  = repoPath + packageName + (hasVersion ? "/" + version : "");
                        string name = null;

                        if (hasVersion)
                        {
                            name = packageName + "." + version;
                        }
                        else
                        {
                            var webRequest = (HttpWebRequest)WebRequest.Create(uri + "?" + new Random().Next());
                            webRequest.AllowAutoRedirect = false;
                            var webResponse = (HttpWebResponse)webRequest.GetResponse();

                            if (!String.IsNullOrEmpty(webResponse.Headers["Location"]))
                            {
                                var fileName = System.IO.Path.GetFileName(webResponse.Headers["Location"]);
                                name = Path.GetFileNameWithoutExtension(fileName);
                            }

                            BridgeVersion.ValidatePackageVersion(packageName, repoPath, name.Substring(packageName.Length + 1));

                            webResponse.Dispose();
                        }

                        name = name[0].ToString().ToUpper() + name.Substring(1);
                        var packageFolder = Path.Combine(packagesFolder, name);

                        result.Folder = packageFolder;
                        if (Directory.Exists(packageFolder))
                        {
                            result.Exists  = true;
                            result.Success = true;
                        }
                        else
                        {
                            WebClient client = new WebClient();
                            client.DownloadFile(uri, localFile);
                            client.Dispose();
                            result.Success = true;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                result.Success   = false;
                result.Exception = new Exception("Package request failed.", e);
            }

            return(result);
        }
Example #2
0
        /// <summary>
        /// This just throws exceptions if some package sanity checks do not pass.
        /// </summary>
        /// <param name="packageName"></param>
        /// <param name="repoPath"></param>
        /// <param name="version"></param>
        public static void ValidatePackageVersion(string packageName, string repoPath, InformationalVersion version)
        {
            switch (packageName)
            {
            case string p when p == "Bridge" || p == "Bridge.Min" || p == "Bridge.Core":
                if (!Program.EnablePrerelease && version.IsPrerelease)
                {
                    throw new Exception("Invalid package version: " + version + "." + nl +
                                        "Prerelease packages handling is disabled. If you want to enable it, please" + nl +
                                        "specify the --enable-prerelease commandline argument.");
                }

                if (Constants.MinBridgeVersion > version)
                {
                    throw new Exception("Latest available " + packageName + " version on repository is behind minimum required." + nl +
                                        "Repository path: " + repoPath + nl +
                                        "Latest available version: " + version + nl +
                                        "Minimal version required: " + Constants.MinBridgeVersion);
                }
                break;
            }
        }