diff --git a/src/Translator.php b/src/Translator.php index c6a64a9..a12b51a 100644 --- a/src/Translator.php +++ b/src/Translator.php @@ -80,6 +80,9 @@ public function run() private function checkEnv(): void { + if (!$this->getTxToken()) { + throw new RuntimeException('Could not get a valid token ~/.transifexrc'); + } $txInstructions = 'Install the new go version of the client https://developers.transifex.com/docs/cli'; try { $this->exec('which tx'); @@ -271,7 +274,8 @@ private function transifexPullSource() // ensure .tx/config is up to date $contents = file_get_contents("$modulePath/.tx/config"); if (strpos($contents, '[o:') === false) { - $this->exec('tx migrate', $modulePath); + $token = $this->getTxToken(); + $this->exec("TX_TOKEN=$token tx migrate", $modulePath); // delete .bak files created as part of tx migrate foreach (scandir("$modulePath/.tx") as $filename) { if (pathinfo($filename, PATHINFO_EXTENSION) !== 'bak') { @@ -282,7 +286,8 @@ private function transifexPullSource() } } // pull from transifex - $this->exec("tx pull -a -s -t -f --minimum-perc={$this->txMinimumPerc}", $modulePath); + $token = $this->getTxToken(); + $this->exec("TX_TOKEN=$token tx pull -a -s -t -f --minimum-perc={$this->txMinimumPerc}", $modulePath); } } @@ -502,7 +507,8 @@ private function transifexPushSource(): void return; } foreach ($this->modulePaths as $modulePath) { - $this->exec('tx push -s', $modulePath); + $token = $this->getTxToken(); + $this->exec("TX_TOKEN=$token tx push -s", $modulePath); } } @@ -837,4 +843,21 @@ 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 as an environemnt variable. + */ + private function getTxToken(): string + { + if (!$this->exec('if [ -f ~/.transifexrc ]; then echo 1; else echo 0; fi')) { + throw new RuntimeException('Could not find ~/.transifexrc'); + } + $contents = $this->exec('cat ~/.transifexrc'); + if (!preg_match('/\ntoken *= *(.+)(\n|$)/', $contents, $matches)) { + return ''; + } + return $matches[1]; + } }