Skip to content

Commit

Permalink
fix: prevent provided deps from being removed as rogue deps
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyashsaitwal committed Jan 28, 2024
1 parent 2d41af5 commit dddd8ea
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
21 changes: 11 additions & 10 deletions lib/src/commands/deps/sync.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class SyncSubCommand extends Command<int> {
// Clear all the cache if force is used.
if (useForce) {
if (!onlyExtDeps) {
await libService.providedDepsBox.clear();
await libService.ai2ProvidedDepsBox.clear();
await libService.buildLibsBox.clear();
}
if (!onlyDevDeps && config != null) {
Expand Down Expand Up @@ -126,7 +126,7 @@ class SyncSubCommand extends Command<int> {
try {
await Future.wait([
sync(
cacheBox: libService.providedDepsBox,
cacheBox: libService.ai2ProvidedDepsBox,
coordinates: {Scope.compile: ai2ProvidedDepsToFetch},
downloadSources: true,
excludeCoordinates: ['com.google.android:android:2.1.2'],
Expand All @@ -147,7 +147,7 @@ class SyncSubCommand extends Command<int> {
'android-$androidPlatformSdkVersion.jar',
'kawa-1.11-modified.jar',
'physicaloid-library.jar'
], libService.providedDepsBox),
], libService.ai2ProvidedDepsBox),
]);
_lgr.stopTask();
} else if (!onlyExtDeps) {
Expand Down Expand Up @@ -179,19 +179,19 @@ class SyncSubCommand extends Command<int> {
final timestampBox = await Hive.openLazyBox<DateTime>(timestampBoxName);

final extensionDeps = await libService.extensionDependencies(config);
final needSync = await extensionDepsNeedSync(timestampBox, extensionDeps);
final needSync = await _doProjectDepsNeedSync(timestampBox, extensionDeps);
if (useForce || (!onlyDevDeps && needSync)) {
_lgr.startTask('Syncing project dependencies');

final extDepCoords = config.dependencies
.where((el) => !el.endsWith('.jar') && !el.endsWith('.aar'));
final extProvidedDepCoord = config.providedDependencies
final extProvidedDepCoords = config.providedDependencies
.where((el) => !el.endsWith('.jar') && !el.endsWith('.aar'));

try {
await sync(
cacheBox: libService.extensionDepsBox!,
coordinates: {Scope.provided: extProvidedDepCoord},
coordinates: {Scope.provided: extProvidedDepCoords},
repositories: config.repositories,
downloadSources: true,
);
Expand All @@ -210,7 +210,8 @@ class SyncSubCommand extends Command<int> {
_lgr.stopTask(false);
return 1;
}
await _removeRogueDeps(extDepCoords, libService.extensionDepsBox!);
await _removeRogueDeps({...extDepCoords, ...extProvidedDepCoords},
libService.extensionDepsBox!);
_lgr.stopTask();
} else if (!onlyDevDeps) {
_lgr
Expand Down Expand Up @@ -245,14 +246,14 @@ class SyncSubCommand extends Command<int> {
return 0;
}

static Future<bool> extensionDepsNeedSync(LazyBox<DateTime> timestampBox,
List<Artifact> extensionDependencies) async {
static Future<bool> _doProjectDepsNeedSync(
LazyBox<DateTime> timestampBox, List<Artifact> projectDeps) async {
// Re-fetch deps if they are outdated, ie, if the config file is modified
// or if the dep artifacts are missing
final configFileModified = (await timestampBox.get(configTimestampKey))
?.isBefore(_fs.configFile.lastModifiedSync()) ??
true;
final isAnyDepMissing = extensionDependencies.any((el) =>
final isAnyDepMissing = projectDeps.any((el) =>
!el.artifactFile.endsWith('.pom') &&
!el.artifactFile.asFile().existsSync());
return configFileModified || isAnyDepMissing;
Expand Down
18 changes: 8 additions & 10 deletions lib/src/services/lib_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ class LibService {
}
}

late final LazyBox<Artifact> providedDepsBox;
late final LazyBox<Artifact> ai2ProvidedDepsBox;
late final LazyBox<Artifact> buildLibsBox;
late final LazyBox<Artifact>? extensionDepsBox;

static Future<LibService> instantiate() async {
final instance = LibService._();
instance.providedDepsBox = await Hive.openLazyBox<Artifact>(
instance.ai2ProvidedDepsBox = await Hive.openLazyBox<Artifact>(
providedDepsBoxName,
path: p.join(_fs.rushHomeDir.path, 'cache'),
);
Expand Down Expand Up @@ -95,7 +95,7 @@ class LibService {

if (config == null) {
return [
...await _retrieveArtifactsFromBox(providedDepsBox),
...await _retrieveArtifactsFromBox(ai2ProvidedDepsBox),
...local,
];
}
Expand All @@ -119,9 +119,8 @@ class LibService {
});

return [
...await _retrieveArtifactsFromBox(providedDepsBox),
...(_requiredDeps(
await _retrieveArtifactsFromBox(extensionDepsBox!), extProvidedDeps)),
...await _retrieveArtifactsFromBox(ai2ProvidedDepsBox),
...extProvidedDeps,
...extLocalProvided,
...local,
];
Expand Down Expand Up @@ -149,12 +148,11 @@ class LibService {
}) async {
final allExtRemoteDeps = await _retrieveArtifactsFromBox(extensionDepsBox!);

final directRemoteDeps = config.dependencies
final projectDeps = config.dependencies
.map((el) =>
allExtRemoteDeps.firstWhereOrNull((dep) => dep.coordinate == el))
.whereNotNull();
final requiredRemoteDeps =
_requiredDeps(allExtRemoteDeps, directRemoteDeps);
final requiredDeps = _requiredDeps(allExtRemoteDeps, projectDeps);

final projectProvidedDeps = config.providedDependencies
.map((el) =>
Expand All @@ -180,7 +178,7 @@ class LibService {
.map((el) => el.artifactFile));

return [
...requiredRemoteDeps,
...requiredDeps,
if (includeLocal) ...localDeps,
if (includeAi2ProvidedDeps) ...await providedDependencies(config),
if (includeProjectProvidedDeps) ...requiredProjectProvidedDeps,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/utils/constants.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const providedDepsBoxName = 'provided-deps';
const providedDepsBoxName = 'ai2-provided-deps';
const buildLibsBoxName = 'build-libs';
const extensionDepsBoxName = 'deps';

Expand Down

0 comments on commit dddd8ea

Please sign in to comment.