Exemple #1
0
        protected static IEnumerable <PackageInfo> FromUpmPackageInfo(PackageManager.PackageInfo info, bool isCurrent = true)
        {
            var origin      = PackageInfo.IsModule(info.name) ? PackageOrigin.Builtin : PackageOrigin.Registry;
            var packages    = new List <PackageInfo>();
            var displayName = info.displayName;

            if (string.IsNullOrEmpty(displayName))
            {
                displayName = info.name.Replace("com.unity.modules.", "");
                displayName = displayName.Replace("com.unity.", "");
                displayName = new CultureInfo("en-US").TextInfo.ToTitleCase(displayName);
            }

            var lastCompatible = info.versions.latestCompatible;
            var versions       = new List <string>();

            versions.AddRange(info.versions.compatible);
            if (versions.FindIndex(version => version == info.version) == -1)
            {
                versions.Add(info.version);

                versions.Sort((left, right) =>
                {
                    if (left == null || right == null)
                    {
                        return(0);
                    }

                    SemVersion leftVersion = left;
                    SemVersion righVersion = right;
                    return(leftVersion.CompareByPrecedence(righVersion));
                });

                SemVersion packageVersion = info.version;
                if (!string.IsNullOrEmpty(lastCompatible))
                {
                    SemVersion lastCompatibleVersion =
                        string.IsNullOrEmpty(lastCompatible) ? (SemVersion)null : lastCompatible;
                    if (packageVersion != null && string.IsNullOrEmpty(packageVersion.Prerelease) &&
                        packageVersion.CompareByPrecedence(lastCompatibleVersion) > 0)
                    {
                        lastCompatible = info.version;
                    }
                }
                else
                {
                    if (packageVersion != null && string.IsNullOrEmpty(packageVersion.Prerelease))
                    {
                        lastCompatible = info.version;
                    }
                }
            }

            foreach (var version in versions)
            {
                var isVersionCurrent = version == info.version && isCurrent;
                var state            = (origin == PackageOrigin.Builtin || info.version == lastCompatible) ? PackageState.UpToDate : PackageState.Outdated;

                // Happens mostly when using a package that hasn't been in production yet.
                if (info.versions.all.Length <= 0)
                {
                    state = PackageState.UpToDate;
                }

                if (info.errors.Length > 0)
                {
                    state = PackageState.Error;
                }

                var packageInfo = new PackageInfo
                {
                    Name        = info.name,
                    DisplayName = displayName,
                    PackageId   = version == info.version ? info.packageId : null,
                    Version     = version,
                    Description = info.description,
                    Category    = info.category,
                    IsCurrent   = isVersionCurrent,
                    IsLatest    = version == lastCompatible,
                    IsVerified  = version == info.versions.recommended,
                    Errors      = info.errors.ToList(),
                    Group       = GroupName(origin),
                    State       = state,
                    Origin      = origin,
                    Author      = null
                };

                packages.Add(packageInfo);
            }

            return(packages);
        }
        protected static IEnumerable <PackageInfo> FromUpmPackageInfo(PackageManager.PackageInfo info, bool isCurrent = true)
        {
            var packages    = new List <PackageInfo>();
            var displayName = info.displayName;

            if (string.IsNullOrEmpty(displayName))
            {
                displayName = info.name.Replace(PackageInfo.ModulePrefix, "");
                displayName = displayName.Replace(PackageInfo.UnityPrefix, "");
                displayName = new CultureInfo("en-US").TextInfo.ToTitleCase(displayName);
            }

            string author = info.author.name;

            if (string.IsNullOrEmpty(info.author.name) && info.name.StartsWith(PackageInfo.UnityPrefix))
            {
                author = "Unity Technologies Inc.";
            }

            var lastCompatible = info.versions.latestCompatible;
            var versions       = new List <string>();

            versions.AddRange(info.versions.compatible);
            if (versions.FindIndex(version => version == info.version) == -1)
            {
                versions.Add(info.version);

                versions.Sort((left, right) =>
                {
                    if (left == null || right == null)
                    {
                        return(0);
                    }

                    SemVersion leftVersion = left;
                    SemVersion righVersion = right;
                    return(leftVersion.CompareByPrecedence(righVersion));
                });

                SemVersion packageVersion = info.version;
                if (!string.IsNullOrEmpty(lastCompatible))
                {
                    SemVersion lastCompatibleVersion =
                        string.IsNullOrEmpty(lastCompatible) ? (SemVersion)null : lastCompatible;
                    if (packageVersion != null && string.IsNullOrEmpty(packageVersion.Prerelease) &&
                        packageVersion.CompareByPrecedence(lastCompatibleVersion) > 0)
                    {
                        lastCompatible = info.version;
                    }
                }
                else
                {
                    if (packageVersion != null && string.IsNullOrEmpty(packageVersion.Prerelease))
                    {
                        lastCompatible = info.version;
                    }
                }
            }

            foreach (var version in versions)
            {
                var isVersionCurrent = version == info.version && isCurrent;
                var isBuiltIn        = info.source == PackageSource.BuiltIn;
                var isVerified       = string.IsNullOrEmpty(SemVersion.Parse(version).Prerelease) && version == info.versions.verified;
                var state            = (isBuiltIn || info.version == lastCompatible ||   !isCurrent) ? PackageState.UpToDate : PackageState.Outdated;

                // Happens mostly when using a package that hasn't been in production yet.
                if (info.versions.all.Length <= 0)
                {
                    state = PackageState.UpToDate;
                }

                if (info.errors.Length > 0)
                {
                    state = PackageState.Error;
                }

                var packageInfo = new PackageInfo
                {
                    Name         = info.name,
                    DisplayName  = displayName,
                    PackageId    = version == info.version ? info.packageId : null,
                    Version      = version,
                    Description  = info.description,
                    Category     = info.category,
                    IsInstalled  = isVersionCurrent,
                    IsLatest     = version == lastCompatible,
                    IsVerified   = isVerified,
                    Errors       = info.errors.ToList(),
                    Group        = GroupName(info.source, info.type),
                    Type         = info.type,
                    State        = state,
                    Origin       = isBuiltIn || isVersionCurrent ? info.source : PackageSource.Registry,
                    Author       = author,
                    Info         = info,
                    HasFullFetch = version == info.version
                };

                if (version == info.version && !string.IsNullOrEmpty(info.resolvedPath))
                {
                    var loadedSamples = SampleJsonHelper.LoadSamplesFromPackageJson(info.resolvedPath);
                    packageInfo.Samples = new List <Sample>();
                    foreach (var sample in loadedSamples)
                    {
                        sample.resolvedPath = Path.Combine(info.resolvedPath, sample.path);
                        sample.importPath   = IOUtils.CombinePaths
                                              (
                            Application.dataPath,
                            "Samples",
                            IOUtils.SanitizeFileName(packageInfo.DisplayName),
                            packageInfo.Version.ToString(),
                            IOUtils.SanitizeFileName(sample.displayName)
                                              );
                        packageInfo.Samples.Add(new Sample(sample));
                    }
                }
                packages.Add(packageInfo);
            }

            return(packages);
        }
        private void RebuildDependenciesDictionnary()
        {
            projectDependencies = new Dictionary <string, SemVersion>();
            var allPackageInfos   = new List <PackageInfo>(LatestListPackages);
            var installedPackages = allPackageInfos.Where(p => p.IsInstalled);

            foreach (var p in installedPackages)
            {
                if (projectDependencies.ContainsKey(p.Name))
                {
                    var version = projectDependencies[p.Name];
                    if (p.Version.CompareByPrecedence(version) > 0)
                    {
                        projectDependencies[p.Name] = p.Version;
                    }
                }
                else
                {
                    if (p.IsInDevelopment)
                    {
                        projectDependencies[p.Name] = EmbdeddedVersion;
                    }
                    else if (p.IsLocal)
                    {
                        projectDependencies[p.Name] = LocalVersion;
                    }
                    else
                    {
                        projectDependencies[p.Name] = p.Version;
                    }
                }

                var dependencies = p.Info == null ? null : p.Info.resolvedDependencies;
                if (dependencies == null)
                {
                    continue;
                }
                foreach (var dependency in dependencies)
                {
                    if (dependency.version.StartsWith("file:"))
                    {
                        var dependencyPath = dependency.version.Substring(5).Replace('\\', '/');
                        var projectPath    = Directory.GetCurrentDirectory().Replace('\\', '/');
                        if (dependencyPath.StartsWith(projectPath))
                        {
                            projectDependencies[dependency.name] = EmbdeddedVersion;
                        }
                        else
                        {
                            projectDependencies[dependency.name] = LocalVersion;
                        }
                    }
                    else
                    {
                        SemVersion newVersion = dependency.version;
                        if (projectDependencies.ContainsKey(dependency.name))
                        {
                            var version = projectDependencies[dependency.name];
                            if (newVersion.CompareByPrecedence(version) > 0)
                            {
                                projectDependencies[dependency.name] = newVersion;
                            }
                        }
                        else
                        {
                            projectDependencies[dependency.name] = newVersion;
                        }
                    }
                }
            }
        }