Skip to content

Commit

Permalink
Merge branch '1.1' into 1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Nov 5, 2024
2 parents 36b2cee + 64494cd commit f647dc7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"symfony/process": "^4 || ^5 || ^6",
"symfony/yaml": "^4 || ^5 || ^6",
"symfony/dotenv": "^5 || ^6",
"symfony/filesystem": "^5 || ^6",
"guzzlehttp/guzzle": "^6 || ^7"
},
"conflict": {
Expand Down
30 changes: 29 additions & 1 deletion src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use LogicException;
use Symfony\Component\Console\Formatter\OutputFormatter;
use SilverStripe\SupportedModules\MetaData;
use Symfony\Component\Filesystem\Path;

class Translator
{
Expand Down Expand Up @@ -47,6 +48,8 @@ class Translator

private ?OutputFormatter $outputFormatter = null;

private string $txToken = '';

public function run()
{
$this->outputFormatter = new OutputFormatter(true);
Expand Down Expand Up @@ -81,6 +84,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');
Expand Down Expand Up @@ -625,7 +629,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);
Expand Down Expand Up @@ -844,4 +854,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];
}
}

0 comments on commit f647dc7

Please sign in to comment.