/** * Returns Git info * * @return array */ public static function getGitInfo() { $gitBinary = VP_GIT_BINARY; $info = []; $process = new Process(ProcessUtils::escapeshellarg($gitBinary) . " --version"); $process->run(); $info['git-binary-as-configured'] = $gitBinary; $info['git-available'] = $process->getErrorOutput() === null || !strlen($process->getErrorOutput()); if ($info['git-available'] === false) { $info['output'] = ['stdout' => trim($process->getOutput()), 'stderr' => trim($process->getErrorOutput())]; $info['env-path'] = getenv('PATH'); return $info; } $output = trim($process->getOutput()); $match = Strings::match($output, "~git version (\\d[\\d\\.]+\\d).*~"); $version = $match[1]; $gitPath = "unknown"; if ($gitBinary == "git") { $osSpecificWhereCommand = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? "where" : "which"; $process = new Process("{$osSpecificWhereCommand} git"); $process->run(); if ($process->isSuccessful()) { $gitPath = $process->getOutput(); } } else { $gitPath = $gitBinary; } $info['git-version'] = $version; $info['git-binary-as-called-by-vp'] = $gitBinary; $info['git-full-path'] = $gitPath; $info['versionpress-min-required-version'] = RequirementsChecker::GIT_MINIMUM_REQUIRED_VERSION; $info['matches-min-required-version'] = RequirementsChecker::gitMatchesMinimumRequiredVersion($version); return $info; }
private function detectChanges() { $currentComposerLock = file_get_contents(VP_PROJECT_ROOT . '/composer.lock'); $process = new Process(VP_GIT_BINARY . ' show HEAD:composer.lock', VP_PROJECT_ROOT); $process->run(); $previousComposerLock = $process->getOutput(); $currentPackages = $this->getPackagesFromLockFile($currentComposerLock); $previousPackages = $this->getPackagesFromLockFile($previousComposerLock); $installedPackages = array_diff_key($currentPackages, $previousPackages); $removedPackages = array_diff_key($previousPackages, $currentPackages); $packagesWithChangedVersion = array_filter(array_intersect_key($currentPackages, $previousPackages), function ($package) use($previousPackages) { return $package['version'] !== $previousPackages[$package['name']]['version']; }); return ['installed' => $installedPackages, 'removed' => $removedPackages, 'updated' => $packagesWithChangedVersion]; }
/** * Low-level helper, generally use runShellCommand() * * @param $cmd * @return array */ private function runProcess($cmd) { /* * MAMP / XAMPP issue on Mac OS X, see #106. * * http://stackoverflow.com/a/16903162/1243495 */ $dyldLibraryPath = getenv("DYLD_LIBRARY_PATH"); if ($dyldLibraryPath != "") { putenv("DYLD_LIBRARY_PATH="); } $process = new Process($cmd, $this->workingDirectoryRoot); if ($this->gitProcessTimeout !== null) { $process->setTimeout($this->gitProcessTimeout); } $process->run(); $result = ['stdout' => $process->getOutput(), 'stderr' => $process->getErrorOutput()]; putenv("DYLD_LIBRARY_PATH={$dyldLibraryPath}"); if ($result['stdout'] !== null) { $result['stdout'] = trim($result['stdout']); } if ($result['stderr'] !== null) { $result['stderr'] = trim($result['stderr']); } return $result; }
/** * Executes a command. If the command is WP-CLI command (starts with "wp ...") * * @param string $command * @param string $executionPath Working directory for the command. If null, the path will be determined * automatically. * * @param bool $debug * @param null|array $env * @return string When process execution is not successful * @throws Exception */ private function exec($command, $executionPath = null, $debug = false, $env = null) { $command = $this->rewriteWpCliCommand($command); if (!$executionPath) { $executionPath = $this->siteConfig->path; } // Changing env variables for debugging // We don't need the xdebug enabled in the subprocesses, // but sometimes on the other hand we need it enabled only in the subprocess. $isDebug = isset($_SERVER["XDEBUG_CONFIG"]); $childEnv = $_SERVER; if ($isDebug == $debug) { // same as this process } elseif ($debug) { $childEnv["XDEBUG_CONFIG"] = "idekey=xdebug"; // turn debug on } else { unset($childEnv["XDEBUG_CONFIG"]); // turn debug off } $childEnv = array_merge($childEnv, (array) $env); $process = new Process($command, $executionPath, $childEnv); $process->run(); if (!$process->isSuccessful()) { throw new Exception("Error executing cmd '{$command}' from working directory " . "'{$executionPath}':\n{$process->getConsoleOutput()}"); } return $process->getOutput(); }