Ejemplo n.º 1
0
        private async Task <NuGetInstallResult> InstallOrUpdateAsync(string packageName, string version = null, bool isPreRelease = false, bool isUpdate = false)
        {
            var previousVersion = await m_NuGetPackageManager.GetLatestPackageIdentityAsync(packageName);

            if (isUpdate && previousVersion == null)
            {
                return(new NuGetInstallResult(NuGetInstallCode.PackageOrVersionNotFound));
            }

            var package = await m_NuGetPackageManager.QueryPackageExactAsync(packageName, version, isPreRelease);

            if (package == null)
            {
                return(new NuGetInstallResult(NuGetInstallCode.PackageOrVersionNotFound));
            }

            version ??= package.Identity.Version.OriginalVersion;

            var packageIdentity = new PackageIdentity(package.Identity.Id, new NuGetVersion(version));

            var result = await m_NuGetPackageManager.InstallAsync(packageIdentity, isPreRelease);

            if (result.Code != NuGetInstallCode.Success)
            {
                return(result);
            }

            if (isUpdate)
            {
                await m_NuGetPackageManager.RemoveAsync(previousVersion);
            }
            return(result);
        }
Ejemplo n.º 2
0
        public async Task BootstrapAsync(
            string rocketFolder,
            IEnumerable <string> packageIds,
            bool allowPrereleaseVersions = false,
            string repository            = DefaultNugetRepository,
            ILogger logger = null)
        {
            logger = logger ?? new NuGetConsoleLogger();
            logger.LogInformation("Bootstrap has started.");

            rocketFolder = Path.GetFullPath(rocketFolder);

            if (string.IsNullOrEmpty(repository))
            {
                await InitializeRuntimeAsync();

                return;
            }

            var packagesDirectory = Path.Combine(rocketFolder, "Packages");

            if (!Directory.Exists(packagesDirectory))
            {
                Directory.CreateDirectory(packagesDirectory);
            }

            var nugetInstaller = new NuGetPackageManager(logger, packagesDirectory, new[] { repository });

            foreach (var packageId in packageIds)
            {
                PackageIdentity packageIdentity;
                if (!await nugetInstaller.PackageExistsAsync(packageId))
                {
                    logger.LogInformation("Searching for: " + packageId);
                    var rocketPackage = await nugetInstaller.QueryPackageExactAsync(packageId, null, allowPrereleaseVersions);

                    logger.LogInformation($"Downloading {rocketPackage.Identity.Id} v{rocketPackage.Identity.Version} via NuGet, this might take a while...");
                    var installResult = await nugetInstaller.InstallAsync(rocketPackage.Identity, allowPrereleaseVersions);

                    if (installResult.Code != NuGetInstallCode.Success)
                    {
                        logger.LogInformation($"Downloading has failed for {rocketPackage.Identity.Id}: " + installResult.Code);
                        return;
                    }

                    packageIdentity = installResult.Identity;
                    logger.LogInformation($"Finished downloading \"{packageId}\"");
                }
                else
                {
                    packageIdentity = await nugetInstaller.GetLatestPackageIdentityAsync(packageId);
                }

                logger.LogInformation($"Loading {packageId}.");
                await LoadPackageAsync(nugetInstaller, packageIdentity);
            }

            await InitializeRuntimeAsync();
        }
Ejemplo n.º 3
0
        private async Task <bool> TryInstallRequiredDependenciesAsync(Assembly providerAssembly, Dictionary <string, SemVersion> missingAssemblies)
        {
            foreach (var assembly in missingAssemblies)
            {
                var packagetToInstall = await m_NuGetPackageManager.QueryPackageExactAsync(assembly.Key, assembly.Value.ToString());

                var result = await m_NuGetPackageManager.InstallAsync(packagetToInstall.Identity);

                if (result.Code == NuGetInstallCode.Success)
                {
                    continue;
                }

                m_Logger.LogWarning($"Failed to install \"{assembly.Key}\": " + result.Code, Color.DarkRed);
                m_Logger.LogWarning($"Plugin {providerAssembly} can not load without it!", Color.DarkRed);
                return(false);
            }

            return(true);
        }
Ejemplo n.º 4
0
        public async Task <object> BootstrapAsync(
            string openModFolder,
            string[] commandLineArgs,
            IEnumerable <string> packageIds,
            IEnumerable <string> ignoredDependencies,
            bool allowPrereleaseVersions = false,
            ILogger logger = null)
        {
            var shouldAutoUpdate = false;

            if (!commandLineArgs.Any(arg => arg.Equals("-NoOpenModAutoUpdate", StringComparison.InvariantCultureIgnoreCase)))
            {
                if (!bool.TryParse(Environment.GetEnvironmentVariable("OpenMod_EnableAutoUpdate"), out shouldAutoUpdate))
                {
                    shouldAutoUpdate = true;
                }
            }


            logger ??= new NuGetConsoleLogger();
            openModFolder = Path.GetFullPath(openModFolder);

            var packagesDirectory = Path.Combine(openModFolder, "packages");

            if (!Directory.Exists(packagesDirectory))
            {
                Directory.CreateDirectory(packagesDirectory);
            }

            Environment.SetEnvironmentVariable("NUGET_COMMON_APPLICATION_DATA", Path.GetFullPath(packagesDirectory));

            var nugetPackageManager = new NuGetPackageManager(packagesDirectory)
            {
                Logger = logger
            };

            // these dependencies do not exist on NuGet and create warnings
            // they are not required
            nugetPackageManager.IgnoreDependencies(
                "Microsoft.NETCore.Platforms",
                "Microsoft.Packaging.Tools",
                "NETStandard.Library",
                "System.IO.FileSystem.Watcher");

            nugetPackageManager.IgnoreDependencies(ignoredDependencies.ToArray());

            var hostAssemblies = new List <Assembly>();

            foreach (var packageId in packageIds)
            {
                var packageIdentity = await nugetPackageManager.GetLatestPackageIdentityAsync(packageId);

                var shouldInstallOrUpdate = packageIdentity == null;

                IPackageSearchMetadata openModPackage = null;
                if (packageIdentity == null || shouldAutoUpdate)
                {
                    openModPackage = await nugetPackageManager.QueryPackageExactAsync(packageId, null, allowPrereleaseVersions);
                }

                if (packageIdentity != null && shouldAutoUpdate)
                {
                    var availableVersions = await openModPackage.GetVersionsAsync();

                    shouldInstallOrUpdate = availableVersions.Any(d => d.Version > packageIdentity.Version);
                }

                if (shouldInstallOrUpdate)
                {
                    logger.LogInformation($"Downloading {openModPackage.Identity.Id} v{openModPackage.Identity.Version} via NuGet");
                    var installResult = await nugetPackageManager.InstallAsync(openModPackage.Identity, allowPrereleaseVersions);

                    if (installResult.Code == NuGetInstallCode.Success)
                    {
                        packageIdentity = installResult.Identity;
                        logger.LogInformation($"Finished downloading \"{packageId}\".");
                    }
                    else
                    {
                        logger.LogError($"Downloading has failed for {openModPackage.Identity.Id} v{openModPackage.Identity.Version.OriginalVersion}: {installResult.Code}");
                        if (packageIdentity == null)
                        {
                            return(null);
                        }
                    }
                }

                logger.LogInformation($"Loading {packageIdentity.Id} v{packageIdentity.Version}");
                var packageAssemblies = await LoadPackageAsync(nugetPackageManager, packageIdentity);

                hostAssemblies.AddRange(packageAssemblies);
            }

            return(await InitializeRuntimeAsync(nugetPackageManager, hostAssemblies, openModFolder, commandLineArgs));
        }
        public async Task BootstrapAsync(
            string openModFolder,
            string[] commandLineArgs,
            IEnumerable <string> packageIds,
            bool allowPrereleaseVersions = false,
            ILogger logger = null)
        {
            var shouldAutoUpdate = false;

            if (!commandLineArgs.Any(arg => arg.Equals("-NoOpenModAutoUpdate", StringComparison.InvariantCultureIgnoreCase)))
            {
                if (!bool.TryParse(Environment.GetEnvironmentVariable("OpenMod__AutoUpdate"), out shouldAutoUpdate))
                {
                    shouldAutoUpdate = true;
                }
            }


            logger ??= new NuGetConsoleLogger();
            openModFolder = Path.GetFullPath(openModFolder);

            var packagesDirectory = Path.Combine(openModFolder, "packages");

            if (!Directory.Exists(packagesDirectory))
            {
                Directory.CreateDirectory(packagesDirectory);
            }

            var nugetInstaller = new NuGetPackageManager(packagesDirectory)
            {
                Logger = logger
            };

            var hostAssemblies = new List <Assembly>();

            foreach (var packageId in packageIds)
            {
                PackageIdentity packageIdentity;
                var             installedPackage = await nugetInstaller.GetLatestPackageIdentityAsync(packageId);

                var shouldInstallOrUpdate = installedPackage == null;

                IPackageSearchMetadata openModPackage = null;
                if (installedPackage == null || shouldAutoUpdate)
                {
                    openModPackage = await nugetInstaller.QueryPackageExactAsync(packageId, null, allowPrereleaseVersions);
                }

                if (installedPackage != null && shouldAutoUpdate)
                {
                    var availableVersions = await openModPackage.GetVersionsAsync();

                    shouldInstallOrUpdate = availableVersions.Any(d => d.Version > installedPackage.Version);
                }

                if (shouldInstallOrUpdate)
                {
                    logger.LogInformation($"Downloading {openModPackage.Identity.Id} v{openModPackage.Identity.Version} via NuGet");
                    var installResult = await nugetInstaller.InstallAsync(openModPackage.Identity, allowPrereleaseVersions);

                    if (installResult.Code != NuGetInstallCode.Success)
                    {
                        logger.LogError($"Downloading has failed for {openModPackage.Identity.Id} v{openModPackage.Identity.Version.OriginalVersion}: " + installResult.Code);
                        return;
                    }

                    packageIdentity = installResult.Identity;
                    logger.LogInformation($"Finished downloading \"{packageId}\".");
                }
                else
                {
                    packageIdentity = await nugetInstaller.GetLatestPackageIdentityAsync(packageId);
                }

                logger.LogInformation($"Loading {packageIdentity.Id} v{packageIdentity.Version}");
                var packageAssemblies = await LoadPackageAsync(nugetInstaller, packageIdentity);

                hostAssemblies.AddRange(packageAssemblies);
            }

            await InitializeRuntimeAsync(hostAssemblies, openModFolder, commandLineArgs);
        }