|
| 1 | +# Inline `source` |
| 2 | + |
| 3 | +Inline `source`-ed code. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +The advised installation method is using the [`bpkg` package manager][bpkg], as |
| 8 | +this will allow versioning to be used. |
| 9 | + |
| 10 | +Alternatively, the latest version of this project's main script can be |
| 11 | +downloaded directly: |
| 12 | + |
| 13 | +```sh |
| 14 | +curl -Lo- https://bpkg.pother.ca/inline-source/dist/inline_source |
| 15 | +chmod +x inline_source |
| 16 | +``` |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +This script will output a file for a given path, will all `sourced` files inlined. |
| 21 | + |
| 22 | +```sh |
| 23 | +inline_source <source-file> |
| 24 | +``` |
| 25 | + |
| 26 | +For instance, given a file `a.sh` and file `b.sh` like this: |
| 27 | + |
| 28 | +**a.sh** |
| 29 | +```sh |
| 30 | +#!/usr/bin/env bash |
| 31 | + |
| 32 | +echo 'I am file A.' |
| 33 | + |
| 34 | +source 'b.sh.' |
| 35 | +``` |
| 36 | + |
| 37 | +**b.sh** |
| 38 | +```sh |
| 39 | +#!/usr/bin/env bash |
| 40 | + |
| 41 | +echo 'I am file B.' |
| 42 | +``` |
| 43 | + |
| 44 | +Calling `inline_source a.sh` will output: |
| 45 | + |
| 46 | +```sh |
| 47 | +#!/usr/bin/env bash |
| 48 | + |
| 49 | +echo 'I am file A.' |
| 50 | + |
| 51 | +echo 'I am file B.' |
| 52 | +``` |
| 53 | + |
| 54 | +If a script sources files that are not in the `$PATH`, this will need to be |
| 55 | +resolved before calling `inline_source`: |
| 56 | + |
| 57 | +```sh |
| 58 | +export PATH="${PATH}:/path/to/sources" && inline_source a.sh |
| 59 | +``` |
| 60 | + |
| 61 | +All that really happens is that the code of any file that is `sourced` in the |
| 62 | +main script is placed inline. |
| 63 | + |
| 64 | +This allows for a single script to be created for distribution purposes. |
| 65 | + |
| 66 | +For instance, the distribution file in this repo was created by pipe-ing the |
| 67 | +output to a file: |
| 68 | + |
| 69 | +```sh |
| 70 | +inline_source inline_source.sh > dist/inline_source |
| 71 | +``` |
| 72 | + |
| 73 | +The script leaves everything "as-is", except for any shebang lines that it might |
| 74 | +find. To clean things up, pipe this script through a formatter, for instance |
| 75 | +[shfmt](https://github.com/mvdan/sh): |
| 76 | + |
| 77 | +```sh |
| 78 | +inline_source inline_source.sh | shfmt -i 2 -ci -s > dist/inline_source |
| 79 | +``` |
| 80 | + |
| 81 | +## Development |
| 82 | + |
| 83 | +This repository is set up like this: |
| 84 | + |
| 85 | +``` |
| 86 | + . |
| 87 | + ├── deps/ <-- Third-party dependencies |
| 88 | + ├── dist/ <-- Bundled distrubtion script |
| 89 | + ├── src/ <-- Source files |
| 90 | + ├── bpkg.json <-- Package declaration |
| 91 | + └── README.md <-- You are here |
| 92 | +``` |
| 93 | + |
| 94 | +Dependencies needed by this project are managed through [`bpkg`][bpkg]. |
| 95 | + |
| 96 | +To install them (in the `deps/` folder) run: |
| 97 | + |
| 98 | +```sh |
| 99 | +bpkg getdeps |
| 100 | +``` |
| 101 | + |
| 102 | +After that, edits can be made to files in the `src/` directory. |
| 103 | + |
| 104 | +Finally, to create a single file containing all the source logic (this time |
| 105 | +using a docker image for `shfmt`), run: |
| 106 | + |
| 107 | +```sh |
| 108 | +bash inline_source.sh inline_source.sh \ |
| 109 | + | docker run -i --rm --volume="$PWD:/mnt" -w /mnt mvdan/shfmt -i 2 -ci -s \ |
| 110 | + > dist/inline_source |
| 111 | +``` |
| 112 | + |
| 113 | +This will create a distribution file in the `dist/` directory. |
| 114 | + |
| 115 | +[bpkg]: https://github.com/bpkg/bpkg |
0 commit comments