From ad12ffaa46d628f37fc77a2721d97c70a725bee5 Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Tue, 21 Jul 2020 17:37:17 +0200 Subject: [PATCH] Include all the .60 files in sixtyfps_widgets by default --- sixtyfps_compiler/Cargo.toml | 1 + sixtyfps_compiler/build.rs | 44 +++++++++++++++++++++++++++++++ sixtyfps_compiler/lib.rs | 24 ++++++++++++----- sixtyfps_compiler/typeregister.rs | 24 ++++++++++++++--- sixtyfps_widgets/Button.60 | 2 ++ tests/cases/external_type.60 | 5 +++- 6 files changed, 89 insertions(+), 11 deletions(-) create mode 100644 sixtyfps_compiler/build.rs create mode 100644 sixtyfps_widgets/Button.60 diff --git a/sixtyfps_compiler/Cargo.toml b/sixtyfps_compiler/Cargo.toml index fcacde73c..ea0760cd1 100644 --- a/sixtyfps_compiler/Cargo.toml +++ b/sixtyfps_compiler/Cargo.toml @@ -3,6 +3,7 @@ name = "sixtyfps_compilerlib" version = "0.1.0" authors = ["Sixty FPS "] edition = "2018" +build = "build.rs" [lib] path = "lib.rs" diff --git a/sixtyfps_compiler/build.rs b/sixtyfps_compiler/build.rs new file mode 100644 index 000000000..bbf523d8c --- /dev/null +++ b/sixtyfps_compiler/build.rs @@ -0,0 +1,44 @@ +use std::fs::read_dir; +use std::io::Write; +use std::path::{Path, PathBuf}; + +fn main() -> std::io::Result<()> { + let mut library_dir = PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").unwrap()); + library_dir.pop(); + library_dir.push("sixtyfps_widgets"); + + println!("cargo:rerun-if-changed={}", library_dir.display()); + + let output_file_path = Path::new(&std::env::var_os("OUT_DIR").unwrap()) + .join(Path::new("included_library").with_extension("rs")); + + let library_files: Vec = read_dir(library_dir)? + .filter_map(Result::ok) + .filter(|entry| { + entry.path().is_file() + && entry.path().extension().unwrap_or_default() == std::ffi::OsStr::new("60") + }) + .map(|entry| entry.path()) + .collect(); + + let mut file = std::fs::File::create(&output_file_path)?; + write!(file, "pub fn widget_library() -> &'static [(&'static str, &'static str)] {{ &[")?; + write!( + file, + "{}", + library_files + .iter() + .map(|file| format!( + "(\"{}\" ,include_str!(\"{}\"))", + file.file_name().unwrap().to_string_lossy(), + file.display() + )) + .collect::>() + .join(",") + )?; + write!(file, "] }}")?; + + println!("cargo:rustc-env=SIXTYFPS_WIDGETS_LIBRARY={}", output_file_path.display()); + + Ok(()) +} diff --git a/sixtyfps_compiler/lib.rs b/sixtyfps_compiler/lib.rs index 1e3ed4d5f..c3107db27 100644 --- a/sixtyfps_compiler/lib.rs +++ b/sixtyfps_compiler/lib.rs @@ -61,9 +61,18 @@ pub fn compile_syntax_node( let mut build_diagnostics = diagnostics::BuildDiagnostics::default(); let global_type_registry = typeregister::TypeRegister::builtin(); - let type_registry = if !compiler_config.include_paths.is_empty() { - let library = Rc::new(RefCell::new(typeregister::TypeRegister::new(&global_type_registry))); + let type_registry = + Rc::new(RefCell::new(typeregister::TypeRegister::new(&global_type_registry))); + for (path, source) in library::widget_library() { + build_diagnostics.add(typeregister::TypeRegister::add_type_from_source( + &type_registry, + source.to_string(), + &path, + )); + } + + if !compiler_config.include_paths.is_empty() { build_diagnostics.extend( compiler_config .include_paths @@ -77,16 +86,13 @@ pub fn compile_syntax_node( } else { path.clone() }; - typeregister::TypeRegister::add_from_directory(&library, path) + typeregister::TypeRegister::add_from_directory(&type_registry, path) }) .filter_map(Result::ok) .flatten(), ); - - library - } else { - global_type_registry }; + let doc = crate::object_tree::Document::from_node(doc_node, &mut diagnostics, &type_registry); build_diagnostics.add(diagnostics); @@ -112,3 +118,7 @@ pub fn run_passes( passes::repeater_component::create_repeater_components(&doc.root_component); passes::move_declarations::move_declarations(&doc.root_component); } + +mod library { + include!(env!("SIXTYFPS_WIDGETS_LIBRARY")); +} diff --git a/sixtyfps_compiler/typeregister.rs b/sixtyfps_compiler/typeregister.rs index 2e6d18914..25477d462 100644 --- a/sixtyfps_compiler/typeregister.rs +++ b/sixtyfps_compiler/typeregister.rs @@ -447,6 +447,24 @@ impl TypeRegister { /// Loads the .60 file and adds it to the type registry. An error is returned if there were I/O problems, /// otherwise the diagnostics collected during the parsing are returned. pub fn add_type_from_source>( + registry: &Rc>, + source: String, + path: P, + ) -> FileDiagnostics { + let (syntax_node, mut diag) = crate::parser::parse(source, Some(path.as_ref())); + + let doc = crate::object_tree::Document::from_node(syntax_node, &mut diag, ®istry); + + if !doc.root_component.id.is_empty() { + registry.borrow_mut().add(doc.root_component); + } + + diag + } + + /// Loads the .60 file and adds it to the type registry. An error is returned if there were I/O problems, + /// otherwise the diagnostics collected during the parsing are returned. + pub fn add_type_from_path>( registry: &Rc>, path: P, ) -> std::io::Result { @@ -482,7 +500,7 @@ impl TypeRegister { } }) .map(|path| { - TypeRegister::add_type_from_source(registry, &path) + TypeRegister::add_type_from_path(registry, &path) .unwrap_or_else(|ioerr| FileDiagnostics::new_from_error(path, ioerr)) }) .collect()) @@ -528,7 +546,7 @@ fn test_extend_registry_from_source() { } test_source_path.set_file_name("lib_test.60"); - let result = TypeRegister::add_type_from_source(&local_types, &test_source_path); + let result = TypeRegister::add_type_from_path(&local_types, &test_source_path); assert!(result.is_ok()); assert_ne!(local_types.borrow().lookup("PublicType"), Type::Invalid); @@ -536,7 +554,7 @@ fn test_extend_registry_from_source() { // Now try again. test_source_path.set_file_name("lib_test2.60"); - let result = TypeRegister::add_type_from_source(&local_types, &test_source_path); + let result = TypeRegister::add_type_from_path(&local_types, &test_source_path); assert!(result.is_ok()); let diagnostics = result.unwrap(); diff --git a/sixtyfps_widgets/Button.60 b/sixtyfps_widgets/Button.60 new file mode 100644 index 000000000..9ca181fe2 --- /dev/null +++ b/sixtyfps_widgets/Button.60 @@ -0,0 +1,2 @@ + +Button := Rectangle {} diff --git a/tests/cases/external_type.60 b/tests/cases/external_type.60 index 9d994a7b0..e0cba0202 100644 --- a/tests/cases/external_type.60 +++ b/tests/cases/external_type.60 @@ -1,2 +1,5 @@ //include_path: ../helper_components -TestCase := TestButton {} \ No newline at end of file +TestCase := Rectangle { + TestButton {} // from external file + Button {} // from standard library +} \ No newline at end of file