GithubHelp home page GithubHelp logo

bmarkons / script Goto Github PK

View Code? Open in Web Editor NEW
1.0 0.0 0.0 64 KB

Compose neat Ruby scripts ๐Ÿ’Ž

Home Page: https://rubygems.org/gems/script

License: MIT License

Ruby 62.34% Shell 37.66%
ruby scripting helper

script's Introduction

Script

Build Status Gem Version

The Script is everything you need to make the most of Ruby - a fabulous scripting language. The one started with idea of having nicely formatted output, but ended up in much more...

Table of contents

Setup

Install gem:

gem install script

Or add it as dependency to your Gemfile:

gem 'script'

Then simply require it in your script file:

require 'script'

And you're good to go.

Usage

Steps

Steps encourages you to split commands into higher level constructs which are essential for more complex scripts.

Use Script to define the steps:

#!/usr/bin/env ruby

require "script"

Script.define do |s|
  s.step("step 1") do
    # Commands
  end

  s.step("step 2") do
    # Commands
  end
end

Once you've run the ruby script, it will execute each of the steps in the order they were defined.

Error handling

In case of an error in one of the steps, the script is stopped right away without executing further steps.

Output

Nicely formatted output is the primary motivation behind the Script. Most of the time you want to separate your commands output, in order to help investigation or debugging later.

Let's take for an example the following script:

#!/usr/bin/env ruby

require "script"

Script.define do |s|
  s.step("setup") do
    system("bundle install") 
  end

  s.step("build") do
    system("bundle exec rake spec")
  end

  s.step("deploy") do
    system("gem push script-*")
  end
end

It will produce the following output:

--------------------------------------------------------------------------------
Started: setup

Using rake 10.4.2
Using bundler 1.16.0
Using byebug 9.0.6
Using colorize 0.8.1
Using diff-lcs 1.3
Using rspec-support 3.7.0
Using rspec-core 3.7.0
Using rspec-expectations 3.7.0
Using rspec-mocks 3.7.0
Using rspec 3.7.0
Using rspec-autotest 1.0.0
Using script 1.0.0 from source at `.`
Bundle complete! 6 Gemfile dependencies, 12 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

Succeded: setup
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Started: build

/usr/bin/ruby2.3 -I/var/lib/gems/2.3.0/gems/rspec-core-3.7.0/lib:/var/lib/gems/2.3.0/gems/rspec-support-3.7.0/lib /var/lib/gems/2.3.0/gems/rspec-core-3.7.0/exe/rspec --pattern spec/\*\*\{,/\*/\*\*\}/\*_spec.rb

Script::Engine
  #register_step
    registers new step
  #run
    runs all registered steps

Script::Output
  #started
    returns the output to be printed before the step is run
  #result
    when the step has succeded
      returns the output with the result after the step has finished
    when the step has failed
      returns the output with the result after the step has finished

Script
  has a version number
  #step
    registers the step on the engine
  #run
    runs each step from the script

Finished in 0.01004 seconds (files took 0.07973 seconds to load)
8 examples, 0 failures


Succeded: build
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Started: deploy

Pushing gem to https://rubygems.org...
Successfully registered gem: script (1.0.0)

Succeded: deploy
--------------------------------------------------------------------------------

Shareables

In case you need to share data between steps, you can pass hash of shareables to the step block:

script.step("Step 1") do |shareables|
  shareables["environment"] = "production"
end

script.step("Step 2") do |shareables|
  environment = shareables["environment"]
  puts "Deploying on #{environment} environment"
end

Contributing

# Clone the repo
git clone [email protected]:bmarkons/script.git

# Install
bundle install

# Run tests
bundle exec rspec

Pull requests are always welcome. In case you notice any bug or simply want to propose an improvement, please feel free to open an issue.

Code of Conduct

Please be nice.

License

The gem is available as open source under the terms of the MIT License.

script's People

Stargazers

Adam Daniels avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.