Skip to content
Snippets Groups Projects
Unverified Commit c48c2a58 authored by Jeremy Soller's avatar Jeremy Soller
Browse files

Cargo format

parent 27d91e42
No related branches found
No related tags found
No related merge requests found
use std::{env, io, process};
use std::ffi::OsString;
use std::{env, io, process};
use crate::{status_error, toolchain, target};
use crate::{status_error, target, toolchain};
fn inner<I: Iterator<Item=String>>(mut args: I) -> io::Result<()> {
fn inner<I: Iterator<Item = String>>(mut args: I) -> io::Result<()> {
let toolchain_dir = toolchain()?;
// PATH must be set first so cargo is sourced from the toolchain path
......@@ -11,18 +11,13 @@ fn inner<I: Iterator<Item=String>>(mut args: I) -> io::Result<()> {
let path = env::var_os("PATH").unwrap_or(OsString::new());
let mut paths = env::split_paths(&path).collect::<Vec<_>>();
paths.insert(0, toolchain_dir.join("bin"));
let new_path = env::join_paths(paths).map_err(|err| io::Error::new(
io::ErrorKind::Other,
err
))?;
let new_path =
env::join_paths(paths).map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
env::set_var("PATH", new_path);
}
// TODO: Ensure no spaces in toolchain_dir
let rustflags = format!(
"-L {}",
toolchain_dir.join(target()).join("lib").display()
);
let rustflags = format!("-L {}", toolchain_dir.join(target()).join("lib").display());
let command = args.next().unwrap();
let subcommand = args.next().unwrap();
......@@ -35,21 +30,21 @@ fn inner<I: Iterator<Item=String>>(mut args: I) -> io::Result<()> {
match arg.as_str() {
"-g" | "--gui" if matching => {
gui = true;
},
}
"-o" | "--output" if matching => match args.next() {
Some(output) => {
output_opt = Some(output);
},
}
None => {
//TODO: usage();
},
}
},
"--" if matching => {
matching = false;
},
}
_ => {
arguments.push(arg);
},
}
}
}
......@@ -66,7 +61,8 @@ fn inner<I: Iterator<Item=String>>(mut args: I) -> io::Result<()> {
crate::env::command("cargo")?
.arg(subcommand)
.arg("--target").arg(target())
.arg("--target")
.arg(target())
.args(arguments)
.env("CARGO_TARGET_X86_64_UNKNOWN_REDOX_RUNNER", runner)
.env("RUSTFLAGS", rustflags)
......@@ -80,7 +76,7 @@ pub fn main(args: &[String]) {
match inner(args.iter().cloned()) {
Ok(()) => {
process::exit(0);
},
}
Err(err) => {
eprintln!("redoxer cargo: {}", err);
process::exit(1);
......
use std::{env, ffi, io, process};
use crate::{status_error, toolchain, target};
use crate::{status_error, target, toolchain};
pub fn command<S: AsRef<ffi::OsStr>>(program: S) -> io::Result<process::Command> {
let toolchain_dir = toolchain()?;
......@@ -10,10 +10,8 @@ pub fn command<S: AsRef<ffi::OsStr>>(program: S) -> io::Result<process::Command>
let path = env::var_os("PATH").unwrap_or(ffi::OsString::new());
let mut paths = env::split_paths(&path).collect::<Vec<_>>();
paths.insert(0, toolchain_dir.join("bin"));
let new_path = env::join_paths(paths).map_err(|err| io::Error::new(
io::ErrorKind::Other,
err
))?;
let new_path =
env::join_paths(paths).map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
env::set_var("PATH", new_path);
}
......@@ -34,11 +32,8 @@ pub fn command<S: AsRef<ffi::OsStr>>(program: S) -> io::Result<process::Command>
Ok(command)
}
fn inner<I: Iterator<Item=String>>(args: I) -> io::Result<()> {
command("env")?
.args(args)
.status()
.and_then(status_error)?;
fn inner<I: Iterator<Item = String>>(args: I) -> io::Result<()> {
command("env")?.args(args).status().and_then(status_error)?;
Ok(())
}
......@@ -47,7 +42,7 @@ pub fn main(args: &[String]) {
match inner(args.iter().cloned().skip(2)) {
Ok(()) => {
process::exit(0);
},
}
Err(err) => {
eprintln!("redoxer env: {}", err);
process::exit(1);
......
use std::{io, path, process};
pub (crate) use self::toolchain::toolchain;
pub(crate) use self::toolchain::toolchain;
mod cargo;
mod env;
......@@ -25,7 +25,8 @@ fn installed(program: &str) -> io::Result<bool> {
}
fn redoxer_dir() -> path::PathBuf {
dirs::home_dir().unwrap_or(path::PathBuf::from("."))
dirs::home_dir()
.unwrap_or(path::PathBuf::from("."))
.join(".redoxer")
}
......@@ -33,10 +34,7 @@ fn status_error(status: process::ExitStatus) -> io::Result<()> {
if status.success() {
Ok(())
} else {
Err(io::Error::new(
io::ErrorKind::Other,
format!("{}", status)
))
Err(io::Error::new(io::ErrorKind::Other, format!("{}", status)))
}
}
......@@ -63,7 +61,11 @@ pub fn target() -> &'static str {
let target_from_env = std::env::var("TARGET").unwrap_or("".to_string());
let index = if SUPPORTED_TARGETS.contains(&&*target_from_env) == true {
SUPPORTED_TARGETS.iter().position(|t| **t == target_from_env).unwrap().into()
SUPPORTED_TARGETS
.iter()
.position(|t| **t == target_from_env)
.unwrap()
.into()
} else {
0usize
};
......@@ -74,14 +76,9 @@ pub fn target() -> &'static str {
pub fn main(args: &[String]) {
match args.get(1) {
Some(arg) => match arg.as_str() {
"bench" |
"build" |
"check" |
"doc" |
"install" |
"run" |
"rustc" |
"test" => cargo::main(args),
"bench" | "build" | "check" | "doc" | "install" | "run" | "rustc" | "test" => {
cargo::main(args)
}
"env" => env::main(args),
"exec" => exec::main(args),
"toolchain" => toolchain::main(args),
......
use redoxfs::{DiskFile, FileSystem};
use std::{fs, io, thread, time};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::mpsc::{channel, TryRecvError};
use std::{fs, io, thread, time};
use crate::{status_error, syscall_error};
......@@ -15,10 +15,7 @@ impl RedoxFs {
pub fn new<P: AsRef<Path>, Q: AsRef<Path>>(image: P, dir: Q) -> io::Result<Self> {
let image = image.as_ref().to_owned();
let dir = fs::canonicalize(dir)?;
let mut s = Self {
image,
dir
};
let mut s = Self { image, dir };
s.mount()?;
Ok(s)
}
......@@ -29,7 +26,7 @@ impl RedoxFs {
if self.mounted()? {
return Err(io::Error::new(
io::ErrorKind::Other,
"directory was already mounted"
"directory was already mounted",
));
}
......@@ -39,28 +36,28 @@ impl RedoxFs {
let fs = FileSystem::open(disk, None, None, true).map_err(syscall_error)?;
let dir = self.dir.clone();
thread::spawn(move || {
let _ = tx.send(redoxfs::mount(
fs,
dir,
|_| {}
));
let _ = tx.send(redoxfs::mount(fs, dir, |_| {}));
});
while ! self.mounted()? {
while !self.mounted()? {
match rx.try_recv() {
Ok(res) => match res {
Ok(()) => return Err(io::Error::new(
io::ErrorKind::NotConnected,
"redoxfs thread exited early"
)),
Ok(()) => {
return Err(io::Error::new(
io::ErrorKind::NotConnected,
"redoxfs thread exited early",
))
}
Err(err) => return Err(err),
},
Err(err) => match err {
TryRecvError::Empty => (),
TryRecvError::Disconnected => return Err(io::Error::new(
io::ErrorKind::NotConnected,
"redoxfs thread did not send a result"
)),
TryRecvError::Disconnected => {
return Err(io::Error::new(
io::ErrorKind::NotConnected,
"redoxfs thread did not send a result",
))
}
},
}
thread::sleep(time::Duration::from_millis(1));
......@@ -82,7 +79,7 @@ impl RedoxFs {
if self.mounted()? {
return Err(io::Error::new(
io::ErrorKind::Other,
"directory was still mounted"
"directory was still mounted",
));
}
}
......@@ -98,7 +95,7 @@ impl RedoxFs {
for mount_res in MountIter::new()? {
let mount = mount_res?;
if mount.dest == self.dir {
return Ok(true)
return Ok(true);
}
}
......
use std::{env, fs, io};
use std::path::{Path, PathBuf};
use std::process::{self, Command};
use std::{env, fs, io};
use crate::{redoxer_dir, status_error, target};
//TODO: Rewrite with hyper or reqwest, tar-rs, sha2, and some gzip crate?
fn download<P: AsRef<Path>>(url: &str, path: P) -> io::Result<()> {
Command::new("curl")
.arg("--proto").arg("=https")
.arg("--proto")
.arg("=https")
.arg("--tlsv1.2")
.arg("--fail")
.arg("--output").arg(path.as_ref())
.arg("--output")
.arg(path.as_ref())
.arg(url)
.status()
.and_then(status_error)
......@@ -18,12 +20,10 @@ fn download<P: AsRef<Path>>(url: &str, path: P) -> io::Result<()> {
//TODO: Rewrite with hyper or reqwest, tar-rs, sha2, and some gzip crate?
fn shasum<P: AsRef<Path>>(path: P) -> io::Result<bool> {
let parent = path.as_ref().parent().ok_or(
io::Error::new(
io::ErrorKind::Other,
"shasum path had no parent"
)
)?;
let parent = path.as_ref().parent().ok_or(io::Error::new(
io::ErrorKind::Other,
"shasum path had no parent",
))?;
Command::new("sha256sum")
.arg("--check")
.arg("--ignore-missing")
......@@ -42,7 +42,7 @@ pub fn toolchain() -> io::Result<PathBuf> {
let url = format!("https://static.redox-os.org/toolchain/{}", target());
let toolchain_dir = redoxer_dir().join("toolchain");
if ! toolchain_dir.is_dir() {
if !toolchain_dir.is_dir() {
println!("redoxer: building toolchain");
let toolchain_partial = redoxer_dir().join("toolchain.partial");
......@@ -57,17 +57,16 @@ pub fn toolchain() -> io::Result<PathBuf> {
let prefix_tar = toolchain_partial.join("rust-install.tar.gz");
download(&format!("{}/rust-install.tar.gz", url), &prefix_tar)?;
if ! shasum(&shasum_file)? {
return Err(io::Error::new(
io::ErrorKind::Other,
"shasum invalid"
));
if !shasum(&shasum_file)? {
return Err(io::Error::new(io::ErrorKind::Other, "shasum invalid"));
}
Command::new("tar")
.arg("--extract")
.arg("--file").arg(&prefix_tar)
.arg("-C").arg(&toolchain_partial)
.arg("--file")
.arg(&prefix_tar)
.arg("-C")
.arg(&toolchain_partial)
.arg(".")
.status()
.and_then(status_error)?;
......@@ -82,7 +81,7 @@ pub fn main(_args: &[String]) {
match toolchain() {
Ok(_) => {
process::exit(0);
},
}
Err(err) => {
eprintln!("redoxer toolchain: {}", err);
process::exit(1);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment