2024-01-01 14:58:21 -05:00
|
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-09-15 20:42:09 -04:00
|
|
|
|
|
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
|
use deno_core::serde_json;
|
|
|
|
|
use deno_core::serde_json::json;
|
|
|
|
|
use std::env::current_exe;
|
|
|
|
|
use std::io::Write;
|
|
|
|
|
use std::path::Path;
|
2024-05-21 16:35:21 -04:00
|
|
|
|
|
2024-05-29 18:46:47 -04:00
|
|
|
|
use jupyter_runtime::dirs::user_data_dir;
|
2023-09-15 20:42:09 -04:00
|
|
|
|
|
|
|
|
|
const DENO_ICON_32: &[u8] = include_bytes!("./resources/deno-logo-32x32.png");
|
|
|
|
|
const DENO_ICON_64: &[u8] = include_bytes!("./resources/deno-logo-64x64.png");
|
2023-10-06 16:31:12 -04:00
|
|
|
|
const DENO_ICON_SVG: &[u8] = include_bytes!("./resources/deno-logo-svg.svg");
|
2023-09-15 20:42:09 -04:00
|
|
|
|
|
|
|
|
|
pub fn status() -> Result<(), AnyError> {
|
2024-05-21 16:35:21 -04:00
|
|
|
|
let user_data_dir = user_data_dir()?;
|
2023-09-15 20:42:09 -04:00
|
|
|
|
|
2024-05-21 16:35:21 -04:00
|
|
|
|
let kernel_spec_dir_path = user_data_dir.join("kernels").join("deno");
|
|
|
|
|
let kernel_spec_path = kernel_spec_dir_path.join("kernel.json");
|
2023-09-15 20:42:09 -04:00
|
|
|
|
|
2024-05-21 16:35:21 -04:00
|
|
|
|
if kernel_spec_path.exists() {
|
|
|
|
|
log::info!("✅ Deno kernel already installed");
|
|
|
|
|
Ok(())
|
|
|
|
|
} else {
|
|
|
|
|
log::warn!("ℹ️ Deno kernel is not yet installed, run `deno jupyter --install` to set it up");
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2023-09-15 20:42:09 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn install_icon(
|
|
|
|
|
dir_path: &Path,
|
|
|
|
|
filename: &str,
|
|
|
|
|
icon_data: &[u8],
|
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
|
let path = dir_path.join(filename);
|
|
|
|
|
let mut file = std::fs::File::create(path)?;
|
|
|
|
|
file.write_all(icon_data)?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn install() -> Result<(), AnyError> {
|
2024-05-21 16:35:21 -04:00
|
|
|
|
let user_data_dir = user_data_dir()?;
|
|
|
|
|
let kernel_dir = user_data_dir.join("kernels").join("deno");
|
|
|
|
|
|
|
|
|
|
std::fs::create_dir_all(&kernel_dir)?;
|
|
|
|
|
|
|
|
|
|
let kernel_json_path = kernel_dir.join("kernel.json");
|
2023-09-15 20:42:09 -04:00
|
|
|
|
|
|
|
|
|
// TODO(bartlomieju): add remaining fields as per
|
|
|
|
|
// https://jupyter-client.readthedocs.io/en/stable/kernels.html#kernel-specs
|
|
|
|
|
// FIXME(bartlomieju): replace `current_exe` before landing?
|
|
|
|
|
let json_data = json!({
|
2024-01-18 17:16:14 -05:00
|
|
|
|
"argv": [current_exe().unwrap().to_string_lossy(), "jupyter", "--kernel", "--conn", "{connection_file}"],
|
2023-09-15 20:42:09 -04:00
|
|
|
|
"display_name": "Deno",
|
|
|
|
|
"language": "typescript",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let f = std::fs::File::create(kernel_json_path)?;
|
|
|
|
|
serde_json::to_writer_pretty(f, &json_data)?;
|
2024-10-16 08:45:21 -04:00
|
|
|
|
install_icon(&kernel_dir, "logo-32x32.png", DENO_ICON_32)?;
|
|
|
|
|
install_icon(&kernel_dir, "logo-64x64.png", DENO_ICON_64)?;
|
|
|
|
|
install_icon(&kernel_dir, "logo-svg.svg", DENO_ICON_SVG)?;
|
2023-09-15 20:42:09 -04:00
|
|
|
|
|
2024-05-08 22:45:06 -04:00
|
|
|
|
log::info!("✅ Deno kernelspec installed successfully.");
|
2023-09-15 20:42:09 -04:00
|
|
|
|
Ok(())
|
|
|
|
|
}
|