Skip to content

Commit

Permalink
⚗️ implement smart watch feature
Browse files Browse the repository at this point in the history
Signed-off-by: birjuvachhani <[email protected]>
  • Loading branch information
BirjuVachhani committed Feb 16, 2020
1 parent 872b360 commit 1840d72
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
9 changes: 6 additions & 3 deletions bin/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ ArgResults parseArguments(List<String> arguments) {
..addFlag('watch',
abbr: 'w',
negatable: false,
help: 'Watches for file changes and re-generates dart code')
help: 'Watches for any file changes and re-generates dart code')
..addFlag('smart-watch',
negatable: false,
help:
'Smartly watches for file changes that matters and re-generates dart code')
..addFlag('verbose',
abbr: 'v', negatable: false, help: 'prints verbose logs');

Expand All @@ -138,11 +142,10 @@ void processBuildCommand(ArgResults command) {
if (command.arguments.contains('--help')) {
stdout.writeln(HelpManuals.BUILD_HELP);
} else {
var watch = command.arguments.contains('--watch');
var verbose = command.arguments.contains('--verbose');
Logger.root.level = verbose ? Level.ALL : Level.INFO;
final spider = Spider(Directory.current.path);
spider.build(watch);
spider.build(command.arguments);
}
}

Expand Down
7 changes: 3 additions & 4 deletions lib/spider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,14 @@ class Spider {

/// Triggers build
/// [watch] determines if the directory should be watched for changes
void build(
bool watch,
) {
void build([List<String> options = const []]) {
if (groups == null) {
exit_with('No groups found in config file.');
}
for (var group in groups) {
var generator = DartClassGenerator(group);
generator.generate(watch);
generator.generate(
options.contains('--watch'), options.contains('--smart-watch'));
}
}

Expand Down
34 changes: 32 additions & 2 deletions lib/src/dart_class_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import 'dart:io';

import 'package:dart_style/dart_style.dart';
import 'package:path/path.dart' as path;
import 'package:watcher/watcher.dart';

import 'Formatter.dart';
import 'asset_group.dart';
Expand All @@ -35,10 +36,13 @@ class DartClassGenerator {
DartClassGenerator(this.group);

/// generates dart class code and returns it as a single string
void generate(bool watch) {
void generate(bool watch, bool smartWatch) {
if (watch) {
verbose('path ${group.path} is requested to be watched');
_watchDirectory();
} else if (smartWatch) {
verbose('path ${group.path} is requested to be watched smartly');
_smartWatchDirectory();
}
process();
}
Expand Down Expand Up @@ -113,8 +117,34 @@ ${properties_strings.join('\n')}
/// Watches assets dir for file changes and rebuilds dart code
void _watchDirectory() {
info('Watching for changes in directory ${group.path}...');
Directory(group.path).watch(events: FileSystemEvent.all).listen((data) {
final watcher = DirectoryWatcher(group.path);

watcher.events.listen((event) {
verbose('something changed...');
if (!_processing) {
_processing = true;
Future.delayed(Duration(seconds: 1), () => process());
}
});
}

/// Smartly watches assets dir for file changes and rebuilds dart code
void _smartWatchDirectory() {
info('Watching for changes in directory ${group.path}...');
final watcher = DirectoryWatcher(group.path);
watcher.events.listen((event) {
verbose('something changed...');
final filename = path.basename(event.path);
if (event.type == ChangeType.MODIFY) {
verbose('$filename is modified. '
'${group.className} class will not be rebuilt');
return;
}
if (!group.types.contains(path.extension(event.path))) {
verbose('$filename does not have allowed extension for the group '
'${group.path}. ${group.className} class will not be rebuilt');
return;
}
if (!_processing) {
_processing = true;
Future.delayed(Duration(seconds: 1), () => process());
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies:
args: ^1.5.2
dart_style: ^1.3.3
logging: ^0.11.4
watcher: ^0.9.7+13

dev_dependencies:
build_runner: ^1.7.4
Expand Down

0 comments on commit 1840d72

Please sign in to comment.