diff --git a/bin/main.dart b/bin/main.dart index 3d44f8e..96f6bdf 100644 --- a/bin/main.dart +++ b/bin/main.dart @@ -10,6 +10,7 @@ import 'package:alfred_workflow/alfred_workflow.dart' AlfredWorkflow; import 'package:algolia/algolia.dart' show AlgoliaQuerySnapshot; import 'package:args/args.dart' show ArgParser, ArgResults; +import 'package:cli_script/cli_script.dart'; import 'package:collection/collection.dart' show IterableExtension; import 'src/constants/config.dart' show Config; @@ -17,139 +18,69 @@ import 'src/extensions/string_helpers.dart' show StringHelpers; import 'src/models/search_result.dart' show SearchResult; import 'src/services/algolia_search.dart' show AlgoliaSearch; -final AlfredWorkflow workflow = AlfredWorkflow(); -final AlfredUpdater updater = AlfredUpdater( - githubRepositoryUrl: Config.githubRepositoryUrl, - currentVersion: Config.version, - updateInterval: Duration(days: 7), -); -bool verbose = false; -bool update = false; +part 'main_helpers.dart'; -void main(List arguments) async { - try { - exitCode = 0; +bool _verbose = false; +bool _update = false; - workflow.clearItems(); +void main(List arguments) { + wrapMain(() async { + try { + exitCode = 0; - final ArgParser parser = ArgParser() - ..addOption('query', abbr: 'q', defaultsTo: '') - ..addFlag('verbose', abbr: 'v', defaultsTo: false) - ..addFlag('update', abbr: 'u', defaultsTo: false); - final ArgResults args = parser.parse(arguments); + _workflow.clearItems(); - update = args['update']; - if (update) { - stdout.writeln('Updating workflow...'); + final ArgParser parser = ArgParser() + ..addOption('query', abbr: 'q', defaultsTo: '') + ..addFlag('verbose', abbr: 'v', defaultsTo: false) + ..addFlag('update', abbr: 'u', defaultsTo: false); + final ArgResults args = parser.parse(arguments); - return await updater.update(); - } - - verbose = args['verbose']; + _update = args['update']; + if (_update) { + stdout.writeln('Updating workflow...'); - List query = - args['query'].replaceAll(RegExp(r'\s+'), ' ').trim().split(' '); - String? version = - query.firstWhereOrNull((el) => Config.supportedVersions.contains(el)); - if (version != null) { - query.removeWhere((str) => str == version); - } else { - version = Config.supportedVersions.last; - } - final String queryString = query.join(' ').trim().toLowerCase(); + return await _updater.update(); + } - if (verbose) stdout.writeln('Query: "$queryString"'); + _verbose = args['verbose']; - if (queryString.isEmpty) { - _showPlaceholder(); - } else { - workflow.cacheKey = '${queryString}_$version'; - if (await workflow.getItems() == null) { - await _performSearch(queryString, version: version); - } - } - } on FormatException catch (err) { - exitCode = 2; - workflow.addItem(AlfredItem(title: err.toString())); - } catch (err) { - exitCode = 1; - workflow.addItem(AlfredItem(title: err.toString())); - if (verbose) rethrow; - } finally { - if (!update) { - if (await updater.updateAvailable()) { - workflow.run(addToBeginning: updateItem); + List query = + args['query'].replaceAll(RegExp(r'\s+'), ' ').trim().split(' '); + String? version = + query.firstWhereOrNull((el) => Config.supportedVersions.contains(el)); + if (version != null) { + query.removeWhere((str) => str == version); } else { - workflow.run(); + version = Config.supportedVersions.last; } - } - } -} + final String queryString = query.join(' ').trim().toLowerCase(); -const updateItem = AlfredItem( - title: 'Auto-Update available!', - subtitle: 'Press to auto-update to a new version of this workflow.', - arg: 'update:workflow', - match: - 'Auto-Update available! Press to auto-update to a new version of this workflow.', - icon: AlfredItemIcon(path: 'alfredhatcog.png'), - valid: true, -); + if (_verbose) stdout.writeln('Query: "$queryString"'); -void _showPlaceholder() { - workflow.addItem( - const AlfredItem( - title: 'Search the Django docs...', - icon: AlfredItemIcon(path: 'icon.png'), - ), - ); -} - -Future _performSearch(String query, {String? version}) async { - final AlgoliaQuerySnapshot snapshot = await AlgoliaSearch.query( - query, - version: version, - ); - - if (snapshot.nbHits > 0) { - final AlfredItems items = AlfredItems( - snapshot.hits.map((snapshot) => SearchResult.fromJson(snapshot.data)).map( - (result) { - return AlfredItem( - uid: result.objectID, - title: result.prettyTitle, - subtitle: result.content.isNotEmpty - ? result.content.truncate(75) - : result.id, - arg: result.permalink, - text: AlfredItemText( - largeType: result.id, - copy: result.id, - ), - quickLookUrl: result.permalink, - icon: AlfredItemIcon(path: 'icon.png'), - valid: true, - ); - }, - ).toList(), - ); - workflow.addItems(items.items); - } else { - final Uri url = - Uri.https('www.google.com', '/search', {'q': 'Django $query'}); - - workflow.addItem( - AlfredItem( - title: 'No matching answers found', - subtitle: 'Shall I try and search Google?', - arg: url.toString(), - text: AlfredItemText( - copy: url.toString(), - ), - quickLookUrl: url.toString(), - icon: AlfredItemIcon(path: 'google.png'), - valid: true, - ), - ); - } + if (queryString.isEmpty) { + _showPlaceholder(); + } else { + _workflow.cacheKey = '${queryString}_$version'; + if (await _workflow.getItems() == null) { + await _performSearch(queryString, version: version); + } + } + } on FormatException catch (err) { + exitCode = 2; + _workflow.addItem(AlfredItem(title: err.toString())); + } catch (err) { + exitCode = 1; + _workflow.addItem(AlfredItem(title: err.toString())); + if (_verbose) rethrow; + } finally { + if (!_update) { + if (await _updater.updateAvailable()) { + _workflow.run(addToBeginning: _updateItem); + } else { + _workflow.run(); + } + } + } + }); } diff --git a/bin/main_helpers.dart b/bin/main_helpers.dart new file mode 100644 index 0000000..092954b --- /dev/null +++ b/bin/main_helpers.dart @@ -0,0 +1,78 @@ +part of 'main.dart'; + +final AlfredWorkflow _workflow = AlfredWorkflow(); + +final AlfredUpdater _updater = AlfredUpdater( + githubRepositoryUrl: Config.githubRepositoryUrl, + currentVersion: Config.version, + updateInterval: Duration(days: 7), +); + + +const _updateItem = AlfredItem( + title: 'Auto-Update available!', + subtitle: 'Press to auto-update to a new version of this workflow.', + arg: 'update:workflow', + match: + 'Auto-Update available! Press to auto-update to a new version of this workflow.', + icon: AlfredItemIcon(path: 'alfredhatcog.png'), + valid: true, +); + +void _showPlaceholder() { + _workflow.addItem( + const AlfredItem( + title: 'Search the Django docs...', + icon: AlfredItemIcon(path: 'icon.png'), + ), + ); +} + +Future _performSearch(String query, {String? version}) async { + final AlgoliaQuerySnapshot snapshot = await AlgoliaSearch.query( + query, + version: version, + ); + + if (snapshot.nbHits > 0) { + final AlfredItems items = AlfredItems( + snapshot.hits.map((snapshot) => SearchResult.fromJson(snapshot.data)).map( + (result) { + return AlfredItem( + uid: result.objectID, + title: result.prettyTitle, + subtitle: result.content.isNotEmpty + ? result.content.truncate(75) + : result.id, + arg: result.permalink, + text: AlfredItemText( + largeType: result.id, + copy: result.id, + ), + quickLookUrl: result.permalink, + icon: AlfredItemIcon(path: 'icon.png'), + valid: true, + ); + }, + ).toList(), + ); + _workflow.addItems(items.items); + } else { + final Uri url = + Uri.https('www.google.com', '/search', {'q': 'Django $query'}); + + _workflow.addItem( + AlfredItem( + title: 'No matching answers found', + subtitle: 'Shall I try and search Google?', + arg: url.toString(), + text: AlfredItemText( + copy: url.toString(), + ), + quickLookUrl: url.toString(), + icon: AlfredItemIcon(path: 'google.png'), + valid: true, + ), + ); + } +} \ No newline at end of file diff --git a/pubspec.lock b/pubspec.lock index 00d9bbc..6000e4c 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -7,7 +7,7 @@ packages: name: _fe_analyzer_shared url: "https://pub.dartlang.org" source: hosted - version: "49.0.0" + version: "50.0.0" alfred_workflow: dependency: "direct main" description: @@ -28,7 +28,7 @@ packages: name: analyzer url: "https://pub.dartlang.org" source: hosted - version: "5.1.0" + version: "5.2.0" analyzer_plugin: dependency: transitive description: @@ -91,7 +91,7 @@ packages: name: build_resolvers url: "https://pub.dartlang.org" source: hosted - version: "2.0.10" + version: "2.1.0" build_runner: dependency: "direct dev" description: @@ -120,6 +120,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "8.4.2" + charcode: + dependency: transitive + description: + name: charcode + url: "https://pub.dartlang.org" + source: hosted + version: "1.3.1" checked_yaml: dependency: transitive description: @@ -127,6 +134,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.1" + cli_script: + dependency: "direct main" + description: + name: cli_script + url: "https://pub.dartlang.org" + source: hosted + version: "0.2.6" clock: dependency: transitive description: @@ -182,7 +196,7 @@ packages: name: dart_code_metrics url: "https://pub.dartlang.org" source: hosted - version: "4.21.3" + version: "5.0.1" dart_style: dependency: transitive description: @@ -477,6 +491,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.0.0" + tuple: + dependency: transitive + description: + name: tuple + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.1" typed_data: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 0ece242..47a8fd3 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -13,11 +13,12 @@ dependencies: algolia: ^1.1.1 args: ^2.3.0 alfred_workflow: ^0.2.3 + cli_script: ^0.2.6 collection: ^1.16.0 json_annotation: ^4.7.0 dev_dependencies: build_runner: ^2.1.7 - dart_code_metrics: ^4.8.1 + dart_code_metrics: ^5.0.1 json_serializable: ^6.1.5 lints: ^2.0.0