Skip to content
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

feat: lockfiles #3231

Merged
merged 14 commits into from
Nov 3, 2019
Merged
Prev Previous commit
Next Next commit
use @hayd's suggestion for flags
  • Loading branch information
ry committed Oct 29, 2019
commit e8745d1a17d66d3c3b3a7bb78f3941310c776b51
31 changes: 14 additions & 17 deletions cli/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ pub struct DenoFlags {
// Use tokio::runtime::current_thread
pub current_thread: bool,

pub lock_check: Option<String>,
pub lock_write: Option<String>,
pub lock: Option<String>,
pub lock_write: bool,
}

static ENV_VARIABLES_HELP: &str = "ENVIRONMENT VARIABLES:
Expand Down Expand Up @@ -227,18 +227,16 @@ Examples: https://github.com/WICG/import-maps#the-import-map",
})
.global(true),
).arg(
Arg::with_name("lock-check")
.long("lock-check")
Arg::with_name("lock")
.long("lock")
.value_name("FILE")
.help("Check the specified lockfile")
.help("Check the specified lock file")
.takes_value(true)
.global(true),
).arg(
Arg::with_name("lock-write")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it may be more user friendly to do deno --lock-write lockfile.json rather than deno --lock lockfile.json --lock-write.
I'd be okay with doing this in in a follow-up PR tho.

.long("lock-write")
.value_name("FILE")
.help("Write a lock file to specified filename")
.takes_value(true)
.help("Write lock file. Use with --lock to specify file.")
.global(true),
).arg(
Arg::with_name("v8-options")
Expand Down Expand Up @@ -651,13 +649,12 @@ pub fn parse_flags(
}
}
}
if matches.is_present("lock-check") {
let lockfile = matches.value_of("lock-check").unwrap();
flags.lock_check = Some(lockfile.to_string());
if matches.is_present("lock") {
let lockfile = matches.value_of("lock").unwrap();
flags.lock = Some(lockfile.to_string());
}
if matches.is_present("lock-write") {
let lockfile = matches.value_of("lock-write").unwrap();
flags.lock_write = Some(lockfile.to_string());
flags.lock_write = true;
}

flags = parse_run_args(flags, matches);
Expand Down Expand Up @@ -1920,15 +1917,15 @@ mod tests {
fn test_flags_from_vec_38() {
let (flags, subcommand, argv) = flags_from_vec(svec![
"deno",
"--lock-write=deno_lock.json",
"--lock-check=deno_lock2.json",
"--lock-write",
"--lock=lock.json",
"script.ts"
]);
assert_eq!(
flags,
DenoFlags {
lock_write: Some("deno_lock.json".to_string()),
lock_check: Some("deno_lock2.json".to_string()),
lock_write: true,
lock: Some("lock.json".to_string()),
..DenoFlags::default()
}
);
Expand Down
11 changes: 8 additions & 3 deletions cli/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,14 @@ fn run_script(flags: DenoFlags, argv: Vec<String>) {
worker
.execute_mod_async(&main_module, None, false)
.and_then(move |()| {
if let Some(ref lockfile) = state.lock_write {
let g = lockfile.lock().unwrap();
g.write()?;
if state.flags.lock_write {
if let Some(ref lockfile) = state.lockfile {
let g = lockfile.lock().unwrap();
g.write()?;
} else {
eprintln!("--lock flag must be specified when using --lock-write");
std::process::exit(11);
ry marked this conversation as resolved.
Show resolved Hide resolved
}
}
Ok(())
})
Expand Down
34 changes: 15 additions & 19 deletions cli/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ pub struct State {

pub include_deno_namespace: bool,

pub lock_check: Option<Mutex<Lockfile>>,
pub lock_write: Option<Mutex<Lockfile>>,
pub lockfile: Option<Mutex<Lockfile>>,
}

impl Clone for ThreadSafeState {
Expand Down Expand Up @@ -259,10 +258,8 @@ impl ThreadSafeState {

let modules = Arc::new(Mutex::new(deno::Modules::new()));

let lock_write = Lockfile::from_flag(&flags.lock_write).map(Mutex::new);

// Note: reads lazily from disk on first call to lock_check.check()
let lock_check = Lockfile::from_flag(&flags.lock_check).map(Mutex::new);
// Note: reads lazily from disk on first call to lockfile.check()
let lockfile = Lockfile::from_flag(&flags.lock).map(Mutex::new);

let state = State {
main_module,
Expand All @@ -285,8 +282,7 @@ impl ThreadSafeState {
js_compiler: JsCompiler {},
json_compiler: JsonCompiler {},
include_deno_namespace,
lock_check,
lock_write,
lockfile,
};

Ok(ThreadSafeState(Arc::new(state)))
Expand Down Expand Up @@ -323,20 +319,20 @@ impl ThreadSafeState {
}
})
.and_then(move |compiled_module| {
if let Some(ref lockfile) = state2.lock_check {
if let Some(ref lockfile) = state2.lockfile {
let mut g = lockfile.lock().unwrap();
if !g.check(&compiled_module)? {
eprintln!(
"lock file check failed {} {}",
g.filename, compiled_module.name
);
std::process::exit(10);
if state2.flags.lock_write {
g.insert(&compiled_module);
} else {
if !g.check(&compiled_module)? {
eprintln!(
"lock file check failed {} {}",
g.filename, compiled_module.name
);
std::process::exit(10);
}
}
}
if let Some(ref lockfile) = state2.lock_write {
let mut g = lockfile.lock().unwrap();
g.insert(&compiled_module);
}
Ok(compiled_module)
})
}
Expand Down
4 changes: 2 additions & 2 deletions cli/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,13 +349,13 @@ itest!(_050_more_jsons {
});

itest!(lock_check_ok {
args: "run --lock-check=lock_check_ok.json https://127.0.0.1:4545/cli/tests/003_relative_import.ts",
args: "run --lock=lock_check_ok.json https://127.0.0.1:4545/cli/tests/003_relative_import.ts",
output: "003_relative_import.ts.out",
http_server: true,
});

itest!(lock_check_err {
args: "run --lock-check=lock_check_err.json https://127.0.0.1:4545/cli/tests/003_relative_import.ts",
args: "run --lock=lock_check_err.json https://127.0.0.1:4545/cli/tests/003_relative_import.ts",
output: "lock_check_err.out",
check_stderr: true,
exit_code: 10,
Expand Down