Skip to content

Commit

Permalink
Add wp-cli
Browse files Browse the repository at this point in the history
Remove todo, proxy php & ngrok works
  • Loading branch information
mischabraam committed Jul 18, 2023
1 parent d95f280 commit 183cfec
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 52 deletions.
4 changes: 1 addition & 3 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ Here are a few key differences compared to the original Valet:
- ~~Ioncube~~
- Rewrite/unrewrite public domain to local environment
- ~~DevTools~~
- Binaries (magerun, magerun2, drush)
- ~~ngrok~~
- ~~composer commands~~
- Binaries (magerun, magerun2, drush, wp-cli)

### Changes vs Valet+ 2

Expand Down
120 changes: 79 additions & 41 deletions cli/ValetPlus/Binary.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace WeProvide\ValetPlus;

use DomainException;
use Valet\Brew;
use Valet\CommandLine;
use Valet\Filesystem;
use function Valet\info;
Expand All @@ -18,6 +19,8 @@ class Binary
protected const N98_MAGERUN_2 = 'magerun2';
/** @var string */
protected const DRUSH_LAUNCHER = 'drush';
/** @var string */
protected const WP_CLI = 'wp';

/**
* Supported binaries for the binary manager. Example:
Expand All @@ -28,7 +31,7 @@ class Binary
* 'bin_location' => '/usr/local/bin'
* ]
*/
protected const SUPPORTED_CUSTOM_BINARIES = [
protected const SUPPORTED_BINARIES = [
self::N98_MAGERUN => [
'url' => 'https://files.magerun.net/n98-magerun-2.3.0.phar',
'shasum' => 'b3e09dafccd4dd505a073c4e8789d78ea3def893cfc475a214e1154bff3aa8e4',
Expand All @@ -46,22 +49,30 @@ class Binary
'shasum' => '0ae18cd3f8745fdd58ab852481b89428b57be6523edf4d841ebef198c40271be',
'bin_location' => BREW_PREFIX . '/bin/',
'framework' => 'Drupal'
],
self::WP_CLI => [
'brew_formula' => 'wp-cli'
]
];

/** @var Brew */
protected $brew;
/** @var CommandLine */
protected $cli;
/** @var Filesystem */
protected $files;

/**
* @param Brew $brew
* @param CommandLine $cli
* @param Filesystem $files
*/
public function __construct(
Brew $brew,
CommandLine $cli,
Filesystem $files
) {
$this->brew = $brew;
$this->cli = $cli;
$this->files = $files;
}
Expand All @@ -73,7 +84,7 @@ public function __construct(
*/
public function getSupported()
{
return array_keys(static::SUPPORTED_CUSTOM_BINARIES);
return array_keys(static::SUPPORTED_BINARIES);
}

/**
Expand All @@ -82,20 +93,26 @@ public function getSupported()
* @param string $binary
* @return bool
*/
public function installed($binary)
public function installed($binary): bool
{
return $this->files->exists(static::SUPPORTED_CUSTOM_BINARIES[$binary]['bin_location'] . $binary);
if (isset(static::SUPPORTED_BINARIES[$binary]['bin_location'])) {
return $this->files->exists(static::SUPPORTED_BINARIES[$binary]['bin_location'] . $binary);
}

if (isset(static::SUPPORTED_BINARIES[$binary]['brew_formula'])) {
return $this->brew->installed(static::SUPPORTED_BINARIES[$binary]['brew_formula']);
}

return false;
}

/**
* Install all binaries.
*/
public function install()
{
foreach (static::SUPPORTED_CUSTOM_BINARIES as $binary => $binInfo) {
if (!$this->installed($binary)) {
$this->installBinary($binary);
}
foreach (static::SUPPORTED_BINARIES as $binary => $binInfo) {
$this->installBinary($binary);
}
}

Expand All @@ -106,7 +123,7 @@ public function install()
*/
public function installBinary($binary)
{
if (!isset(static::SUPPORTED_CUSTOM_BINARIES[$binary])) {
if (!isset(static::SUPPORTED_BINARIES[$binary])) {
throw new DomainException(
sprintf(
'Invalid binary given. Available binaries: %s',
Expand All @@ -120,37 +137,46 @@ public function installBinary($binary)
return;
}

$url = $this->getUrl($binary);
$urlSplit = explode('/', $url);
$fileName = $urlSplit[count($urlSplit) - 1];
// Download and install binary.
if (isset(static::SUPPORTED_BINARIES[$binary]['bin_location'])) {
$url = $this->getUrl($binary);
$urlSplit = explode('/', $url);
$fileName = $urlSplit[count($urlSplit) - 1];

// Download binary file.
info("Binary $binary installing from: $url");
$this->cli->passthru("cd /tmp && curl -OL $url");
// Download binary file.
info("Binary $binary installing from: $url");
$this->cli->passthru("cd /tmp && curl -OL $url");

// Check the checksum of downloaded file.
if (!$this->checkShasum($binary, $fileName)) {
$this->cli->runAsUser("rm /tmp/$fileName");
warning("Binary $binary could not be installed, $fileName checksum does not match: " . $this->getShasum($binary));
// Check the checksum of downloaded file.
if (!$this->checkShasum($binary, $fileName)) {
$this->cli->runAsUser("rm /tmp/$fileName");
warning("Binary $binary could not be installed, $fileName checksum does not match: " . $this->getShasum($binary));

return;
}
return;
}

// Move file.
$binLocation = $this->getBinLocation($binary);
$this->cli->run("sudo mv /tmp/$fileName $binLocation");

// Move file.
$binLocation = $this->getBinLocation($binary);
$this->cli->run("sudo mv /tmp/$fileName $binLocation");
// Make file executable.
$this->cli->run("sudo chmod +x $binLocation");
info("Binary $binary installed to: $binLocation");
}

// Make file executable.
$this->cli->run("sudo chmod +x $binLocation");
info("Binary $binary installed to: $binLocation");
// Install brew formula.
if (isset(static::SUPPORTED_BINARIES[$binary]['brew_formula'])) {
$formula = static::SUPPORTED_BINARIES[$binary]['brew_formula'];
$this->brew->ensureInstalled($formula);
}
}

/**
* Uninstall all binaries.
*/
public function uninstall()
{
foreach (static::SUPPORTED_CUSTOM_BINARIES as $binary => $binInfo) {
foreach (static::SUPPORTED_BINARIES as $binary => $binInfo) {
if ($this->installed($binary)) {
$this->uninstallBinary($binary);
}
Expand All @@ -164,21 +190,33 @@ public function uninstall()
*/
public function uninstallBinary($binary)
{
$binaryLocation = $this->getBinLocation($binary);
$this->cli->runAsUser('rm ' . $binaryLocation);
if ($this->files->exists($binaryLocation)) {
throw new DomainException('Could not remove binary! Please remove manually using: rm ' . $binaryLocation);
// Remove downloaded binary.
if (isset(static::SUPPORTED_BINARIES[$binary]['bin_location'])) {
$binaryLocation = $this->getBinLocation($binary);
$this->cli->runAsUser('rm ' . $binaryLocation);
if ($this->files->exists($binaryLocation)) {
throw new DomainException('Could not remove binary! Please remove manually using: rm ' . $binaryLocation);
}
info("Binary $binary successfully uninstalled!");
}

// Uninstall brew formula.
if (isset(static::SUPPORTED_BINARIES[$binary]['brew_formula'])) {
$formula = static::SUPPORTED_BINARIES[$binary]['brew_formula'];
$this->brew->uninstallFormula($formula);
}
info("Binary $binary successfully uninstalled!");
}

/**
* @param string $binary
*/
protected function update($binary)
{
$binLocation = $this->getBinLocation($binary);
$this->cli->run("sudo $binLocation self-update");
if (isset(static::SUPPORTED_BINARIES[$binary]['bin_location'])) {
info("Binary $binary updating...");
$binLocation = $this->getBinLocation($binary);
$this->cli->run("sudo $binLocation self-update");
}
}

/**
Expand All @@ -189,8 +227,8 @@ protected function update($binary)
*/
protected function getUrl($binary)
{
if (array_key_exists('url', static::SUPPORTED_CUSTOM_BINARIES[$binary])) {
return static::SUPPORTED_CUSTOM_BINARIES[$binary]['url'];
if (array_key_exists('url', static::SUPPORTED_BINARIES[$binary])) {
return static::SUPPORTED_BINARIES[$binary]['url'];
}
throw new DomainException('url key is required for binaries.');
}
Expand All @@ -203,8 +241,8 @@ protected function getUrl($binary)
*/
protected function getShasum($binary)
{
if (array_key_exists('shasum', static::SUPPORTED_CUSTOM_BINARIES[$binary])) {
return static::SUPPORTED_CUSTOM_BINARIES[$binary]['shasum'];
if (array_key_exists('shasum', static::SUPPORTED_BINARIES[$binary])) {
return static::SUPPORTED_BINARIES[$binary]['shasum'];
}
throw new DomainException('shasum key is required for binaries.');
}
Expand All @@ -217,8 +255,8 @@ protected function getShasum($binary)
*/
protected function getBinLocation($binary)
{
if (array_key_exists('bin_location', static::SUPPORTED_CUSTOM_BINARIES[$binary])) {
return static::SUPPORTED_CUSTOM_BINARIES[$binary]['bin_location'] . $binary;
if (array_key_exists('bin_location', static::SUPPORTED_BINARIES[$binary])) {
return static::SUPPORTED_BINARIES[$binary]['bin_location'] . $binary;
}
throw new DomainException('bin_location key is required for binaries.');
}
Expand Down
2 changes: 1 addition & 1 deletion cli/valet.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
'with-binary',
'b',
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
"Install with binary, default no binaries are installed\n" .
"Install with binary, by default all binaries are installed\n" .
"Supported binaries: " . implode(', ', Binary::getSupported()) . "\n"
);

Expand Down
7 changes: 0 additions & 7 deletions valet-plus
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ then
else
DIR="$( cd "$( dirname "$SOURCE" )" && pwd )"
fi
#echo $DIR

# If we are in the global Composer "bin" directory, we need to bump our
# current directory up two, so that we will correctly proxy into the
Expand All @@ -29,8 +28,6 @@ LARAVEL_DIR="$DIR/vendor/laravel/valet"
if [ ! -d "$LARAVEL_DIR" ]; then
LARAVEL_DIR=$(php -r "echo realpath('$DIR/../../laravel/valet');")
fi
#echo $LARAVEL_DIR


# Get a command-line executable we can use for php that's 8+; if this
# is the inside loop (Valet runs itself 2x in some settings), skip
Expand All @@ -51,9 +48,7 @@ then
else
PHP=$PHP_EXECUTABLE
fi
#echo $PHP

#TODO: check
# If the command is the "share" command we will need to resolve out any
# symbolic links for the site. Before starting Ngrok, we will fire a
# process to retrieve the live Ngrok tunnel URL in the background.
Expand Down Expand Up @@ -162,7 +157,6 @@ then
exit
fi

#TODO: check
# Proxy PHP commands to the "php" executable on the isolated site
elif [[ "$1" = "php" ]]
then
Expand All @@ -175,7 +169,6 @@ then

exit

#TODO: check
# Proxy Composer commands with the "php" executable on the isolated site
elif [[ "$1" = "composer" ]]
then
Expand Down

0 comments on commit 183cfec

Please sign in to comment.