Kristina Ste
Kristina Ste

Reputation: 23

Is there any better way to execute several system commands from a rake task?

I have a rake task in my calabash project which executes several system commands, like:

desc 'Export and execute features from JIRA '
  task :run_jira, [:key, :language, :apk_file] do |_, args|
  key = args[:key]
  language = args[:language] || 'en'
  apk_file = args[:apk_file] || default_apk
  system "curl -u admin:admin -X GET http://myjira/rest/raven/1.0/export/test?keys=#{key} > jira.zip"
  system "unzip -o jira.zip -d features/jira"
  system "rm -rf jira.zip"
  system "calabash-android run #{apk_file} -p android -p #{language} -p common -f json -o results/reports/calabash-#{key}.json  features/jira"
  system "curl -H 'Content-Type: application/json' -X POST -u admin:admin --data @results/reports/calabash-#{key}.json http://myjira/rest/raven/1.0/import/execution/cucumber"
end

Is there any better way to execute those 5 system calls? My idea is to make an .sh script and start it from the task, but as the script will be executed on both OS X and Linux machines, I think it could create more troubles.

Upvotes: 2

Views: 399

Answers (1)

Keith Bennett
Keith Bennett

Reputation: 4970

You could join all those commands into a single command and execute that. Assuming you created an array commands containing all your commands, you could do this:

composite_command = commands.join('; ')
system(composite_command)

If you want execution to stop if any contain errors you could replace the semicolons with double ampersands ampersands:

composite_command = commands.join(' && ')
system(composite_command)

This illustrates what && does:

$ ls foo && echo hi
ls: foo: No such file or directory
$ touch foo
$ ls foo && echo hi
foo
hi

The shell defines failure as the returning of an exit code other than 0.

There is a maximum command length but I expect that it would always be at least 1024.

Upvotes: 2

Related Questions