Skip to content

Commit

Permalink
Adding build script to set nvm and npm into a single context when run…
Browse files Browse the repository at this point in the history
…ning the build. Using application's meta storage to pass branches between `branch` and `release` commands. Adjusting release branches cleanup to make sure not to cleanup the default branch from the product's repo.
  • Loading branch information
message-dimke committed Nov 7, 2023
1 parent 13b8d0d commit 31f9639
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 32 deletions.
15 changes: 8 additions & 7 deletions bin/build
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#! /bin/bash
#!/bin/bash

source ~/.nvm/nvm.sh
nvm use

case $1 in
pnpm) echo 'pnpm'
nvm use && pnpm install && pnpm run build
pnpm)
pnpm install && pnpm run build
;;
npm) echo 'npm'
nvm use && npm install && npm run build
npm)
npm install && npm run build
;;
*) echo 'default'
nvm use && $1
*)
eval $1
;;
esac
43 changes: 24 additions & 19 deletions packages/php/woorelease-extension/src/Commands/Branch.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,16 @@ protected function execute( InputInterface $input, OutputInterface $output ) {
list( $product, $gh_org, $branch ) = Utils::parse_product_info( $github_url );

// Prepare the release: check release/branch to exist.
$repository_url = sprintf( 'https://github.com/%1$s/%2$s', $gh_org, $product );
$repository_url = sprintf( 'https://github.com/%1$s/%2$s', $gh_org, $product );

if ( $cleanup ) {
return $this->cleanup( $repository_url, $branch );
// Not to delete the default repo's branch.
if ( $branch !== $default_branch ) {
$output->write( sprintf( "\n<info>Cleaning up `%s` branch ...</info>\n", $branch ) );
return $this->cleanup( $repository_url, $branch );
}
$output->write( sprintf( "\n<info>Default branch `%s` cannot be deleted.</info>\n", $branch ) );
return Command::SUCCESS;
}

$is_release_branch = WooGrowGit::does_branch_exist( $repository_url, $branch );
Expand All @@ -73,24 +79,20 @@ protected function execute( InputInterface $input, OutputInterface $output ) {

if ( $do_create_release_branch ) {
$create = WooGrowGit::create_branch( $branch );
/*if ( $release ) {*/
$push = $create && WooGrowGit::push_branch( $repository_url, $branch );
if ( ! $push ) {
$do_release_from_default = Utils::yes_no(
sprintf(
'Branch %s has failed to create. Do you want to release from default %s branch?',
$branch,
$default_branch
)
);

if ( ! $do_release_from_default ) {
throw new Exception( 'Release cancelled.' );
}
$push = $create && WooGrowGit::push_branch( $repository_url, $branch );
if ( ! $push ) {
$do_release_from_default = Utils::yes_no(
sprintf(
'Branch %s has failed to create. Do you want to release from default %s branch?',
$branch,
$default_branch
)
);

if ( ! $do_release_from_default ) {
throw new Exception( 'Release cancelled.' );
}
/*} else {
$logger->notice( 'Simulation mode. Creating local branch {branch}. In simulation mode branch won\'t be pushed to remote.', array( 'branch' => $branch ) );
}*/
}
} else {
$branch = $default_branch;
$do_release_from_default = Utils::yes_no(
Expand All @@ -110,6 +112,9 @@ protected function execute( InputInterface $input, OutputInterface $output ) {
// End release.
$logger->notice( sprintf( 'Release branch %s is ready.', $branch ) );

// Setting active branch name to the output.
$this->getApplication()->set_meta( "{$product}_branch", $branch );

return Command::SUCCESS;
} catch ( \Exception $e ) {
Utils::handle_error( $e );
Expand Down
13 changes: 10 additions & 3 deletions packages/php/woorelease-extension/src/Commands/Release.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ protected function execute( InputInterface $input, OutputInterface $output ) {
if ( Command::SUCCESS !== $command->run( new ArrayInput( $arguments ), $output ) ) {
return Command::FAILURE;
}
// Get current active branch name after the branch command.
$active_branch = $this->getApplication()->get_meta( "{$product}_branch" ) ?? $branch;
// Update $branch and $github_url with currently active branch name.
if ( $branch !== $active_branch ) {
$branch = $active_branch;
$github_url = sprintf( 'https://github.com/%1$s/%2$s/tree/%3$s', $gh_org, $product, $branch );
}

if ( empty( $folder ) ) {
$output->writeln( sprintf( "\n<error>Release FAILED for %s</error>\n", $product ) );
Expand Down Expand Up @@ -227,10 +234,10 @@ protected function execute( InputInterface $input, OutputInterface $output ) {
'default_branch' => $default_branch,
'--cleanup' => true,
);
if ( Command::SUCCESS !== $command->run( new ArrayInput( $arguments ), $output ) ) {
$logger->error( 'Simulation mode. Cleaning up the release branch has failed. Please cleanup manually.' );
if ( Command::SUCCESS === $command->run( new ArrayInput( $arguments ), $output ) ) {
$logger->notice( 'Simulation mode. Release branch cleanup has succeeded.' );
} else {
$logger->notice( 'Simulation mode. Cleaning up the release branch succeeded.' );
$logger->error( 'Simulation mode. Release branch cleanup has failed. Please cleanup `{branch}` branch manually.', [ 'branch' => $branch ] );
}
}

Expand Down
7 changes: 4 additions & 3 deletions packages/php/woorelease-extension/src/Utils/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ public static function maybe_build_with_nvm( $use_nvm, $grow_root_path, $product
$logger = Logger::default();
if ( $use_nvm ) {
$logger->notice( 'Attempting to use the product\'s recommended node version.' );
static::build( $grow_root_path, $product, $folder );
return static::build( $grow_root_path, $product, $folder );
} else {
return \WR\Tools\Product::build( $product, $folder );
}
return \WR\Tools\Product::build( $product, $folder );
}

/**
Expand All @@ -51,7 +52,7 @@ public static function build( $grow_root_path, $product, $folder ) {
}

if ( $build_step ) {
shell_exec( "{$grow_root_path}/bin/build {$build_step}" );
shell_exec( "{$grow_root_path}/bin/build '{$build_step}'" );
} elseif ( $use_pnpm ) {
shell_exec( "{$grow_root_path}/bin/build pnpm" );
} else {
Expand Down

0 comments on commit 31f9639

Please sign in to comment.