|
1 | 1 | import { ReleaseType } from 'semver'; |
2 | 2 | import { Updater } from '../interface'; |
3 | | -import { calculateNextVersion, FileHandler } from '../utils'; |
| 3 | +import { calculateNextVersion, ManifestParser } from '../utils'; |
4 | 4 |
|
5 | 5 | export class PHPUpdater implements Updater { |
6 | 6 | platform = 'php'; |
| 7 | + private manifestPath: string | null = null; |
7 | 8 |
|
8 | 9 | canHandle(): boolean { |
9 | | - return ( |
10 | | - FileHandler.fileExists('composer.json') || |
11 | | - FileHandler.fileExists('VERSION') || |
12 | | - FileHandler.fileExists('version.php') || |
13 | | - FileHandler.fileExists('config.php') |
14 | | - ); |
| 10 | + this.manifestPath = ManifestParser.detectManifest([ |
| 11 | + 'composer.json', |
| 12 | + 'VERSION', |
| 13 | + 'version.php', |
| 14 | + 'config.php', |
| 15 | + ]); |
| 16 | + return this.manifestPath !== null; |
15 | 17 | } |
16 | 18 |
|
17 | 19 | getCurrentVersion(): string | null { |
18 | | - // composer.json |
19 | | - if (FileHandler.fileExists('composer.json')) { |
20 | | - const composer = JSON.parse(FileHandler.readFile('composer.json')); |
21 | | - return composer.version || null; |
22 | | - } |
23 | | - |
24 | | - // VERSION file |
25 | | - if (FileHandler.fileExists('VERSION')) { |
26 | | - return FileHandler.readFile('VERSION').trim(); |
27 | | - } |
| 20 | + if (!this.manifestPath) return null; |
28 | 21 |
|
29 | | - // version.php |
30 | | - if (FileHandler.fileExists('version.php')) { |
31 | | - const content = FileHandler.readFile('version.php'); |
32 | | - const match = content.match(/['"]([\d.]+)['"]/); |
33 | | - return match ? match[1] : null; |
| 22 | + switch (this.manifestPath) { |
| 23 | + case 'composer.json': |
| 24 | + return ManifestParser.getVersion(this.manifestPath, 'json', { |
| 25 | + jsonPath: ['version'], |
| 26 | + }); |
| 27 | + case 'VERSION': |
| 28 | + return ManifestParser.getVersion(this.manifestPath, 'regex', { |
| 29 | + regex: /^([\d.]+)$/m, // Matches the entire content as version |
| 30 | + }); |
| 31 | + case 'version.php': |
| 32 | + return ManifestParser.getVersion(this.manifestPath, 'regex', { |
| 33 | + regex: /['"]([\d.]+)['']/, // Matches version in quotes |
| 34 | + }); |
| 35 | + case 'config.php': |
| 36 | + return ManifestParser.getVersion(this.manifestPath, 'regex', { |
| 37 | + regex: /'version'\s*=>\s*'([\d.]+)'/, // Matches version in config array |
| 38 | + }); |
| 39 | + default: |
| 40 | + return null; |
34 | 41 | } |
35 | | - |
36 | | - // config.php |
37 | | - if (FileHandler.fileExists('config.php')) { |
38 | | - const content = FileHandler.readFile('config.php'); |
39 | | - const match = content.match(/'version'\s*=>\s*'([\d.]+)'/); |
40 | | - return match ? match[1] : null; |
41 | | - } |
42 | | - |
43 | | - return null; |
44 | 42 | } |
45 | 43 |
|
46 | 44 | bumpVersion(releaseType: ReleaseType): string { |
47 | | - const version = this.getCurrentVersion() || '0.1.0'; |
48 | | - const newVersion = calculateNextVersion(version, releaseType); |
| 45 | + if (!this.manifestPath) throw new Error('PHP manifest file not found'); |
| 46 | + const current = this.getCurrentVersion(); |
| 47 | + if (!current) throw new Error('PHP version not found'); |
49 | 48 |
|
50 | | - // composer.json |
51 | | - if (FileHandler.fileExists('composer.json')) { |
52 | | - const composer = JSON.parse(FileHandler.readFile('composer.json')); |
53 | | - composer.version = newVersion; |
54 | | - FileHandler.writeFile('composer.json', JSON.stringify(composer, null, 2)); |
55 | | - return newVersion; |
56 | | - } |
57 | | - |
58 | | - // VERSION file |
59 | | - if (FileHandler.fileExists('VERSION')) { |
60 | | - FileHandler.writeFile('VERSION', newVersion); |
61 | | - return newVersion; |
62 | | - } |
63 | | - |
64 | | - // version.php |
65 | | - if (FileHandler.fileExists('version.php')) { |
66 | | - const content = FileHandler.readFile('version.php'); |
67 | | - const updated = content.replace(/(['"])[\d.]+(['"])/, `$1${newVersion}$2`); |
68 | | - FileHandler.writeFile('version.php', updated); |
69 | | - return newVersion; |
70 | | - } |
| 49 | + const newVersion = calculateNextVersion(current, releaseType); |
71 | 50 |
|
72 | | - // config.php |
73 | | - if (FileHandler.fileExists('config.php')) { |
74 | | - const content = FileHandler.readFile('config.php'); |
75 | | - const updated = content.replace(/'version'\s*=>\s*'[\d.]+'/, `'version' => '${newVersion}'`); |
76 | | - FileHandler.writeFile('config.php', updated); |
77 | | - return newVersion; |
| 51 | + switch (this.manifestPath) { |
| 52 | + case 'composer.json': |
| 53 | + ManifestParser.updateVersion(this.manifestPath, newVersion, 'json', { |
| 54 | + jsonPath: ['version'], |
| 55 | + }); |
| 56 | + break; |
| 57 | + case 'VERSION': |
| 58 | + ManifestParser.updateVersion(this.manifestPath, newVersion, 'regex', { |
| 59 | + regexReplace: /^([\d.]+)$/m, |
| 60 | + }); |
| 61 | + break; |
| 62 | + case 'version.php': |
| 63 | + ManifestParser.updateVersion(this.manifestPath, newVersion, 'regex', { |
| 64 | + regexReplace: /(['"])[\d.]+(['"])/, |
| 65 | + }); |
| 66 | + break; |
| 67 | + case 'config.php': |
| 68 | + ManifestParser.updateVersion(this.manifestPath, newVersion, 'regex', { |
| 69 | + regexReplace: /'version'\s*=>\s*'[\d.]+'/, // Matches version in config array |
| 70 | + }); |
| 71 | + break; |
| 72 | + default: |
| 73 | + throw new Error(`Unsupported PHP manifest file: ${this.manifestPath}`); |
78 | 74 | } |
79 | 75 |
|
80 | 76 | return newVersion; |
|
0 commit comments