Skip to content

Commit

Permalink
show warning if not satisfy version
Browse files Browse the repository at this point in the history
  • Loading branch information
Roger-luo committed Feb 10, 2023
1 parent d2a9f35 commit 1d64691
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/bin/ion/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub fn exec(config: &mut Config, matches: &ArgMatches) -> CliResult {
};
mk_package_dir(&path, force)?;

let mut ctx = Context::new(prompt, Julia::default(), Project::new(package, path));
let mut ctx = Context::new(prompt, Julia::new(&config), Project::new(package, path));
let name = matches.get_one::<String>("template").unwrap().to_owned();
let template = Template::from_name(config, &name)?;
if let Err(e) = template.render(config, &mut ctx) {
Expand Down
3 changes: 2 additions & 1 deletion src/bin/ion/commands/pkg/why.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clap::parser::ArgMatches;
use clap::{arg, Command, ValueHint};
use ion::config::Config;
use ion::errors::CliResult;
use ion::utils::Julia;
use ion::utils::{assert_julia_version, Julia};

pub fn cli() -> Command {
Command::new("why")
Expand All @@ -18,6 +18,7 @@ pub fn cli() -> Command {
}

pub fn exec(config: &mut Config, matches: &ArgMatches) -> CliResult {
assert_julia_version(config, ">=1.9.0-beta")?;
format!("using Pkg; Pkg.why([{}])", package_spec_list(matches))
.julia_exec(config, matches.get_flag("global"))?;
Ok(())
Expand Down
8 changes: 4 additions & 4 deletions src/ion/blueprints/context.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::badge::Badge;
use crate::blueprints::*;
use crate::utils::find::julia_version;
use anyhow::Error;
use clap::ArgMatches;
use dialoguer::Input;
Expand Down Expand Up @@ -109,7 +110,7 @@ impl Context {
self
}

pub fn from_matches(matches: &ArgMatches) -> Result<Self, Error> {
pub fn from_matches(config: &Config, matches: &ArgMatches) -> Result<Self, Error> {
let prompt = !matches.get_flag("no-interactive");
let package = match matches.get_one::<String>("name") {
Some(name) => name.to_owned(),
Expand Down Expand Up @@ -141,11 +142,10 @@ impl Context {
}
std::fs::create_dir_all(&path).unwrap();

let julia_version_str = julia_version()?[14..].to_string();
let version = node_semver::Version::parse(&julia_version_str)?;
let version = julia_version(&config)?;
let compat = format!("{}.{}", version.major, version.minor);
let julia = Julia {
version: julia_version_str,
version: version.to_string(),
compat,
};

Expand Down
14 changes: 5 additions & 9 deletions src/ion/blueprints/info.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::blueprints::*;
use crate::spec::Author;
use crate::utils::find::julia_version;
use serde_derive::Serialize;
use std::path::PathBuf;

Expand All @@ -15,21 +16,16 @@ pub struct Julia {
pub compat: String,
}

impl Default for Julia {
fn default() -> Self {
let julia_version_str = match julia_version() {
Ok(version_str) => version_str[14..].to_string(),
Err(_) => "1.6.0".to_string(),
};

let version = match node_semver::Version::parse(&julia_version_str) {
impl Julia {
pub fn new(config: &Config) -> Self {
let version = match julia_version(&config) {
Ok(version) => version,
Err(_) => node_semver::Version::parse("1.6.0").unwrap(),
};

let compat = format!("{}.{}", version.major, version.minor);
Julia {
version: julia_version_str,
version: version.to_string(),
compat,
}
}
Expand Down
14 changes: 0 additions & 14 deletions src/ion/blueprints/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::blueprints::*;
use crate::spec::Author;
use anyhow::{format_err, Error, Result};
use dialoguer::{Confirm, Input};
use std::process::Command;

pub fn git_get_user() -> Result<(String, String)> {
let user = if let Some(name) = git_config_get("user.name") {
Expand Down Expand Up @@ -113,19 +112,6 @@ pub fn list_templates(config: &Config) -> Result<()> {
Ok(())
}

pub fn julia_version() -> Result<String> {
let output = Command::new("julia").arg("--version").output();

match output {
Err(e) => Err(Error::new(e)),
Ok(output) => {
let version = String::from_utf8(output.stdout)?;
let version = version.trim();
Ok(version.to_string())
}
}
}

pub fn git_config_get(key: &str) -> Option<String> {
let output = std::process::Command::new("git")
.arg("config")
Expand Down
12 changes: 12 additions & 0 deletions src/ion/utils/find.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::config::Config;
use crate::JuliaProject;
use anyhow::Result;
use dirs::home_dir;
use std::path::PathBuf;
use std::process::Command;

pub fn current_project(dir: PathBuf) -> Option<PathBuf> {
let home = home_dir().unwrap();
Expand Down Expand Up @@ -46,3 +49,12 @@ pub fn current_root_project(dir: PathBuf) -> Option<(JuliaProject, PathBuf)> {
}
}
}

pub fn julia_version(config: &Config) -> Result<node_semver::Version> {
let output = Command::new(config.julia().exe).arg("--version").output()?;

let version = String::from_utf8(output.stdout)?;
let version = version.trim();
let version = version.split_whitespace().last().unwrap();
Ok(node_semver::Version::parse(version)?)
}
12 changes: 12 additions & 0 deletions src/ion/utils/julia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use std::process::{Command, Output};

use crate::config::Config;

use super::julia_version;

pub struct JuliaCommand {
cmd: Command,
script: String,
Expand Down Expand Up @@ -132,3 +134,13 @@ mod test {
assert_eq!(args[2], "Baz");
}
}

pub fn assert_julia_version(config: &Config, version_spec: impl AsRef<str>) -> Result<()> {
let range = node_semver::Range::parse(version_spec.as_ref()).expect("Invalid version spec");
let version = julia_version(config)?;
range.satisfies(&version).then(|| ()).ok_or_else(|| {
format_err!(
"Invalid Julia version: Julia version {version} does not satisfy version range {range}",
)
})
}

0 comments on commit 1d64691

Please sign in to comment.