Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add cargo-doc-live #246

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
/example/share-services/pgweb/data/
/example/simple/data/
/example/grafana-tempo/data/
/example/cargo-doc-live/target

/.pre-commit-config.yaml
44 changes: 44 additions & 0 deletions doc/cargo-doc-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# cargo-doc-live

[cargo-doc-live] is live server version of `cargo doc` ― edit Rust code, and see the docs view in your web browser update automatically.

https://github.com/srid/cargo-doc-live/assets/3998/37378858-dda1-40fb-8f6a-f76dc857a661

{#start}

## Getting started

```nix
# In `perSystem.process-compose.<name>`
{
services.cargo-doc-live."cargo-doc-live1".enable = true;
}
```

{#port}

### The port for `cargo doc`, the default value is 8008, while you could override it if 8008 is in use for another service.

```nix
{
services.cargo-doc-live."cargo-doc-live1" = {
enable = true;
projectRoot = ./.;
port = 8080;
};
}
```

{#crateName}

### The crate to use when opening docs in browser, the crate name will be derived from Cargo.toml.

```nix
{
services.cargo-doc-live."cargo-doc-live1" = {
enable = true;
projectRoot = ./.;
crateName = "chrono";
};
}
```
3 changes: 2 additions & 1 deletion doc/services.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ short-title: Services

# Supported services

>[!warning] WIP
> [!warning] WIP
> Documentation for the services is still in progress. Please refer to [this issue][gh] for progress.

- [[apache-kafka]]#
Expand All @@ -25,5 +25,6 @@ short-title: Services
- [[prometheus]]#
- [[cassandra]]#
- [[weaviate]]#
- [[cargo-doc-live]]#

[gh]: https://github.com/juspay/services-flake/issues/132
7 changes: 7 additions & 0 deletions example/cargo-doc-live/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions example/cargo-doc-live/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[package]
name = "test"
8 changes: 8 additions & 0 deletions example/cargo-doc-live/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
A demonstration of cargo-doc-live.

To run,

```
cd ./example/cargo-doc-live
nix run .#cargo-doc-live
```
106 changes: 106 additions & 0 deletions example/cargo-doc-live/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions example/cargo-doc-live/flake.nix
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a ./example/cargo-doc-live/README.md that explains how to run it?

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
flake-parts.url = "github:hercules-ci/flake-parts";
systems.url = "github:nix-systems/default";
process-compose-flake.url = "github:Platonic-Systems/process-compose-flake";
services-flake.url = "github:juspay/services-flake";
};
outputs = inputs:
inputs.flake-parts.lib.mkFlake { inherit inputs; } {
systems = import inputs.systems;
imports = [
inputs.process-compose-flake.flakeModule
];
perSystem = { self', pkgs, lib, ... }: {
process-compose."cargo-doc-live" = _:
{
tui = false;

imports = [
inputs.services-flake.processComposeModules.default
];

preHook = ''
# Set pipefail option for safer bash
set -euo pipefail

# Copy project root to a mutable area
export HOME="$TMP"
cp -r ${inputs.self} "$HOME"/project
chmod -R a+w "$HOME"/project
cd "$HOME"/project
'';

services.cargo-doc-live."cargo-doc-live1" = {
projectRoot = inputs.self;
enable = true;
port = 8009;
};

settings.processes.test = {
command = pkgs.writeShellApplication {
name = "cargo-doc-live-test";
runtimeInputs = [ pkgs.curl ];
text = ''
curl http://127.0.0.1:8009/test
'';
};
depends_on."cargo-doc-live1-browser-sync".condition = "process_healthy";
};
};
};
};
}
3 changes: 3 additions & 0 deletions example/cargo-doc-live/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}
4 changes: 4 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
};
dir = "./example/share-services/pgweb";
};
cargo-doc-live-example = {
inherit overrideInputs;
dir = "./example/cargo-doc-live";
};
test = {
inherit overrideInputs;
dir = "./test";
Expand Down
91 changes: 91 additions & 0 deletions nix/cargo-doc-live.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{ pkgs, lib, name, config, ... }:
let
inherit (lib) types;
in
{
options = {
enable = lib.mkEnableOption name;

projectRoot = lib.mkOption {
type = types.path;
description = "Path to the cargo project root";
default = ./.;
};

port = lib.mkOption {
type = types.port;
description = "The port for 'cargo doc'";
default = 8008;
};

crateName = lib.mkOption {
type = types.str;
description = "The crate to use when opening docs in browser";
default = builtins.replaceStrings [ "-" ] [ "_" ]
((lib.trivial.importTOML "${config.projectRoot}/Cargo.toml").package.name);
defaultText = "The crate name is derived from the Cargo.toml file";
};

outputs.settings = lib.mkOption {
type = types.deferredModule;
internal = true;
readOnly = true;
default = {
processes = {
"${name}-cargo-doc" = {
command = pkgs.writeShellApplication {
name = "cargo-doc";
runtimeInputs = with pkgs; [ cargo cargo-watch nodePackages.browser-sync ];
text =
''
run-cargo-doc() {
cargo doc --document-private-items --all-features
browser-sync reload --port ${toString config.port} # Trigger reload in browser
}; export -f run-cargo-doc
cargo-watch watch -s run-cargo-doc
'';
};
readiness_probe = {
period_seconds = 1;
failure_threshold = 100000; # 'cargo doc' can take quite a while.
exec.command = ''
# Wait for the first 'cargo doc' to have completed.
# We'll use this state to block browser-sync from starting
# and opening the URL in the browser.
ls target/doc/${config.crateName}/index.html
'';
};
namespace = name;
availability.restart = "on_failure";
};
"${name}-browser-sync" = {
command = pkgs.writeShellApplication {
name = "browser-sync";
runtimeInputs = [ pkgs.nodePackages.browser-sync ];
text =
''
browser-sync start --port ${toString config.port} --ss target/doc -s target/doc \
--startPath /${config.crateName}/
'';
};
readiness_probe = {
http_get = {
scheme = "http";
host = "localhost";
port = config.port;
path = config.crateName;
};
initial_delay_seconds = 15;
period_seconds = 10;
timeout_seconds = 2;
success_threshold = 1;
failure_threshold = 5;
};
namespace = name;
depends_on."${name}-cargo-doc".condition = "process_healthy";
};
};
};
};
};
}
1 change: 1 addition & 0 deletions nix/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ in
./tempo.nix
./weaviate.nix
./searxng.nix
./cargo-doc-live.nix
];
}