diff --git a/composer.json b/composer.json index 0b4d8c5..77606f1 100644 --- a/composer.json +++ b/composer.json @@ -17,6 +17,7 @@ "symfony/process": "^4 || ^5 || ^6", "symfony/yaml": "^4 || ^5 || ^6", "symfony/dotenv": "^5 || ^6", + "symfony/filesytem": "^6", "guzzlehttp/guzzle": "^6 || ^7" }, "conflict": { diff --git a/src/Translator.php b/src/Translator.php index c6a64a9..dba3632 100644 --- a/src/Translator.php +++ b/src/Translator.php @@ -9,6 +9,7 @@ use RuntimeException; use LogicException; use Symfony\Component\Console\Formatter\OutputFormatter; +use Symfony\Component\Filesystem\Path; class Translator { @@ -46,6 +47,8 @@ class Translator private ?OutputFormatter $outputFormatter = null; + private string $txToken = ''; + public function run() { $this->outputFormatter = new OutputFormatter(true); @@ -80,6 +83,7 @@ public function run() private function checkEnv(): void { + $this->readTxToken(); $txInstructions = 'Install the new go version of the client https://developers.transifex.com/docs/cli'; try { $this->exec('which tx'); @@ -618,7 +622,13 @@ private function getModuleName(string $modulePath): string private function exec(string $command, ?string $cwd = null): string { - $this->log("Running $command"); + if (str_starts_with($command, 'tx ')) { + $token = $this->txToken; + $command = "TX_TOKEN=$token $command"; + } + $message = "Running $command"; + $message = preg_replace('/TX_TOKEN=.+? tx/', 'TX_TOKEN=**** tx', $message); + $this->log($message); $process = Process::fromShellCommandline($command, $cwd); // set a timeout of 5 minutes - tx pull operations may take a long time $process->setTimeout(300); @@ -837,4 +847,22 @@ private function recursiveKeySort(array &$arr): void } } } + + /** + * The tx binary doesn't appear to correctly read from ~/.transifexrc, even though it will correctly + * write to it after prompting for it if it's missing. This is a workaround to read the token and + * use it a TX_TOKEN environment variable, which the tx binary will use. + */ + private function readTxToken(): void + { + $path = Path::canonicalize('~/.transifexrc'); + if (!file_exists($path)) { + throw new RuntimeException("$path does not exist"); + } + $contents = file_get_contents($path); + if (!preg_match('/\ntoken *= *(.+)(\n|$)/', $contents, $matches)) { + throw new RuntimeException("Could not get a valid token from $path"); + } + $this->txToken = $matches[1]; + } }