Skip to content

Commit

Permalink
refactor(shorebird_cli): upgrade analysis_options (#2720)
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel authored Dec 19, 2024
1 parent 82dfa29 commit 1b38845
Show file tree
Hide file tree
Showing 73 changed files with 509 additions and 197 deletions.
2 changes: 1 addition & 1 deletion packages/shorebird_cli/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
include: package:very_good_analysis/analysis_options.5.1.0.yaml
include: package:very_good_analysis/analysis_options.7.0.0.yaml
analyzer:
exclude:
- lib/**.g.dart
Expand Down
10 changes: 0 additions & 10 deletions packages/shorebird_cli/lib/shorebird_cli.dart

This file was deleted.

2 changes: 1 addition & 1 deletion packages/shorebird_cli/lib/src/artifact_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ final artifactBuilderRef = create(ArtifactBuilder.new);
ArtifactBuilder get artifactBuilder => read(artifactBuilderRef);

extension on String {
/// Converts this base64-encoded public key into the Map<String, String>:
/// Converts this base64-encoded public key into the `Map<String, String>`:
/// {'SHOREBIRD_PUBLIC_KEY': this}
///
/// SHOREBIRD_PUBLIC_KEY is the name expected by the Shorebird's Flutter tool
Expand Down
13 changes: 9 additions & 4 deletions packages/shorebird_cli/lib/src/auth/auth.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// ignore_for_file: public_member_api_docs
// cspell:words googleapis bryanoltman endtemplate CLI tgvek orctktiabrek
// cspell:words GOCSPX googleusercontent Pkkwp Entra
import 'dart:convert';
Expand Down Expand Up @@ -162,7 +161,7 @@ class AuthenticatedClient extends http.BaseClient {
client,
authEndpoints: authEndpoints,
);
} catch (e, s) {
} on Exception catch (e, s) {
logger
..err('Failed to refresh credentials.')
..info(
Expand Down Expand Up @@ -328,7 +327,9 @@ class Auth {
json.decode(contents) as Map<String, dynamic>,
);
_email = _credentials?.email;
} catch (_) {}
} on Exception {
// Swallow json decode exceptions.
}
}
}

Expand Down Expand Up @@ -365,7 +366,7 @@ extension JwtClaims on oauth2.AccessCredentials {
final Jwt jwt;
try {
jwt = Jwt.parse(token);
} catch (_) {
} on Exception {
return null;
}

Expand Down Expand Up @@ -409,12 +410,15 @@ extension OauthAuthProvider on Jwt {
}
}

/// Extension on [AuthProvider] which exposes OAuth 2.0 values.
extension OauthValues on AuthProvider {
/// The OAuth 2.0 endpoints for the provider.
oauth2.AuthEndpoints get authEndpoints => switch (this) {
(AuthProvider.google) => const oauth2.GoogleAuthEndpoints(),
(AuthProvider.microsoft) => MicrosoftAuthEndpoints(),
};

/// The OAuth 2.0 client ID for the provider.
oauth2.ClientId get clientId {
switch (this) {
case AuthProvider.google:
Expand Down Expand Up @@ -442,6 +446,7 @@ extension OauthValues on AuthProvider {
}
}

/// The OAuth 2.0 scopes for the provider.
List<String> get scopes => switch (this) {
(AuthProvider.google) => [
'openid',
Expand Down
35 changes: 31 additions & 4 deletions packages/shorebird_cli/lib/src/cache.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// ignore_for_file: public_member_api_docs

import 'dart:io' hide Platform;

import 'package:http/http.dart' as http;
Expand Down Expand Up @@ -32,10 +30,10 @@ class CacheUpdateFailure implements Exception {
String toString() => 'CacheUpdateFailure: $message';
}

// A reference to a [Cache] instance.
/// A reference to a [Cache] instance.
final cacheRef = create(Cache.new);

// The [Cache] instance available in the current zone.
/// The [Cache] instance available in the current zone.
Cache get cache => read(cacheRef);

/// {@template cache}
Expand All @@ -46,12 +44,14 @@ Cache get cache => read(cacheRef);
/// [ShorebirdArtifacts] since uses the current Shorebird environment.
/// {@endtemplate}
class Cache {
/// {@macro cache}
Cache() {
registerArtifact(PatchArtifact(cache: this, platform: platform));
registerArtifact(BundleToolArtifact(cache: this, platform: platform));
registerArtifact(AotToolsArtifact(cache: this, platform: platform));
}

/// Register a new [CachedArtifact] with the cache.
void registerArtifact(CachedArtifact artifact) => _artifacts.add(artifact);

/// Update all artifacts in the cache.
Expand Down Expand Up @@ -119,10 +119,13 @@ class Cache {

final List<CachedArtifact> _artifacts = [];

/// The storage base url.
String get storageBaseUrl => 'https://storage.googleapis.com';

/// The storage bucket host.
String get storageBucket => 'download.shorebird.dev';

/// Clear the cache.
Future<void> clear() async {
final cacheDir = shorebirdCacheDirectory;
if (cacheDir.existsSync()) {
Expand All @@ -131,10 +134,17 @@ class Cache {
}
}

/// {@template cached_artifact}
/// An artifact which is cached by Shorebird.
/// {@endtemplate}
abstract class CachedArtifact {
/// {@macro cached_artifact}
CachedArtifact({required this.cache, required this.platform});

/// The cache instance to use.
final Cache cache;

/// The platform to use.
final Platform platform;

/// The on-disk name of the artifact.
Expand All @@ -156,6 +166,7 @@ abstract class CachedArtifact {
/// is assumed to be correct.
String? get checksum;

/// Extract the artifact from the provided [stream] to the [outputPath].
Future<void> extractArtifact(http.ByteStream stream, String outputPath) {
final file = File(p.join(outputPath, fileName))
..createSync(recursive: true);
Expand All @@ -169,6 +180,7 @@ abstract class CachedArtifact {
/// Used to validate that the artifact was fully downloaded and extracted.
File get stampFile => File('${file.path}.stamp');

/// Whether the artifact is valid (has a matching checksum).
Future<bool> isValid() async {
if (!file.existsSync() || !stampFile.existsSync()) {
return false;
Expand All @@ -184,6 +196,7 @@ abstract class CachedArtifact {
return checksumChecker.checkFile(file, checksum!);
}

/// Re-fetch the artifact from the storage URL.
Future<void> update() async {
// Clear any existing artifact files.
await _delete();
Expand Down Expand Up @@ -271,7 +284,12 @@ allowed to access $storageUrl.''',
}
}

/// {@template aot_tools_artifact}
/// The aot_tools.dill artifact.
/// Used for linking and generating optimized AOT snapshots.
/// {@endtemplate}
class AotToolsArtifact extends CachedArtifact {
/// {@macro aot_tools_artifact}
AotToolsArtifact({required super.cache, required super.platform});

@override
Expand Down Expand Up @@ -301,7 +319,11 @@ class AotToolsArtifact extends CachedArtifact {
String? get checksum => null;
}

/// {@template patch_artifact}
/// The patch artifact which is used to apply binary patches.
/// {@endtemplate}
class PatchArtifact extends CachedArtifact {
/// {@macro patch_artifact}
PatchArtifact({required super.cache, required super.platform});

@override
Expand Down Expand Up @@ -342,7 +364,12 @@ class PatchArtifact extends CachedArtifact {
String? get checksum => null;
}

/// {@template bundle_tool_artifact}
/// The bundletool.jar artifact.
/// Used for interacting with Android app bundles (aab).
/// {@endtemplate}
class BundleToolArtifact extends CachedArtifact {
/// {@macro bundle_tool_artifact}
BundleToolArtifact({required super.cache, required super.platform});

@override
Expand Down
7 changes: 2 additions & 5 deletions packages/shorebird_cli/lib/src/code_push_client_wrapper.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// TODO(felangel): Add public member API docs and remove the ignore.
// ignore_for_file: public_member_api_docs
// cspell:words endtemplate pubspec sideloadable bryanoltman archs sideload
// cspell:words xcarchive codesigned xcframework
Expand Down Expand Up @@ -87,11 +88,7 @@ class CodePushClientWrapper {
}) async {
late final String displayName;
if (appName == null) {
String? defaultAppName;
try {
defaultAppName = shorebirdEnv.getPubspecYaml()?.name;
} catch (_) {}

final defaultAppName = shorebirdEnv.getPubspecYaml()?.name;
displayName = logger.prompt(
'${lightGreen.wrap('?')} How should we refer to this app?',
defaultValue: defaultAppName,
Expand Down
13 changes: 6 additions & 7 deletions packages/shorebird_cli/lib/src/commands/doctor_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,9 @@ Android Toolchain
• Gradle: ${gradlewVersion ?? notDetected}''');
}

logger.info(output.toString());

// ignore: cascade_invocations
logger.info('URL Reachability');
logger
..info(output.toString())
..info('URL Reachability');
await networkChecker.checkReachability();
logger.info('');

Expand All @@ -111,7 +110,7 @@ Android Toolchain
);
} on NetworkCheckerException catch (error) {
uploadProgress.fail('GCP upload speed test failed: ${error.message}');
} catch (error) {
} on Exception catch (error) {
uploadProgress.fail('GCP upload speed test failed: $error');
}

Expand All @@ -127,7 +126,7 @@ Android Toolchain
downloadProgress.fail(
'GCP download speed test failed: ${error.message}',
);
} catch (error) {
} on Exception catch (error) {
downloadProgress.fail(
'GCP download speed test failed: $error',
);
Expand All @@ -143,7 +142,7 @@ Android Toolchain
Future<String?> _tryGetFlutterVersion() async {
try {
return await shorebirdFlutter.getVersionString();
} catch (error) {
} on Exception catch (error) {
logger.detail('Unable to determine Flutter version.\n$error');
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class FlutterVersionsListCommand extends ShorebirdCommand {
try {
versions = await shorebirdFlutter.getVersions();
progress.cancel();
} catch (error) {
} on Exception catch (error) {
progress.fail('Failed to fetch Flutter versions.');
logger.err('$error');
return ExitCode.software.code;
Expand Down
8 changes: 4 additions & 4 deletions packages/shorebird_cli/lib/src/commands/init_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Please make sure you are running "shorebird init" from within your Flutter proje
''');
return ExitCode.noInput.code;
}
} catch (error) {
} on Exception catch (error) {
logger.err('Error parsing "pubspec.yaml": $error');
return ExitCode.software.code;
}
Expand Down Expand Up @@ -108,7 +108,7 @@ Please make sure you are running "shorebird init" from within your Flutter proje
logger.info(' - $flavor');
}
}
} catch (error) {
} on Exception catch (error) {
detectFlavorsProgress.fail();
logger.err('Unable to extract product flavors.\n$error');
return ExitCode.software.code;
Expand Down Expand Up @@ -138,7 +138,7 @@ Please make sure you are running "shorebird init" from within your Flutter proje
existingApp = await codePushClientWrapper.getApp(
appId: shorebirdYaml!.appId,
);
} catch (e) {
} on Exception catch (e) {
updateShorebirdYamlProgress.fail('Failed to get existing app info: $e');
return ExitCode.software.code;
}
Expand Down Expand Up @@ -227,7 +227,7 @@ Please make sure you are running "shorebird init" from within your Flutter proje
flavors = values;
appId = flavors.values.first;
}
} catch (error) {
} on Exception catch (error) {
logger.err('$error');
return ExitCode.software.code;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/shorebird_cli/lib/src/commands/login_ci_command.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// ignore_for_file: public_member_api_docs

import 'package:mason_logger/mason_logger.dart';
import 'package:shorebird_cli/src/auth/auth.dart';
import 'package:shorebird_cli/src/logging/logging.dart';
Expand All @@ -12,6 +10,7 @@ import 'package:shorebird_code_push_protocol/shorebird_code_push_protocol.dart'
/// Login as a CI user.
/// {@endtemplate}
class LoginCiCommand extends ShorebirdCommand {
/// {@macro login_ci_command}
LoginCiCommand() {
argParser.addOption(
'provider',
Expand Down Expand Up @@ -54,7 +53,7 @@ We could not find a Shorebird account for ${error.email}.''',
'''If you have not yet created an account, go to "${link(uri: Uri.parse('https://console.shorebird.dev'))}" to create one. If you believe this is an error, please reach out to us via Discord, we're happy to help!''',
);
return ExitCode.software.code;
} catch (error) {
} on Exception catch (error) {
logger.err(error.toString());
return ExitCode.software.code;
}
Expand All @@ -72,6 +71,7 @@ ${lightCyan.wrap('export $shorebirdTokenEnvVar="\$SHOREBIRD_TOKEN" && shorebird
return ExitCode.success.code;
}

/// Prompt the user to visit the provided [url] to authorize the CLI.
void prompt(String url) {
logger.info('''
The Shorebird CLI needs your authorization to manage apps, releases, and patches on your behalf.
Expand Down
2 changes: 1 addition & 1 deletion packages/shorebird_cli/lib/src/commands/login_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ We could not find a Shorebird account for ${error.email}.''',
"""If you have not yet created an account, you can do so at "${link(uri: consoleUri)}". If you believe this is an error, please reach out to us via Discord, we're happy to help!""",
);
return ExitCode.software.code;
} catch (error) {
} on Exception catch (error) {
logger.err(error.toString());
return ExitCode.software.code;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class AarPatcher extends Patcher {
Uri.parse(releaseArtifact.value.url),
);
releaseArtifactPaths[releaseArtifact.key] = releaseArtifactFile.path;
} catch (error) {
} on Exception catch (error) {
downloadReleaseArtifactProgress.fail('$error');
throw ProcessExit(ExitCode.software.code);
}
Expand Down Expand Up @@ -163,7 +163,7 @@ class AarPatcher extends Patcher {
hash: hash,
size: await File(diffPath).length(),
);
} catch (error) {
} on Exception catch (error) {
createDiffProgress.fail('$error');
throw ProcessExit(ExitCode.software.code);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ Please refer to ${link(uri: Uri.parse('https://github.com/shorebirdtech/shorebir
message: 'Downloading release artifact ${i + 1}/$numArtifacts',
);
releaseArtifactPaths[releaseArtifact.key] = releaseArtifactFile.path;
} catch (error) {
} on Exception {
throw ProcessExit(ExitCode.software.code);
}
}
Expand Down Expand Up @@ -227,7 +227,7 @@ Please refer to ${link(uri: Uri.parse('https://github.com/shorebirdtech/shorebir
size: await File(diffPath).length(),
hashSignature: hashSignature,
);
} catch (error) {
} on Exception catch (error) {
createDiffProgress.fail('$error');
throw ProcessExit(ExitCode.software.code);
}
Expand Down
Loading

0 comments on commit 1b38845

Please sign in to comment.