Skip to content

Commit

Permalink
fix pub get/upgrade with cache for workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
alextekartik committed Jan 22, 2025
1 parent 49f210a commit 0ed06c4
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 22 deletions.
23 changes: 12 additions & 11 deletions lib/bin/pubget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:async';
import 'package:args/args.dart';
import 'package:dev_build/menu/menu_run_ci.dart';
import 'package:process_run/stdio.dart';
import 'package:tekartik_pub/bin/src/pub_workspace_cache.dart';
import 'package:tekartik_pub/bin/src/pubbin_utils.dart';
import 'package:tekartik_pub/io.dart';

Expand Down Expand Up @@ -57,6 +58,7 @@ Future main(List<String> arguments) async {
rest = ['.'];
}

initPubWorkspacesCache();
await pubGet(
rest,
PubGetOptions()
Expand All @@ -75,28 +77,27 @@ Future pubGet(List<String> directories, PubGetOptions options) async {
if (options.verbose == true) {
print('found package(s): $pkgPaths');
}
var offline = options.offline ?? false;
var futures = <Future>[];
for (final dir in pkgPaths) {
var pubIoPackage = PubIoPackage(dir);
await pubIoPackage.ready;
var isFlutterPub = pubIoPackage.dofPub == 'flutter pub';
final pkg = PubPackage(dir);
ProcessCmd cmd;
if (isFlutterPub) {
if (!isFlutterSupported) {

if (pubWorkspacesCache != null &&
(pubIoPackage.hasWorkspaceResolution || pubIoPackage.isWorkspace)) {
var root = await pubIoPackage.getWorkspaceRootPath();
var cache = PubWorkspaceCache(root, PubWorkspaceCacheAction.get, offline);
if (!pubWorkspacesCache!.cacheIfNeeded(cache)) {
continue;
}
cmd = FlutterCmd(['pub', 'get'])..workingDirectory = dir;
} else {
cmd = pkg.pubCmd(pubGetArgs(
offline: options.offline, packagesDir: options.packagesDir));
}

var future = () async {
await shellStdioLinesGrouper.runZoned(() async {
try {
await runCmd(cmd, options: options);
await pubIoPackage.pubGet(offline: offline);
} catch (e) {
stderr.writeln('Error in $pkg: $e');
stderr.writeln('Error in $pubIoPackage: $e');
if (options.ignoreErrors ?? false) {
// ok
} else {
Expand Down
21 changes: 10 additions & 11 deletions lib/bin/pubupgrade.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'dart:async';
import 'package:args/args.dart';
import 'package:dev_build/menu/menu_run_ci.dart';
import 'package:process_run/stdio.dart';
import 'package:tekartik_pub/bin/src/pub_workspace_cache.dart';
import 'package:tekartik_pub/bin/src/pubbin_utils.dart';
import 'package:tekartik_pub/io.dart';

Expand Down Expand Up @@ -50,7 +51,7 @@ Future main(List<String> arguments) async {
if (rest.isEmpty) {
rest = ['.'];
}

initPubWorkspacesCache();
await pubUpgrade(
rest,
PubGetOptions()
Expand All @@ -64,28 +65,26 @@ Future main(List<String> arguments) async {
}

Future pubUpgrade(List<String> directories, PubGetOptions options) async {
var offline = options.offline ?? false;
final pkgPaths = await recursivePubPath(directories);
var futures = <Future>[];
for (final dir in pkgPaths) {
final pkg = PubPackage(dir);
ProcessCmd cmd;
var pubIoPackage = PubIoPackage(dir);
await pubIoPackage.ready;
var isFlutterPub = pubIoPackage.dofPub == 'flutter pub';
if (isFlutterPub) {
if (!isFlutterSupported) {

if (pubWorkspacesCache != null &&
(pubIoPackage.hasWorkspaceResolution || pubIoPackage.isWorkspace)) {
var root = await pubIoPackage.getWorkspaceRootPath();
var cache = PubWorkspaceCache(root, PubWorkspaceCacheAction.get, offline);
if (!pubWorkspacesCache!.cacheIfNeeded(cache)) {
continue;
}
cmd = FlutterCmd(['pub', 'upgrade'])..workingDirectory = dir;
} else {
cmd = pkg.pubCmd(pubUpgradeArgs(
offline: options.offline, packagesDir: options.packagesDir));
}

var future = () async {
await shellStdioLinesGrouper.runZoned(() async {
try {
await runCmd(cmd, options: options);
await pubIoPackage.pubUpgrade(offline: offline);
} catch (e) {
stderr.writeln('Error in $pkg: $e');
if (options.ignoreErrors ?? false) {
Expand Down
60 changes: 60 additions & 0 deletions lib/bin/src/pub_workspace_cache.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
enum PubWorkspaceCacheAction { get, upgrade, downgrade }

/// Last action done on a workspace, invalide others.
class PubWorkspaceCache {
final String workspaceRoot;
final bool offline;
final PubWorkspaceCacheAction action;

PubWorkspaceCache(this.workspaceRoot, this.action, this.offline);

@override
int get hashCode => workspaceRoot.hashCode;

@override
bool operator ==(Object other) {
if (other is PubWorkspaceCache) {
return workspaceRoot == other.workspaceRoot &&
offline == other.offline &&
action == other.action;
}
return false;
}
}

/// Workspace cache
abstract class PubWorkspacesCache {
factory PubWorkspacesCache() => _PubWorkspacesCache();

/// Returns true if the cache was updated (i.e. must run, next call will return false)
bool cacheIfNeeded(PubWorkspaceCache cache);
PubWorkspaceCache? getWorkspaceCache(String workspaceRoot);
}

class _PubWorkspacesCache implements PubWorkspacesCache {
final _map = <String, PubWorkspaceCache>{};

_PubWorkspacesCache();

@override
PubWorkspaceCache? getWorkspaceCache(String workspaceRoot) {
return _map[workspaceRoot];
}

@override
bool cacheIfNeeded(PubWorkspaceCache cache) {
var existing = _map[cache.workspaceRoot];
if (existing != cache) {
_map[cache.workspaceRoot] = cache;
return true;
}
return false;
}
}

PubWorkspacesCache? pubWorkspacesCache;

/// Internal only use for run_ci binary for now
void initPubWorkspacesCache() {
pubWorkspacesCache = PubWorkspacesCache();
}

0 comments on commit 0ed06c4

Please sign in to comment.