Skip to content

Commit c86905e

Browse files
authored
doc: Switching documentation to Sphinx (#6)
1 parent fc800a0 commit c86905e

17 files changed

+1222
-12
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: documentation
2+
run-name: ${{ github.ref_name }} documentation
3+
on: [push, pull_request]
4+
permissions:
5+
contents: write
6+
jobs:
7+
docs:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v3
11+
- uses: actions/setup-python@v3
12+
- name: Install dependencies
13+
run: |
14+
pip install sphinx sphinx_rtd_theme myst_parser
15+
- name: Sphinx build
16+
run: |
17+
sphinx-build doc _build
18+
- name: Deploy to GitHub Pages
19+
uses: peaceiris/actions-gh-pages@v3
20+
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
21+
with:
22+
publish_branch: gh-pages
23+
github_token: ${{ secrets.GITHUB_TOKEN }}
24+
publish_dir: _build/
25+
force_orphan: true

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11

22
*~
33
.objects/
4+
doc/_build

README.md

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
[![test (mac)](https://github.com/EngineeringSoftware/gobash/actions/workflows/test-mac.yml/badge.svg)](https://github.com/EngineeringSoftware/gobash/actions/workflows/test-mac.yml)
77
[![lint](https://github.com/EngineeringSoftware/gobash/actions/workflows/lint.yml/badge.svg)](https://github.com/EngineeringSoftware/gobash/actions/workflows/lint.yml)
88

9-
`gobash` library is a set of functions that improve programming
10-
experience in `bash` (by providing collections, languages features,
11-
APIs, testing package, command line flag parsing, etc.) without
12-
modifying the shell interpreter(s). It works with any bash version
13-
(on Linux and Mac). Parts of the API are matching those in Go.
9+
`gobash` is a set of functions that improve programming experience in
10+
`bash` (by providing collections, languages features, APIs, testing
11+
package, command line flag parsing, etc.) without modifying the shell
12+
interpreter(s). It works with any bash version (on Linux and Mac).
13+
Parts of the API are matching those in Go.
1414

1515
If you cannot wait to see code, here is a quick example (but check
1616
later sections for a lot more):
@@ -411,7 +411,6 @@ make_and_print
411411
# }
412412
```
413413

414-
415414
### Methods
416415

417416
Adding a method to a struct is done by implementing a function that is
@@ -753,9 +752,9 @@ $ $p to_string
753752
# }
754753
```
755754

756-
One of the (obvious) implications is that you can now write scripts
757-
that accept objects, and those scripts can be invoked from your
758-
terminal with objects made in the terminal process.
755+
One of the implications is that you can now write scripts that accept
756+
objects, and those scripts can be invoked from your terminal with
757+
objects made in the terminal process.
759758

760759
Consider the script below (`ai`). (This is the same example we used
761760
in an earlier section to illustrate inter-process communication.)
@@ -980,9 +979,9 @@ improving the performance, but we might wait for a couple of users.
980979

981980
## Dependencies
982981

983-
`gobash` uses several binaries widely avaialble on Unix. Although
984-
things keep changing, the list likely includes `jq`, `sed`, `grep`,
985-
`awk`, `date`.
982+
`gobash` uses several bash builtins, GNU coreutils, and binaries
983+
widely avaialble on Unix. Although the repository keep changing, the
984+
list probably includes `jq`, `sed`, `grep`, `awk`, `date`.
986985

987986

988987
## Versioning

doc/Makefile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Minimal makefile for Sphinx documentation
2+
#
3+
4+
# You can set these variables from the command line, and also
5+
# from the environment for the first two.
6+
SPHINXOPTS ?=
7+
SPHINXBUILD ?= sphinx-build
8+
SOURCEDIR = .
9+
BUILDDIR = _build
10+
11+
# Put it first so that "make" without argument is like "make help".
12+
help:
13+
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
14+
15+
.PHONY: help Makefile
16+
17+
# Catch-all target: route all unknown targets to Sphinx using the new
18+
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
19+
%: Makefile
20+
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

doc/collections.rst

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
Collections
3+
===========
4+
5+
gobash introduces two key collections: `List` and `Map`. Both
6+
collections include a number of methods that can be convenient for
7+
everyday development.
8+
9+
In the example below, we use an instance of a `List` to keep URLs of
10+
several GitHub projects and then close each of those projects in a
11+
loop.
12+
13+
.. code-block:: bash
14+
15+
#!/bin/bash
16+
. gobash/gobash
17+
18+
lst=$(List)
19+
$lst add "https://github.com/apache/commons-math"
20+
$lst add "https://github.com/apache/commons-io"
21+
22+
# Print length.
23+
$lst len
24+
25+
# Clone each repo.
26+
for (( i=0; i<$($lst len); i++)); do
27+
git clone $($lst get $i)
28+
done
29+
30+
# Print the list.
31+
$lst to_string
32+
33+
.. note::
34+
35+
Equality in `gobash` is done based on object identity. Future
36+
changes could consider using `eq` methods to check for equality
37+
(similar to other programming languages).
38+
39+
.. toctree::
40+
:maxdepth: 2

doc/command-line-flags.rst

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
Command Line Flags
3+
==================
4+
5+
gobash can simplify parsing command line flags. In the next example,
6+
we illustrate parsing using the `flags` package. Specifically, we
7+
create `Flags` with desired documentation and add two flags. Each flag
8+
has to include name, type (int, bool, float, or string), and
9+
documentation.
10+
11+
.. code-block:: bash
12+
13+
#!/bin/bash
14+
. gobash/gobash
15+
16+
min=$(Flag "x" "int" "Min value.")
17+
max=$(Flag "y" "int" "Max value.")
18+
19+
flags=$(Flags "Flags to demo flag parsing.")
20+
$flags add "$min"
21+
$flags add "$max"
22+
23+
Once we build the flags, we can print a help message simply like this:
24+
25+
.. code-block:: bash
26+
27+
$flags help
28+
29+
Parsing flags is then done in a few steps:
30+
31+
.. code-block:: bash
32+
33+
args=$(Args) # an object that will keep parsed values
34+
ctx=$(ctx_make) # context will store an issue is encountered during parsing
35+
$flags $ctx parse "$args" "$@" || \
36+
{ ctx_show $ctx; exit 1; } # checking for errors
37+
38+
$args x # print the parsed x value
39+
$args y # print the parsed y value
40+
41+
.. toctree::
42+
:maxdepth: 2

doc/conf.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Configuration file for the Sphinx documentation builder.
2+
#
3+
# For the full list of built-in configuration values, see the documentation:
4+
# https://www.sphinx-doc.org/en/master/usage/configuration.html
5+
6+
# -- Project information -----------------------------------------------------
7+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
8+
9+
project = 'gobash'
10+
copyright = '2024, Milos Gligoric'
11+
author = 'Milos Gligoric'
12+
13+
# -- General configuration ---------------------------------------------------
14+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
15+
16+
extensions = []
17+
18+
templates_path = ['_templates']
19+
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
20+
21+
22+
23+
# -- Options for HTML output -------------------------------------------------
24+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
25+
26+
html_theme = 'alabaster'
27+
html_static_path = ['_static']

doc/design.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
Design
3+
======
4+
5+
Key design goal: Implement everything using functions and files (i.e.,
6+
there is no need to change existing shells or existing user code).
7+
8+
As the result, a user can adopt gobash as needed without being forced
9+
to rewrite any of their code, but can benefit by using some/all of
10+
gobash features, which can be introduced gradually. Additionally,
11+
gobash should work with any `bash` version and OS that runs `bash`.
12+
13+
.. toctree::
14+
:maxdepth: 2

doc/get-started.rst

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
2+
Get Started
3+
===========
4+
5+
It is trivial to get started using gobash, because it only adds to the
6+
knowledge you already have about shell programming. Also, it does not
7+
require any special setup, because everything is pure bash.
8+
9+
There are two modes in which gobash can be used: (a) as a library,
10+
or (b) as a tool. Most of the examples below use gobash as a library
11+
(i.e., source gobash into a script and use available functions). There
12+
is a separate section to describe using gobash as a tool.
13+
14+
We will use several examples in this section to get you started.
15+
16+
Prepare Environment
17+
-------------------
18+
19+
Create a space for trying gobash:
20+
21+
.. code-block:: bash
22+
23+
mkdir space
24+
cd space
25+
26+
Clone the gobash repository:
27+
28+
.. code-block:: bash
29+
30+
git clone [email protected]:EngineeringSoftware/gobash
31+
32+
Alternatively, you can avoid cloning the repo and directly source the
33+
library in your bash script:
34+
35+
.. code-block:: bash
36+
37+
source /dev/stdin <<< "$(curl https://raw.githubusercontent.com/EngineeringSoftware/gobash/master/hsabog 2>/dev/null)"
38+
39+
Now, we are ready to run some examples.
40+
41+
Collection Example
42+
------------------
43+
44+
Write your first script (let's call it `s`) that uses gobash. The
45+
example below "imports" the entire gobash library and uses the `List`
46+
collection for storing values.
47+
48+
.. code-block:: bash
49+
50+
#!/bin/bash
51+
. gobash/gobash
52+
53+
# Instantiate a list and add two elements.
54+
lst=$(List)
55+
$lst add 55
56+
$lst add 100
57+
58+
# Get the length of the list.
59+
$lst len
60+
# Output: 2
61+
62+
# Print the list (default print is in the json format).
63+
$lst to_string
64+
# Output:
65+
# [
66+
# "55",
67+
# "100"
68+
# ]
69+
70+
Struct Example
71+
--------------
72+
73+
In the following example (`point.sh`), we introduce a `struct` for a
74+
2D point, set/get values, and write a function to add two points.
75+
76+
.. code-block:: bash
77+
78+
#!/bin/bash
79+
. gobash/gobash
80+
81+
function Point() {
82+
make_ $FUNCNAME \
83+
"x" "$1" \
84+
"y" "$2"
85+
}
86+
87+
function point_add() {
88+
local p1="${1}"
89+
local p2="${2}"
90+
91+
local x=$(( $($p1 x) + $($p2 x) ))
92+
local y=$(( $($p1 y) + $($p2 y) ))
93+
local p3=$(Point "${x}" "${y}")
94+
echo "$p3"
95+
}
96+
97+
p1=$(Point 3 4)
98+
p2=$(Point 8 9)
99+
p3=$(point_add "$p1" "$p2")
100+
$p3 to_string
101+
# Output:
102+
# {
103+
# "x": "11",
104+
# "y": "13"
105+
# }
106+
107+
Test Example
108+
------------
109+
110+
This example illustrate a way to write tests using a testing package.
111+
The tests can be executed with the gobash tool.
112+
113+
We will extend the previous example to add tests (`point_test.sh`) for
114+
the function `point_add`.
115+
116+
.. code-block:: bash
117+
118+
#!/bin/bash
119+
120+
. point.sh
121+
122+
function test_point() {
123+
p1=$(Point 3 4)
124+
p2=$(Point 8 9)
125+
p3=$(point_add "$p1" "$p2")
126+
assert_ze $?
127+
assert_eq 11 $($p3 x)
128+
assert_eq 13 $($p3 y)
129+
}
130+
131+
Tests can be run with the following command:
132+
133+
.. code-block:: bash
134+
135+
./gobash test --paths point_test.sh --verbose
136+
137+
The output of this execution will be along these lines:
138+
139+
.. code-block:: bash
140+
141+
test_point start
142+
test_point PASSED
143+
./point_test.sh 1[sec]
144+
Tests run: 1, failed: 0, skipped: 0.
145+
Total time: 1[sec]
146+
147+
Further Reading
148+
---------------
149+
150+
There are a number of other examples that illustrate gobash in the
151+
`examples directory
152+
<https://github.com/EngineeringSoftware/gobash/tree/main/examples/README.md>`_.
153+
If you like learning by examples, that is the best place to go next.
154+
If you prefer to read higher level doc, then check
155+
:doc:`language`.
156+
157+
.. toctree::
158+
:maxdepth: 2

0 commit comments

Comments
 (0)