Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warning sidekick init downgrade #181

Closed
wants to merge 10 commits into from
31 changes: 30 additions & 1 deletion sidekick/lib/src/commands/init_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:sidekick/src/util/dcli_ask_validators.dart';
import 'package:sidekick/src/util/directory_extension.dart';
import 'package:sidekick/src/util/name_suggester.dart';
import 'package:sidekick_core/sidekick_core.dart';
import 'package:sidekick_core/sidekick_core.dart' as core;
import 'package:sidekick_core/sidekick_core.dart' as core show version;

class InitCommand extends Command {
@override
Expand Down Expand Up @@ -121,6 +121,35 @@ class InitCommand extends Command {
throw 'The CLI name $cliName is already taken by an executable on your system see $cliNameCollisions';
}

final packageDirectory = cliPackageDir.directory("${cliName}_sidekick");
if (packageDirectory.existsSync()) {
print(
'\nYou already have an existing sidekick project initialized in ${relative(packageDirectory.path)}.\n'
'In order to update your existing project run ${dcli.cyan('<cli> sidekick update')} instead.');
final override = dcli.confirm(
'Do you want to override your existing CLI?',
defaultValue: false,
);
if (!override) {
return;
}
final packageVersion = sidekickPackageCliVersion();
if (packageVersion != null && packageVersion > core.version) {
print(
'\n'
'Your current sidekick version would be downgraded '
'from $packageVersion to ${core.version}',
);
final downgrade =
dcli.confirm('Do you want to downgrade?', defaultValue: false);
if (!downgrade) {
print(
'In order to update sidekick run ${dcli.cyan('sidekick update')}.');
return;
}
}
}

print("\nGenerating ${cliName}_sidekick");

final packages = Repository(root: repoRoot).findAllPackages();
Expand Down
15 changes: 11 additions & 4 deletions sidekick/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -452,10 +452,10 @@ packages:
sidekick_core:
dependency: "direct main"
description:
name: sidekick_core
url: "https://pub.dartlang.org"
source: hosted
version: "0.13.1"
path: "../sidekick_core"
relative: true
source: path
version: "0.14.0"
sidekick_test:
dependency: "direct dev"
description:
Expand Down Expand Up @@ -554,6 +554,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.21"
test_descriptor:
dependency: "direct dev"
description:
name: test_descriptor
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.1"
test_process:
dependency: "direct dev"
description:
Expand Down
4 changes: 3 additions & 1 deletion sidekick/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ dependencies:
path: ^1.8.0
process: ^4.2.4
recase: ^4.0.0
sidekick_core: '>=0.13.0 <1.0.0'
sidekick_core:
path: ../sidekick_core

dev_dependencies:
lint: ^1.5.0
sidekick_test:
path: ../sidekick_test
test_descriptor: ^2.0.1
test: ^1.17.0
test_process: ^2.0.2

Expand Down
160 changes: 160 additions & 0 deletions sidekick/test/downgrade_on_init_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import 'package:dcli/dcli.dart' as dcli;
import 'package:path/path.dart' as path;
import 'package:sidekick/sidekick.dart' as sidekick show main;
import 'package:sidekick_core/sidekick_core.dart' hide version;
import 'package:test/test.dart';
import 'package:test_descriptor/test_descriptor.dart' as descriptor;
import 'package:test_process/test_process.dart';
import 'util/cli_runner.dart';

//TODO(danielmolnar) Remove text_descriptor dependency once this file gets deleted
/// Starts a Dart process running [script] in a main method.
Future<TestProcess> _startDartProcess(String script) {
final dartPath = path.join(descriptor.sandbox, 'test.dart');
File(dartPath).writeAsStringSync('''
import 'dart:async';
import 'dart:convert';
import 'dart:io';
var stdinLines = stdin
.transform(utf8.decoder)
.transform(new LineSplitter());
void main() {
$script
}
''');
return TestProcess.start(Platform.executable, ['--enable-asserts', dartPath]);
}

void main() {
test('stdin writes to the process', () async {
final process = await _startDartProcess(r'''
stdinLines.listen((line) => print("> $line"));
''');

process.stdin.writeln('hello');
await expectLater(process.stdout, emits('> hello'));
process.stdin.writeln('world');
await expectLater(process.stdout, emits('> world'));
await process.kill();
});

group('Case of pre-existing sidekick in directory', () {
test(
'Warning',
() async {
final tempDir = Directory.systemTemp.createTempSync();
addTearDown(() => tempDir.deleteSync(recursive: true));
final package = tempDir.directory('dashi_sidekick')..createSync();
package.file('pubspec.yaml').writeAsStringSync("""
name: dashi_sidekick
environment:
sdk: '>=2.14.0 <3.0.0'
sidekick:
cli_version: 0.13.1
""");

// Create sidekick again in the same directory
final TestProcess process = await cachedGlobalSidekickCli.run(
['init', '-n', 'dashi', '-c', '.'],
workingDirectory: package.parent,
);
await process.shouldExit(0);

await expectLater(
process.stdout,
emitsThrough(
"Welcome to sidekick. You're about to initialize a sidekick project",
),
);

await expectLater(
process.stdout,
emitsThrough(
"You already have an existing sidekick project initialized in dashi_sidekick.",
),
);
await expectLater(
process.stdout,
emitsThrough(
"In order to update your existing project run ${dcli.cyan('<cli> sidekick update')} instead.",
),
);
},
);
test(
'Override',
() async {
final tempDir = Directory.systemTemp.createTempSync();
addTearDown(() => tempDir.deleteSync(recursive: true));
final package = tempDir.directory('dashi_sidekick')..createSync();
package.file('pubspec.yaml').writeAsStringSync("""
name: dashi_sidekick
environment:
sdk: '>=2.14.0 <3.0.0'
sidekick:
cli_version: 0.13.1
""");

// Create sidekick again in the same directory
final TestProcess process = await cachedGlobalSidekickCli.run(
['init', '-n', 'dashi', '-c', '.'],
workingDirectory: package.parent,
forwardStdio: true,
);

process.stdoutStream().listen((event) {
if (event == "Do you want to override your existing CLI?") {
process.stdin.writeln('y');
}
print(event);
});

process.stdin.writeln('y');

await process.shouldExit(0);

await expectLater(
process.stdout,
emitsThrough(
"Welcome to sidekick. You're about to initialize a sidekick project",
),
);

await expectLater(
process.stdout,
emitsThrough(
"You already have an existing sidekick project initialized in dashi_sidekick.",
),
);
await expectLater(
process.stdout,
emitsThrough(
"In order to update your existing project run ${dcli.cyan('<cli> sidekick update')} instead.",
),
);
await expectLater(
process.stdout,
emitsThrough(
"Do you want to override your existing CLI?",
),
);

await expectLater(
process.stdout,
emitsThrough(
"y",
),
);
},
);
});

/* secondProcess.stdoutStream().listen((event) {
if (event == "Do you want to override your existing CLI?") {
secondProcess.stdin.writeln('y');
}
print(event);
});

await secondProcess.shouldExit(0);*/
}
Loading