-
-
Notifications
You must be signed in to change notification settings - Fork 41
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
Extract CLI crate and add shell completion and man page #42
Changes from 8 commits
37e0351
c98188a
3e67710
db8db5f
5694ee7
939a44a
004db6b
9e3056e
41758ea
305cc0e
dd2f2d8
dc9798c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,15 @@ | ||
[workspace] | ||
members = ["tests"] | ||
members = ["crates/*", "tests"] | ||
default-members = ["crates/svg2pdf"] | ||
resolver = "2" | ||
|
||
[workspace.package] | ||
version = "0.9.0" | ||
authors = ["Martin Haug <[email protected]>", "Laurenz Stampfl <[email protected]>"] | ||
authors = [ | ||
"Martin Haug <[email protected]>", | ||
"Laurenz Stampfl <[email protected]>", | ||
] | ||
edition = "2021" | ||
repository = "https://github.com/typst/svg2pdf" | ||
readme = "README.md" | ||
license = "MIT OR Apache-2.0" | ||
|
||
[package] | ||
name = "svg2pdf" | ||
description = "Convert SVG files to PDFs." | ||
categories = ["encoding", "graphics", "multimedia"] | ||
keywords = ["svg", "pdf", "vector-graphics", "conversion"] | ||
version.workspace = true | ||
authors.workspace = true | ||
edition.workspace = true | ||
repository.workspace = true | ||
license.workspace = true | ||
|
||
[features] | ||
default = ["image"] | ||
image = ["dep:image"] | ||
cli = ["dep:clap", "dep:termcolor", "usvg/text", "dep:fontdb"] | ||
|
||
[lib] | ||
test = false | ||
doctest = false | ||
|
||
[[bin]] | ||
name = "svg2pdf" | ||
path = "src/main.rs" | ||
required-features = ["cli"] | ||
|
||
[dependencies] | ||
miniz_oxide = "0.7" | ||
pdf-writer = "0.9" | ||
usvg = { version = "0.36", default-features = false } | ||
image = { version = "0.24", default-features = false, features = ["jpeg", "png", "gif"], optional = true } | ||
termcolor = { version = "1", optional = true } | ||
clap = { version = "4.4.2", features = ["derive"], optional = true } | ||
fontdb = { version = "0.15", optional= true } | ||
|
||
[dev-dependencies] | ||
usvg = { version = "0.36.0" } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
[package] | ||
name = "svg2pdf-cli" | ||
description = "The command line interface for svg2pdf." | ||
categories = ["encoding", "graphics", "multimedia", "command-line-utilities"] | ||
keywords = ["svg2pdf", "cli"] | ||
version.workspace = true | ||
authors.workspace = true | ||
edition.workspace = true | ||
repository.workspace = true | ||
license.workspace = true | ||
build = "build.rs" | ||
|
||
[[bin]] | ||
name = "svg2pdf" | ||
path = "src/main.rs" | ||
test = false | ||
doctest = false | ||
bench = false | ||
doc = false | ||
|
||
[dependencies] | ||
svg2pdf = { path = "../svg2pdf" } | ||
laurmaedje marked this conversation as resolved.
Show resolved
Hide resolved
|
||
miniz_oxide = "0.7" | ||
pdf-writer = "0.9" | ||
usvg = { version = "0.36", default-features = false, features = ["text"] } | ||
image = { version = "0.24", default-features = false, features = [ | ||
"jpeg", | ||
"png", | ||
"gif", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess that's personal preference, but maybe better to keep everything in a single line? Not sure about this one, though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had a TOML formatter turned on for VSCode which did this, but I can revert it if you prefer, no problem. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer single lines, too. |
||
] } | ||
termcolor = { version = "1" } | ||
clap = { version = "4.4.2", features = ["derive"] } | ||
fontdb = { version = "0.15" } | ||
|
||
[build-dependencies] | ||
clap = { version = "4.4.2", features = ["derive", "string"] } | ||
clap_complete = "4.4.3" | ||
clap_mangen = "0.2.14" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add newlines at the end of files where they are missing. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use std::{env, path::Path}; | ||
|
||
use clap::{CommandFactory, ValueEnum}; | ||
use clap_complete::{generate_to, Shell}; | ||
|
||
mod args { | ||
include!("src/cli.rs"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the name |
||
} | ||
|
||
fn main() -> Result<(), std::io::Error> { | ||
let outdir_str = match env::var_os("OUT_DIR") { | ||
None => return Ok(()), | ||
Some(outdir) => outdir, | ||
}; | ||
|
||
// Put the files in the same level as the binary (e.g. /target/debug folder) | ||
let outdir_path = &Path::new(&outdir_str).ancestors().nth(3).unwrap(); | ||
|
||
let mut cmd = args::Args::command(); | ||
|
||
let man = clap_mangen::Man::new(cmd.clone()); | ||
let mut manpage_file = std::fs::File::create(outdir_path.join("svg2pdf.1"))?; | ||
man.render(&mut manpage_file)?; | ||
|
||
for shell in Shell::value_variants() { | ||
generate_to(*shell, &mut cmd, "svg2pdf", outdir_path).unwrap(); | ||
} | ||
|
||
Ok(()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use std::path::PathBuf; | ||
|
||
use clap::Parser; | ||
|
||
#[derive(Debug, Parser)] | ||
#[clap(about, version)] | ||
pub struct Args { | ||
/// Path to read SVG file from. | ||
pub input: PathBuf, | ||
/// Path to write PDF file to. | ||
pub output: Option<PathBuf>, | ||
/// The number of SVG pixels per PDF points. | ||
#[clap(long, default_value = "72.0")] | ||
pub dpi: f32, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
[package] | ||
name = "svg2pdf" | ||
description = "Convert SVG files to PDFs." | ||
categories = ["encoding", "graphics", "multimedia"] | ||
keywords = ["svg", "pdf", "vector-graphics", "conversion"] | ||
version.workspace = true | ||
authors.workspace = true | ||
edition.workspace = true | ||
repository.workspace = true | ||
license.workspace = true | ||
|
||
[lib] | ||
doctest = false | ||
bench = false | ||
|
||
[features] | ||
default = ["image"] | ||
image = ["dep:image"] | ||
|
||
[dependencies] | ||
miniz_oxide = "0.7" | ||
pdf-writer = "0.9" | ||
usvg = { version = "0.36", default-features = false } | ||
image = { version = "0.24", default-features = false, features = [ | ||
"jpeg", | ||
"png", | ||
"gif", | ||
], optional = true } | ||
|
||
[dev-dependencies] | ||
usvg = { version = "0.36.0" } |
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.
I would change this to
default-members = ["crates/typst-cli"]
, since the cli is what you want to execute by default when runningcargo run
in the root directory.