Skip to content

Convert multiline and commented groovy scripts into one line of code

License

Notifications You must be signed in to change notification settings

pedrocunha/groovy_oneliner

Repository files navigation

GroovyOneliner

Build Status Code Climate

Convert any groovy script file to a single line script. It is recommended (and also in order for this gem to work) that all lines must be terminated with ;.

What would this help with ?

In order to do scripting in ElasticSearch you can pick an handful of languages to inject code in your queries however ElasticSearch requires you that any script is written as a single line of code at query time.

One of the languages supported is Groovy. Groovy is very similar to Java.

GroovyOneliner was extracted from one of the production apps at HouseTrip. It allows to have a separate file for the Groovy script, which benifits: syntax highlighting, multilines, comments etc... and convert that script into one liner that you can use for queries.

What this gem won't do for you?

  • Check if there is any syntax errors
  • Check if you hate semi-colons

What does it actually do?

  • Removes all // & \* ... */ comments
  • Attempts to remove as much white space as possible such as empty lines, between ;, etc..

Some file examples and the corresponding output when using this gem:

Example

// This line is amazing
a = 1;

/*
 * Foo
 */
b = 2;

Converts to

a = 1;b = 2;

Installation

Add this line to your application's Gemfile:

gem 'groovy_oneliner'

And then execute:

$ bundle

Or install it yourself as:

$ gem install groovy_oneliner

Usage

Simplest case

GroovyOneliner.compute(path: 'path/to/file')

You can also concat files

GroovyOneliner.compute(path: ['path/to/file1', 'path/to/file2'])

If you want to cache reading the file (recommended for production & staging environments)

GroovyOneliner.compute(path: 'path/to/file', cache: true)

You can also set the cache at class level

GroovyOneliner.always_cache = true
GroovyOneliner.compute(path: 'path/to/file')

Contributing

  1. Fork it ( https://github.com/pedrocunha/groovy_oneliner/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

About

Convert multiline and commented groovy scripts into one line of code

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages