-
Notifications
You must be signed in to change notification settings - Fork 15
Create your first reactive script
A reactive script is a script that reacts to environment events. Here we will look at how to create a basic script triggering on file changes. To create new reactive script run the command oi rscript new hello-filechange.py. This will open the new rscript in the editor. It will look something like this:
#!/usr/bin/env python
import sys
def main(argv):
if len(argv) > 1:
if argv[1] == 'reactive-script-reacts-to':
# Write one event pr line that this script will react to
# print "'goto*.cs|*"
return
# Write scirpt code here.
# Param 1: event
# Param 2: global profile name
# Param 3: local profile name
#
# When calling other commands use the --profile=PROFILE_NAME and
# --global-profile=PROFILE_NAME argument to ensure calling scripts
# with the right profile.
if __name__ == "__main__":
main(sys.argv)
First off the script will be called with reactive-script-reacts-to. The script will then respond with event patterns it reacts to. Let's modify the script to react to new and changed files and just print some script output. First off when working with reactive scripts it's convenient to listen to it's output through the event listener. To start listening run oi event-listener "'rscript-hello-filechange*". Now update the script and start changing some files.
#!/usr/bin/env python
import sys
def main(argv):
if len(argv) > 1:
if argv[1] == 'reactive-script-reacts-to':
print("'codemodel' 'raw-filesystem-change-filecreated' *")
print("'codemodel' 'raw-filesystem-change-filechanged' *")
return
print("Changed file: " + argv[1][49:-1])
if __name__ == "__main__":
main(sys.argv)