Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/bsd 358 update deps #359

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 155 additions & 0 deletions RoboFile.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Robo\ResultData;
use Robo\Tasks;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -30,4 +31,158 @@ public function customCommand(InputInterface $input, OutputInterface $output): v
$io->comment('This is just a placeholder command, please add your own custom commands here. Please edit : ' . __FILE__);
}

/**
* Shared functionality to help create and re-tag a release.
*
* @param string $hotfix_or_release
* Either 'hotfix' or 'release'.
* @param string $semantic_version
* A semantic version number in the form x.y.z. Release must end in 0.
*
* @return array
* An indexed array of [$tag_description, $new_branch_name, $source_branch].
*
* @throws \Exception
*/

protected function getVariablesForRelease(string $hotfix_or_release, string $semantic_version): array
{
if (!in_array($hotfix_or_release, ['hotfix', 'release'])) {
throw new InvalidArgumentException("hotfix_or_release must be either 'hotfix' or 'release', '$hotfix_or_release' given.");
}

// @see https://regex101.com/r/Ly7O1x/3/.
if ($hotfix_or_release === 'hotfix') {
if (!preg_match('/^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>[1-9]\d*)$/', $semantic_version)) {
throw new InvalidArgumentException("semantic_version must be in the form x.y.z, where z is greater than 0, '$semantic_version' given.");
}
} else if (!preg_match('/^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0)$/', $semantic_version)) {
throw new InvalidArgumentException("semantic_version must be in the form x.y.z, where z is 0, '$semantic_version' given.");
}
$this->_exec('git status');
if (`git status --porcelain`) {
throw new \Exception('Your "git status" must be clean of any changes or untracked files before continuing. Please see the output of "git status" above.');
}
if ($hotfix_or_release === 'hotfix') {
$source_branch = 'main';
$tag_description = "Hotfix version $semantic_version";
} else {
$source_branch = 'develop';
$tag_description = "Release version $semantic_version";
}

$new_branch_name = "$hotfix_or_release/$semantic_version";

return [$tag_description, $new_branch_name, $source_branch];
}

/**
* Create a release.
*
* @command drupal-project:create-release
*
* @aliases create-release
*
* @param string $hotfix_or_release
* Either 'hotfix' or 'release'.
* @param string $semantic_version
* A semantic version number in the form x.y.z. Release must end in 0.
*
* @return \Robo\ResultData
*
* @throws \Exception
*/
public function createRelease(
InputInterface $input,
OutputInterface $output,
string $hotfix_or_release,
string $semantic_version,
): ResultData
{
[$tag_description, $new_branch_name, $source_branch] = $this->getVariablesForRelease($hotfix_or_release, $semantic_version);

`git fetch`;
// Checkout the branch that the release will be created from.
$this->taskGitStack()
->stopOnFail()
->checkout($source_branch)
->run();

// If you trying to test this function, you will need to temp change
// $source_branch to whatever branch you are working in, otherwise,
// your changes will get wiped out.
// You will also want to comment the following line out, since it will
// also wipe out your changes.
`git reset --hard origin/$source_branch`;

// Create the new release branch.
`git checkout -b $new_branch_name`;

// Create a new release tag and push the release branch and tag.
$this->taskGitStack()
->stopOnFail()
->push('origin', $new_branch_name)
->tag("v$semantic_version", $tag_description)
->push('origin', "v$semantic_version")
->run();

return new ResultData();
}

/**
* Re-creates the tag for a release after updates have been pushed.
*
* The release branch will already be up to date because a feature branch
* should have been pushed to it, but the initial tag will be out of date
* now. This checks out the current version of the release, deletes the tag
* then pushes back up the tag.
*
* @command drupal-project:re-tag-release
*
* @aliases re-tag
*
* @param string $hotfix_or_release
* Either 'hotfix' or 'release'.
* @param string $semantic_version
* A semantic version number in the form x.y.z. Release must end in 0.
*
* @return \Robo\ResultData
*
* @throws \Exception
*/
public function reTagRelease(
InputInterface $input,
OutputInterface $output,
string $hotfix_or_release,
string $semantic_version,
): ResultData
{
[$tag_description, $new_branch_name] = $this->getVariablesForRelease($hotfix_or_release, $semantic_version);

`git fetch`;
// Check back out the release branch that has been updated by a feature
// request and is ahead of the source branch.
$this->taskGitStack()
->stopOnFail()
->checkout($new_branch_name)
->run();

// Ensure that the release is at the latest.
`git reset --hard origin/$new_branch_name`;

// Delete the old tag locally and remotely.
`git tag --delete v$semantic_version`;
`git push origin --delete v$semantic_version`;

// Create a new tag of the same named based on the updated release
// branch.
$this->taskGitStack()
->stopOnFail()
->tag("v$semantic_version", $tag_description)
->push('origin', "v$semantic_version")
->run();

return new ResultData();
}

}
Loading
Loading