-
Notifications
You must be signed in to change notification settings - Fork 31
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
leomayleomay
wants to merge
9
commits into
juspay:main
Choose a base branch
from
leomayleomay:add-cargo-doc-live
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7e2eb09
feat: Add cargo-doc-live
leomayleomay 109a6db
Add missing inputs
leomayleomay 3695662
fix: Move test to example
leomayleomay 7a465f8
Merge branch 'main' into add-cargo-doc-live
srid 7877ddc
docs: address PR feedback - round 1
leomayleomay 6de6f59
fix: add preHook to enter the correct directory
leomayleomay 3745923
docs: add more doc
leomayleomay 3fa26c0
fix: wait until browser-sync is ready
leomayleomay 8a003fa
fix: change the way of checking readiness
leomayleomay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
}; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[package] | ||
name = "test" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
}; | ||
}; | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn main() { | ||
println!("Hello, world!"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
}; | ||
}; | ||
}; | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,5 +22,6 @@ in | |
./tempo.nix | ||
./weaviate.nix | ||
./searxng.nix | ||
./cargo-doc-live.nix | ||
]; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?