Exemplo n.º 1
0
        public int Process(DirectoryPath root, DirectoryPath repository, string version)
        {
            _log.Write("Processing packages.config files...");
            var packageProcessor = new PackageProcessor(_filesystem, _environment, _log);
            var processed        = packageProcessor.Process(root, repository, version);

            if (processed == 0)
            {
                _log.Error("Nothing was updated. Probably a newer repository.");
            }

            return(processed);
        }
Exemplo n.º 2
0
        private static bool UpdatePackage(IConsoleLog log, FilePath file, string package, string version, FilePath nuget)
        {
            log.Write("Updating package {0} to {1}...", package, version);
            var info = new ProcessStartInfo(nuget.FullPath)
            {
                Arguments = $"update {file.FullPath} -Id {package} -Version {version} -RepositoryPath {file.GetDirectory().Combine(new DirectoryPath("../packages")).Collapse()}",
                RedirectStandardOutput = true
            };

            using (var process = Process.Start(info))
            {
                if (process != null)
                {
                    using (var reader = process.StandardOutput)
                    {
                        reader.ReadToEnd();
                    }
                    return(process.ExitCode == 0);
                }
            }
            return(false);
        }
Exemplo n.º 3
0
        private static bool RestorePackages(IConsoleLog log, FilePath file, FilePath nuget)
        {
            var info = new ProcessStartInfo(nuget.FullPath)
            {
                Arguments = $"restore {file.FullPath} -PackagesDirectory {file.GetDirectory().Combine(new DirectoryPath("../packages")).Collapse()}",
                RedirectStandardOutput = true
            };

            log.Write("Restoring packages for {0}...", file.FullPath);
            using (var process = Process.Start(info))
            {
                if (process != null)
                {
                    using (var reader = process.StandardOutput)
                    {
                        reader.ReadToEnd();
                    }
                    return(process.ExitCode == 0);
                }
            }
            return(false);
        }
Exemplo n.º 4
0
        public override int Execute(UpdateSettings settings, ILookup <string, string> remaining)
        {
            // Get the user.
            var client = new GitHubClient(new ProductHeaderValue("Cake-Addin-Updater"))
            {
                Credentials = new Octokit.Credentials(settings.Token)
            };

            // Validate the provided version.
            if (!System.Version.TryParse(settings.Version, out var _))
            {
                _log.Error("The provided version is not valid.");
                return(1);
            }

            // Get the root.
            var root = settings.WorkingDirectory ?? new DirectoryPath(".");

            root = root.MakeAbsolute(_environment);

            // Get the user.
            var user      = client.User.Get(settings.User).Result;
            var userEmail = GetUserEmail(client);

            // Get the repository parts.
            var info = GetRepositoryInfo(settings);

            // Does the directory contains anything?
            var path = CreateRepositoryDirectory(root, info);

            if (_filesystem.Directory.GetFiles(path, "*.*", SearchScope.Current).Any() ||
                _filesystem.Directory.GetDirectories(path, "*.*", SearchScope.Current).Any())
            {
                _log.Error($"Repository '{path}' already exist on disk.");
                _log.Write("Remove it and try again.");
                return(1);
            }

            // Fork the repository.
            var repository = ForkRepository(client, info);

            if (string.IsNullOrWhiteSpace(repository?.Name))
            {
                _log.Error("Could not fork repository.");
                return(1);
            }

            // Get the default branch.
            var defaultBranch = repository.DefaultBranch;

            if (string.IsNullOrWhiteSpace(defaultBranch))
            {
                _log.Error("Could not get default branch for repository.");
                return(1);
            }

            // Clone the repository at the specified path.
            _log.Write("Cloning repository...");
            GitRepository.Clone($"https://github.com/{settings.User}/{repository.Name}", path.FullPath, new CloneOptions
            {
                Checkout = true
            });

            using (var gitRepository = new GitRepository(path.FullPath))
            {
                // Create a new branch in the repository.
                _log.Write("Creating branch...");
                gitRepository.CreateBranch($"feature/cake-{settings.Version}");
                _log.Write("Checking out branch...");
                GitCommands.Checkout(gitRepository, $"feature/cake-{settings.Version}");

                // Update all package references in project.
                var processed = _processor.Process(root, path, settings.Version);
                if (processed == 0)
                {
                    _log.Error("Nothing was updated. Probably a newer repository.");
                    return(1);
                }

                // Commit?
                if (settings.Commit)
                {
                    _log.Write("Staging changes...");
                    GitCommands.Stage(gitRepository, "*");

                    var status = gitRepository.RetrieveStatus();
                    if (status.Any())
                    {
                        _log.Write("Committing changes...");
                        var author = new GitSignature(user.Name, userEmail, DateTime.Now);
                        gitRepository.Commit($"Updated to Cake {settings.Version}.", author, author);

                        // Push?
                        if (settings.Push)
                        {
                            // Build everything first.
                            if (!BuildProject(path))
                            {
                                return(1);
                            }

                            // Push the commit.
                            if (!Push(settings, path))
                            {
                                return(1);
                            }

                            // Create a pull request?
                            if (settings.OpenPullRequest)
                            {
                                CreatePullRequest(client, settings, info);
                            }
                        }
                    }
                    else
                    {
                        _log.Error("No changes in repository. Already updated?");
                    }
                }
            }

            return(0);
        }
Exemplo n.º 5
0
        public int Process(DirectoryPath root, DirectoryPath repository, string version)
        {
            var comparer = new PathComparer(_environment);
            var regex    = new Regex(Pattern);

            var toolPackages = repository.CombineWithFilePath(new FilePath("tools/packages.config"));
            var packageFiles = _globber.Match("**/packages.config", new GlobberSettings
            {
                Root     = repository,
                Comparer = comparer
            }).OfType <FilePath>();

            var processed = 0;

            foreach (var packageFile in packageFiles)
            {
                _log.Write("Processing {0}...", packageFile);

                using (var scope = _log.Indent())
                {
                    if (comparer.Equals(packageFile, toolPackages))
                    {
                        scope.Write("Skipping packages.config in tools.");
                        continue;
                    }

                    if (!_nuget.Restore(scope, root, packageFile))
                    {
                        scope.Error("Could not restore NuGet package.");
                        throw new InvalidOperationException("An error occured when restoring packages!");
                    }

                    // Read the file contents.
                    string contents;
                    using (var stream = _filesystem.File.OpenRead(packageFile))
                        using (var reader = new StreamReader(stream))
                        {
                            contents = reader.ReadToEnd();
                        }

                    // Find all packages that need to be updated.
                    var result = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
                    var match  = regex.Match(contents);
                    if (match.Success)
                    {
                        while (match.Success)
                        {
                            var package = match.Groups["package"].Value;
                            if (package == "Cake.Core" || package == "Cake.Testing")
                            {
                                result.Add(package);
                            }
                            match = match.NextMatch();
                        }
                        processed++;
                    }

                    foreach (var package in result)
                    {
                        if (!_nuget.Update(scope, root, packageFile, package, version))
                        {
                            scope.Error("Could not update package '{0}' in file '{1}'.", package, packageFile);
                            throw new InvalidOperationException("An error occured when updating package!");
                        }
                    }
                }
            }

            return(processed);
        }