Skip to content

Commit e8566a1

Browse files
committed
Refactor code
1 parent 8c64a96 commit e8566a1

File tree

3 files changed

+118
-63
lines changed

3 files changed

+118
-63
lines changed

phpunit.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
<directory>src</directory>
1515
</include>
1616
<exclude>
17-
<file>src/Installer.php</file>
1817
<file>src/Plugin.php</file>
1918
</exclude>
2019
</source>

src/Installer.php

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/Plugin.php

Lines changed: 118 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,147 @@
44

55
use Composer\Composer;
66
use Composer\EventDispatcher\EventSubscriberInterface;
7-
use Composer\Installer\PackageEvents;
87
use Composer\IO\IOInterface;
8+
use Composer\Installer\PackageEvent;
9+
use Composer\Installer\PackageEvents;
910
use Composer\Plugin\PluginInterface;
11+
use Symfony\Component\Process\Process;
12+
13+
use function copy;
14+
use function is_dir;
15+
use function is_file;
16+
use function mkdir;
17+
use function realpath;
18+
use function sprintf;
19+
use function strtoupper;
20+
use function substr;
1021

1122
class Plugin implements PluginInterface, EventSubscriberInterface
1223
{
24+
private const PACKAGE_NAME = 'sass-embedded-php';
25+
26+
private string $packagePath;
27+
28+
private string $binDir;
29+
1330
public function activate(Composer $composer, IOInterface $io): void
1431
{
15-
$io->write('This is the way.');
32+
$config = $composer->getConfig();
33+
34+
$this->packagePath = realpath(__DIR__ . '/../');
35+
$this->binDir = (string) $config->get('bin-dir');
1636
}
1737

1838
public function deactivate(Composer $composer, IOInterface $io): void
1939
{
20-
$io->write('May the Force be with you.');
2140
}
2241

2342
public function uninstall(Composer $composer, IOInterface $io): void
2443
{
25-
$io->write('I\'ll see you again. I promise ©️');
2644
}
2745

46+
/* @uses onPostPackageUpdate */
2847
public static function getSubscribedEvents(): array
2948
{
3049
return [
31-
PackageEvents::POST_PACKAGE_INSTALL => 'onPostPackageInstall',
50+
PackageEvents::POST_PACKAGE_INSTALL => 'onPostPackageUpdate',
51+
PackageEvents::POST_PACKAGE_UPDATE => 'onPostPackageUpdate',
3252
];
3353
}
3454

35-
public function onPostPackageInstall(): void
55+
public function onPostPackageUpdate(PackageEvent $event): void
56+
{
57+
static $alreadyRun = false;
58+
59+
if ($alreadyRun) {
60+
return;
61+
}
62+
63+
$alreadyRun = true;
64+
65+
$this->installNpm($event->getIO());
66+
$this->copyBinary($event->getIO());
67+
}
68+
69+
private function installNpm(IOInterface $io): void
3670
{
37-
Installer::postInstall();
71+
if ($this->packagePath === '') {
72+
$io->write(sprintf(
73+
'<warning>[%s]</warning> package directory not found, skipping npm install.',
74+
self::PACKAGE_NAME
75+
));
76+
77+
return;
78+
}
79+
80+
$packageJson = $this->packagePath . '/package.json';
81+
if (! is_file($packageJson)) {
82+
$io->write(sprintf(
83+
'<warning>[%s]</warning> package.json not found in %s, skipping npm install.',
84+
self::PACKAGE_NAME,
85+
$this->packagePath
86+
));
87+
88+
return;
89+
}
90+
91+
$npmBin = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? 'npm.cmd' : 'npm';
92+
93+
$checkNpm = new Process([$npmBin, '--version']);
94+
$checkNpm->run();
95+
if (! $checkNpm->isSuccessful()) {
96+
$io->write(sprintf(
97+
'<warning>[%s]</warning> npm not found in PATH, skipping npm install.',
98+
self::PACKAGE_NAME
99+
));
100+
101+
return;
102+
}
103+
104+
$nodeModules = $this->packagePath . '/node_modules/sass-embedded';
105+
if (is_dir($nodeModules)) {
106+
$io->write(sprintf(
107+
'<info>[%s]</info> node_modules/sass-embedded already exists, skipping npm install.',
108+
self::PACKAGE_NAME
109+
));
110+
111+
return;
112+
}
113+
114+
$process = new Process([$npmBin, 'install'], $this->packagePath);
115+
$process->setTimeout(null);
116+
$process->run(function ($type, $buffer): void { echo $buffer; });
117+
118+
if ($process->isSuccessful()) {
119+
$io->write(sprintf(
120+
'<info>[%s]</info> npm install completed successfully.',
121+
self::PACKAGE_NAME
122+
));
123+
124+
return;
125+
}
126+
127+
$io->write(sprintf('<error>[%s]</error> npm install failed:', self::PACKAGE_NAME));
128+
$io->write($process->getErrorOutput());
129+
}
130+
131+
private function copyBinary(IOInterface $io): void
132+
{
133+
$source = $this->packagePath . '/bin/bridge.js';
134+
$target = $this->binDir . '/bridge.js';
135+
136+
if (! is_file($source)) {
137+
return;
138+
}
139+
140+
if (! is_dir($this->binDir)) {
141+
@mkdir($this->binDir, 0777, true);
142+
}
143+
144+
if (@copy($source, $target)) {
145+
$io->write(sprintf('<info>[%s]</info> Binary overwritten: %s', self::PACKAGE_NAME, $target));
146+
} else {
147+
$io->writeError(sprintf('<error>[%s]</error> Failed to copy binary', self::PACKAGE_NAME));
148+
}
38149
}
39150
}

0 commit comments

Comments
 (0)