Search rustfmt along all toolchains

This commit is contained in:
Kogia-sima 2020-08-04 03:32:56 +09:00
parent d79a5d3b4b
commit 5666f92b9d
3 changed files with 39 additions and 2 deletions

10
Cargo.lock generated
View File

@ -27,6 +27,14 @@ name = "glob"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "home"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "integration-tests"
version = "0.2.0"
@ -116,6 +124,7 @@ dependencies = [
name = "sailfish-compiler"
version = "0.2.0"
dependencies = [
"home 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 2.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"pretty_assertions 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
"proc-macro2 1.0.18 (registry+https://github.com/rust-lang/crates.io-index)",
@ -249,6 +258,7 @@ dependencies = [
"checksum ctor 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "cf6b25ee9ac1995c54d7adb2eff8cfffb7260bc774fb63c601ec65467f43cd9d"
"checksum difference 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198"
"checksum glob 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"
"checksum home 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2456aef2e6b6a9784192ae780c0f15bc57df0e918585282325e8c8ac27737654"
"checksum itoa 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "b8b7a7c0c47db5545ed3fef7468ee7bb5b74691498139e4b3f6a20685dc6dd8e"
"checksum itoap 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8e804a5759b475f44377998918a7e3be9da3767056f5e77751ef7803893db0e9"
"checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"

View File

@ -25,6 +25,7 @@ config = ["yaml-rust"]
memchr = "2.3.3"
quote = { version = "1.0.6", default-features = false }
yaml-rust = { version = "0.4.4", optional = true }
home = "0.5.3"
[dependencies.syn]
version = "1.0.21"

View File

@ -1,5 +1,6 @@
use std::fs;
use std::io::{self, Write};
use std::path::Path;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
pub fn read_to_string(path: &Path) -> io::Result<String> {
@ -16,13 +17,38 @@ pub fn read_to_string(path: &Path) -> io::Result<String> {
Ok(content)
}
fn find_rustfmt() -> io::Result<Option<PathBuf>> {
let mut toolchain_dir = home::rustup_home()?;
toolchain_dir.push("toolchains");
for e in fs::read_dir(toolchain_dir)? {
let mut path = e?.path();
path.push("bin");
path.push("rustfmt");
if path.exists() {
return Ok(Some(path));
}
}
Ok(None)
}
/// Format block expression using `rustfmt` command
pub fn rustfmt_block(source: &str) -> io::Result<String> {
let rustfmt = match find_rustfmt()? {
Some(p) => p,
None => {
return Err(io::Error::new(
io::ErrorKind::NotFound,
"rustfmt command not found",
))
}
};
let mut new_source = String::with_capacity(source.len() + 11);
new_source.push_str("fn render()");
new_source.push_str(source);
let mut child = Command::new("rustfmt")
let mut child = Command::new(rustfmt)
.args(&["--emit", "stdout", "--color", "never", "--quiet"])
.stdin(Stdio::piped())
.stdout(Stdio::piped())