Skip to content

Commit 1c87ff6

Browse files
committed
refactor(android): follow the DI and options guidelines in the new plugin code
The plugin mirror and the native file hashes memo are internal contracts without a name, registered lazily and injected by token. The Gradle args service reads hostProjectPath from the build data instead of the process-wide options.
1 parent ea89ed3 commit 1c87ff6

16 files changed

Lines changed: 179 additions & 136 deletions

‎lib/bootstrap.ts‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import { provideLazy } from "./common/di";
12
import { injector } from "./common/yok";
23
import { registerBuiltInCommand } from "./common/services/command-definition-adapter";
34
import type { fontsCommandDefinition } from "./commands/fonts";
5+
import { AndroidPluginMirrorService } from "./contracts/internal/android-plugin-mirror-service";
6+
import { NativeFileHashesMemo } from "./contracts/internal/native-file-hashes-memo";
47

58
require("./common/bootstrap");
69

@@ -48,9 +51,13 @@ injector.require(
4851
"gradleBuildArgsService",
4952
"./services/android/gradle-build-args-service",
5053
);
51-
injector.require(
52-
"androidPluginMirrorService",
53-
"./services/android/android-plugin-mirror-service",
54+
injector.register(
55+
provideLazy(
56+
AndroidPluginMirrorService,
57+
() =>
58+
require("./services/android/android-plugin-mirror-service")
59+
.AndroidPluginMirrorServiceImpl,
60+
),
5461
);
5562
injector.require(
5663
"androidBundleToolService",
@@ -657,7 +664,13 @@ injector.requirePublic(
657664
);
658665

659666
injector.require("filesHashService", "./services/files-hash-service");
660-
injector.require("nativeFileHashesMemo", "./services/native-file-hashes-memo");
667+
injector.register(
668+
provideLazy(
669+
NativeFileHashesMemo,
670+
() =>
671+
require("./services/native-file-hashes-memo").NativeFileHashesMemoImpl,
672+
),
673+
);
661674
injector.require("logParserService", "./services/log-parser-service");
662675
injector.require(
663676
"iOSDebuggerPortService",
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Contract } from "../../common/di/contract";
2+
import type { IAndroidPluginMirror } from "../../definitions/android-plugin-migrator";
3+
4+
/**
5+
* Keeps CLI-owned copies of plugins' Android files.
6+
*/
7+
@Contract()
8+
export abstract class AndroidPluginMirrorService {
9+
/**
10+
* Brings `<mirrorsRoot>/<dir>` in line with each plugin's package.json and
11+
* platforms/android, and removes mirrors of plugins not in `mirrors`.
12+
* @returns The names of the plugins whose mirror changed.
13+
*/
14+
abstract syncMirrors(
15+
mirrorsRoot: string,
16+
mirrors: IAndroidPluginMirror[],
17+
): string[];
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Contract } from "../../common/di/contract";
2+
3+
/**
4+
* sha1 hashes of plugins' native files computed earlier in the same prepare,
5+
* so a later step does not read the same files again. Cleared when a prepare
6+
* starts; each hash is served once.
7+
*/
8+
@Contract()
9+
export abstract class NativeFileHashesMemo {
10+
abstract clear(): void;
11+
12+
abstract remember(filePath: string, sha1: string): void;
13+
14+
abstract take(filePath: string): string | undefined;
15+
}

‎lib/definitions/android-plugin-migrator.d.ts‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,6 @@ interface IAndroidPluginMirror {
7878
excludedNativeFiles?: string[];
7979
}
8080

81-
interface IAndroidPluginMirrorService {
82-
/**
83-
* Brings `<mirrorsRoot>/<dir>` in line with each plugin's package.json and
84-
* platforms/android, and removes mirrors of plugins not in `mirrors`.
85-
* @returns The names of the plugins whose mirror changed.
86-
*/
87-
syncMirrors(mirrorsRoot: string, mirrors: IAndroidPluginMirror[]): string[];
88-
}
89-
9081
/**
9182
* Describes data required for building plugin for Android.
9283
* The data can be consumed in the buildAndroidPlugin hook.

‎lib/definitions/build.d.ts‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,11 @@ interface IiOSBuildData extends IBuildData {
2828
}
2929

3030
interface IAndroidBuildData
31-
extends IBuildData,
32-
IAndroidSigningData,
33-
IHasAndroidBundle {
31+
extends IBuildData, IAndroidSigningData, IHasAndroidBundle {
3432
gradleFlavor?: string;
3533
gradlePath?: string;
3634
gradleArgs?: string;
35+
hostProjectPath?: string;
3736
}
3837

3938
interface IAndroidSigningData {
@@ -57,35 +56,35 @@ interface IBuildDataService {
5756
interface IBuildArtifactsService {
5857
getAllAppPackages(
5958
buildOutputPath: string,
60-
validBuildOutputData: IValidBuildOutputData
59+
validBuildOutputData: IValidBuildOutputData,
6160
): IApplicationPackage[];
6261
getLatestAppPackagePath(
6362
platformData: IPlatformData,
64-
buildOutputOptions: IBuildOutputOptions
63+
buildOutputOptions: IBuildOutputOptions,
6564
): Promise<string>;
6665
copyLatestAppPackage(
6766
targetPath: string,
6867
platformData: IPlatformData,
69-
buildOutputOptions: IBuildOutputOptions
68+
buildOutputOptions: IBuildOutputOptions,
7069
): void;
7170
}
7271

7372
interface IBuildInfoFileService {
7473
getLocalBuildInfo(
7574
platformData: IPlatformData,
76-
buildData: IBuildData
75+
buildData: IBuildData,
7776
): IBuildInfo;
7877
getDeviceBuildInfo(
7978
device: Mobile.IDevice,
80-
projectData: IProjectData
79+
projectData: IProjectData,
8180
): Promise<IBuildInfo>;
8281
saveLocalBuildInfo(
8382
platformData: IPlatformData,
84-
buildInfoFileDirname: string
83+
buildInfoFileDirname: string,
8584
): void;
8685
saveDeviceBuildInfo(
8786
device: Mobile.IDevice,
8887
projectData: IProjectData,
89-
outputFilePath: string
88+
outputFilePath: string,
9089
): Promise<void>;
9190
}

‎lib/definitions/files-hash-service.d.ts‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,3 @@ interface IFilesHashService {
3333
newHashes: IStringDictionary,
3434
): boolean;
3535
}
36-
37-
/**
38-
* sha1 hashes of plugins' native files computed earlier in the same prepare,
39-
* so a later step does not read the same files again. Cleared when a prepare
40-
* starts; each hash is served once.
41-
*/
42-
interface INativeFileHashesMemo {
43-
clear(): void;
44-
remember(filePath: string, sha1: string): void;
45-
take(filePath: string): string | undefined;
46-
}

‎lib/services/android-project-service.ts‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,23 @@ import {
3939
} from "../common/declarations";
4040
import {
4141
IAndroidPluginBuildService,
42-
IAndroidPluginMirrorService,
4342
IPluginBuildOptions,
4443
} from "../definitions/android-plugin-migrator";
4544
import {
4645
createGradleFacts,
4746
getGradleFactsFilePath,
4847
serializeGradleFacts,
4948
} from "./android/gradle-facts";
50-
import {
51-
IFilesHashService,
52-
INativeFileHashesMemo,
53-
} from "../definitions/files-hash-service";
49+
import { IFilesHashService } from "../definitions/files-hash-service";
5450
import {
5551
IGradleCommandService,
5652
IGradleBuildService,
5753
} from "../definitions/gradle";
5854
import { IInjector } from "../common/definitions/yok";
55+
import { inject } from "../common/di";
5956
import { injector } from "../common/yok";
57+
import { AndroidPluginMirrorService } from "../contracts/internal/android-plugin-mirror-service";
58+
import { NativeFileHashesMemo } from "../contracts/internal/native-file-hashes-memo";
6059
import { INotConfiguredEnvOptions } from "../common/definitions/commands";
6160

6261
interface NativeDependency {
@@ -203,6 +202,9 @@ export class AndroidProjectService
203202
private static ANDROID_PLATFORM_NAME = "android";
204203
private static MIN_RUNTIME_VERSION_WITH_GRADLE = "1.5.0";
205204

205+
private androidPluginMirrorService = inject(AndroidPluginMirrorService);
206+
private nativeFileHashesMemo = inject(NativeFileHashesMemo);
207+
206208
constructor(
207209
private $androidToolsInfo: IAndroidToolsInfo,
208210
private $errors: IErrors,
@@ -220,9 +222,7 @@ export class AndroidProjectService
220222
private $gradleBuildService: IGradleBuildService,
221223
private $analyticsService: IAnalyticsService,
222224
private $nodeModulesDependenciesBuilder: INodeModulesDependenciesBuilder,
223-
private $androidPluginMirrorService: IAndroidPluginMirrorService,
224225
private $staticConfig: Config.IStaticConfig,
225-
private $nativeFileHashesMemo: INativeFileHashesMemo,
226226
) {
227227
super($fs, $projectDataService);
228228
}
@@ -929,7 +929,7 @@ export class AndroidProjectService
929929
projectData: IProjectData,
930930
nativeDependencyData: IDependencyData[],
931931
): IAndroidPluginLayout {
932-
this.$nativeFileHashesMemo.clear();
932+
this.nativeFileHashesMemo.clear();
933933
const platformDir = this.getAndroidPlatformDir(projectData);
934934
const pluginBuildLocation = this.getPluginBuildLocation(projectData);
935935
const mirrorsRoot = path.join(
@@ -982,7 +982,7 @@ export class AndroidProjectService
982982
}
983983

984984
const tempPluginDirPath = this.getTempPluginDirPath(projectData);
985-
const synced = this.$androidPluginMirrorService.syncMirrors(
985+
const synced = this.androidPluginMirrorService.syncMirrors(
986986
mirrorsRoot,
987987
nativeDependencyData.map((dep) => {
988988
const upstreamNativeDir = path.join(

0 commit comments

Comments
 (0)