Browse Source

Add option for running sets

This commit adds the ability to define multiple commands to be run
in a set.

Sets are run the same way as commands, with the --run option.
master
Blaine Motsinger 3 months ago
parent
commit
235b44a97f
  1. 4
      .onchange.example
  2. 56
      README
  3. 83
      bin/onchange

4
.onchange.example

@ -6,3 +6,7 @@ file=testing
[command]
example=echo 'this is an example command'
another_example=echo 'this is another example command'
yet_another_example=echo 'this is not included in set examples'
[set]
examples=example, another_example

56
README

@ -2,25 +2,26 @@ NAME
onchange - run commands when files change
SYNOPSIS
onchange [--run <command>]
onchange [--run <command|set>]
[--help]
DESCRIPTION
"onchange" is a program that watches for changes to files and
directories then runs configured sets of commands.
directories then runs configured commands.
"onchange" detects changes recursively within the current working
directory. Files/directories to ignore and commands to run are read from
the ".onchange" file within the current working directory.
OPTIONS
--run <command>
Defines which commands to run on changes.
--run <command|set>
Defines which command or set to run on changes.
The "command" specified must be defined in the ".onchange"
The "command" or "set" specified must be defined in the ".onchange"
configuration file.
Multiple "--run" options may be defined.
Multiple "--run" options may be defined and are run in the order
they're passed.
--help
Print the help menu.
@ -55,6 +56,28 @@ CONFIGURATION
$ onchange --run example --run another_example
(will run example, then another_example)
set
The "set" section key.
Keys within this section define sets of commands to run when that set
argument is passed.
[command]
example=echo 'this is an example command'
another_example=echo 'this is another example command'
yet_another_example=echo 'this is not included in set examples'
[set]
examples=example, another_example
Multiple commands can be defined per line if separated by comma.
Defined commands are run as arguments to "onchange". Multiple commands
are run in the order they're passed.
$ onchange --run examples --run yet_another_example
(will run example, then another_example, then yet_another_example)
EXAMPLES
Run two specific commands when changes are detected
# create a .onchange file in the directory to watch
@ -66,10 +89,13 @@ CONFIGURATION
[command]
example=echo 'this is an example command'
another_example=echo 'this is another example command'
yet_another_example=echo 'this is yet another example command'
yet_another_example=echo 'this is not included in set examples'
[set]
examples=example, another_example
# run onchange, specifying both commands to run as defined in the .onchange file
~/git/example $ onchange --run example --run another_example
~/git/example $ onchange --run example --run another_example
[1617588530][ignore]: scratch,testing
[1617588530][ignore]: tmp,t
[1617588530][watch]: /home/blaine/git/example
@ -88,3 +114,17 @@ CONFIGURATION
[1617588707][run another_example]: echo 'this is another example command'
this is another example command
Run a set of commands and one specific command when changes are detected
# with the following commands and set in the .onchange file
[command]
example=echo 'this is an example command'
another_example=echo 'this is another example command'
yet_another_example=echo 'this is not included in set examples'
[set]
examples=example, another_example
# run onchange, specifying the set and additional command to run
~/git/example $ onchange --run examples --run yet_another_example
(will run example, then another_example, then yet_another_example)

83
bin/onchange

@ -28,10 +28,20 @@ my $config = load_config($dir);
my $commands = [];
foreach my $key (@{$opt{run}}) {
Pod::Usage::pod2usage( -message => "Argument $key is not a defined command", -exitval => 1 )
unless defined $config->{command}{$key};
Pod::Usage::pod2usage( -message => "Argument $key is not a defined command or set", -exitval => 1 )
unless ( defined $config->{command}{$key} || defined $config->{set}{$key} );
push @{$commands}, { $key => $config->{command}{$key} };
if ( defined $config->{command}{$key} ) {
push @{$commands}, { $key => $config->{command}{$key} };
}
if ( defined $config->{set}{$key} ) {
my $string = $config->{set}{$key};
$string =~ s/\s+//g;
push @{$commands},
map +( { $_ => $config->{command}{$_} } ),
extract_list($string);
}
}
my $dir_exclude = $dir . "/(?:.+\/)?";
@ -47,7 +57,7 @@ foreach my $key ( keys %{$config->{ignore}} ) {
push @{$exclude},
map { qr($dir_exclude$_) }
( $string =~ /,/ ? split /,/, $string : $string );
extract_list($string);
}
my $watcher =
@ -66,6 +76,12 @@ while ( my @events = $watcher->wait_for_events ) {
exit 0;
sub extract_list {
my $string = shift;
return ( $string =~ /,/ ? split /,/, $string : $string );
}
sub logger {
my $event = shift;
my $entry = shift;
@ -124,13 +140,13 @@ onchange - run commands when files change
=head1 SYNOPSIS
onchange [--run <command>]
onchange [--run <command|set>]
[--help]
=head1 DESCRIPTION
C<onchange> is a program that watches for changes to files and directories
then runs configured sets of commands.
then runs configured commands.
C<onchange> detects changes recursively within the current working directory.
Files/directories to ignore and commands to run are read from the C<.onchange>
@ -140,13 +156,13 @@ file within the current working directory.
=over
=item --run <command>
=item --run <command|set>
Defines which commands to run on changes.
Defines which command or set to run on changes.
The C<command> specified must be defined in the C<.onchange> configuration file.
The C<command> or C<set> specified must be defined in the C<.onchange> configuration file.
Multiple C<--run> options may be defined.
Multiple C<--run> options may be defined and are run in the order they're passed.
=item --help
@ -181,12 +197,35 @@ argument is passed.
example=echo 'this is an example command'
another_example=echo 'this is another example command'
Defined commands are run as arguments to "onchange". Multiple commands are
Defined commands are run as arguments to C<onchange>. Multiple commands are
run in the order they're passed.
$ onchange --run example --run another_example
(will run example, then another_example)
=head2 set
The C<set> section key.
Keys within this section define sets of commands to run when that set
argument is passed.
[command]
example=echo 'this is an example command'
another_example=echo 'this is another example command'
yet_another_example=echo 'this is not included in set examples'
[set]
examples=example, another_example
Multiple commands can be defined per line if separated by comma.
Defined commands are run as arguments to C<onchange>. Multiple commands are
run in the order they're passed.
$ onchange --run examples --run yet_another_example
(will run example, then another_example, then yet_another_example)
=head2 EXAMPLES
=head3 Run two specific commands when changes are detected
@ -200,10 +239,13 @@ run in the order they're passed.
[command]
example=echo 'this is an example command'
another_example=echo 'this is another example command'
yet_another_example=echo 'this is yet another example command'
yet_another_example=echo 'this is not included in set examples'
[set]
examples=example, another_example
# run onchange, specifying both commands to run as defined in the .onchange file
~/git/example $ onchange --run example --run another_example
~/git/example $ onchange --run example --run another_example
[1617588530][ignore]: scratch,testing
[1617588530][ignore]: tmp,t
[1617588530][watch]: /home/blaine/git/example
@ -222,4 +264,19 @@ run in the order they're passed.
[1617588707][run another_example]: echo 'this is another example command'
this is another example command
=head3 Run a set of commands and one specific command when changes are detected
# with the following commands and set in the .onchange file
[command]
example=echo 'this is an example command'
another_example=echo 'this is another example command'
yet_another_example=echo 'this is not included in set examples'
[set]
examples=example, another_example
# run onchange, specifying the set and additional command to run
~/git/example $ onchange --run examples --run yet_another_example
(will run example, then another_example, then yet_another_example)
=cut
Loading…
Cancel
Save