Skip to content

Commit

Permalink
♻️ Refactor main file
Browse files Browse the repository at this point in the history
  • Loading branch information
techouse committed Nov 13, 2022
1 parent fc66279 commit da1eebf
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 128 deletions.
177 changes: 54 additions & 123 deletions bin/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,146 +10,77 @@ 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;
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<String> arguments) async {
try {
exitCode = 0;
bool _verbose = false;
bool _update = false;

workflow.clearItems();
void main(List<String> 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<String> 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<String> 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 <enter> to auto-update to a new version of this workflow.',
arg: 'update:workflow',
match:
'Auto-Update available! Press <enter> 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<void> _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();
}
}
}
});
}
78 changes: 78 additions & 0 deletions bin/main_helpers.dart
Original file line number Diff line number Diff line change
@@ -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 <enter> to auto-update to a new version of this workflow.',
arg: 'update:workflow',
match:
'Auto-Update available! Press <enter> 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<void> _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,
),
);
}
}
29 changes: 25 additions & 4 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -120,13 +120,27 @@ 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:
name: checked_yaml
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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit da1eebf

Please sign in to comment.