Skip to content

Create your first script

Svein Arne Ackenhausen edited this page Mar 25, 2014 · 1 revision

Extending OpenIDE is part of the workflow of using it. As developers we create small scripts to solve tasks in our daily routine. OpenIDE helps to structure all these small pieces of code. This is one of the main reasons for the local configuration points. All scripts created in the local configuration point can be checked into source control making the toolset part of the repository.

Scripts can be written in any language supporting stdin and stdout. Currently there are templates for bash, batch, python, javascript, ruby and more. Now let's create our first script. To create a new script in the local configuration point run the oi script new hello.py. If the editor and environment is running you will have the new script open in the editor. It's contents wil be something like this:

#!/usr/bin/env python
import sys

# Script parameters
#	Param 1: Script run location
#	Param 2: global profile name
#	Param 3: local profile name
#	Param 4-: Any passed argument
#
# When calling oi use the --profile=PROFILE_NAME and 
# --global-profile=PROFILE_NAME argument to ensure calling scripts
# with the right profile.
#
# To post back oi commands print command prefixed by command| to standard output
# To post a comment print to std output prefixed by comment|
# To post an error print to std output prefixed by error|

def printDefinitions():
	# Definition format usually represented as a single line:

	# Script description|
	# command1|"Command1 description"
	# 	param|"Param description" end
	# end
	# command2|"Command2 description"
	# 	param|"Param description" end
	# end
	sys.stdout.write("Script description\n")

def main(argv):
	if len(argv) > 1:
		if argv[2] == 'get-command-definitions':
			printDefinitions()
			return
	#Place script core here

if __name__ == "__main__":
	main(sys.argv)

As you can see from the contents there are two ways to call a script. First off the script will be called using KEY and get-command-definitions as parameters. The script then writes back it's usage information as seen in the example. When run the script working directory will be the configuration root. Now let's change the script so that it looks like this:

#!/usr/bin/env python
import sys

def write(line):
	sys.stdout.write(line+"\n")

def printDefinitions():
	write('Prints greeting message|')
	write('NAME|"The person to greet" end ')

def main(argv):
	if len(argv) > 1:
		if argv[2] == 'get-command-definitions':
			printDefinitions()
			return

	if len(argv) == 5:
		write('Hello ' + argv[4])
	else:
		write('error|Invalid arguments')

if __name__ == "__main__":
	main(sys.argv)

Now if we run the command oi help hello we can see the usage information for the script. If we run oi hello Joe We'll see Hello Joe printed to the screen. To list available scripts run oi script

Clone this wiki locally