Skip to content

Commit

Permalink
Extracted version guessing logic and created new 'debug:guess-version…
Browse files Browse the repository at this point in the history
…' command. (#103)
  • Loading branch information
TravisCarden authored Aug 21, 2020
1 parent 5ce1258 commit ca2e3dd
Show file tree
Hide file tree
Showing 8 changed files with 420 additions and 49 deletions.
107 changes: 99 additions & 8 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
* [`analyze`](#qastatic-analysis)
* [`backup`](#fixturebackup)
* [`core`](#debugcore-versions)
* [`cov`](#reportcode-coverage)
* [`coverage`](#reportcode-coverage)
* [`deprecations`](#qadeprecated-code-scan)
* [`enexts`](#fixtureenable-extensions)
* [`fix`](#qafixer)
* [`guess`](#debugguess-version)
* [`help`](#help)
* [`init`](#fixtureinit)
* [`list`](#list)
Expand All @@ -26,6 +25,7 @@

* [`debug:core-versions`](#debugcore-versions)
* [`debug:env-vars`](#debugenv-vars)
* [`debug:guess-version`](#debugguess-version)
* [`debug:packages`](#debugpackages)

**fixture:**
Expand All @@ -46,10 +46,6 @@
* [`qa:fixer`](#qafixer)
* [`qa:static-analysis`](#qastatic-analysis)

**report:**

* [`report:code-coverage`](#reportcode-coverage)

`help`
------

Expand Down Expand Up @@ -371,6 +367,93 @@ Do not ask any interactive question
* Is multiple: no
* Default: `false`

`debug:guess-version`
---------------------

Displays ORCA environment variables

### Usage

* `debug:guess-version <path>`
* `guess`

Displays ORCA environment variables

### Arguments

#### `path`

The path to guess the version for

* Is required: yes
* Is array: no
* Default: `NULL`

### Options

#### `--help|-h`

Display this help message

* Accept value: no
* Is value required: no
* Is multiple: no
* Default: `false`

#### `--quiet|-q`

Do not output any message

* Accept value: no
* Is value required: no
* Is multiple: no
* Default: `false`

#### `--verbose|-v|-vv|-vvv`

Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

* Accept value: no
* Is value required: no
* Is multiple: no
* Default: `false`

#### `--version|-V`

Display this application version

* Accept value: no
* Is value required: no
* Is multiple: no
* Default: `false`

#### `--ansi`

Force ANSI output

* Accept value: no
* Is value required: no
* Is multiple: no
* Default: `false`

#### `--no-ansi`

Disable ANSI output

* Accept value: no
* Is value required: no
* Is multiple: no
* Default: `false`

#### `--no-interaction|-n`

Do not ask any interactive question

* Accept value: no
* Is value required: no
* Is multiple: no
* Default: `false`

`debug:packages`
----------------

Expand Down Expand Up @@ -1586,7 +1669,7 @@ Runs static analysis tools

### Usage

* `qa:static-analysis [--composer] [--phpcs] [--phpcs-standard PHPCS-STANDARD] [--phplint] [--phploc] [--phpmd] [--yamllint] [--] <path>`
* `qa:static-analysis [--composer] [--coverage] [--phpcs] [--phpcs-standard PHPCS-STANDARD] [--phplint] [--phploc] [--phpmd] [--yamllint] [--] <path>`
* `analyze`

Tools can be specified individually or in combination. If none are specified, all will be run.
Expand All @@ -1612,6 +1695,15 @@ Run the Composer validation tool
* Is multiple: no
* Default: `false`

#### `--coverage`

Run the code coverage estimator. Implies "--phploc"

* Accept value: no
* Is value required: no
* Is multiple: no
* Default: `false`

#### `--phpcs`

Run the PHP Code Sniffer tool
Expand Down Expand Up @@ -1731,7 +1823,6 @@ Do not ask any interactive question
* Is value required: no
* Is multiple: no
* Default: `false`

`report:code-coverage`
----------------------

Expand Down
65 changes: 64 additions & 1 deletion src/Composer/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,30 @@

namespace Acquia\Orca\Composer;

use Acquia\Orca\Helper\Config\ConfigLoader;
use Acquia\Orca\Helper\Exception\FileNotFoundException as OrcaFileNotFoundException;
use Acquia\Orca\Helper\Exception\OrcaException;
use Acquia\Orca\Helper\Exception\ParseError;
use Acquia\Orca\Helper\Filesystem\FixturePathHandler;
use Acquia\Orca\Helper\Process\ProcessRunner;
use Composer\Package\Version\VersionGuesser;
use Exception;
use InvalidArgumentException;
use Noodlehaus\Exception\FileNotFoundException as NoodlehausFileNotFoundExceptionAlias;
use Noodlehaus\Exception\ParseException;

/**
* Provides a facade for encapsulating Composer interactions.
*/
class Composer {

/**
* The config loader.
*
* @var \Acquia\Orca\Helper\Config\ConfigLoader
*/
private $configLoader;

/**
* The fixture path.
*
Expand All @@ -25,17 +40,30 @@ class Composer {
*/
private $processRunner;

/**
* The version guesser.
*
* @var \Composer\Package\Version\VersionGuesser
*/
private $versionGuesser;

/**
* Constructs an instance.
*
* @param \Acquia\Orca\Helper\Config\ConfigLoader $config_loader
* The config loader.
* @param \Acquia\Orca\Helper\Filesystem\FixturePathHandler $fixture_path_handler
* The fixture path handler.
* @param \Acquia\Orca\Helper\Process\ProcessRunner $process_runner
* The process runner.
* @param \Composer\Package\Version\VersionGuesser $version_guesser
* The version guesser.
*/
public function __construct(FixturePathHandler $fixture_path_handler, ProcessRunner $process_runner) {
public function __construct(ConfigLoader $config_loader, FixturePathHandler $fixture_path_handler, ProcessRunner $process_runner, VersionGuesser $version_guesser) {
$this->configLoader = $config_loader;
$this->fixture = $fixture_path_handler->getPath();
$this->processRunner = $process_runner;
$this->versionGuesser = $version_guesser;
}

/**
Expand Down Expand Up @@ -63,6 +91,41 @@ public function createProject(string $project_template_string, string $stability
]);
}

/**
* Guesses the version of a local package.
*
* @param string $path
* The path to the package to guess.
*
* @return string
* The guessed version string.
*
* @throws \Acquia\Orca\Helper\Exception\FileNotFoundException
* @throws \Acquia\Orca\Helper\Exception\OrcaException
* @throws \Acquia\Orca\Helper\Exception\ParseError
*/
public function guessVersion(string $path): string {
try {
$composer_json_path = "{$path}/composer.json";
$package_config = $this->configLoader
->load($composer_json_path)
->all();
}
catch (NoodlehausFileNotFoundExceptionAlias $e) {
throw new OrcaFileNotFoundException("No such file: {$composer_json_path}");
}
catch (ParseException $e) {
throw new ParseError("Cannot parse {$composer_json_path}");
}
catch (Exception $e) {
throw new OrcaException("Unknown error guessing version at {$path}");
}

$guess = $this->versionGuesser
->guessVersion($package_config, $path);
return (empty($guess['version'])) ? '@dev' : $guess['version'];
}

/**
* Removes packages.
*
Expand Down
73 changes: 73 additions & 0 deletions src/Console/Command/Debug/DebugGuessVersionCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Acquia\Orca\Console\Command\Debug;

use Acquia\Orca\Composer\Composer;
use Acquia\Orca\Console\Helper\StatusCode;
use Acquia\Orca\Helper\Exception\OrcaException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Provides a command.
*/
class DebugGuessVersionCommand extends Command {

/**
* The default command name.
*
* @var string
*/
protected static $defaultName = 'debug:guess-version';

/**
* The Composer facade.
*
* @var \Acquia\Orca\Composer\Composer
*/
private $composer;

/**
* Constructs an instance.
*
* @param \Acquia\Orca\Composer\Composer $composer
* The Composer facade.
*/
public function __construct(Composer $composer) {
parent::__construct(self::$defaultName);
$this->composer = $composer;
}

/**
* {@inheritdoc}
*/
protected function configure(): void {
$this
->setAliases(['guess'])
->setDescription('Displays ORCA environment variables')
->addArgument('path', InputArgument::REQUIRED, 'The path to guess the version for');
}

/**
* {@inheritdoc}
*
* @SuppressWarnings(PHPMD.StaticAccess)
*/
public function execute(InputInterface $input, OutputInterface $output): int {
$path = $input->getArgument('path');

try {
$guess = $this->composer->guessVersion($path);
}
catch (OrcaException $e) {
$output->writeln("Error: {$e->getMessage()}");
return StatusCode::ERROR;
}

$output->writeln($guess);
return StatusCode::OK;
}

}
20 changes: 3 additions & 17 deletions src/Fixture/FixtureCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@
use Composer\IO\NullIO;
use Composer\Json\JsonFile;
use Composer\Package\PackageInterface;
use Composer\Package\Version\VersionGuesser;
use Composer\Package\Version\VersionSelector;
use Composer\Repository\RepositoryFactory;
use Composer\Semver\Comparator;
use Composer\Semver\VersionParser;
use Noodlehaus\Config;
use Symfony\Component\Console\Style\SymfonyStyle;
use UnexpectedValueException;

Expand Down Expand Up @@ -185,13 +183,6 @@ class FixtureCreator {
*/
private $useSqlite = TRUE;

/**
* The Composer version guesser.
*
* @var \Composer\Package\Version\VersionGuesser
*/
private $versionGuesser;

/**
* The Semver version parser.
*
Expand Down Expand Up @@ -229,12 +220,10 @@ class FixtureCreator {
* The package manager.
* @param \Acquia\Orca\Fixture\SubextensionManager $subextension_manager
* The subextension manager.
* @param \Composer\Package\Version\VersionGuesser $version_guesser
* The Composer version guesser.
* @param \Composer\Semver\VersionParser $version_parser
* The Semver version parser.
*/
public function __construct(CodebaseCreator $codebase_creator, Composer $composer, DrupalCoreVersionFinder $core_version_finder, FixturePathHandler $fixture_path_handler, FixtureInspector $fixture_inspector, SiteInstaller $site_installer, SymfonyStyle $output, ProcessRunner $process_runner, PackageManager $package_manager, SubextensionManager $subextension_manager, VersionGuesser $version_guesser, VersionParser $version_parser) {
public function __construct(CodebaseCreator $codebase_creator, Composer $composer, DrupalCoreVersionFinder $core_version_finder, FixturePathHandler $fixture_path_handler, FixtureInspector $fixture_inspector, SiteInstaller $site_installer, SymfonyStyle $output, ProcessRunner $process_runner, PackageManager $package_manager, SubextensionManager $subextension_manager, VersionParser $version_parser) {
$this->blt = $package_manager->getBlt();
$this->codebaseCreator = $codebase_creator;
$this->composer = $composer;
Expand All @@ -246,7 +235,6 @@ public function __construct(CodebaseCreator $codebase_creator, Composer $compose
$this->packageManager = $package_manager;
$this->siteInstaller = $site_installer;
$this->subextensionManager = $subextension_manager;
$this->versionGuesser = $version_guesser;
$this->versionParser = $version_parser;
}

Expand Down Expand Up @@ -928,10 +916,8 @@ private function getLocalPackageString(Package $package): string {
* The versions of the given package, e.g., "@dev" or "dev-8.x-1.x".
*/
private function getLocalPackageVersion(Package $package): string {
$path = $this->fixture->getPath($package->getRepositoryUrlRaw());
$package_config = (array) new Config("{$path}/composer.json");
$guess = $this->versionGuesser->guessVersion($package_config, $path);
return (empty($guess['version'])) ? '@dev' : $guess['version'];
$path = $package->getRepositoryUrlAbsolute();
return $this->composer->guessVersion($path);
}

/**
Expand Down
Loading

0 comments on commit ca2e3dd

Please sign in to comment.