Recreate Main.enso file if it is missing. (https://github.com/enso-org/ide/pull/705)

Original commit: 709c277ded
This commit is contained in:
Michał Wawrzyniec Urbańczyk 2020-08-04 09:43:30 +02:00 committed by GitHub
parent 89be0ed4f5
commit e5010efe81

View File

@ -3,6 +3,7 @@
use crate::prelude::*; use crate::prelude::*;
use crate::controller::FilePath;
use crate::model::module::Path as ModulePath; use crate::model::module::Path as ModulePath;
use crate::view::layout::ViewLayout; use crate::view::layout::ViewLayout;
@ -42,6 +43,9 @@ pub const INITIAL_MODULE_NAME:&str = "Main";
/// This is the definition whose graph will be opened on IDE start. /// This is the definition whose graph will be opened on IDE start.
pub const MAIN_DEFINITION_NAME:&str = "main"; pub const MAIN_DEFINITION_NAME:&str = "main";
/// The default content of the newly created initial module file.
pub const DEFAULT_MAIN_CONTENT:&str = r#"main = IO.println "Hello, World!""#;
// =================== // ===================
@ -76,13 +80,26 @@ pub fn initial_module_path(project:&model::Project) -> FallibleResult<ModulePath
model::module::Path::from_name_segments(project.content_root_id(),&[INITIAL_MODULE_NAME]) model::module::Path::from_name_segments(project.content_root_id(),&[INITIAL_MODULE_NAME])
} }
/// Create a file with default content if it does not already exist.
pub async fn recreate_if_missing(project:&model::Project, path:&FilePath, default_content:String)
-> FallibleResult<()> {
let rpc = project.json_rpc();
if !rpc.file_exists(path).await?.exists {
rpc.write_file(path,&default_content).await?;
}
Ok(())
}
impl ProjectView { impl ProjectView {
/// Create a new ProjectView. /// Create a new ProjectView.
pub async fn new(logger:impl AnyLogger, model:model::Project) pub async fn new(logger:impl AnyLogger, model:model::Project)
-> FallibleResult<Self> { -> FallibleResult<Self> {
let logger = Logger::sub(logger,"ProjectView"); let logger = Logger::sub(logger,"ProjectView");
let module_path = initial_module_path(&model)?; let module_path = initial_module_path(&model)?;
let file_path = module_path.file_path().clone(); let file_path = module_path.file_path().clone();
// TODO [mwu] This solution to recreate missing main file should be considered provisional
// until proper decision is made. See: https://github.com/enso-org/enso/issues/1050
recreate_if_missing(&model,&file_path,DEFAULT_MAIN_CONTENT.into()).await?;
let text_controller = controller::Text::new(&logger,&model,file_path).await?; let text_controller = controller::Text::new(&logger,&model,file_path).await?;
let method = module_path.method_pointer(MAIN_DEFINITION_NAME); let method = module_path.method_pointer(MAIN_DEFINITION_NAME);
let graph_controller = controller::ExecutedGraph::new(&logger,model.clone_ref(),method); let graph_controller = controller::ExecutedGraph::new(&logger,model.clone_ref(),method);