README.md in script-0.0.4 vs README.md in script-1.0.0
- old
+ new
@@ -6,10 +6,11 @@
###### Table of contents
- [Setup](#setup)
- [Usage](#usage)
- [Steps](#steps)
+ - [Output](#output)
- [Shareables](#shareables)
- [Contributing](#contributing)
- [License](#license)
- [Code of Conduct](#code-of-conduct)
@@ -19,69 +20,91 @@
```
gem install script
```
-And require it in your script file:
+Or add it as dependency to your Gemfile:
+```ruby
+gem 'script'
```
+
+Then simply require it in your script file:
+
+```
require 'script'
```
+And you're good to go.
+
## Usage
### Steps
-Reason about the step as a logical group of commands. Lets take for instance, the deploy script which builds a docker image, pushes it to Dockerhub and eventually creates the deployment on Kubernetes.
+Steps encourages you to split commands into higher level constructs which are essential for more complex scripts.
+Use Script to define the steps:
+
```ruby
-#!/usr/local/bin/ruby
+#!/usr/bin/env ruby
require "script"
-deploy = Script.new
+Script.define do |s|
+ s.step("step 1") do
+ # Commands
+ end
-deploy.step("Setup tools") do
- `sudo apt-get install -y google-cloud-sdk kubectl`
- `gcloud auth activate-service-account $GCLOUD_SERVICE_ACCOUNT_NAME`
- `gcloud config set project dummy-project`
- `gcloud container clusters get-credentials default --zone us-east`
+ s.step("step 2") do
+ # Commands
+ end
end
+```
-deploy.step("Deploy docker image") do
- `docker pull dummy`
- `docker build --cache-from dummy -t dummy`
- `docker build -t scripter/script .`
- `docker push dummy`
-end
+Once you've run the ruby script, it will execute each of the steps in the order they were defined.
-deploy.step("Deploy to Kubernetes cluster") do
- `kubectl apply -f k8s.yml --record`
-end
+### Error handling
-# Finally, run the script
+In case of an error in one of the steps, the script is stopped right away without executing further steps.
-deploy.run
-```
+### Output
-The steps are run in order in which they are registered. The output from the script commands is nicely formatted and divided per steps:
+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.
-![output](https://i.imgur.com/a6F2iAh.png)
+Let's take for an example the following script:
-In case of an exception in one of the steps, the execution of the script is aborted instantly.
+```ruby
+#!/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
+```
+
### Shareables
In case you need to share data between steps, you can pass hash of shareables to the step block:
```ruby
script.step("Step 1") do |shareables|
- shareables["environment"] = "production"
+shareables["environment"] = "production"
end
script.step("Step 2") do |shareables|
- environment = shareables["environment"]
- puts "Deploying on #{environment} environment"
+environment = shareables["environment"]
+puts "Deploying on #{environment} environment"
end
```
## Contributing