Skip to content

Commit a20c2f2

Browse files
authored
Refactore + support download from unpkg.com (#1)
1 parent 879702c commit a20c2f2

14 files changed

+418
-226
lines changed

README.md

+20-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,26 @@ Helper to install via composer ...
77
- [jQuery](https://github.com/components/jquery)
88
- [TinyMCE](https://github.com/tinymce/tinymce)
99

10-
<small>Note: composer install is broken since ChartJs and flatpickr. Until its fixed we try to download from https://unpkg.com</small>
10+
## Support packages from unpkg.com
11+
12+
With v3 it ist possible to download files from https://unpkg.com/.
13+
14+
Example: add a package to composers extra section
15+
16+
```
17+
"openmage-unpkg-packages": {
18+
"@eastdesire/jscolor": {
19+
"version": "2.5.2",
20+
"source": "",
21+
"target": "js/jscolor",
22+
"files": [
23+
"jscolor.js"
24+
]
25+
}
26+
}
27+
```
28+
29+
<small>Note: composer install is broken since ChartJs v3. Until its fixed we try to download from https://unpkg.com</small>
1130

1231
---
1332

src/OpenMage/ComposerPlugin/Copy/AbstractCopyPlugin.php

+82-172
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,11 @@ abstract class AbstractCopyPlugin implements CopyInterface
3131
public array $installedComposerPackages = [];
3232

3333
/**
34-
* Packages installad via NPM downloadd
34+
* Packages installad via Unpkg downloadd
3535
*
3636
* @var array<string, array<string, string>>
3737
*/
38-
public array $installedNpmPackages = [];
39-
40-
/**
41-
* Package version
42-
*/
43-
public ?string $version = null;
38+
public array $installedUnpkgPackages = [];
4439

4540
/**
4641
* Composer event
@@ -59,101 +54,112 @@ public function __construct(Event $event)
5954
*/
6055
public function processComposerInstall(): void
6156
{
62-
$copySourcePath = null;
63-
if ($this instanceof CopyFromComposerInterface) {
64-
$package = $this->getComposerPackage();
65-
if (!$package) {
66-
return;
67-
}
68-
$copySourcePath = $this->getCopySourcePath();
57+
if (!$this instanceof CopyFromComposerInterface) {
58+
return;
6959
}
7060

61+
$package = $this->getComposerPackage();
62+
if (!$package) {
63+
return;
64+
}
65+
66+
$copySourcePath = sprintf(
67+
'%s/%s/%s',
68+
$this->getVendorDirectoryFromComposer(),
69+
$this->getComposerName(),
70+
$this->getComposerSource(),
71+
);
72+
7173
$filesystem = new Filesystem();
7274

73-
if ($this instanceof CopyFromNpmInterface && (!$copySourcePath || !$filesystem->exists($copySourcePath))) {
75+
if (!$filesystem->exists($copySourcePath) && $this instanceof CopyFromUnpkgInterface) {
7476
if ($this->event->getIO()->isVerbose()) {
75-
$this->event->getIO()->write(sprintf('Fallback to NPM for %s', $this->getNpmPackageName()));
77+
$this->event->getIO()->write(sprintf(
78+
'Fallback to Unpkg %s for %s',
79+
$this->getUnpkgName(),
80+
$this->getComposerName(),
81+
));
7682
}
77-
$this->processNpmInstall();
83+
$this->processUnpkgInstall();
7884
return;
7985
}
8086

81-
if ($copySourcePath && $this instanceof CopyFromComposerInterface) {
82-
$finder = Finder::create()
83-
->files()
84-
->in($copySourcePath)
85-
->name($this->getFilesByName());
87+
$finder = Finder::create()
88+
->files()
89+
->in($copySourcePath)
90+
->name($this->getComposerFiles());
8691

87-
foreach ($finder as $file) {
88-
$copySource = $file->getPathname();
89-
$copytarget = $this->getCopyTargetPath() . '/' . $file->getRelativePathname();
92+
foreach ($finder as $file) {
93+
$copySource = $file->getPathname();
94+
$copytarget = $this->getCopyTargetPath() . '/' . $file->getRelativePathname();
9095

91-
try {
92-
$filesystem->copy($copySource, $copytarget);
93-
if ($this->event->getIO()->isVeryVerbose()) {
94-
$this->event->getIO()->write(sprintf('Copy %s to %s', $copySource, $copytarget));
95-
}
96-
} catch (IOException $IOException) {
97-
$this->event->getIO()->write($IOException->getMessage());
96+
try {
97+
$filesystem->copy($copySource, $copytarget);
98+
if ($this->event->getIO()->isVeryVerbose()) {
99+
$this->event->getIO()->write(sprintf('Copy %s to %s', $copySource, $copytarget));
98100
}
101+
} catch (IOException $IOException) {
102+
$this->event->getIO()->write($IOException->getMessage());
99103
}
100104
}
101105
}
102106

103107
/**
104-
* Copy files as defined in NPM copy-plugin
108+
* Copy files as defined in Unpkg copy-plugin
105109
*/
106-
public function processNpmInstall(): void
110+
public function processUnpkgInstall(): void
107111
{
108-
if ($this instanceof CopyFromNpmInterface) {
109-
if (!$this->getVersion()) {
110-
return;
111-
}
112+
if (!$this instanceof CopyFromUnpkgInterface) {
113+
return;
114+
}
115+
116+
if (!$this->getUnpkgVersion()) {
117+
return;
118+
}
112119

113-
$sourcePath = $this->getNpmFilePath();
120+
$sourcePath = $this->getUnpkSourcePath();
114121

115-
if ($this->event->getIO()->isVerbose()) {
116-
$this->event->getIO()->write(sprintf(
117-
'Trying to download %s %s from %s',
118-
$this->getNpmPackageName(),
119-
$this->getVersion(),
120-
$sourcePath,
121-
));
122-
}
122+
if ($this->event->getIO()->isVerbose()) {
123+
$this->event->getIO()->write(sprintf(
124+
'Trying to download %s %s from %s',
125+
$this->getUnpkgName(),
126+
$this->getUnpkgVersion(),
127+
$sourcePath,
128+
));
129+
}
123130

124-
foreach ($this->getNpmPackageFiles() as $fileName) {
125-
$sourceFilePath = $sourcePath . $fileName;
126-
try {
127-
$content = file_get_contents($sourceFilePath);
128-
} catch (\ErrorException $errorException) {
129-
$this->event->getIO()->write($errorException->getMessage());
130-
return;
131-
}
131+
foreach ($this->getUnpkgFiles() as $fileName) {
132+
$sourceFilePath = $sourcePath . $fileName;
133+
try {
134+
$content = file_get_contents($sourceFilePath);
135+
} catch (\ErrorException $errorException) {
136+
$this->event->getIO()->write($errorException->getMessage());
137+
return;
138+
}
132139

133-
if (!$content) {
134-
$this->event->getIO()->write(sprintf('Could not read from %s', $sourceFilePath));
135-
return;
136-
}
140+
if (!$content) {
141+
$this->event->getIO()->write(sprintf('Could not read from %s', $sourceFilePath));
142+
return;
143+
}
137144

138-
try {
139-
$filesystem = new Filesystem();
140-
$targetFilePath = $this->getCopyTargetPath() . '/' . $fileName;
141-
$filesystem->dumpFile($targetFilePath, $content);
142-
if ($this->event->getIO()->isVerbose()) {
143-
$this->event->getIO()->write(sprintf('Added %s', $fileName));
144-
}
145-
} catch (IOException $IOException) {
146-
$this->event->getIO()->write($IOException->getMessage());
147-
return;
145+
try {
146+
$filesystem = new Filesystem();
147+
$targetFilePath = $this->getCopyTargetPath() . '/' . $fileName;
148+
$filesystem->dumpFile($targetFilePath, $content);
149+
if ($this->event->getIO()->isVerbose()) {
150+
$this->event->getIO()->write(sprintf('Added %s', $targetFilePath));
148151
}
152+
} catch (IOException $IOException) {
153+
$this->event->getIO()->write($IOException->getMessage());
154+
return;
149155
}
150156
}
151157
}
152158

153159
public function getComposerPackage(): ?BasePackage
154160
{
155161
if ($this instanceof CopyFromComposerInterface) {
156-
$vendorName = $this->getComposerPackageName();
162+
$vendorName = $this->getComposerName();
157163
$module = $this->getInstalledComposerPackage($vendorName);
158164
if ($module) {
159165
return $module;
@@ -175,87 +181,20 @@ public function getComposerPackage(): ?BasePackage
175181
return null;
176182
}
177183

178-
/**
179-
* @return array<string, string>|null
180-
*/
181-
public function getNpmPackage(): ?array
182-
{
183-
if ($this instanceof CopyFromNpmInterface) {
184-
$vendorName = $this->getNpmPackageName();
185-
186-
$locker = $this->event->getComposer()->getLocker();
187-
$repo = $locker->getLockedRepository();
188-
189-
$packages = $repo->getPackages();
190-
$packages[] = $this->event->getComposer()->getPackage();
191-
192-
foreach ($packages as $package) {
193-
/** @var array<string, string|array<string>> $extra */
194-
$extra = $package->getExtra();
195-
196-
if (!isset($extra[self::EXTRA_NPM_PACKAGES][$vendorName])) {
197-
continue;
198-
}
199-
200-
$packageData = $extra[self::EXTRA_NPM_PACKAGES][$vendorName];
201-
202-
if (!is_array($packageData)) {
203-
throw new Exception(sprintf('Configuration is invalid for %s', $vendorName));
204-
}
205-
206-
if (array_key_exists('version', $packageData) && is_string($packageData['version'])) {
207-
$this->setInstalledNpmPackage($vendorName, $packageData);
208-
if ($this->event->getIO()->isVerbose()) {
209-
$this->event->getIO()->write(sprintf(
210-
'%s found with version %s',
211-
$vendorName,
212-
$packageData['version'],
213-
));
214-
}
215-
return $this->getInstalledNpmPackage($vendorName);
216-
}
217-
}
218-
}
219-
return null;
220-
}
221-
222184
/**
223185
* Get path to NPM dist
224186
*/
225-
protected function getNpmFilePath(): string
187+
protected function getUnpkSourcePath(): string
226188
{
227-
if ($this instanceof CopyFromNpmInterface) {
189+
if ($this instanceof CopyFromUnpkgInterface) {
228190
$search = ['{{package}}', '{{version}}'];
229-
$replace = [$this->getNpmPackageName(), $this->getVersion()];
230-
return str_replace($search, $replace, CopyFromNpmInterface::NPM_FALLBACK_URL);
191+
$replace = [$this->getUnpkgName(), $this->getUnpkgVersion()];
192+
$path = str_replace($search, $replace, CopyFromUnpkgInterface::UNPKG_URL);
193+
return $path . ($this->getUnpkgSource() ? $this->getUnpkgSource() . '/' : '');
231194
}
232195
return '';
233196
}
234197

235-
/**
236-
* Get package version
237-
*/
238-
private function getVersion(): string
239-
{
240-
if (is_null($this->version)) {
241-
$version = '';
242-
switch (true) {
243-
case $this instanceof CopyFromComposerInterface:
244-
$package = $this->getComposerPackage();
245-
$version = $package ? $package->getPrettyVersion() : '';
246-
break;
247-
case $this instanceof CopyFromNpmInterface:
248-
$package = $this->getNpmPackage();
249-
$version = $package ? $package['version'] : '';
250-
break;
251-
}
252-
253-
$this->version = ltrim($version, 'v');
254-
}
255-
256-
return $this->version;
257-
}
258-
259198
/**
260199
* Get current working directory
261200
*/
@@ -294,20 +233,7 @@ protected function getMageRootDirectoryFromComposer(): string
294233
return $magentoRootDir;
295234
}
296235

297-
protected function getCopySourcePath(): string
298-
{
299-
if ($this instanceof CopyFromComposerInterface) {
300-
return sprintf(
301-
'%s/%s/%s',
302-
$this->getVendorDirectoryFromComposer(),
303-
$this->getComposerPackageName(),
304-
$this->getCopySource(),
305-
);
306-
}
307-
return '';
308-
}
309-
310-
protected function getCopyTargetPath(): string
236+
private function getCopyTargetPath(): string
311237
{
312238
return sprintf(
313239
'%s/%s%s',
@@ -326,20 +252,4 @@ private function setInstalledComposerPackage(string $vendorName, BasePackage $pa
326252
{
327253
$this->installedComposerPackages[$vendorName] = $package;
328254
}
329-
330-
/**
331-
* @return array<string, string>|null
332-
*/
333-
protected function getInstalledNpmPackage(string $vendorName): ?array
334-
{
335-
return $this->installedNpmPackages[$vendorName] ?? null;
336-
}
337-
338-
/**
339-
* @param array<string, string> $package
340-
*/
341-
private function setInstalledNpmPackage(string $vendorName, array $package): void
342-
{
343-
$this->installedNpmPackages[$vendorName] = $package;
344-
}
345255
}

src/OpenMage/ComposerPlugin/Copy/CopyFromComposerInterface.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@ interface CopyFromComposerInterface
1919
/**
2020
* Npm name
2121
*/
22-
public function getComposerPackageName(): string;
22+
public function getComposerName(): string;
2323

24-
public function getCopySource(): string;
24+
public function getComposerSource(): string;
2525

2626
/**
2727
* @return string[]
2828
*/
29-
public function getFilesByName(): array;
30-
31-
public function processComposerInstall(): void;
29+
public function getComposerFiles(): array;
3230
}

0 commit comments

Comments
 (0)