private static int ProcessSuccessfulUpdates(AssemblyUpdaterUpdateTask[] tasks)
        {
            var assembliesToUpdate = GetAssembliesToBeUpdated();
            var succeededUpdates   = tasks.Where(t => t.Result == APIUpdaterAssemblyHelper.UpdatesApplied);

            if (!succeededUpdates.Any())
            {
                assembliesToUpdate.Clear();
                return(0);
            }

            if (AskForConsent(false) != APIUpdaterStatus.Accepted)
            {
                APIUpdaterLogger.WriteToFile(L10n.Tr("User declined to run APIUpdater"));
                return(0);
            }

            var assemblyPaths2 = succeededUpdates.Select(u => u.Candidate.Path).ToArray();

            APIUpdaterHelper.HandleFilesInPackagesVirtualFolder(assemblyPaths2);
            if (!APIUpdaterHelper.CheckReadOnlyFiles(assemblyPaths2))
            {
                return(0);
            }

            foreach (var succeedd in succeededUpdates)
            {
                APIUpdaterLogger.WriteToFile("{0}{1}", Environment.NewLine, succeedd.StdOut);
                FileUtil.MoveFileIfExists(succeedd.OutputPath, succeedd.Candidate.Path);
            }

            assembliesToUpdate.Clear();
            return(succeededUpdates.Count());
        }
Example #2
0
        private static int ProcessSuccessfulUpdates(AssemblyUpdaterUpdateTask[] tasks)
        {
            var assembliesToUpdate = GetAssembliesToBeUpdated();
            var succeededUpdates   = tasks.Where(t => t.Result == APIUpdaterAssemblyHelper.UpdatesApplied);

            if (!succeededUpdates.Any())
            {
                assembliesToUpdate.Clear();
                return(0);
            }

            var assembliesRequiringConsent = FilterOutLocalAndEmbeddedPackagesWhenAskingForConsent(assembliesToUpdate.Select(a => a.Path)).ToArray();

            if (assembliesRequiringConsent.Length > 0 && !AskForConsent(assembliesRequiringConsent))
            {
                APIUpdaterLogger.WriteToFile(L10n.Tr("User declined to run APIUpdater"));
                return(0);
            }

            var updatedAssemblyPaths = succeededUpdates.Select(u => u.Candidate.Path).ToArray();

            if (!CheckoutFromVCSIfNeeded(updatedAssemblyPaths))
            {
                return(-1);
            }

            APIUpdaterHelper.HandleFilesInPackagesVirtualFolder(updatedAssemblyPaths);
            if (!APIUpdaterHelper.CheckReadOnlyFiles(updatedAssemblyPaths))
            {
                return(0);
            }

            foreach (var succeed in succeededUpdates)
            {
                APIUpdaterLogger.WriteToFile("{0}{1}", Environment.NewLine, succeed.StdOut);
                FileUtil.MoveFileIfExists(succeed.OutputPath, succeed.Candidate.Path);
            }

            assembliesToUpdate.Clear();
            return(succeededUpdates.Count());

            bool CheckoutFromVCSIfNeeded(string[] assemblyPathsToCheck)
            {
                // Only try to connect to VCS if there are files under VCS that need to be updated
                var assembliesInAssetsFolder = assemblyPathsToCheck.Where(path => path.IndexOf("Assets/", StringComparison.OrdinalIgnoreCase) != -1).ToArray();

                if (!assembliesInAssetsFolder.Any())
                {
                    return(true);
                }

                if (!WaitForVCSServerConnection())
                {
                    return(false);
                }

                var failedToCheckoutFiles = !APIUpdaterHelper.MakeEditable(assembliesInAssetsFolder);

                if (failedToCheckoutFiles)
                {
                    assembliesToUpdate.Clear();
                    return(false);
                }

                return(true);
            }

            IEnumerable <string> FilterOutLocalAndEmbeddedPackagesWhenAskingForConsent(IEnumerable <string> ass)
            {
                foreach (var path in ass.Select(path => path.Replace("\\", "/"))) // package manager paths are always separated by /
                {
                    var packageInfo = UnityEditor.PackageManager.PackageInfo.FindForAssetPath(path);
                    if (packageInfo == null || packageInfo.source == PackageSource.Local || packageInfo.source == PackageSource.Embedded)
                    {
                        yield return(path);
                    }
                }
            }
        }