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

Add helper method to throw if command is not installed #912

Merged
merged 4 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions tools/sz_repo_cli/lib/src/commands/src/format_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'dart:io';

import 'package:sz_repo_cli/src/common/common.dart';
import 'package:sz_repo_cli/src/common/src/run_source_of_truth_command.dart';
import 'package:sz_repo_cli/src/common/src/throw_if_command_is_not_installed.dart';

class FormatCommand extends ConcurrentCommand {
FormatCommand(SharezoneRepo repo) : super(repo);
Expand All @@ -37,22 +38,10 @@ class FormatCommand extends ConcurrentCommand {
await formatCode(package);

Future<void> _throwIfPrettierIsNotInstalled() async {
if (Platform.isWindows) {
// We skip this check on Windows because "which -s" is not available.
return;
}

// Check if "which -s prettier" returns 0.
// If not, throw an exception.
final result = await runProcess(
'which',
['-s', 'prettier'],
await throwIfCommandIsNotInstalled(
command: 'prettier',
instructionsToInstall: 'Run `npm install -g prettier` to install it.',
);
if (result.exitCode != 0) {
throw Exception(
'Prettier is not installed. Run `npm install -g prettier` to install it.',
);
}
}

Future<void> _formatActionFiles({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:args/args.dart';
import 'package:sz_repo_cli/src/common/src/apple_track.dart';
import 'package:sz_repo_cli/src/common/src/run_process.dart';
import 'package:sz_repo_cli/src/common/src/sharezone_repo.dart';
import 'package:sz_repo_cli/src/common/src/throw_if_command_is_not_installed.dart';

const privateKeyOptionName = 'private-key';
const keyIdOptionName = 'key-id';
Expand Down Expand Up @@ -131,17 +132,11 @@ AppleTrack _getAppleTrack({
}

Future<void> throwIfCodemagiCliToolsAreNotInstalled() async {
// Check if "which -s app-store-connect" returns 0.
// If not, throw an exception.
final result = await runProcess(
'which',
['-s', 'app-store-connect'],
await throwIfCommandIsNotInstalled(
command: 'app-store-connect',
instructionsToInstall:
'Docs to install them: https://github.com/codemagic-ci-cd/cli-tools#installing',
);
if (result.exitCode != 0) {
throw Exception(
'Codemagic CLI tools are not installed. Docs to install them: https://github.com/codemagic-ci-cd/cli-tools#installing',
);
}
}

void addWhatsNewOption(ArgParser argParser) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2023 Sharezone UG (haftungsbeschränkt)
// Licensed under the EUPL-1.2-or-later.
//
// You may obtain a copy of the Licence at:
// https://joinup.ec.europa.eu/software/page/eupl
//
// SPDX-License-Identifier: EUPL-1.2

import 'dart:io';

import 'package:sz_repo_cli/src/common/src/run_process.dart';

/// Throws an exception if [command] is not installed.
///
/// Optionally, you can pass [instructionsToInstall] to throw an helpful
/// exception.
///
/// Currently, we skip this method for [Platform.isWindows] because "which -s"
/// is not available for Windows.
Future<void> throwIfCommandIsNotInstalled({
required String command,
String? instructionsToInstall,
}) async {
if (Platform.isWindows) {
// We skip on Windows the check because "which -s" is not available for
// Windows.
return;
}

final result = await runProcess(
'which',
['-s', command],
);
if (result.exitCode != 0) {
String message = 'Command "$command" is not installed.';
if (instructionsToInstall != null) {
message += ' $instructionsToInstall';
}

throw Exception(message);
}
}