Skip to content

Commit 8e2f90b

Browse files
committed
docs: Update CHANGELOG.md with DIP refactoring entry
refactor: Apply Dependency Inversion Principle for file handling - Updated `CHANGELOG.md` to include the entry for applying DIP. - Refactored `FileHandler` to be a regular class, allowing it to be instantiated. - Modified `ManifestParser` to accept `FileHandler` as a dependency in its constructor. - Updated all updater implementations to instantiate `ManifestParser` with a `FileHandler` instance, adhering to DIP.
1 parent 947f84c commit 8e2f90b

12 files changed

Lines changed: 217 additions & 103 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ All notable changes for each version of the Ambient Music extension.
77
## v0.7.2 2025 08 24
88

99
- refactor: Moved `UpdaterRegistry` to `src/registry` for better organization and updated all relevant imports.
10+
- refactor: Applied Dependency Inversion Principle (DIP) by passing `FileHandler` as a dependency to `ManifestParser`, and `ManifestParser` as a dependency to updaters. This improves testability and flexibility.
1011

1112
## v0.7.1 2025 08 24
1213

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ To use this action in your workflow, add the following step:
2222
2323
## Inputs
2424
25-
| Name | Description | Default |
26-
| -------------- | -------------------------------------------------- | ------- |
27-
| `release_type` | Select the version bump type (patch, minor, major) | `patch` |
28-
| `git_tag` | Whether to create a Git tag after bump | `true` |
25+
| Name | Description | Default |
26+
| ----------------- | --------------------------------------------------------------------------------------------------------------------------------- | ------- |
27+
| `release_type` | Select the version bump type (patch, minor, major) | `patch` |
28+
| `git_tag` | Whether to create a Git tag after bump | `true` |
2929
| `target_platform` | Explicitly specify the platform to update (e.g., `node`, `python`). If not provided, the platform will be detected automatically. | `''` |
3030

3131
### Explicit Platform Targeting (`target_platform`)

dist/index.js

Lines changed: 119 additions & 53 deletions
Large diffs are not rendered by default.

src/updaters/dockerUpdater.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
import { ReleaseType } from 'semver';
22
import { UpdaterInterface } from '../interface';
3-
import { calculateNextVersion, ManifestParser } from '../utils';
3+
import { calculateNextVersion, ManifestParser, FileHandler } from '../utils';
44

55
export class DockerUpdater implements UpdaterInterface {
66
platform = 'docker';
77
private manifestPath: string | null = null;
8+
private manifestParser: ManifestParser;
9+
10+
constructor() {
11+
const fileHandler = new FileHandler();
12+
this.manifestParser = new ManifestParser(fileHandler);
13+
}
814

915
canHandle(): boolean {
10-
this.manifestPath = ManifestParser.detectManifest(['Dockerfile']);
16+
this.manifestPath = this.manifestParser.detectManifest(['Dockerfile']);
1117
return this.manifestPath !== null;
1218
}
1319

1420
getCurrentVersion(): string | null {
1521
if (!this.manifestPath) return null;
16-
return ManifestParser.getVersion(this.manifestPath, 'regex', {
22+
return this.manifestParser.getVersion(this.manifestPath, 'regex', {
1723
regex: /LABEL version="([^"]+)"/,
1824
});
1925
}
@@ -24,7 +30,7 @@ export class DockerUpdater implements UpdaterInterface {
2430
if (!current) throw new Error('Docker version not found');
2531

2632
const newVersion = calculateNextVersion(current, releaseType);
27-
ManifestParser.updateVersion(this.manifestPath, newVersion, 'regex', {
33+
this.manifestParser.updateVersion(this.manifestPath, newVersion, 'regex', {
2834
regexReplace: /LABEL version="[^"]+"/,
2935
});
3036

src/updaters/goUpdater.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
import { ReleaseType } from 'semver';
22
import { UpdaterInterface } from '../interface';
3-
import { calculateNextVersion, ManifestParser } from '../utils';
3+
import { calculateNextVersion, ManifestParser, FileHandler } from '../utils';
44

55
export class GoUpdater implements UpdaterInterface {
66
platform = 'go';
77
private manifestPath: string | null = null;
8+
private manifestParser: ManifestParser;
9+
10+
constructor() {
11+
const fileHandler = new FileHandler();
12+
this.manifestParser = new ManifestParser(fileHandler);
13+
}
814

915
canHandle(): boolean {
10-
this.manifestPath = ManifestParser.detectManifest(['go.mod']);
16+
this.manifestPath = this.manifestParser.detectManifest(['go.mod']);
1117
return this.manifestPath !== null;
1218
}
1319

1420
getCurrentVersion(): string | null {
1521
if (!this.manifestPath) return null;
16-
return ManifestParser.getVersion(this.manifestPath, 'regex', {
22+
return this.manifestParser.getVersion(this.manifestPath, 'regex', {
1723
regex: /module\s+.*\n.*v(\d+\.\d+\.\d+)/,
1824
});
1925
}
@@ -24,7 +30,7 @@ export class GoUpdater implements UpdaterInterface {
2430
if (!current) throw new Error('Go version not found');
2531

2632
const newVersion = calculateNextVersion(current, releaseType);
27-
ManifestParser.updateVersion(this.manifestPath, `v${newVersion}`, 'regex', {
33+
this.manifestParser.updateVersion(this.manifestPath, `v${newVersion}`, 'regex', {
2834
regexReplace: /v\d+\.\d+\.\d+/,
2935
});
3036

src/updaters/nodeUpdater.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
import { ReleaseType } from 'semver';
22
import { UpdaterInterface } from '../interface';
3-
import { calculateNextVersion, ManifestParser } from '../utils';
3+
import { calculateNextVersion, ManifestParser, FileHandler } from '../utils';
44

55
export class NodeUpdater implements UpdaterInterface {
66
platform = 'node';
77
private manifestPath: string | null = null;
8+
private manifestParser: ManifestParser;
9+
10+
constructor() {
11+
const fileHandler = new FileHandler();
12+
this.manifestParser = new ManifestParser(fileHandler);
13+
}
814

915
canHandle(): boolean {
10-
this.manifestPath = ManifestParser.detectManifest(['package.json']);
16+
this.manifestPath = this.manifestParser.detectManifest(['package.json']);
1117
return this.manifestPath !== null;
1218
}
1319

1420
getCurrentVersion(): string | null {
1521
if (!this.manifestPath) return null;
16-
return ManifestParser.getVersion(this.manifestPath, 'json', {
22+
return this.manifestParser.getVersion(this.manifestPath, 'json', {
1723
jsonPath: ['version'],
1824
});
1925
}
@@ -24,7 +30,7 @@ export class NodeUpdater implements UpdaterInterface {
2430
if (!current) throw new Error('Node version not found');
2531

2632
const newVersion = calculateNextVersion(current, releaseType);
27-
ManifestParser.updateVersion(this.manifestPath, newVersion, 'json', {
33+
this.manifestParser.updateVersion(this.manifestPath, newVersion, 'json', {
2834
jsonPath: ['version'],
2935
});
3036

src/updaters/phpUpdater.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import { ReleaseType } from 'semver';
22
import { UpdaterInterface } from '../interface';
3-
import { calculateNextVersion, ManifestParser } from '../utils';
3+
import { calculateNextVersion, ManifestParser, FileHandler } from '../utils';
44

55
export class PHPUpdater implements UpdaterInterface {
66
platform = 'php';
77
private manifestPath: string | null = null;
8+
private manifestParser: ManifestParser;
9+
10+
constructor() {
11+
const fileHandler = new FileHandler();
12+
this.manifestParser = new ManifestParser(fileHandler);
13+
}
814

915
canHandle(): boolean {
10-
this.manifestPath = ManifestParser.detectManifest([
16+
this.manifestPath = this.manifestParser.detectManifest([
1117
'composer.json',
1218
'VERSION',
1319
'version.php',
@@ -21,19 +27,19 @@ export class PHPUpdater implements UpdaterInterface {
2127

2228
switch (this.manifestPath) {
2329
case 'composer.json':
24-
return ManifestParser.getVersion(this.manifestPath, 'json', {
30+
return this.manifestParser.getVersion(this.manifestPath, 'json', {
2531
jsonPath: ['version'],
2632
});
2733
case 'VERSION':
28-
return ManifestParser.getVersion(this.manifestPath, 'regex', {
34+
return this.manifestParser.getVersion(this.manifestPath, 'regex', {
2935
regex: /^([\d.]+)$/m, // Matches the entire content as version
3036
});
3137
case 'version.php':
32-
return ManifestParser.getVersion(this.manifestPath, 'regex', {
38+
return this.manifestParser.getVersion(this.manifestPath, 'regex', {
3339
regex: /['"]([\d.]+)['']/, // Matches version in quotes
3440
});
3541
case 'config.php':
36-
return ManifestParser.getVersion(this.manifestPath, 'regex', {
42+
return this.manifestParser.getVersion(this.manifestPath, 'regex', {
3743
regex: /'version'\s*=>\s*'([\d.]+)'/, // Matches version in config array
3844
});
3945
default:
@@ -50,22 +56,22 @@ export class PHPUpdater implements UpdaterInterface {
5056

5157
switch (this.manifestPath) {
5258
case 'composer.json':
53-
ManifestParser.updateVersion(this.manifestPath, newVersion, 'json', {
59+
this.manifestParser.updateVersion(this.manifestPath, newVersion, 'json', {
5460
jsonPath: ['version'],
5561
});
5662
break;
5763
case 'VERSION':
58-
ManifestParser.updateVersion(this.manifestPath, newVersion, 'regex', {
64+
this.manifestParser.updateVersion(this.manifestPath, newVersion, 'regex', {
5965
regexReplace: /^([\d.]+)$/m,
6066
});
6167
break;
6268
case 'version.php':
63-
ManifestParser.updateVersion(this.manifestPath, newVersion, 'regex', {
69+
this.manifestParser.updateVersion(this.manifestPath, newVersion, 'regex', {
6470
regexReplace: /(['"])[\d.]+(['"])/,
6571
});
6672
break;
6773
case 'config.php':
68-
ManifestParser.updateVersion(this.manifestPath, newVersion, 'regex', {
74+
this.manifestParser.updateVersion(this.manifestPath, newVersion, 'regex', {
6975
regexReplace: /'version'\s*=>\s*'[\d.]+'/, // Matches version in config array
7076
});
7177
break;

src/updaters/pythonUpdater.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import { ReleaseType } from 'semver';
22
import { UpdaterInterface } from '../interface';
3-
import { calculateNextVersion, ManifestParser } from '../utils';
3+
import { calculateNextVersion, ManifestParser, FileHandler } from '../utils';
44

55
export class PythonUpdater implements UpdaterInterface {
66
platform = 'python';
77
private manifestPath: string | null = null;
8+
private manifestParser: ManifestParser;
9+
10+
constructor() {
11+
const fileHandler = new FileHandler();
12+
this.manifestParser = new ManifestParser(fileHandler);
13+
}
814

915
canHandle(): boolean {
10-
this.manifestPath = ManifestParser.detectManifest(['pyproject.toml', 'setup.py']);
16+
this.manifestPath = this.manifestParser.detectManifest(['pyproject.toml', 'setup.py']);
1117
return this.manifestPath !== null;
1218
}
1319

@@ -16,11 +22,11 @@ export class PythonUpdater implements UpdaterInterface {
1622

1723
switch (this.manifestPath) {
1824
case 'pyproject.toml':
19-
return ManifestParser.getVersion(this.manifestPath, 'regex', {
25+
return this.manifestParser.getVersion(this.manifestPath, 'regex', {
2026
regex: /version\s*=\s*"([^"]+)"/,
2127
});
2228
case 'setup.py':
23-
return ManifestParser.getVersion(this.manifestPath, 'regex', {
29+
return this.manifestParser.getVersion(this.manifestPath, 'regex', {
2430
regex: /version\s*=\s*["']([^"']+)["']/, // Matches single or double quotes
2531
});
2632
default:
@@ -37,12 +43,12 @@ export class PythonUpdater implements UpdaterInterface {
3743

3844
switch (this.manifestPath) {
3945
case 'pyproject.toml':
40-
ManifestParser.updateVersion(this.manifestPath, newVersion, 'regex', {
46+
this.manifestParser.updateVersion(this.manifestPath, newVersion, 'regex', {
4147
regexReplace: /version\s*=\s*"[^"]+"/,
4248
});
4349
break;
4450
case 'setup.py':
45-
ManifestParser.updateVersion(this.manifestPath, newVersion, 'regex', {
51+
this.manifestParser.updateVersion(this.manifestPath, newVersion, 'regex', {
4652
regexReplace: /version\s*=\s*["'][^"']+["']/, // Matches single or double quotes
4753
});
4854
break;

src/updaters/rustUpdater.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
import { ReleaseType } from 'semver';
22
import { UpdaterInterface } from '../interface';
3-
import { calculateNextVersion, ManifestParser } from '../utils';
3+
import { calculateNextVersion, ManifestParser, FileHandler } from '../utils';
44

55
export class RustUpdater implements UpdaterInterface {
66
platform = 'rust';
77
private manifestPath: string | null = null;
8+
private manifestParser: ManifestParser;
9+
10+
constructor() {
11+
const fileHandler = new FileHandler();
12+
this.manifestParser = new ManifestParser(fileHandler);
13+
}
814

915
canHandle(): boolean {
10-
this.manifestPath = ManifestParser.detectManifest(['Cargo.toml']);
16+
this.manifestPath = this.manifestParser.detectManifest(['Cargo.toml']);
1117
return this.manifestPath !== null;
1218
}
1319

1420
getCurrentVersion(): string | null {
1521
if (!this.manifestPath) return null;
16-
return ManifestParser.getVersion(this.manifestPath, 'regex', {
22+
return this.manifestParser.getVersion(this.manifestPath, 'regex', {
1723
regex: /version\s*=\s*"([^"]+)"/,
1824
});
1925
}
@@ -24,7 +30,7 @@ export class RustUpdater implements UpdaterInterface {
2430
if (!current) throw new Error('Rust version not found');
2531

2632
const newVersion = calculateNextVersion(current, releaseType);
27-
ManifestParser.updateVersion(this.manifestPath, newVersion, 'regex', {
33+
this.manifestParser.updateVersion(this.manifestPath, newVersion, 'regex', {
2834
regexReplace: /version\s*=\s*"[^"]+"/,
2935
});
3036

src/utils/fileHandler.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import fs from 'fs';
22

33
export class FileHandler {
4-
static fileExists(filePath: string): boolean {
4+
constructor() {}
5+
6+
fileExists(filePath: string): boolean {
57
return fs.existsSync(filePath);
68
}
79

8-
static readFile(filePath: string): string {
10+
readFile(filePath: string): string {
911
return fs.readFileSync(filePath, 'utf8');
1012
}
1113

12-
static writeFile(filePath: string, content: string): void {
14+
writeFile(filePath: string, content: string): void {
1315
fs.writeFileSync(filePath, content);
1416
}
1517
}

0 commit comments

Comments
 (0)