Skip to content

Commit

Permalink
FIX Manually read token from .transifexrc
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Nov 4, 2024
1 parent 72ae09b commit a1a1e5e
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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') {
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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];
}
}

0 comments on commit a1a1e5e

Please sign in to comment.