Skip to content

Commit

Permalink
#370 Support for explicit channel argument
Browse files Browse the repository at this point in the history
  • Loading branch information
anti-social committed Feb 20, 2020
1 parent 0f1941e commit f45cd9f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 14 deletions.
19 changes: 14 additions & 5 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
use std::{env, path::PathBuf};

use rustc_version::Channel;

use crate::Target;
use crate::cargo::Subcommand;
use crate::rustc::TargetList;
use crate::errors::Result;
use crate::rustc::{ChannelExt, TargetList};

#[derive(Debug)]
pub struct Args {
pub all: Vec<String>,
pub subcommand: Option<Subcommand>,
pub channel: Option<Channel>,
pub target: Option<Target>,
pub target_dir: Option<PathBuf>,
}

pub fn parse(target_list: &TargetList) -> Args {
pub fn parse(target_list: &TargetList) -> Result<Args> {
let mut channel = None;
let mut target = None;
let mut target_dir = None;
let mut sc = None;
Expand All @@ -20,7 +26,9 @@ pub fn parse(target_list: &TargetList) -> Args {
{
let mut args = env::args().skip(1);
while let Some(arg) = args.next() {
if arg == "--target" {
if arg.starts_with("+") {
channel = Some(Channel::from_str(&arg[1..])?);
} else if arg == "--target" {
all.push(arg);
if let Some(t) = args.next() {
target = Some(Target::from(&t, target_list));
Expand Down Expand Up @@ -50,10 +58,11 @@ pub fn parse(target_list: &TargetList) -> Args {
}
}

Args {
Ok(Args {
all,
subcommand: sc,
channel,
target,
target_dir,
}
})
}
7 changes: 7 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,11 @@ error_chain! {
Io(std::io::Error);
Which(which::Error);
}

errors {
InvalidChannelName(channel: String) {
description("invalid channel name")
display("invalid channel name: '{}'", channel)
}
}
}
26 changes: 18 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use toml::{Value, value::Table};

use self::cargo::{Root, Subcommand};
use self::errors::*;
use self::rustc::{TargetList, VersionMetaExt};
use self::rustc::{ChannelExt, TargetList, VersionMetaExt};

#[allow(non_camel_case_types)]
#[derive(Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -208,7 +208,7 @@ pub fn main() {

fn run() -> Result<ExitStatus> {
let target_list = rustc::target_list(false)?;
let args = cli::parse(&target_list);
let args = cli::parse(&target_list)?;

if args.all.iter().any(|a| a == "--version" || a == "-V") &&
args.subcommand.is_none() {
Expand All @@ -228,9 +228,19 @@ fn run() -> Result<ExitStatus> {
.unwrap_or_else(|| Target::from(host.triple(), &target_list));
let toml = toml(&root)?;

let sysroot = rustc::sysroot(&host, &target, verbose)?;
let toolchain = sysroot.file_name().and_then(|file_name| file_name.to_str())
let mut sysroot = rustc::sysroot(&host, &target, verbose)?;
let default_toolchain = sysroot.file_name().and_then(|file_name| file_name.to_str())
.ok_or("couldn't get toolchain name")?;
let toolchain = if let Some(channel) = args.channel {
[channel.to_string()].iter().map(|c| c.as_str()).chain(
default_toolchain.splitn(2, '-').skip(1)
)
.collect::<Vec<_>>()
.join("-")
} else {
default_toolchain.to_string()
};
sysroot.set_file_name(&toolchain);

let installed_toolchains = rustup::installed_toolchains(verbose)?;

Expand All @@ -250,13 +260,13 @@ fn run() -> Result<ExitStatus> {

if !uses_xargo && !available_targets.is_installed(&target) {
rustup::install(&target, &toolchain, verbose)?;
} else if !rustup::component_is_installed("rust-src", toolchain, verbose)? {
rustup::install_component("rust-src", toolchain, verbose)?;
} else if !rustup::component_is_installed("rust-src", &toolchain, verbose)? {
rustup::install_component("rust-src", &toolchain, verbose)?;
}

if args.subcommand.map(|sc| sc == Subcommand::Clippy).unwrap_or(false) &&
!rustup::component_is_installed("clippy", toolchain, verbose)? {
rustup::install_component("clippy", toolchain, verbose)?;
!rustup::component_is_installed("clippy", &toolchain, verbose)? {
rustup::install_component("clippy", &toolchain, verbose)?;
}

let needs_interpreter = args.subcommand.map(|sc| sc.needs_interpreter()).unwrap_or(false);
Expand Down
29 changes: 28 additions & 1 deletion src/rustc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::path::PathBuf;
use std::process::Command;

use rustc_version::{Version, VersionMeta};
use rustc_version::{Channel, Version, VersionMeta};

use crate::{Host, Target};
use crate::errors::*;
Expand Down Expand Up @@ -38,6 +38,33 @@ impl VersionMetaExt for VersionMeta {
}
}

pub(crate) trait ChannelExt {
fn from_str(chan: &str) -> Result<Channel>;
fn to_string(&self) -> String;
}

impl ChannelExt for Channel {
fn from_str(chan: &str) -> Result<Channel> {
Ok(match chan {
"stable" => Channel::Stable,
"nightly" => Channel::Nightly,
"dev" => Channel::Dev,
"beta" => Channel::Beta,
_ => return Err(
ErrorKind::InvalidChannelName(chan.to_string()).into()
),
})
}
fn to_string(&self) -> String {
match self {
Channel::Stable => "stable",
Channel::Nightly => "nightly",
Channel::Dev => "dev",
Channel::Beta => "beta",
}.to_string()
}
}

pub fn target_list(verbose: bool) -> Result<TargetList> {
Command::new("rustc")
.args(&["--print", "target-list"])
Expand Down

0 comments on commit f45cd9f

Please sign in to comment.