/// <summary>
        /// Apply registry changes to the modifier.
        /// </summary>
        /// <param name="manifestModifier">Object that modifies the project's manifest.</param>
        /// <param name="registriesToAdd">Registries added to the modifier.</param>
        /// <param name="registriesToRemove">Registries removed from the modifier.</param>
        /// <param name="availableRegistries">Registries that are available in the
        /// configuration.</param>
        /// <param name="manifestRegistries">Registries that are present in the manifest.</param>
        /// <param name="selectedRegistryUrls">URLs of selected registries, these should be items in
        /// availableRegistries.</param>
        /// <param name="addRegistries">Whether to add selected registries to the manifest.</param>
        /// <param name="removeRegistries">Whether to remove unselected registries from the
        /// manifest.</param>
        /// <param name="invertSelection">If false, adds the selected registries and removes the
        /// unselected registries.  If true, removes the selected registries and adds the unselected
        /// registries.</param>
        /// <returns>true if the manifest is modified.</returns>
        private static bool SyncRegistriesToModifier(
            PackageManifestModifier manifestModifier,
            out List <PackageManagerRegistry> registriesToAdd,
            out List <PackageManagerRegistry> registriesToRemove,
            Dictionary <string, PackageManagerRegistry> availableRegistries,
            Dictionary <string, List <PackageManagerRegistry> > manifestRegistries,
            HashSet <string> selectedRegistryUrls,
            bool addRegistries    = true,
            bool removeRegistries = true,
            bool invertSelection  = false)
        {
            // Build a list of registries to add to and remove from the modifier.
            registriesToAdd    = new List <PackageManagerRegistry>();
            registriesToRemove = new List <PackageManagerRegistry>();

            foreach (var availableRegistry in availableRegistries.Values)
            {
                var  url        = availableRegistry.Url;
                bool isSelected = selectedRegistryUrls.Contains(url);
                if (invertSelection)
                {
                    isSelected = !isSelected;
                }

                bool currentlyInManifest = manifestRegistries.ContainsKey(url);

                if (isSelected)
                {
                    if (addRegistries && !currentlyInManifest)
                    {
                        registriesToAdd.Add(availableRegistry);
                    }
                }
                else
                {
                    if (removeRegistries && currentlyInManifest)
                    {
                        registriesToRemove.Add(availableRegistry);
                    }
                }
            }

            bool manifestModified = false;

            if (registriesToAdd.Count > 0)
            {
                manifestModifier.AddRegistries(registriesToAdd);
                manifestModified = true;
            }
            if (registriesToRemove.Count > 0)
            {
                manifestModifier.RemoveRegistries(registriesToRemove);
                manifestModified = true;
            }
            return(manifestModified);
        }
Example #2
0
        /// <summary>
        /// Apply registry changes to the projects manifest.
        /// </summary>
        /// <param name="manifestModifier">Object that modifies the project's manifest.</param>
        /// <param name="availableRegistries">Registries that are available in the
        /// configuration.</param>
        /// <param name="manifestRegistries">Registries that are present in the manifest.</param>
        /// <param name="selectedRegistryUrls">URLs of selected registries, these should be items in
        /// availableRegistries.</param>
        /// <param name="addRegistries">Whether to add selected registries to the manifest.</param>
        /// <param name="removeRegistries">Whether to remove unselected registries from the
        /// manifest.</param>
        /// <param name="invertSelection">If false, adds the selected registries and removes the
        /// unselected registries.  If true, removes the selected registries and adds the unselected
        /// registries.</param>
        /// <returns>true if successful, false otherwise.</returns>
        private static bool SyncRegistriesToManifest(
            PackageManifestModifier manifestModifier,
            Dictionary <string, UnityPackageManagerRegistry> availableRegistries,
            Dictionary <string, List <UnityPackageManagerRegistry> > manifestRegistries,
            HashSet <string> selectedRegistryUrls,
            bool addRegistries    = true,
            bool removeRegistries = true,
            bool invertSelection  = false)
        {
            // Build a list of registries to add to and remove from the manifest.
            var registriesToAdd    = new List <UnityPackageManagerRegistry>();
            var registriesToRemove = new List <UnityPackageManagerRegistry>();

            foreach (var availableRegistry in availableRegistries.Values)
            {
                var  url        = availableRegistry.Url;
                bool isSelected = selectedRegistryUrls.Contains(url);
                if (invertSelection)
                {
                    isSelected = !isSelected;
                }

                bool currentlyInManifest = manifestRegistries.ContainsKey(url);

                if (isSelected)
                {
                    if (addRegistries && !currentlyInManifest)
                    {
                        registriesToAdd.Add(availableRegistry);
                    }
                }
                else
                {
                    if (removeRegistries && currentlyInManifest)
                    {
                        registriesToRemove.Add(availableRegistry);
                    }
                }
            }

            bool manifestModified = false;

            if (registriesToAdd.Count > 0)
            {
                manifestModifier.AddRegistries(registriesToAdd);
                manifestModified = true;
            }
            if (registriesToRemove.Count > 0)
            {
                manifestModifier.RemoveRegistries(registriesToRemove);
                manifestModified = true;
            }

            bool successful = true;

            if (manifestModified)
            {
                successful = manifestModifier.WriteManifest();
                if (successful)
                {
                    if (registriesToAdd.Count > 0)
                    {
                        logger.Log(String.Format(
                                       "Added registries to {0}:\n{1}",
                                       PackageManifestModifier.MANIFEST_FILE_PATH,
                                       UnityPackageManagerRegistry.ToString(registriesToAdd)));
                    }
                    if (registriesToRemove.Count > 0)
                    {
                        logger.Log(String.Format(
                                       "Removed registries from {0}:\n{1}",
                                       PackageManifestModifier.MANIFEST_FILE_PATH,
                                       UnityPackageManagerRegistry.ToString(registriesToRemove)));
                    }
                    analytics.Report(
                        "registry_manifest/write/success",
                        new KeyValuePair <string, string>[] {
                        new KeyValuePair <string, string>("added", registriesToAdd.Count.ToString()),
                        new KeyValuePair <string, string>("removed",
                                                          registriesToRemove.Count.ToString())
                    },
                        "Project Manifest Modified");
                }
                else
                {
                    analytics.Report("registry_manifest/write/failed", "Project Manifest Write Failed");
                }
            }
            return(successful);
        }