public static function fromFile($path) { if (!is_file($path)) { return \Result\Result::err('file not found'); } return self::fromString(file_get_contents($path)); }
private function parsePluginsFile($raw_file) { // Check for #-comments $lines = explode("\n", $raw_file); foreach ($lines as $line) { if (preg_match('/^\\s*#/', $line)) { return \Result\Result::err('Comments beginning with # are not permitted'); } } $plugins = parse_ini_string($raw_file); if (!is_array($plugins)) { return \Result\Result::err('Unable to parse Plugins file'); } // Got plugins - turn names to sources $source = $append = ''; $plugins_manifest = new \stdClass(); foreach ($plugins as $plugin => $data) { // // Special lines // if ($plugin == 'source') { if (empty($data)) { return \Result\Result::err("Source is empty. It should just specify a repo root:\n\n source = 'git@git.dxw.net:wordpress-plugins/'\n\nWhippet will attempt to find a source for your plugins by appending the plugin name to this URL."); } $source = $data; continue; } if ($plugin == 'append') { $append = $data; continue; } $repository = $revision = ''; // // Everything else should be a plugin // // First see if there is data. if (!empty($data)) { // Format: LABEL[, REPO] if (strpos($data, ',') !== false) { list($revision, $repository) = explode(',', $data); } else { $revision = $data; } } // if (empty($repository)) { // $repository = "{$source}{$plugin}{$append}"; // } if (empty($revision)) { $revision = 'master'; } // We should now have repo and revision $plugins_manifest->{$plugin} = new \stdClass(); $plugins_manifest->{$plugin}->repository = $repository; $plugins_manifest->{$plugin}->revision = $revision; } return \Result\Result::ok(['source' => $source, 'plugins' => $plugins_manifest]); }
public static function find($cwd) { $path = $cwd; while (dirname($path) !== $path) { if (is_file($path . '/whippet.json')) { return \Result\Result::ok(new self($path)); } $path = dirname($path); } return \Result\Result::err('whippet.json not found'); }
public function testInstallMissingWhippetLock() { $dir = $this->getDir(); file_put_contents($dir . '/whippet.json', 'foobar'); $this->addFactoryCallStatic('\\Dxw\\Whippet\\Files\\WhippetLock', 'fromFile', $dir . '/whippet.lock', \Result\Result::err('file not found')); $dependencies = new \Dxw\Whippet\Dependencies\Installer($this->getFactory(), $this->getProjectDirectory($dir)); ob_start(); $result = $dependencies->install(); $output = ob_get_clean(); $this->assertEquals(true, $result->isErr()); $this->assertEquals('whippet.lock: file not found', $result->getErr()); $this->assertEquals('', $output); }
private function installDependency($type, $dep) { $path = $this->dir . '/wp-content/' . $type . '/' . $dep['name']; $git = $this->factory->newInstance('\\Dxw\\Whippet\\Git\\Git', $path); if (!$git->is_repo()) { echo sprintf("[Adding %s/%s]\n", $type, $dep['name']); $result = $git->clone_repo($dep['src']); if ($result === false) { return \Result\Result::err('could not clone repository'); } } else { echo sprintf("[Checking %s/%s]\n", $type, $dep['name']); } $result = $git->checkout($dep['revision']); if ($result === false) { return \Result\Result::err('could not checkout revision'); } return \Result\Result::ok(); }
public function testUpdateNoExistingWhippetLock() { $dir = $this->getDir(); $whippetJson = $this->getWhippetJson(['src' => ['themes' => 'git@git.dxw.net:wordpress-themes/', 'plugins' => 'git@git.dxw.net:wordpress-plugins/'], 'themes' => [['name' => 'my-theme', 'ref' => 'v1.4']], 'plugins' => [['name' => 'my-plugin', 'ref' => 'v1.6']]]); $this->addFactoryCallStatic('\\Dxw\\Whippet\\Files\\WhippetJson', 'fromFile', $dir . '/whippet.json', \Result\Result::ok($whippetJson)); file_put_contents($dir . '/whippet.json', 'foobar'); $gitignore = $this->getGitignore([], ["/wp-content/themes/my-theme\n", "/wp-content/plugins/my-plugin\n"], true, false); $this->addFactoryNewInstance('\\Dxw\\Whippet\\Git\\Gitignore', $dir, $gitignore); $whippetLock = $this->getWhippetLockWritable([['themes', 'my-theme', 'git@git.dxw.net:wordpress-themes/my-theme', '27ba906'], ['plugins', 'my-plugin', 'git@git.dxw.net:wordpress-plugins/my-plugin', 'd961c3d']], sha1('foobar'), $dir . '/whippet.lock', []); $this->addFactoryNewInstance('\\Dxw\\Whippet\\Files\\WhippetLock', [], $whippetLock); $this->addFactoryCallStatic('\\Dxw\\Whippet\\Git\\Git', 'ls_remote', 'git@git.dxw.net:wordpress-themes/my-theme', 'v1.4', \Result\Result::ok('27ba906')); $this->addFactoryCallStatic('\\Dxw\\Whippet\\Git\\Git', 'ls_remote', 'git@git.dxw.net:wordpress-plugins/my-plugin', 'v1.6', \Result\Result::ok('d961c3d')); $this->addFactoryCallStatic('\\Dxw\\Whippet\\Files\\WhippetLock', 'fromFile', $dir . '/whippet.lock', \Result\Result::err('file not found')); $dependencies = new \Dxw\Whippet\Dependencies\Updater($this->getFactory(), $this->getProjectDirectory($dir)); ob_start(); $result = $dependencies->update(); $output = ob_get_clean(); $this->assertFalse($result->isErr()); $this->assertEquals("[Updating themes/my-theme]\n[Updating plugins/my-plugin]\n", $output); }
public static function ls_remote($repo, $ref) { exec(sprintf('git ls-remote %s %s', escapeshellarg($repo), escapeshellarg($ref)), $output, $return); if ($return !== 0) { return \Result\Result::err('git error'); } if (count($output) === 0) { return \Result\Result::err('ref not found'); } return \Result\Result::ok(explode("\t", $output[0])[0]); }
<?php use Result\Result as R; require __DIR__ . "/vendor/autoload.php"; $divide = function ($a, $b) { if ($b === 0 || $b === 0.0) { return R::err("division_by_zero"); } return R::ok($a / $b); }; $calculateSomething = function ($a, $b, $c) use($divide) { $errMapper = function ($err) { return "calculation_error"; }; // $x now has a value of expression ($a / $b) // meaning Result returned from divide was already unwrapped for you $x = (yield $divide($a, $b)); // you can also remap errors if it's needed $x = (yield $divide($x, $c)->remapErr($errMapper)); (yield R::ok($x + 42)); }; $res = R::reduce($calculateSomething(1, 2, 3)); var_dump($res); $res = R::reduce($calculateSomething(1, 0, 1)); var_dump($res); $res = R::reduce($calculateSomething(1, 2, 0)); var_dump($res); // the error was remapped, now it will be "calculation_error"
public function testRemapErr() { $ok = Result::ok("asd"); $this->assertSame($ok, $ok->remapErr(function ($er) { return "err"; })); $err = Result::err("fooerr"); $mapped = $err->remapErr(function ($er) { return ["bar"]; }); $this->assertSame("fooerr", $err->getErr()); $this->assertSame(["bar"], $mapped->getErr()); }
private function addDependencyToLockfile($type, array $dep) { if (isset($dep['src'])) { $src = $dep['src']; } else { $sources = $this->jsonFile->getSources(); if (!isset($sources[$type])) { return \Result\Result::err('missing sources'); } $src = $sources[$type] . $dep['name']; } $ref = 'master'; if (isset($dep['ref'])) { $ref = $dep['ref']; } $commitResult = $this->factory->callStatic('\\Dxw\\Whippet\\Git\\Git', 'ls_remote', $src, $ref); if ($commitResult->isErr()) { return \Result\Result::err(sprintf('git command failed: %s', $commitResult->getErr())); } $this->lockFile->addDependency($type, $dep['name'], $src, $commitResult->unwrap()); return \Result\Result::ok(); }