Page MenuHomePhabricator

D11517.id27944.diff
No OneTemporary

D11517.id27944.diff

diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php
--- a/src/__phutil_library_map__.php
+++ b/src/__phutil_library_map__.php
@@ -335,6 +335,7 @@
'PhutilUtilsTestCase' => 'utils/__tests__/PhutilUtilsTestCase.php',
'PhutilWordPressAuthAdapter' => 'auth/PhutilWordPressAuthAdapter.php',
'PhutilWordPressFuture' => 'future/wordpress/PhutilWordPressFuture.php',
+ 'PhutilXHPASTBinary' => 'parser/xhpast/bin/PhutilXHPASTBinary.php',
'PhutilXHPASTSyntaxHighlighter' => 'markup/syntax/highlighter/PhutilXHPASTSyntaxHighlighter.php',
'PhutilXHPASTSyntaxHighlighterFuture' => 'markup/syntax/highlighter/xhpast/PhutilXHPASTSyntaxHighlighterFuture.php',
'PhutilXHPASTSyntaxHighlighterTestCase' => 'markup/syntax/highlighter/__tests__/PhutilXHPASTSyntaxHighlighterTestCase.php',
diff --git a/src/parser/xhpast/bin/PhutilXHPASTBinary.php b/src/parser/xhpast/bin/PhutilXHPASTBinary.php
new file mode 100644
--- /dev/null
+++ b/src/parser/xhpast/bin/PhutilXHPASTBinary.php
@@ -0,0 +1,123 @@
+<?php
+
+final class PhutilXHPASTBinary {
+
+ /**
+ * The current XHPAST version.
+ *
+ * This is the version that would be obtained with an up-to-date XHPAST
+ * build. The //actual// XHPAST build version may vary.
+ */
+ const VERSION = 'xhpast version 5.5.8/1g';
+
+ /**
+ * The XHPAST build version.
+ *
+ * Cache the result from @{method:getVersion} to prevent excessive calls to
+ * @{function:execx}.
+ */
+ private static $version;
+
+ /**
+ * Builds XHPAST automatically.
+ *
+ * Attempts to build the XHPAST binary automatically. Throws a
+ * @{class:CommandException} in the event of a failure.
+ *
+ * @return void
+ */
+ public static function build() {
+ execx('%s', self::getBuildPath());
+ self::$version = null;
+ }
+
+ /**
+ * Returns human-readable instructions for building XHPAST.
+ *
+ * @return string
+ */
+ public static function getBuildInstructions() {
+ return phutil_console_format(
+ "%s:\n\n \$ %s\n",
+ pht(
+ "Your version of '%s' is unbuilt or out of date. Run this ".
+ "script to build it",
+ 'xhpast'),
+ self::getBuildPath());
+ }
+
+ /**
+ * Returns the path to the script used to build XHPAST.
+ *
+ * @return string
+ */
+ private static function getBuildPath() {
+ $root = phutil_get_library_root('phutil');
+ $make = $root.'/../scripts/build_xhpast.sh';
+ return Filesystem::resolvePath($make);
+ }
+
+ /**
+ * Constructs an @{class:ExecFuture} for XHPAST.
+ *
+ * @param wild Data to pass to the future.
+ * @return ExecFuture
+ */
+ public static function getParserFuture($data) {
+ if (!self::isAvailable()) {
+ try {
+ // Try to build XHPAST automatically. If we can't then just ask the
+ // user to build it themselves.
+ self::build();
+ } catch (CommandException $ex) {
+ throw new PhutilProxyException(self::getBuildInstructions(), $ex);
+ }
+ }
+ $future = new ExecFuture('%s', self::getPath());
+ $future->write($data);
+
+ return $future;
+ }
+
+ /**
+ * Returns the path to the XHPAST binary.
+ *
+ * @return string
+ */
+ public static function getPath() {
+ if (phutil_is_windows()) {
+ return dirname(__FILE__).'\\xhpast.exe';
+ }
+ return dirname(__FILE__).'/xhpast';
+ }
+
+ /**
+ * Returns the XHPAST version.
+ *
+ * @return string
+ */
+ public static function getVersion() {
+ if (self::$version === null) {
+ $bin = self::getPath();
+
+ if (Filesystem::pathExists($bin)) {
+ list($err, $stdout) = exec_manual('%s --version', $bin);
+ if (!$err) {
+ self::$version = trim($stdout);
+ }
+ }
+ }
+
+ return self::$version;
+ }
+
+ /**
+ * Checks if XHPAST is built and up-to-date.
+ *
+ * @return bool
+ */
+ public static function isAvailable() {
+ return self::getVersion() == self::VERSION;
+ }
+
+}
diff --git a/src/parser/xhpast/bin/xhpast_parse.php b/src/parser/xhpast/bin/xhpast_parse.php
--- a/src/parser/xhpast/bin/xhpast_parse.php
+++ b/src/parser/xhpast/bin/xhpast_parse.php
@@ -1,101 +1,25 @@
<?php
-/**
- * Builds XHPAST automatically.
- *
- * Attempts to build the XHPAST binary automatically. Throws a
- * @{class:CommandException} in the event of a failure.
- *
- * @return void
- */
function xhpast_build() {
- $root = phutil_get_library_root('phutil');
- execx('%s', $root.'/../scripts/build_xhpast.sh');
+ return PhutilXHPASTBinary::build();
}
-/**
- * Returns the path to the XHPAST binary.
- *
- * @return string
- */
function xhpast_get_binary_path() {
- if (phutil_is_windows()) {
- return dirname(__FILE__).'\\xhpast.exe';
- }
- return dirname(__FILE__).'/xhpast';
+ return PhutilXHPASTBinary::getPath();
}
-/**
- * Returns human-readable instructions for building XHPAST.
- *
- * @return string
- */
function xhpast_get_build_instructions() {
- $root = phutil_get_library_root('phutil');
- $make = $root.'/../scripts/build_xhpast.sh';
- $make = Filesystem::resolvePath($make);
- return <<<EOHELP
-Your version of 'xhpast' is unbuilt or out of date. Run this script to build it:
-
- \$ {$make}
-
-EOHELP;
+ return PhutilXHPASTBinary::getBuildInstructions();
}
-/**
- * Constructs an @{class:ExecFuture} for XHPAST.
- *
- * @param wild Data to pass to the future.
- * @return ExecFuture
- */
function xhpast_get_parser_future($data) {
- if (!xhpast_is_available()) {
- try {
- // Try to build XHPAST automatically. If we can't then just ask the user
- // to build it themselves.
- xhpast_build();
- } catch (CommandException $ex) {
- throw new PhutilProxyException(xhpast_get_build_instructions(), $ex);
- }
- }
- $future = new ExecFuture('%s', xhpast_get_binary_path());
- $future->write($data);
-
- return $future;
+ return PhutilXHPASTBinary::getParserFuture($data);
}
-/**
- * Checks if XHPAST is built and up-to-date.
- *
- * @return bool
- */
function xhpast_is_available() {
- static $available;
-
- if ($available === null) {
- $available = xhpast_version() == 'xhpast version 5.5.8/1g';
- }
-
- return $available;
+ return PhutilXHPASTBinary::isAvailable();
}
-/**
- * Returns the XHPAST version.
- *
- * @return string
- */
function xhpast_version() {
- static $version;
-
- if ($version === null) {
- $bin = xhpast_get_binary_path();
- if (Filesystem::pathExists($bin)) {
- list($err, $stdout) = exec_manual('%s --version', $bin);
- if (!$err) {
- $version = trim($stdout);
- }
- }
- }
-
- return $version;
+ return PhutilXHPASTBinary::getVersion();
}

File Metadata

Mime Type
text/plain
Expires
Mon, Oct 28, 11:49 PM (1 w, 4 h ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6753299
Default Alt Text
D11517.id27944.diff (6 KB)

Event Timeline