Skip to content

Commit db87add

Browse files
authored
Improve examples (#82)
improve examples
1 parent 6e10277 commit db87add

File tree

52 files changed

+2642
-146
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+2642
-146
lines changed

README.md

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Create beautiful bash scripts from simple YAML configuration
1515

1616
</div>
1717

18+
1819
## Table of Contents
1920

2021
- [Table of Contents](#table-of-contents)
@@ -37,6 +38,7 @@ Create beautiful bash scripts from simple YAML configuration
3738

3839
---
3940

41+
4042
## Installation
4143

4244
```shell
@@ -84,8 +86,17 @@ Bahsly is responsible for:
8486
- **YAML parsing**.
8587
- and more.
8688

89+
8790
## Usage
8891

92+
The `bashly.yml` file can be set up to generate two types of scripts:
93+
94+
1. Script with commands (for example, like `docker` or `git`).
95+
2. Script without commands (for example, like `ls`)
96+
97+
This is detected automatically by the contents of the configuration: If it
98+
contains a `commands` definition, it will generate a script with commands.
99+
89100
In an empty directory, create a sample configuration file by running
90101

91102
```shell
@@ -153,32 +164,11 @@ $ ./download a --force
153164
downloading a with --force
154165
```
155166

156-
## Examples
157-
158-
The `bashly.yml` file can be set up to generate two types of scripts:
159-
160-
1. Script with commands (for example, like `docker` or `git`).
161-
2. Script without commands (for example, like `ls`)
162-
163-
This is detected automatically by the contents of the configuration: If it
164-
contains a `commands` definition, it will generate a script with commands.
165-
166167

167-
### Sample configuration for a script without commands
168-
169-
- Generate this script by running `bashly generate --minimal`
170-
- [See the initial sample bashly.yml file](examples/minimal/src/bashly.yml)
171-
- [See the generated bash script](examples/minimal/download)
172-
173-
### Sample configuration for a script with commands
174-
175-
- Generate this script by running `bashly generate`
176-
- [See the initial sample bashly.yml file](examples/commands/src/bashly.yml)
177-
- [See the generated bash script](examples/commands/cli)
178-
179-
180-
See the [examples](examples) folder for more examples.
168+
## Examples
181169

170+
The [examples folder](examples#readme) contains many detailed and documented
171+
example configuration files, with their output.
182172

183173

184174
## Configuration Reference
@@ -249,7 +239,7 @@ The `-v` and `-h` flags will be used as the short options for `--version` and
249239
`--help` respectively **only if you are not using them in any of your own
250240
flags**.
251241

252-
### Environment Variable options
242+
### Environment variable options
253243

254244
If an environment variable is defined as required (false by default), the
255245
execution of the script will be halted with a friendly error if it is not
@@ -337,7 +327,6 @@ The generated script will execute `git status`.
337327
See the [extensible-delegate example](examples/extensible-delegate).
338328

339329

340-
341330
## Real World Examples
342331

343332
- [Rush][rush] - a Personal Package Manager

Runfile

Lines changed: 95 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ end
1616

1717
help "Run shellcheck on all examples"
1818
action :shellcheck do
19-
examples.each do |example|
19+
Example.executables.each do |example|
2020
success = system "shellcheck #{example}"
2121
color = success ? 'txtgrn' : 'txtred'
2222
say "- shellcheck !#{color}!#{example}"
@@ -31,34 +31,100 @@ action :changelog do
3131
run "cat .changelog.old.md >> CHANGELOG.md"
3232
end
3333

34-
def examples
35-
[
36-
"examples/catch_all/download",
37-
"examples/catch_all_advanced/cli",
38-
"examples/colors/colorly",
39-
"examples/command-default/ftp",
40-
"examples/command-groups/ftp",
41-
"examples/commands-nested/cli",
42-
"examples/commands/cli",
43-
"examples/config-ini/configly",
44-
"examples/custom-includes/download",
45-
"examples/custom-strings/download",
46-
"examples/default-values/convert",
47-
"examples/dependencies/cli",
48-
"examples/docker-like/docker",
49-
"examples/environment-variables/cli",
50-
"examples/extensible-delegate/mygit",
51-
"examples/extensible/cli",
52-
"examples/git-like/git",
53-
"examples/minimal/download",
54-
"examples/minus-v/cli",
55-
"examples/multiline/multi",
56-
"examples/whitelist/login",
57-
"examples/yaml/yaml",
58-
"spec/fixtures/workspaces/catch-all-no-args/download",
59-
"spec/fixtures/workspaces/flag-args-with-dash/argflag",
60-
"spec/fixtures/workspaces/short-command-code/rush",
61-
]
34+
help "Append the content of bashly.yml to all example READMEs"
35+
action :examples do
36+
Example.all.each do |example|
37+
say example.dir
38+
example.regenerate_readme
39+
end
40+
end
41+
42+
class Example
43+
class << self
44+
def dirs
45+
@dirs ||= Dir['examples/*'].select { |f| File.directory? f }
46+
end
47+
48+
def all
49+
dirs.map { |dir| Example.new dir }
50+
end
51+
52+
def executables
53+
all.map &:executable
54+
end
55+
end
56+
57+
attr_reader :dir
58+
59+
def initialize(dir)
60+
@dir = dir
61+
end
62+
63+
def config
64+
@config ||= YAML.load_file(yaml_path)
65+
end
66+
67+
def yaml
68+
@yaml ||= File.read(yaml_path).strip
69+
end
70+
71+
def yaml_path
72+
"#{dir}/src/bashly.yml"
73+
end
74+
75+
def readme_path
76+
"#{dir}/README.md"
77+
end
78+
79+
def readme
80+
File.read readme_path
81+
end
82+
83+
def test_commands
84+
File.read("#{dir}/test.sh").scan(/\.\/#{config['name']}.*/)
85+
end
86+
87+
def test_output
88+
result = "```shell\n"
89+
test_commands.each do |command|
90+
result += "$ #{command}\n\n"
91+
Dir.chdir dir do
92+
result += `#{command} 2>&1`
93+
result += "\n\n"
94+
end
95+
end
96+
result += "```\n\n"
97+
result
98+
end
99+
100+
def regenerate_readme
101+
File.write readme_path, generated_readme
102+
end
103+
104+
def generated_readme
105+
marker = '-----'
106+
content = readme.split(marker)[0].strip
107+
<<~EOF
108+
#{content}
109+
110+
#{marker}
111+
112+
## `bashly.yml`
113+
114+
```yaml
115+
#{yaml}
116+
```
117+
118+
## Generated script output
119+
120+
#{test_output}
121+
122+
EOF
123+
end
124+
125+
def executable
126+
"#{dir}/#{config['name']}"
127+
end
62128
end
63129

64130
require './debug.rb' if File.exist? 'debug.rb'

examples/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Bashly Examples
2+
3+
Each of these examples demonstrates one aspect or feature of bashly.
4+
5+
## Basic use
6+
7+
- [minimal](minimal#readme) - the most basic "hello world" example
8+
- [commands](commands#readme) - a script with sub-commands
9+
- [commands-nested](commands-nested#readme) - a script with nested sub-commands
10+
11+
## Basic features
12+
13+
- [command-default](command-default#readme) - configuring a default command
14+
- [dependencies](dependencies#readme) - halting script execution unless certain dependencies are installed
15+
- [environment-variables](environment-variables#readme) - halting script execution unless certain environment variables are set
16+
- [default-values](default-values#readme) - arguments and flags with default values
17+
- [minus-v](minus-v#readme) - using `-v` and `-h` in your script
18+
- [multiline](multiline#readme) - help messages with multiple lines
19+
20+
## Advanced configuration features
21+
22+
- [catch-all](catch-all#readme) - a command that can receive an arbitrary number of arguments
23+
- [catch-all-advanced](catch-all-advanced#readme) - another example for the `catch_all` option
24+
- [extensible](extensible#readme) - letting your script's users extend the script
25+
- [extensible-delegate](extensible-delegate#readme) - extending your script by delegating commands to an external executable
26+
- [whitelist](whitelist#readme) - arguments and flags with a predefined allowed list of values
27+
28+
## Customization
29+
30+
- [command-groups](command-groups#readme) - grouping sub-commands in logical sections
31+
- [custom-strings](custom-strings#readme) - configuring the script's error and usage texts
32+
- [custom-includes](custom-includes#readme) - adding and organizing your custom functions
33+
34+
## Real-world-like examples
35+
36+
- [docker-like](docker-like#readme) - a sample script with deep commands (like `docker container run`)
37+
- [git-like](git-like#readme) - a sample script with sub-commands similar to git
38+
39+
## Bashly library features
40+
41+
- [config-ini](config-ini#readme) - using the config (INI) functions
42+
- [colors](colors#readme) - using the color print feature
43+
- [yaml](yaml#readme) - using the YAML reading functions

0 commit comments

Comments
 (0)