protected override ResourceObject InstallToDisk(string resourceName, string resourceVersion, DirectoryEntry directory, ResourceInstallFlags flags)
        {
            JObject resourceJson  = null;
            var     npmPackageUrl = RegistryUrl + resourceName;

            try
            {
                using (var client = new HttpClient())
                {
                    var resourceRegistryStr = client.GetStringAsync(npmPackageUrl).Result;
                    resourceJson = JObject.Parse(resourceRegistryStr);
                }
            }
            catch (Exception ex)
            {
                Plugin.Site.Error(
                    $"Unable to load a [{Name}] resource from registry from Url [{npmPackageUrl}]. Reason:{ex.GetReason()}");
                return(null);
            }


            var versions = resourceJson["versions"] as JObject;

            if (versions == null)
            {
                Plugin.Site.Error(
                    $"Unable to find `versions` property from [{Name}] package from Url [{npmPackageUrl}]");
                return(null);
            }

            var             downloads       = new Dictionary <string, string>();
            string          selectedVersion = null;
            SemanticVersion lastVersion     = null;
            bool            isLatestVersion = false;

            foreach (var prop in versions.Properties())
            {
                var versionName  = prop.Name;
                var versionValue = prop.Value as JObject;
                if (versionName != null && versionValue != null)
                {
                    var downloadUrl = ((versionValue["dist"] as JObject)?["tarball"] as JValue)?.Value as string;

                    if (downloadUrl != null)
                    {
                        downloads[versionName] = downloadUrl;

                        if (versionName == resourceVersion)
                        {
                            selectedVersion = resourceVersion;
                        }
                        else if (resourceVersion == "latest")
                        {
                            SemanticVersion version;
                            if (SemanticVersion.TryParse(versionName, out version))
                            {
                                if (!version.IsPrerelease || (flags & ResourceInstallFlags.PreRelease) != 0)
                                {
                                    if (lastVersion == null)
                                    {
                                        lastVersion     = version;
                                        selectedVersion = versionName;
                                    }
                                    else if (version > lastVersion)
                                    {
                                        lastVersion     = version;
                                        selectedVersion = versionName;
                                    }
                                    isLatestVersion = true;
                                }
                            }
                        }
                    }
                }
            }

            if (selectedVersion != null)
            {
                // In case of a latest version, we will try to load the version from the disk if it is already installed
                if (isLatestVersion)
                {
                    var resource = GetOrInstall(resourceName, selectedVersion, flags | ResourceInstallFlags.NoInstall);
                    if (resource != null)
                    {
                        return(resource);
                    }
                }

                // Otherwise, we have to download the package and unzip it
                var downloadUrl = downloads[selectedVersion];
                try
                {
                    using (var client = new HttpClient())
                    {
                        // TODO: check if the file is ending by tgz/tat.gz?
                        using (var stream = client.GetStreamAsync(downloadUrl).Result)
                            using (var gzStream = new GZipStream(stream, CompressionMode.Decompress))
                            {
                                gzStream.UntarTo(directory, "package");
                            }

                        return(LoadFromDisk(resourceName, selectedVersion, directory));
                    }
                }
                catch (Exception ex)
                {
                    Plugin.Site.Error(
                        $"Unable to download and install the [{Name}] package [{resourceName}/{resourceVersion}] from the url [{downloadUrl}]. Reason:{ex.GetReason()}");
                    return(null);
                }
            }

            Plugin.Site.Error($"Unable to find the [{Name}] package [{resourceName}] with the specific version [{resourceVersion}] from the available version [{string.Join(",", downloads.Keys)}]");
            return(null);
        }
Exemple #2
0
 protected abstract ResourceObject InstallToDisk(string resourceName, string resourceVersion, DirectoryEntry directory, ResourceInstallFlags flags);
Exemple #3
0
        public ResourceObject GetOrInstall(string resourceName, string resourceVersion, ResourceInstallFlags flags)
        {
            if (resourceName == null)
            {
                throw new ArgumentNullException(nameof(resourceName));
            }
            if (resourceVersion == null)
            {
                throw new ArgumentNullException(nameof(resourceVersion));
            }

            // Returns an existing resource if any
            var resource = Find(resourceName, resourceVersion);

            if (resource != null)
            {
                return(resource);
            }

            // Otherwise we are going to check if it is already on the disk
            var            resourcePrivatePath = new DirectoryEntry(Plugin.Site.TempMetaFileSystem, ResourcePlugin.ResourceFolder / Name / resourceName / resourceVersion);
            var            resourcePublicPath  = new DirectoryEntry(Plugin.Site.MetaFileSystem, ResourcePlugin.ResourceFolder / Name / resourceName / resourceVersion);
            DirectoryEntry resourcePath        = null;

            if (resourcePublicPath.Exists)
            {
                resourcePath = resourcePublicPath;
            }
            else if (resourcePrivatePath.Exists)
            {
                resourcePath = resourcePrivatePath;
            }

            if (resourcePath != null)
            {
                resource = LoadFromDisk(resourceName, resourceVersion, resourcePath);
            }
            else if ((flags & ResourceInstallFlags.NoInstall) == 0)
            {
                resourcePath = (flags & ResourceInstallFlags.Private) != 0 ? resourcePrivatePath : resourcePublicPath;
                resource     = InstallToDisk(resourceName, resourceVersion, resourcePath, flags);
            }

            if (resource != null && !Resources.Contains(resource))
            {
                Resources.Add(resource);
            }
            return(resource);
        }
Exemple #4
0
        public ResourceObject TryLoadResource(string providerName, string packageName, string packageVersion = null, ResourceInstallFlags flags = 0)
        {
            if (providerName == null)
            {
                throw new ArgumentNullException(nameof(providerName));
            }
            if (packageName == null)
            {
                throw new ArgumentNullException(nameof(packageName));
            }

            packageVersion = packageVersion ?? "latest";

            foreach (var provider in Providers)
            {
                if (provider.Name == providerName)
                {
                    var resource = provider.GetOrInstall(packageName, packageVersion, flags);
                    return(resource);
                }
            }

            return(null);
        }