Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

Excerpt

Provides a shell-like capability for handling external processes.

Module Overview

Groosh is a Unix like shell written in Groovy. It has also been known as the Process Module but was renamed (back) when it was updated to work with Groovy 1.0.

The current version of Groosh is 0.3.6

Team Members

  • Yuri Schimke - Founder and original contributor
  • Alexander Egger [alexander.egger at gmail.com] - Current maintainer

Download

Distributions

Source code and binary releases are available at http://kenai.com/projects/groovy-groosh.

Groosh can be used via Grapes.

Note: If Ivy (Grapes) can not download Groosh add the file

Code Block
grapeConfig.xml

to your

Code Block
~/.groovy

directory with the following content:

Code Block
<ivysettings>
  <property name="ivy.checksums" value=""/>
  <settings defaultResolver="downloadGrapes"/>
  <resolvers>
    <chain name="downloadGrapes">
      <filesystem name="cachedGrapes">
        <ivy pattern="${user.home}/.groovy/grapes/[organisation]/[module]/ivy-[revision].xml"/>
        <artifact pattern="${user.home}/.groovy/grapes/[organisation]/[module]/[type]s/[artifact]-[revision].[ext]"/>
      </filesystem>
      <!-- todo add 'endorsed groovy extensions' resolver here -->
      <ibiblio name="codehaus" root="http://repository.codehaus.org/" m2compatible="true"/>
      <ibiblio name="ibiblio" m2compatible="true"/>
      <ibiblio name="java.net2" root="http://download.java.net/maven/2/" m2compatible="true"/>
    </chain>
  </resolvers>
</ivysettings>

There seams to be some problem with checksums generated by Maven 2.2.1

Installing

No installation required. Groosh can be added to Groovy using Grapes.

Try the following script to check if it worked:

Code Block
@Grapes([
 @Grab(group='org.codehaus.groovy.modules',module='groosh',version='[0.3.6,)'),
 @GrabConfig(systemClassLoader=true)
])

import groosh.Groosh
Groosh.withGroosh(this)

ls() >> stdout

Pre-requisites

Groosh is based on Groovy 1.7.0 or higher.

Documentation

The following example shows Groosh in action:

Code Block
//Read a text file and write it to stdout
@Grapes([
 @Grab(group='org.codehaus.groovy.modules',module='groosh',version='[0.3.6,)'),
 @GrabConfig(systemClassLoader=true)
])

import groosh.Groosh
Groosh.withGroosh(this)

cat('test_scripts/blah.txt') >> stdout

Another example :

Code Block
//Count the lines of all .java files in this directory and all its subdirectories.
//Write the total number of lines to stdout.

@Grapes([
 @Grab(group='org.codehaus.groovy.modules',module='groosh',version='[0.3.6,)'),
 @GrabConfig(systemClassLoader=true)
])

import groosh.Groosh
Groosh.withGroosh(this)

def f = find('.', '-name', '*.java', '-ls')
def total = 0
def lines = grid { values,w ->
  def x = values[2,4,6,10]
  def s = x.join('	')
  w.println(s)
  total += Integer.parseInt(values[6])
}

f | lines
lines >> stdout
println "Total: ${total}"

Sometimes the name of a shell command conflicts with a Groovy method (for example ''grep''). This means that

Code Block
gsh.grep(...)

does not execute the shell command, but the Groovy method grep(...).

As a workaround for that you may prefix any shell command with _ this means the example above becomes

Code Block
gsh._grep(...)

The following example shows a more elaborate example. It uploads photos to a flickr account using the command line tool flickcurl. A photo set of this images is created and named after the current directory.

Code Block
import static groosh.Groosh.groosh
import static org.codehaus.groovy.groosh.stream.DevNull.devnull

ids = [:]
shell = groosh()

//get all images in this folder and upload it to flickr
//remember the photo id we get from flickr
shell.ls().grep(~/.*jpg/).each {
  println "Uploading file $it to flickr"
  flickcurl = shell.flickcurl("upload",it,"friend","family").useError(true)
  id = flickcurl.grep(~/.*Photo ID.*/)[0].split(":")[1].trim()
  ids[it] = id
  println "Photo ID is: $id"
}
//we need to know the first photo id
firstKey = ids.keySet().toList()[0]

//create a set with the name of the directory we are in right now
//use the id of the first photo as set cover
setName = shell.pwd().text.split("/")[-1]
println "Creating set: $setName"
flickcurl = shell.flickcurl("photosets.create",setName,setName,ids[firstKey]).useError(true)
id = flickcurl.grep(~/.*Photoset.*/)[0].split(" ")[2].trim()
println "Photoset ID is: $id"

//make a backup of the ids in a file for later reference
println "Writing ids to a file"
file = new File(shell.pwd().text.trim() + "/.flickrset")
file << "Photoset:" << id << "\n"
ids.each {
  file << it.key << ":" << it.value << "\n"    
}

//the first photo is already part of the photo set so lets remove it
ids.remove(firstKey)

//add the remaining photos to the photo set
ids.each {
  println "Adding photo to set at flickr: $it"
  shell.flickcurl("photosets.addPhoto",id,it.value) | devnull()
}

println "DONE"

Developers

Source Control

The Groosh source code is available from https://svn.kenai.com/svn/groovy-groosh~subversion.

Building

Building with Netbeans

Open the Kenai project in Netbeans and run build.

Building without Netbeans

Check out the source from
https://svn.kenai.com/svn/groovy-groosh~subversion

Groosh uses Maven 2 for building. You have to have Maven 2 installed.
Just execute mvn in the groosh directory groosh gets build.

Contributing

Please contact Alexander Egger [alexander.egger at gmail.com] by e-mail.

Community

For mailing lists, issue trackers, forums etc see http://kenai.com/projects/groovy-groosh