Skip to content

Commit a405a57

Browse files
authored
Extract CLI crate and add shell completion and man page (#42)
1 parent d085353 commit a405a57

24 files changed

Lines changed: 157 additions & 53 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@ jobs:
4949
- uses: actions/checkout@v3
5050
- uses: dtolnay/rust-toolchain@stable
5151
- uses: Swatinem/rust-cache@v2
52-
- run: cargo clippy --all-targets --features cli
52+
- run: cargo clippy --all-targets
5353
- run: cargo fmt --check --all
5454
- run: cargo doc --workspace --no-deps

Cargo.lock

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
[workspace]
2-
members = ["tests"]
2+
members = ["crates/*", "tests"]
3+
default-members = ["crates/svg2pdf-cli"]
4+
resolver = "2"
35

46
[workspace.package]
57
version = "0.9.0"
@@ -9,39 +11,3 @@ repository = "https://github.com/typst/svg2pdf"
911
readme = "README.md"
1012
license = "MIT OR Apache-2.0"
1113

12-
[package]
13-
name = "svg2pdf"
14-
description = "Convert SVG files to PDFs."
15-
categories = ["encoding", "graphics", "multimedia"]
16-
keywords = ["svg", "pdf", "vector-graphics", "conversion"]
17-
version.workspace = true
18-
authors.workspace = true
19-
edition.workspace = true
20-
repository.workspace = true
21-
license.workspace = true
22-
23-
[features]
24-
default = ["image"]
25-
image = ["dep:image"]
26-
cli = ["dep:clap", "dep:termcolor", "usvg/text", "dep:fontdb"]
27-
28-
[lib]
29-
test = false
30-
doctest = false
31-
32-
[[bin]]
33-
name = "svg2pdf"
34-
path = "src/main.rs"
35-
required-features = ["cli"]
36-
37-
[dependencies]
38-
miniz_oxide = "0.7"
39-
pdf-writer = "0.9"
40-
usvg = { version = "0.36", default-features = false }
41-
image = { version = "0.24", default-features = false, features = ["jpeg", "png", "gif"], optional = true }
42-
termcolor = { version = "1", optional = true }
43-
clap = { version = "4.4.2", features = ["derive"], optional = true }
44-
fontdb = { version = "0.15", optional= true }
45-
46-
[dev-dependencies]
47-
usvg = { version = "0.36.0" }

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ std::fs::write("target/time_series.pdf", pdf)?;
3434
This crate also contains a command line interface. Install it by running the command below:
3535

3636
```bash
37-
cargo install svg2pdf --features cli
37+
cargo install svg2pdf-cli
3838
```
3939

4040
You can then convert SVGs to PDFs by running commands like these:

crates/svg2pdf-cli/Cargo.toml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[package]
2+
name = "svg2pdf-cli"
3+
description = "The command line interface for svg2pdf."
4+
categories = ["encoding", "graphics", "multimedia", "command-line-utilities"]
5+
keywords = ["svg2pdf", "cli"]
6+
version.workspace = true
7+
authors.workspace = true
8+
edition.workspace = true
9+
repository.workspace = true
10+
license.workspace = true
11+
build = "build.rs"
12+
13+
[[bin]]
14+
name = "svg2pdf"
15+
path = "src/main.rs"
16+
test = false
17+
doctest = false
18+
bench = false
19+
doc = false
20+
21+
[dependencies]
22+
svg2pdf = { path = "../svg2pdf" }
23+
miniz_oxide = "0.7"
24+
pdf-writer = "0.9"
25+
usvg = { version = "0.36", default-features = false, features = ["text"] }
26+
image = { version = "0.24", default-features = false, features = ["jpeg", "png", "gif"] }
27+
termcolor = { version = "1" }
28+
clap = { version = "4.4.2", features = ["derive"] }
29+
fontdb = { version = "0.15" }
30+
31+
[build-dependencies]
32+
clap = { version = "4.4.2", features = ["derive", "string"] }
33+
clap_complete = "4.4.3"
34+
clap_mangen = "0.2.14"
35+

crates/svg2pdf-cli/build.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use std::{env, path::Path};
2+
3+
use clap::{CommandFactory, ValueEnum};
4+
use clap_complete::{generate_to, Shell};
5+
6+
mod args {
7+
include!("src/args.rs");
8+
}
9+
10+
fn main() -> Result<(), std::io::Error> {
11+
let outdir_str = match env::var_os("OUT_DIR") {
12+
None => return Ok(()),
13+
Some(outdir) => outdir,
14+
};
15+
16+
// Put the files in the artifacts folder (/target/artifacts)
17+
let outdir_path = &Path::new(&outdir_str).ancestors().nth(3).unwrap();
18+
let artifacts_path = outdir_path.join("artifacts");
19+
std::fs::create_dir_all(&artifacts_path)?;
20+
21+
let mut cmd = args::Args::command();
22+
23+
let man = clap_mangen::Man::new(cmd.clone());
24+
let mut manpage_file = std::fs::File::create(artifacts_path.join("svg2pdf.1"))?;
25+
man.render(&mut manpage_file)?;
26+
27+
for shell in Shell::value_variants() {
28+
generate_to(*shell, &mut cmd, "svg2pdf", &artifacts_path).unwrap();
29+
}
30+
31+
Ok(())
32+
}

crates/svg2pdf-cli/src/args.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use std::path::PathBuf;
2+
3+
use clap::Parser;
4+
5+
#[derive(Debug, Parser)]
6+
#[clap(about, version)]
7+
pub struct Args {
8+
/// Path to read SVG file from.
9+
pub input: PathBuf,
10+
/// Path to write PDF file to.
11+
pub output: Option<PathBuf>,
12+
/// The number of SVG pixels per PDF points.
13+
#[clap(long, default_value = "72.0")]
14+
pub dpi: f32,
15+
}

src/main.rs renamed to crates/svg2pdf-cli/src/main.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::io::{self, Write};
2-
use std::path::{Path, PathBuf};
2+
use std::path::Path;
33
use std::process;
44

55
use clap::Parser;
@@ -8,17 +8,7 @@ use usvg::{TreeParsing, TreeTextToPath};
88

99
use svg2pdf::Options;
1010

11-
#[derive(Debug, Parser)]
12-
#[clap(about, version)]
13-
struct Args {
14-
/// Path to read SVG file from.
15-
input: PathBuf,
16-
/// Path to write PDF file to.
17-
output: Option<PathBuf>,
18-
/// The number of SVG pixels per PDF points.
19-
#[clap(long, default_value = "72.0")]
20-
dpi: f32,
21-
}
11+
mod args;
2212

2313
fn main() {
2414
if let Err(msg) = run() {
@@ -28,7 +18,7 @@ fn main() {
2818
}
2919

3020
fn run() -> Result<(), String> {
31-
let args = Args::parse();
21+
let args = args::Args::parse();
3222

3323
let name =
3424
Path::new(args.input.file_name().ok_or("Input path does not point to a file")?);

crates/svg2pdf/Cargo.toml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[package]
2+
name = "svg2pdf"
3+
description = "Convert SVG files to PDFs."
4+
categories = ["encoding", "graphics", "multimedia"]
5+
keywords = ["svg", "pdf", "vector-graphics", "conversion"]
6+
version.workspace = true
7+
authors.workspace = true
8+
edition.workspace = true
9+
repository.workspace = true
10+
license.workspace = true
11+
12+
[lib]
13+
doctest = false
14+
bench = false
15+
16+
[features]
17+
default = ["image"]
18+
image = ["dep:image"]
19+
20+
[dependencies]
21+
miniz_oxide = "0.7"
22+
pdf-writer = "0.9"
23+
usvg = { version = "0.36", default-features = false }
24+
image = { version = "0.24", default-features = false, features = ["jpeg", "png", "gif"], optional = true }
25+
26+
[dev-dependencies]
27+
usvg = { version = "0.36.0" }
28+
File renamed without changes.

0 commit comments

Comments
 (0)