Skip to content

Commit 4070c6c

Browse files
committed
Made download process a little more error-prone.
1 parent da66878 commit 4070c6c

File tree

4 files changed

+409
-72
lines changed

4 files changed

+409
-72
lines changed

src/Command/DownloadFactorioCommand.php

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,31 +63,63 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6363
$version = strval($input->getArgument('version'));
6464
$archiveFileFull = $this->tempDirectory . "/factorio_${version}_full.tar.xz";
6565
$archiveFileHeadless = $this->tempDirectory . "/factorio_${version}_headless.tar.xz";
66+
$tempDirectoryFull = $this->tempDirectory . "/factorio_${version}_full";
67+
$tempDirectoryHeadless = $this->tempDirectory . "/factorio_${version}_headless";
6668

6769
$this->console->writeHeadline("Downloading and installing Factorio version {$version}");
6870

6971
$this->console->writeAction('Downloading full variant of Factorio');
70-
$fullProcess = $this->factorioDownloadService->createFactorioDownloadProcess(
72+
$downloadFullProcess = $this->factorioDownloadService->createFactorioDownloadProcess(
7173
FactorioDownloadService::VARIANT_FULL,
7274
$version,
7375
$archiveFileFull,
7476
);
75-
$fullProcess->start();
77+
$downloadFullProcess->start();
7678

7779
$this->console->writeAction('Downloading headless variant of Factorio');
78-
$headlessProcess = $this->factorioDownloadService->createFactorioDownloadProcess(
80+
$downloadHeadlessProcess = $this->factorioDownloadService->createFactorioDownloadProcess(
7981
FactorioDownloadService::VARIANT_HEADLESS,
8082
$version,
8183
$archiveFileHeadless,
8284
);
83-
$headlessProcess->run();
84-
85+
$downloadHeadlessProcess->run();
86+
if ($downloadHeadlessProcess->getExitCode() !== 0) {
87+
$this->console->writeMessage('<fg=red>Download of headless version failed!</>');
88+
return 1;
89+
}
8590
$this->console->writeAction('Extracting headless variant of Factorio');
86-
$this->factorioDownloadService->extractFactorio($archiveFileHeadless, $this->headlessFactorioDirectory);
91+
$extractHeadlessProcess = $this->factorioDownloadService->createFactorioExtractProcess(
92+
$archiveFileHeadless,
93+
$tempDirectoryHeadless,
94+
);
95+
$extractHeadlessProcess->run();
96+
if ($extractHeadlessProcess->getExitCode() !== 0) {
97+
$this->console->writeMessage('<fg=red>Extracting headless version failed!</>');
98+
return 1;
99+
}
100+
101+
$downloadFullProcess->wait();
102+
if ($downloadFullProcess->getExitCode() !== 0) {
103+
$this->console->writeMessage('<fg=red>Download of full version failed!</>');
104+
return 1;
105+
}
87106

88-
$fullProcess->wait();
89107
$this->console->writeAction('Extracting full variant of Factorio');
90-
$this->factorioDownloadService->extractFactorio($archiveFileFull, $this->fullFactorioDirectory);
108+
$extractFullProcess = $this->factorioDownloadService->createFactorioExtractProcess(
109+
$archiveFileFull,
110+
$tempDirectoryFull,
111+
);
112+
$extractFullProcess->run();
113+
if ($extractFullProcess->getExitCode() !== 0) {
114+
$this->console->writeMessage('<fg=red>Extracting full version failed!</>');
115+
return 1;
116+
}
117+
118+
$this->console->writeAction('Switching Factorio releases');
119+
$this->fileSystem->remove($this->headlessFactorioDirectory);
120+
$this->fileSystem->rename($tempDirectoryHeadless, $this->headlessFactorioDirectory);
121+
$this->fileSystem->remove($this->fullFactorioDirectory);
122+
$this->fileSystem->rename($tempDirectoryFull, $this->fullFactorioDirectory);
91123

92124
$this->console->writeAction('Cleaning up');
93125
$this->fileSystem->remove($archiveFileHeadless);

src/Service/FactorioDownloadService.php

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,15 @@ class FactorioDownloadService
2929
private Filesystem $fileSystem;
3030
private string $factorioApiUsername;
3131
private string $factorioApiToken;
32-
private string $tempDirectory;
3332

3433
public function __construct(
3534
Filesystem $filesystem,
3635
string $factorioApiUsername,
3736
string $factorioApiToken,
38-
string $tempDirectory
3937
) {
4038
$this->fileSystem = $filesystem;
4139
$this->factorioApiUsername = $factorioApiUsername;
4240
$this->factorioApiToken = $factorioApiToken;
43-
$this->tempDirectory = (string) realpath($tempDirectory);
4441
}
4542

4643
/**
@@ -61,28 +58,12 @@ public function createFactorioDownloadProcess(
6158
);
6259
}
6360

64-
/**
65-
* Extracts the specified archive file and replaces the current Factorio instance with its contents.
66-
* @param string $archiveFile
67-
* @param string $destinationDirectory
68-
*/
69-
public function extractFactorio(string $archiveFile, string $destinationDirectory): void
70-
{
71-
$tempDirectory = $this->tempDirectory . '/factorio_temp';
72-
73-
$process = $this->createExtractArchiveProcess($archiveFile, $tempDirectory);
74-
$process->run();
75-
76-
$this->fileSystem->remove($destinationDirectory);
77-
$this->fileSystem->rename($tempDirectory, $destinationDirectory);
78-
}
79-
8061
/**
8162
* @param string $archiveFile
8263
* @param string $destinationDirectory
8364
* @return Process<string>
8465
*/
85-
protected function createExtractArchiveProcess(string $archiveFile, string $destinationDirectory): Process
66+
public function createFactorioExtractProcess(string $archiveFile, string $destinationDirectory): Process
8667
{
8768
$this->fileSystem->remove($destinationDirectory);
8869
$this->fileSystem->mkdir($destinationDirectory);

0 commit comments

Comments
 (0)