# `NBPR.Buildroot.Source`
[🔗](https://github.com/jimsynz/nbpr/blob/v0.7.0/lib/nbpr/buildroot/source.ex#L1)

Manages the cached Buildroot source tree.

Buildroot is large (~50 MB compressed, ~500 MB extracted) and shared
across all nbpr operations targeting the same BR version. Layout under
`$NERVES_DATA_DIR/nbpr/`:

- `buildroot-dl/` — BR source-tarball cache (shared across BR versions;
  accumulates over time, never invalidated)
- `buildroot/<version>/` — one extracted, patched, ready-to-use BR tree
  per version, treated as read-only thereafter

Per-build outputs live elsewhere via BR's `O=<dir>` flag and don't
contend with this cache.

Patching is done via the `patch` binary. The patches come from the
user's `deps/nerves_system_br/patches/buildroot/` and must be applied
in lexicographic order — Buildroot's own patch script does the same.

# `cache_dir`

```elixir
@spec cache_dir(String.t()) :: Path.t()
```

Returns the absolute path where BR `version` is (or will be) extracted.

# `cached?`

```elixir
@spec cached?(String.t()) :: boolean()
```

Returns `true` when BR `version` is already extracted, patched, and
marked ready at `cache_dir/1`.

# `download_dir`

```elixir
@spec download_dir() :: Path.t()
```

Returns the shared download cache directory for BR source tarballs.

# `ensure!`

```elixir
@spec ensure!(String.t(), Path.t() | nil) :: Path.t()
```

Ensures BR `version` is downloaded, extracted, patched against
`patches_dir`, and ready at `cache_dir(version)`. Returns the cache path.

No-op when the version is already cached.

# `tarball_cached?`

```elixir
@spec tarball_cached?(String.t()) :: boolean()
```

Returns `true` when the BR tarball for `version` is sitting in the
download cache. Useful for deciding whether `ensure!/2` would do
network — if both `cached?/1` and `tarball_cached?/1` are false, the
next `ensure!/2` will fetch over HTTP.

# `tarball_path`

```elixir
@spec tarball_path(String.t()) :: Path.t()
```

Returns the path the BR tarball for `version` is (or will be) cached at.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
