Exemple #1
0
        public override object ConvertFrom(ref ObjectContext context, Scalar fromScalar)
        {
            PackageVersionRange versionRange;

            if (!PackageVersionRange.TryParse(fromScalar.Value, out versionRange))
            {
                throw new YamlException(fromScalar.Start, fromScalar.End, "Invalid version dependency format. Unable to decode [{0}]".ToFormat(fromScalar.Value));
            }
            return(versionRange);
        }
Exemple #2
0
        private async Task PreLoadPackageDependencies(ILogger log, SolutionProject project, PackageLoadParameters loadParameters)
        {
            if (log == null)
            {
                throw new ArgumentNullException(nameof(log));
            }
            if (project == null)
            {
                throw new ArgumentNullException(nameof(project));
            }
            if (loadParameters == null)
            {
                throw new ArgumentNullException(nameof(loadParameters));
            }

            bool packageDependencyErrors = false;

            var package = project.Package;

            // TODO: Remove and recheck Dependencies Ready if some secondary packages are removed?
            if (package.State >= PackageState.DependenciesReady)
            {
                return;
            }

            log.Verbose($"Process dependencies for {project.Name}...");

            var packageReferences = new Dictionary <string, PackageVersionRange>();

            // Check if there is any package upgrade to do
            var pendingPackageUpgrades = new List <PendingPackageUpgrade>();

            pendingPackageUpgradesPerPackage.Add(package, pendingPackageUpgrades);

            // Load some informations about the project
            try
            {
                var msProject = VSProjectHelper.LoadProject(project.FullPath, loadParameters.BuildConfiguration, extraProperties: new Dictionary <string, string> {
                    { "SkipInvalidConfigurations", "true" }
                });
                try
                {
                    var packageVersion = msProject.GetPropertyValue("PackageVersion");

                    if (!string.IsNullOrEmpty(packageVersion))
                    {
                        package.Meta.Version = new PackageVersion(packageVersion);
                    }

                    project.TargetPath = msProject.GetPropertyValue("TargetPath");
                    package.Meta.Name  = (msProject.GetProperty("PackageId") ?? msProject.GetProperty("AssemblyName"))?.EvaluatedValue ?? package.Meta.Name;

                    var outputType = msProject.GetPropertyValue("OutputType");

                    project.Type = outputType.ToLowerInvariant() == "winexe" || outputType.ToLowerInvariant() == "exe" ||
                                   outputType.ToLowerInvariant() == "appcontainerexe" || // UWP
                                   msProject.GetPropertyValue("AndroidApplication").ToLowerInvariant() == "true" // Android
                        ? ProjectType.Executable
                        : ProjectType.Library;

                    // Note: Platform might be incorrect if Stride is not restored yet (it won't include Stride targets)
                    // Also, if already set, don't try to query it again
                    if (project.Type == ProjectType.Executable && project.Platform == PlatformType.Shared)
                    {
                        project.Platform = VSProjectHelper.GetPlatformTypeFromProject(msProject) ?? PlatformType.Shared;
                    }

                    foreach (var packageReference in msProject.GetItems("PackageReference").ToList())
                    {
                        if (packageReference.HasMetadata("Version") && PackageVersionRange.TryParse(packageReference.GetMetadataValue("Version"), out var packageRange))
                        {
                            packageReferences[packageReference.EvaluatedInclude] = packageRange;
                        }
                    }

                    // Need to go recursively
                    foreach (var projectReference in msProject.GetItems("ProjectReference").ToList())
                    {
                        var projectFile = new UFile(Path.Combine(Path.GetDirectoryName(project.FullPath), projectReference.EvaluatedInclude));
                        if (File.Exists(projectFile))
                        {
                            var referencedProject = Projects.OfType <SolutionProject>().FirstOrDefault(x => x.FullPath == new UFile(projectFile));
                            if (referencedProject != null)
                            {
                                await PreLoadPackageDependencies(log, referencedProject, loadParameters);

                                // Get package upgrader from dependency (a project might depend on another project rather than referencing Stride directly)
                                // A better system would be to evaluate nuget flattened dependencies WITHOUT doing the actual restore (dry-run).
                                // However I am not sure it's easy/possible to do it (using API) without doing a full restore/download, which we don't want to do
                                // with old version (it might be uninstalled already and we want to avoid re-downloading it again)
                                if (pendingPackageUpgradesPerPackage.TryGetValue(referencedProject.Package, out var dependencyPackageUpgraders))
                                {
                                    foreach (var dependencyPackageUpgrader in dependencyPackageUpgraders)
                                    {
                                        // Make sure this upgrader is not already added
                                        if (!pendingPackageUpgrades.Any(x => x.DependencyPackage == dependencyPackageUpgrader.DependencyPackage))
                                        {
                                            // Note: it's important to clone because once upgraded, each instance will have its Dependency.Version tested/updated
                                            pendingPackageUpgrades.Add(dependencyPackageUpgrader.Clone());
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                finally
                {
                    msProject.ProjectCollection.UnloadAllProjects();
                    msProject.ProjectCollection.Dispose();
                }
            }
            catch (Exception ex)
            {
                log.Error($"Unexpected exception while loading project [{project.FullPath.ToWindowsPath()}]", ex);
            }

            foreach (var packageReference in packageReferences)
            {
                var dependencyName    = packageReference.Key;
                var dependencyVersion = packageReference.Value;

                var packageUpgrader = AssetRegistry.GetPackageUpgrader(dependencyName);
                if (packageUpgrader != null)
                {
                    // Check if this upgrader has already been added due to another package reference
                    if (pendingPackageUpgrades.Any(pendingPackageUpgrade => pendingPackageUpgrade.PackageUpgrader == packageUpgrader))
                    {
                        continue;
                    }

                    // Check if upgrade is necessary
                    if (dependencyVersion.MinVersion >= packageUpgrader.Attribute.UpdatedVersionRange.MinVersion)
                    {
                        continue;
                    }

                    // Check if upgrade is allowed
                    if (dependencyVersion.MinVersion < packageUpgrader.Attribute.PackageMinimumVersion)
                    {
                        // Throw an exception, because the package update is not allowed and can't be done
                        throw new InvalidOperationException($"Upgrading project [{project.Name}] to use [{dependencyName}] from version [{dependencyVersion}] to [{packageUpgrader.Attribute.UpdatedVersionRange.MinVersion}] is not supported (supported only from version [{packageUpgrader.Attribute.PackageMinimumVersion}]");
                    }

                    log.Info($"Upgrading project [{project.Name}] to use [{dependencyName}] from version [{dependencyVersion}] to [{packageUpgrader.Attribute.UpdatedVersionRange.MinVersion}] will be required");

                    pendingPackageUpgrades.Add(new PendingPackageUpgrade(packageUpgrader, new PackageDependency(dependencyName, dependencyVersion), null));
                }
            }

            if (pendingPackageUpgrades.Count > 0)
            {
                var upgradeAllowed = packageUpgradeAllowed != false ? PackageUpgradeRequestedAnswer.Upgrade : PackageUpgradeRequestedAnswer.DoNotUpgrade;

                // Need upgrades, let's ask user confirmation
                if (loadParameters.PackageUpgradeRequested != null && !packageUpgradeAllowed.HasValue)
                {
                    upgradeAllowed = loadParameters.PackageUpgradeRequested(package, pendingPackageUpgrades);
                    if (upgradeAllowed == PackageUpgradeRequestedAnswer.UpgradeAll)
                    {
                        packageUpgradeAllowed = true;
                    }
                    if (upgradeAllowed == PackageUpgradeRequestedAnswer.DoNotUpgradeAny)
                    {
                        packageUpgradeAllowed = false;
                    }
                }

                if (!PackageLoadParameters.ShouldUpgrade(upgradeAllowed))
                {
                    log.Error($"Necessary package migration for [{package.Meta.Name}] has not been allowed");
                    return;
                }

                // Perform pre assembly load upgrade
                foreach (var pendingPackageUpgrade in pendingPackageUpgrades)
                {
                    var expectedVersion = pendingPackageUpgrade.PackageUpgrader.Attribute.UpdatedVersionRange.MinVersion.ToString();

                    // Update NuGet references
                    try
                    {
                        var projectFile    = project.FullPath;
                        var msbuildProject = VSProjectHelper.LoadProject(projectFile.ToWindowsPath());
                        var isProjectDirty = false;

                        foreach (var packageReference in msbuildProject.GetItems("PackageReference").ToList())
                        {
                            if (packageReference.EvaluatedInclude == pendingPackageUpgrade.Dependency.Name && packageReference.GetMetadataValue("Version") != expectedVersion)
                            {
                                packageReference.SetMetadataValue("Version", expectedVersion);
                                isProjectDirty = true;
                            }
                        }

                        if (isProjectDirty)
                        {
                            msbuildProject.Save();
                        }

                        msbuildProject.ProjectCollection.UnloadAllProjects();
                        msbuildProject.ProjectCollection.Dispose();
                    }
                    catch (Exception e)
                    {
                        log.Warning($"Unable to load project [{project.FullPath.GetFileName()}]", e);
                    }

                    var packageUpgrader   = pendingPackageUpgrade.PackageUpgrader;
                    var dependencyPackage = pendingPackageUpgrade.DependencyPackage;
                    if (!packageUpgrader.UpgradeBeforeAssembliesLoaded(loadParameters, package.Session, log, package, pendingPackageUpgrade.Dependency, dependencyPackage))
                    {
                        log.Error($"Error while upgrading package [{package.Meta.Name}] for [{dependencyPackage.Meta.Name}] from version [{pendingPackageUpgrade.Dependency.Version}] to [{dependencyPackage.Meta.Version}]");
                        return;
                    }
                }
            }

            // Now that our references are upgraded, let's do a real nuget restore (download files)
            log.Verbose($"Restore NuGet packages for {project.Name}...");
            if (loadParameters.AutoCompileProjects)
            {
                await VSProjectHelper.RestoreNugetPackages(log, project.FullPath);
            }

            // If platform was unknown (due to missing nuget packages during first pass), check it again
            if (project.Type == ProjectType.Executable && project.Platform == PlatformType.Shared)
            {
                try
                {
                    var msProject = VSProjectHelper.LoadProject(project.FullPath, extraProperties: new Dictionary <string, string> {
                        { "SkipInvalidConfigurations", "true" }
                    });
                    try
                    {
                        project.Platform = VSProjectHelper.GetPlatformTypeFromProject(msProject) ?? PlatformType.Shared;
                    }
                    finally
                    {
                        msProject.ProjectCollection.UnloadAllProjects();
                        msProject.ProjectCollection.Dispose();
                    }
                }
                catch (Exception ex)
                {
                    log.Error($"Unexpected exception while loading project [{project.FullPath.ToWindowsPath()}]", ex);
                }
            }

            UpdateDependencies(project, true, true);

            // 1. Load store package
            foreach (var projectDependency in project.FlattenedDependencies)
            {
                var loadedPackage = packages.Find(projectDependency);
                if (loadedPackage == null)
                {
                    string file = null;
                    switch (projectDependency.Type)
                    {
                    case DependencyType.Project:
                        if (Path.GetExtension(projectDependency.MSBuildProject).ToLowerInvariant() == ".csproj")
                        {
                            file = UPath.Combine(project.FullPath.GetFullDirectory(), (UFile)projectDependency.MSBuildProject);
                        }
                        break;

                    case DependencyType.Package:
                        file = PackageStore.Instance.GetPackageFileName(projectDependency.Name, new PackageVersionRange(projectDependency.Version), constraintProvider);
                        break;
                    }

                    if (file != null && File.Exists(file))
                    {
                        // Load package
                        var loadedProject = LoadProject(log, file, loadParameters);
                        loadedProject.Package.Meta.Name    = projectDependency.Name;
                        loadedProject.Package.Meta.Version = projectDependency.Version;
                        Projects.Add(loadedProject);

                        loadedPackage = loadedProject.Package;
                    }
                }

                if (loadedPackage != null)
                {
                    projectDependency.Package = loadedPackage;
                }
            }

            // 2. Load local packages

            /*foreach (var packageReference in package.LocalDependencies)
             * {
             *  // Check that the package was not already loaded, otherwise return the same instance
             *  if (Packages.ContainsById(packageReference.Id))
             *  {
             *      continue;
             *  }
             *
             *  // Expand the string of the location
             *  var newLocation = packageReference.Location;
             *
             *  var subPackageFilePath = package.RootDirectory != null ? UPath.Combine(package.RootDirectory, newLocation) : newLocation;
             *
             *  // Recursive load
             *  var loadedPackage = PreLoadPackage(log, subPackageFilePath.FullPath, false, loadedPackages, loadParameters);
             *
             *  if (loadedPackage == null || loadedPackage.State < PackageState.DependenciesReady)
             *      packageDependencyErrors = true;
             * }*/

            // 3. Update package state
            if (!packageDependencyErrors)
            {
                package.State = PackageState.DependenciesReady;
            }
        }
Exemple #3
0
 public PackageUpgraderAttribute(string packageName, string packageMinimumVersion, string packageUpdatedVersionRange)
 {
     PackageName           = packageName;
     PackageMinimumVersion = new PackageVersion(packageMinimumVersion);
     PackageVersionRange.TryParse(packageUpdatedVersionRange, out this.updatedVersionRange);
 }