|
| 1 | +# Using the command-line tool |
| 2 | + |
| 3 | +`swift-doc` can be used from the command-line on macOS and Linux. |
| 4 | + |
| 5 | +Before you can use `swift-doc`, you need to install it first. It's available via Homebrew and as a Docker container. |
| 6 | +Alternatively, you can always build it from sources. |
| 7 | + |
| 8 | +## Installation |
| 9 | +### Homebrew |
| 10 | + |
| 11 | +[Homebrew](https://brew.sh/) is a free and open-source package management for macOS and Linux. If you are using Homebrew, |
| 12 | +run the following command to install `swift-doc` using Homebrew: |
| 13 | + |
| 14 | +```terminal |
| 15 | +$ brew install swiftdocorg/formulae/swift-doc |
| 16 | +``` |
| 17 | + |
| 18 | +### Docker |
| 19 | + |
| 20 | +You can run `swift-doc` from the latest [Docker](https://www.docker.com) image with the following commands: |
| 21 | + |
| 22 | +```terminal |
| 23 | +$ docker pull swiftdoc/swift-doc:latest |
| 24 | +$ docker run -it swiftdoc/swift-doc |
| 25 | +``` |
| 26 | + |
| 27 | +### Building from source |
| 28 | + |
| 29 | +You can also build `swift-doc` from source. It is written in Swift and requires Swift 5.3 or later. |
| 30 | +Run the following commands to build and install from sources: |
| 31 | + |
| 32 | +```terminal |
| 33 | +$ git clone https://github.com/SwiftDocOrg/swift-doc |
| 34 | +$ cd swift-doc |
| 35 | +$ make install |
| 36 | +``` |
| 37 | + |
| 38 | +`swift-doc` has a dependency on [libxml2](https://en.wikipedia.org/wiki/Libxml2). It also has an optional dependency on |
| 39 | +[Graphviz](https://www.graphviz.org/) if you want to generate the relationship graphs. |
| 40 | + |
| 41 | +If you're on Linux, you may need to first install these prerequisites. You can install it on Ubuntu or Debian by running |
| 42 | +the following command: |
| 43 | + |
| 44 | +```terminal |
| 45 | +$ apt-get update |
| 46 | +$ apt-get install -y libxml2-dev graphviz |
| 47 | +``` |
| 48 | + |
| 49 | +If you're on macOS, Graphviz is available via Homebrew: |
| 50 | + |
| 51 | +```terminal |
| 52 | +$ brew install graphviz |
| 53 | +``` |
| 54 | + |
| 55 | +# Build your first documentation |
| 56 | + |
| 57 | +Let's build some documentation, now that you have successfully installed `swift-doc`! You need to provide two arguments |
| 58 | +to build the documentation. |
| 59 | + |
| 60 | +The first argument is the name of the module for which you build the documentation. Usually, |
| 61 | +you will provide a name that matches your package name. So if your Swift project is called `AwesomeSwiftLibrary`, you'd |
| 62 | +provide `AwesomeSwiftLibrary` as the name of the module. The module name is provided via the `--module-name` option. |
| 63 | + |
| 64 | +Besides the module name, you need to provide paths to directories containing the Swift source files of your |
| 65 | +project. You need to provide at least one path to a directory, but you can provide as many as you want if your |
| 66 | +project is split into different directories. However, you don't need to provide subdirectories -- `swift-doc` will |
| 67 | +walk through all subdirectories in the provided directories and collect all Swift files from there. |
| 68 | + |
| 69 | +> **Automatically excluded top-level directories**: |
| 70 | +> `swift-doc` tries to do the right thing by default and it optimizes for use cases which are the most common in the |
| 71 | +> Swift community. Therefore, some top-level directories are excluded by default because most likely you don't want to |
| 72 | +> include those sources in your documentation. Those excluded directories are: |
| 73 | +> - `./node-modules/` |
| 74 | +> - `./Packages/` |
| 75 | +> - `./Pods/` |
| 76 | +> - `./Resources/` |
| 77 | +> - `./Tests/` |
| 78 | +> |
| 79 | +> If you want to include those files in your documentation nevertheless, you can always include a direct path to the |
| 80 | +> directory and they will be included. So let's say you have a document structure like this: |
| 81 | +> ``` |
| 82 | +> MyProject/ |
| 83 | +> ├── Tests |
| 84 | +> └── OtherDirectory |
| 85 | +> ``` |
| 86 | +> Then running `swift-doc --module-name MyProject ./MyProject` will only include the files in the subdirectory |
| 87 | +> `OtherDirectory` and automatically exclude the `Tests` subdirectory. But running |
| 88 | +> `swift-doc --module-name MyProject ./MyProject ./MyProject/Tests` will also include all files in the `Tests` |
| 89 | +> subdirectory. |
| 90 | +
|
| 91 | +Let's run the command in the directory of your project. |
| 92 | +
|
| 93 | +```terminal |
| 94 | +$ swift-doc generate --module-name AwesomeSwiftLibrary ./Sources/ |
| 95 | +``` |
| 96 | +
|
| 97 | +And that's it! You successfully created the first documentation of your project. But where can you find it? |
| 98 | + |
| 99 | +By default, `swift-doc` writes the generated documentation into the directory at `.build/documentation/`. You can |
| 100 | +provide a different output directory with the `--output` option: |
| 101 | + |
| 102 | +```terminal |
| 103 | +$ swift-doc generate --module-name AwesomeSwiftLibrary ./Sources/ --output some/other/directory |
| 104 | +``` |
| 105 | + |
| 106 | +## Changing the output format to a rendered website. |
| 107 | + |
| 108 | +If you followed the steps until now and checked the documentation which was created, you could see that `swift-doc` |
| 109 | +generated a collection of markdown files as output your documentation. Those markdown files are build with specific file |
| 110 | +names and with a specific folder structure, so they can be used for publication to your project's |
| 111 | +[GitHub Wiki](https://docs.github.com/en/communities/documenting-your-project-with-wikis/about-wikis). |
| 112 | + |
| 113 | +This might be not what you expected. Maybe you wanted to generate a website which you could publish on the internet as |
| 114 | +a documentation for the end users of your library? |
| 115 | + |
| 116 | +This option is also provided by `swift-doc`. It's called the _output format_ and you can change it by setting the |
| 117 | +`--format` option. In order to generate a website, you need to set the option to `html`: |
| 118 | + |
| 119 | +```terminal |
| 120 | +$ swift-doc generate --module-name AwesomeSwiftLibrary --format html ./Sources/ |
| 121 | +``` |
| 122 | + |
| 123 | +## Choose which symbols are included by setting the access level |
| 124 | + |
| 125 | +You might have noticed that the generated documentation contained less symbols that your library actually has. This is |
| 126 | +because `swift-doc` only includes public symbols by default — as this are also the symbols which are exposed to users |
| 127 | +of your library. |
| 128 | + |
| 129 | +But if you want to generate documentation for apps and not only for library or if you want to generate a documentation |
| 130 | +for developers and not only end users of your library, then you might want to include additional symbols. |
| 131 | + |
| 132 | +Therefore `swift-doc` also provides the possibility to decide which symbols are included by setting the minimum access |
| 133 | +level from which symbols should be included. This is done via the `--minimum-access-level` option. Its possible values |
| 134 | +are: |
| 135 | + |
| 136 | +* `public` (default). This will only include symbols which are declared `public` or `open`. For example, given the |
| 137 | + following swift source file: |
| 138 | + ```swift |
| 139 | + |
| 140 | + public func publicFunction() { } |
| 141 | + |
| 142 | + func internalFunction() { } |
| 143 | + |
| 144 | + private func privateFunction() { } |
| 145 | + |
| 146 | + public class PublicClass { |
| 147 | + public func publicMethod() { } |
| 148 | + |
| 149 | + open func openMethod() { } |
| 150 | + |
| 151 | + func internalMethod() { } |
| 152 | + } |
| 153 | + |
| 154 | + internal class InternalClass { |
| 155 | + private func privateMethod() { } |
| 156 | + } |
| 157 | + ``` |
| 158 | + |
| 159 | + Then the generated documentation will include the function `publicFunction()` and the class `PublicClass`. For the |
| 160 | + documentation of `PublicClass`, it will only include the methods `publicMethod()` and `openMethod()`. |
| 161 | + |
| 162 | +* `internal`. This will include all symbols which are declared `public`, `open`, and `internal`. So in the example |
| 163 | + above, it will additionally include the function `internalFunction()` and the class `InternalClass`. But for the |
| 164 | + documentation of `InternalClass`, it will not include the method `privateMethod()`. |
| 165 | + |
| 166 | +# Next: Understanding documentation comments |
| 167 | + |
| 168 | +Now you know which symbols appear in the generated documentation. [Continue with the guide to |
| 169 | +understand how to write documentation comments in your source code to make the best of your documentation](02-documentation-format.md). |
0 commit comments