Skip to content

Commit

Permalink
Use fvm flutter version for linking
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexios80 committed Apr 26, 2024
1 parent 3db3a17 commit 04a14a4
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 45 deletions.
29 changes: 29 additions & 0 deletions bin/commands.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'package:puby/command.dart';

abstract class Commands {
static final clean = Command(['clean'], parallel: true);
static final convenience = <String, List<Command>>{
'gen': [
Command([
'pub',
'run',
'build_runner',
'build',
'--delete-conflicting-outputs',
]),
],
'test': [
Command(['test']),
],
'clean': [
clean,
],
'mup': [
Command(['pub', 'upgrade', '--major-versions']),
],
'reset': [
clean,
Command(['pub', 'get']),
],
};
}
26 changes: 21 additions & 5 deletions bin/link.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';

import 'package:flutter_tools_task_queue/flutter_tools_task_queue.dart';
import 'package:puby/command.dart';
import 'package:puby/pens.dart';
import 'package:puby/project.dart';
import 'package:puby/time.dart';
Expand All @@ -11,27 +12,42 @@ import 'package:pub_hosted/src/entrypoint.dart';
import 'package:pub_hosted/src/package_name.dart';
import 'package:pub_hosted/src/source/cached.dart';

import 'projects.dart';

final _pubCache = SystemCache();

Future<void> linkDependencies(List<Project> projects) async {
Future<void> linkDependencies(
List<Project> projects, {
SolveType type = SolveType.get,
}) async {
final resolutionStopwatch = Stopwatch()..start();
print('\nResolving all dependencies...');
final dependencies = <PackageId>{};
final resolutionQueue = TaskQueue();
for (final project in projects) {
unawaited(
resolutionQueue.add(() async {
final entry = Entrypoint(project.path, _pubCache);
final resolved =
project.resolveWithCommand(Command(['pub', 'get', '--offline']));
if (resolved.exclude) return;

final flutterVersionOverride = resolved.getFlutterVersionOverride();

final entry = Entrypoint(resolved.path, _pubCache);
try {
final result = await resolveVersions(
SolveType.get,
type,
_pubCache,
entry.workspaceRoot,
sdkOverrides: {
if (flutterVersionOverride != null)
'flutter': flutterVersionOverride,
},
);
dependencies.addAll(result.packages);
print('Resolved dependencies for ${project.path}');
print('Resolved dependencies for ${resolved.path}');
} catch (e) {
print(redPen('Failed to resolve dependencies for ${project.path}'));
print(redPen('Failed to resolve dependencies for ${resolved.path}'));
print(redPen(e));
}
}),
Expand Down
14 changes: 11 additions & 3 deletions bin/projects.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:convert';
import 'dart:io';

import 'package:pub_semver/pub_semver.dart';
import 'package:pubspec_parse/pubspec_parse.dart';
import 'package:puby/command.dart';
import 'package:puby/config.dart';
Expand Down Expand Up @@ -99,6 +100,7 @@ extension ProjectExtension on Project {
command.args.length >= 2 &&
command.args[0] == 'pub' &&
command.args[1] == 'get';
final isOffline = command.args.contains('--offline');

final bool skip;
final String? message;
Expand All @@ -109,8 +111,9 @@ extension ProjectExtension on Project {
} else if (path.startsWith('build/') || path.contains('/build/')) {
message = 'Skipping project in build folder: $path';
skip = true;
} else if (isPubGetInFlutterExample) {
} else if (isPubGetInFlutterExample && !isOffline) {
// Skip flutter pub get in example projects since flutter does it anyways
// Do not skip if this is an offline pub command
message = 'Skipping flutter example project: $path';
skip = true;
} else {
Expand Down Expand Up @@ -141,7 +144,7 @@ extension ProjectExtension on Project {
return copyWith(engine: resolvedEngine, exclude: exclude);
}

String? getFlutterVersionOverride() {
Version? getFlutterVersionOverride() {
if (engine != Engine.fvm) return null;

final result = Process.runSync(
Expand All @@ -150,7 +153,12 @@ extension ProjectExtension on Project {
workingDirectory: path,
);
try {
return jsonDecode(result.stdout as String)['frameworkVersion'] as String?;
final versionString =
jsonDecode(result.stdout as String)['frameworkVersion'] as String?;
if (versionString == null) {
throw 'Version string is null';
}
return Version.parse(versionString);
} catch (e) {
print(redPen('Unable to determine FVM Flutter version: $path'));
return null;
Expand Down
45 changes: 9 additions & 36 deletions bin/puby.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,11 @@ import 'package:puby/pens.dart';
import 'package:puby/project.dart';
import 'package:puby/time.dart';

import 'commands.dart';
import 'link.dart';
import 'projects.dart';

const decoder = Utf8Decoder();
final clean = Command(['clean'], parallel: true);
final convenienceCommands = <String, List<Command>>{
'gen': [
Command([
'pub',
'run',
'build_runner',
'build',
'--delete-conflicting-outputs',
]),
],
'test': [
Command(['test']),
],
'clean': [
clean,
],
'mup': [
Command(['pub', 'upgrade', '--major-versions']),
],
'reset': [
clean,
Command(['pub', 'get']),
],
};

const help = '''
Commands:
Expand All @@ -64,9 +40,11 @@ void main(List<String> arguments) async {
);
}

if (arguments.isEmpty ||
final showHelp = arguments.isEmpty ||
arguments.first == '-h' ||
arguments.first == '--help') {
arguments.first == '--help';

if (showHelp) {
print(magentaPen(help));
exit(1);
}
Expand All @@ -93,8 +71,8 @@ void main(List<String> arguments) async {
parallel: true,
),
);
} else if (convenienceCommands.containsKey(firstArg)) {
for (final command in convenienceCommands[firstArg]!) {
} else if (Commands.convenience.containsKey(firstArg)) {
for (final command in Commands.convenience[firstArg]!) {
command.addArgs(arguments.sublist(1));
commands.add(command);
}
Expand All @@ -120,11 +98,8 @@ Future<int> runInAllProjects(List<Project> projects, Command command) async {
final failures = <String>[];

Future<void> run(Project project) async {
final processExitCode = await runInProject(
project: project,
projectCount: projects.length,
command: command,
);
final processExitCode =
await runInProject(project: project, command: command);

if (processExitCode != 0) {
failures.add(project.path);
Expand Down Expand Up @@ -164,7 +139,6 @@ Future<int> runInAllProjects(List<Project> projects, Command command) async {

Future<int> runInProject({
required Project project,
required int projectCount,
required Command command,
}) async {
final stopwatch = Stopwatch()..start();
Expand Down Expand Up @@ -243,7 +217,6 @@ Future<int> runInProject({
print(yellowPen('\nRetrying with "flutter" engine'));
return runInProject(
project: resolved.copyWith(engine: Engine.flutter),
projectCount: projectCount,
command: command,
);
}
Expand Down
6 changes: 5 additions & 1 deletion lib/command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Command {
final bool parallel;

/// Whether to run the command silently
///
///
/// Right now this is the same as [parallel]
bool get silent => parallel;

Expand All @@ -21,11 +21,15 @@ class Command {
/// If fvm support should be disabled
bool get noFvm => _noFvm;

/// If this command should be run only in the current working directory
final bool global;

/// Constructor
Command(
List<String> args, {
this.raw = false,
this.parallel = false,
this.global = false,
}) {
addArgs(args);
}
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies:
equatable: ^2.0.5
flutter_tools_task_queue: ^1.0.0
pub_hosted: ^1.0.0
pub_semver: ^2.1.4

dev_dependencies:
test: ^1.20.2
Expand Down

0 comments on commit 04a14a4

Please sign in to comment.