Skip to content

Commit

Permalink
Merge pull request #72 from leoafarias/feature/display-progress
Browse files Browse the repository at this point in the history
Changes to process runners, and improved version validation
  • Loading branch information
leoafarias authored Jun 20, 2020
2 parents 1aaea4b + f28438f commit f0fdf98
Show file tree
Hide file tree
Showing 15 changed files with 105 additions and 85 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ doc/api/
# Test Coverage
test/.test_coverage.dart

# Link url to flutter version for tests
fvm

# IntelliJ related
*.iml
*.ipr
Expand All @@ -31,4 +28,6 @@ fvm
.settings/
.vscode/
coverage/lcov.info

# FVM during tests
.fvm
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ And then, for information on each command:

FVM gives you the ability to install many Flutter **releases** or **channels**.

- `version` - use `stable` to install the Stable channel and `v1.8.0` or `1.17.0-dev.3.1` to install the release.
- `--skip-setup` - will skip Flutter setup after install

```bash
> fvm install <version>
```

Version - use `master` to install the Master channel and `v1.8.0` or `1.17.0-dev.3.1` to install the release.

#### Project Config SDK Version

If your project is already configured to use an specifc version. Run `install` without any arguments will make sure the proper version is installed for the project.
Expand Down
Empty file added coverage.json
Empty file.
6 changes: 3 additions & 3 deletions coverage_badge.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions lib/commands/flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class FlutterCommand extends Command {
@override
Future<void> run() async {
Guards.isFlutterProject();
final flutterSdkPath = getFlutterSdkExecPath();
final flutterExec = getFlutterSdkExec();

await processRunner(flutterSdkPath, argResults.arguments,
await flutterCmd(flutterExec, argResults.arguments,
workingDirectory: kWorkingDirectory.path);
}
}
16 changes: 14 additions & 2 deletions lib/commands/install.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:args/command_runner.dart';
import 'package:fvm/exceptions.dart';
import 'package:fvm/utils/guards.dart';
import 'package:fvm/utils/helpers.dart';
import 'package:fvm/utils/project_config.dart';
import 'package:fvm/utils/version_installer.dart';

Expand All @@ -15,7 +16,14 @@ class InstallCommand extends Command {
final description = 'Installs Flutter SDK Version';

/// Constructor
InstallCommand();
InstallCommand() {
argParser
..addFlag(
'skip-setup',
help: 'Skips Flutter setup after install',
negatable: false,
);
}

@override
void run() async {
Expand All @@ -32,6 +40,10 @@ class InstallCommand extends Command {
version = argResults.arguments[0].toLowerCase();
}

await installFlutterVersion(version);
final skipSetup = argResults['skip-setup'] == true;

final flutterVersion = await inferFlutterVersion(version);

await installFlutterVersion(flutterVersion, skipSetup: skipSetup);
}
}
13 changes: 6 additions & 7 deletions lib/commands/remove.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:args/command_runner.dart';
import 'package:fvm/utils/flutter_tools.dart';
import 'package:fvm/utils/helpers.dart';
import 'package:fvm/utils/logger.dart';
import 'package:fvm/utils/print.dart';

/// Removes Flutter SDK
class RemoveCommand extends Command {
Expand All @@ -27,17 +27,16 @@ class RemoveCommand extends Command {
@override
void run() async {
final version = argResults.arguments[0].toLowerCase();

final isValidInstall = isFlutterVersionInstalled(version);
final flutterVersion = await inferFlutterVersion(version);
final isValidInstall = isFlutterVersionInstalled(flutterVersion);

if (!isValidInstall) {
throw Exception('Flutter SDK: $version is not installed');
throw Exception('Flutter SDK: $flutterVersion is not installed');
}

final progress = logger.progress('Removing $version');
Print.success('Removing $flutterVersion');
try {
flutterSdkRemove(version);
finishProgress(progress);
flutterSdkRemove(flutterVersion);
} on Exception {
rethrow;
}
Expand Down
8 changes: 4 additions & 4 deletions lib/commands/use.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ class UseCommand extends Command {
throw Exception('Please provide a version. fvm use <version>');
}
// Make sure is valid Flutter version
await Guards.isFlutterVersion(version);
final flutterVersion = await inferFlutterVersion(version);
// If project use check that is Flutter project
if (!useGlobally) Guards.isFlutterProject();

// Make sure version is installed
await checkAndInstallVersion(version);
await checkAndInstallVersion(flutterVersion);

if (useGlobally) {
// Sets version as the global
setAsGlobalVersion(version);
setAsGlobalVersion(flutterVersion);
} else {
// Updates the project config with version
setAsProjectVersion(version);
setAsProjectVersion(flutterVersion);
}
}
}
37 changes: 20 additions & 17 deletions lib/utils/flutter_tools.dart
Original file line number Diff line number Diff line change
@@ -1,42 +1,37 @@
import 'dart:io';
import 'package:fvm/constants.dart';
import 'package:fvm/exceptions.dart';
import 'package:fvm/utils/git.dart';

import 'package:fvm/utils/helpers.dart';
import 'package:fvm/utils/logger.dart';
import 'package:fvm/utils/print.dart';
import 'package:process_run/cmd_run.dart';
import 'package:process_run/process_run.dart';
import 'package:path/path.dart' as path;
import 'package:io/io.dart';

/// Runs a process
Future<void> processRunner(String cmd, List<String> args,
Future<void> flutterCmd(String exec, List<String> args,
{String workingDirectory}) async {
final manager = ProcessManager();

var pr = await manager.spawn(cmd, args, workingDirectory: workingDirectory);
final exitCode = await pr.exitCode;

exit(exitCode);
var pr = await run(exec, args,
workingDirectory: workingDirectory, stdout: stdout, stderr: stderr);
exitCode = pr.exitCode;
}

/// Clones Flutter SDK from Version Number or Channel
/// Returns exists:true if comes from cache or false if its new fetch.
Future<void> flutterVersionClone(
Future<void> gitCloneCmd(
String version,
) async {
final versionDirectory = Directory(path.join(kVersionsDir.path, version));

if (!isFlutterChannel(version)) {
version = await inferFlutterVersion(version);
}

// If it's installed correctly just return and use cached
if (isInstalledCorrectly(version)) return;

await versionDirectory.create(recursive: true);

final args = [
'clone',
'--progress',
'--depth',
'1',
'--single-branch',
'-b',
version,
Expand All @@ -45,12 +40,20 @@ Future<void> flutterVersionClone(
kFlutterRepo,
versionDirectory.path
];
await processRunner('git', args);

final process = await run('git', args,
stdout: stdout, stderr: stderr, verbose: logger.isVerbose);

if (process.exitCode != 0) {
throw ExceptionCouldNotClone(
'Could not install Flutter version: $version.');
}
}

/// Gets SDK Version
Future<String> flutterSdkVersion(String branch) async {
final branchDirectory = Directory(path.join(kVersionsDir.path, branch));

if (!branchDirectory.existsSync()) {
throw Exception('Could not get version from SDK that is not installed');
}
Expand Down
14 changes: 0 additions & 14 deletions lib/utils/guards.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
import 'dart:io';

import 'package:fvm/constants.dart';
import 'package:fvm/exceptions.dart';
import 'package:fvm/utils/git.dart';
import 'package:fvm/utils/helpers.dart';

/// Guards
class Guards {
Expand All @@ -25,16 +23,4 @@ class Guards {
'You need Git Installed to run fvm. Go to https://git-scm.com/downloads');
}
}

/// Make sure version is valid
static Future<void> isFlutterVersion(String version) async {
// Check if its a channel
if (isFlutterChannel(version)) return;
// Check if ts a version
final flutterVersion = await inferFlutterVersion(version);
if (flutterVersion == null) {
throw ExceptionNotValidVersion(
'"$version" is not a valid Flutter SDK version');
}
}
}
27 changes: 18 additions & 9 deletions lib/utils/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'dart:io';
import 'package:fvm/constants.dart';
import 'package:fvm/exceptions.dart';
import 'package:fvm/utils/confirm.dart';
import 'package:fvm/utils/logger.dart';
import 'package:fvm/utils/print.dart';
import 'package:fvm/utils/project_config.dart';
import 'package:fvm/utils/version_installer.dart';
Expand All @@ -13,14 +12,22 @@ import 'package:fvm/utils/flutter_tools.dart';
/// Returns true if it's a valid Flutter version number
Future<String> inferFlutterVersion(String version) async {
final versions = await flutterListAllSdks();
version = version.toLowerCase();

// Return if its flutter chacnnel
if (isFlutterChannel(version)) return version;

if ((versions).contains(version)) {
return version;
}
final prefixedVersion = 'v$version';

if ((versions).contains(prefixedVersion)) {
return prefixedVersion;
}
throw ExceptionNotValidVersion('"$version" is not a valid version');

throw ExceptionNotValidVersion(
'"$version" is not a valid Flutter SDK version');
}

/// Returns true if it's a valid Flutter channel
Expand All @@ -40,9 +47,7 @@ Future<void> checkAndInstallVersion(String version) async {

// Install if input is confirmed
if (await confirm('Would you like to install it?')) {
final installProgress = logger.progress('Installing $version');
await installFlutterVersion(version);
finishProgress(installProgress);
} else {
// If do not install exist
exit(0);
Expand Down Expand Up @@ -72,12 +77,16 @@ bool isCurrentVersion(String version) {
}

/// The Flutter SDK Path referenced on FVM
String getFlutterSdkPath() {
final config = readProjectConfig();
return path.join(kVersionsDir.path, config.flutterSdkVersion);
String getFlutterSdkPath({String version}) {
var sdkVersion = version;
if (version == null) {
final config = readProjectConfig();
sdkVersion = config.flutterSdkVersion;
}
return path.join(kVersionsDir.path, sdkVersion);
}

String getFlutterSdkExecPath() {
return path.join(getFlutterSdkPath(), 'bin',
String getFlutterSdkExec({String version}) {
return path.join(getFlutterSdkPath(version: version), 'bin',
Platform.isWindows ? 'flutter.bat' : 'flutter');
}
31 changes: 19 additions & 12 deletions lib/utils/version_installer.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
import 'package:fvm/exceptions.dart';
import 'package:fvm/utils/flutter_tools.dart';
import 'package:fvm/utils/logger.dart';
import 'package:io/ansi.dart';
import 'package:fvm/utils/helpers.dart';
import 'package:fvm/utils/print.dart';

Future<void> installFlutterVersion(
String flutterVersion,
) async {
if (flutterVersion == null) {
Future<void> installFlutterVersion(String version,
{bool skipSetup = false}) async {
if (version == null) {
throw ExceptionMissingChannelVersion();
}
final version = flutterVersion.toLowerCase();

final progress = logger.progress(green.wrap('Downloading $version'));
// Skips line in progress
print('');
// If it's installed correctly just return and use cached
if (isInstalledCorrectly(version)) {
Print.success('Version: $version - already installed.');
return;
}

Print.success('Installing Version: $version:');

await flutterVersionClone(version);
await gitCloneCmd(version);

finishProgress(progress);
// Skips Flutter sdk setup
if (skipSetup) return;
Print.success('Setting up Flutter sdk');
Print.info('If you want to skip this next time use "--skip-setup"');
final flutterExec = getFlutterSdkExec(version: version);
await flutterCmd(flutterExec, ['--version']);
}
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:
console: ^3.1.0
io: ^0.3.3
path: ^1.6.4
process_run: ^0.10.10+1

dev_dependencies:
pedantic: ^1.8.0
Expand Down
Loading

0 comments on commit f0fdf98

Please sign in to comment.