Include all the .60 files in sixtyfps_widgets by default

This commit is contained in:
Simon Hausmann 2020-07-21 17:37:17 +02:00
parent abe6888296
commit ad12ffaa46
6 changed files with 89 additions and 11 deletions

View file

@ -3,6 +3,7 @@ name = "sixtyfps_compilerlib"
version = "0.1.0" version = "0.1.0"
authors = ["Sixty FPS <info@sixtyfps.io>"] authors = ["Sixty FPS <info@sixtyfps.io>"]
edition = "2018" edition = "2018"
build = "build.rs"
[lib] [lib]
path = "lib.rs" path = "lib.rs"

View file

@ -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<PathBuf> = 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::<Vec<_>>()
.join(",")
)?;
write!(file, "] }}")?;
println!("cargo:rustc-env=SIXTYFPS_WIDGETS_LIBRARY={}", output_file_path.display());
Ok(())
}

View file

@ -61,9 +61,18 @@ pub fn compile_syntax_node(
let mut build_diagnostics = diagnostics::BuildDiagnostics::default(); let mut build_diagnostics = diagnostics::BuildDiagnostics::default();
let global_type_registry = typeregister::TypeRegister::builtin(); let global_type_registry = typeregister::TypeRegister::builtin();
let type_registry = if !compiler_config.include_paths.is_empty() { let type_registry =
let library = Rc::new(RefCell::new(typeregister::TypeRegister::new(&global_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( build_diagnostics.extend(
compiler_config compiler_config
.include_paths .include_paths
@ -77,16 +86,13 @@ pub fn compile_syntax_node(
} else { } else {
path.clone() path.clone()
}; };
typeregister::TypeRegister::add_from_directory(&library, path) typeregister::TypeRegister::add_from_directory(&type_registry, path)
}) })
.filter_map(Result::ok) .filter_map(Result::ok)
.flatten(), .flatten(),
); );
library
} else {
global_type_registry
}; };
let doc = crate::object_tree::Document::from_node(doc_node, &mut diagnostics, &type_registry); let doc = crate::object_tree::Document::from_node(doc_node, &mut diagnostics, &type_registry);
build_diagnostics.add(diagnostics); build_diagnostics.add(diagnostics);
@ -112,3 +118,7 @@ pub fn run_passes(
passes::repeater_component::create_repeater_components(&doc.root_component); passes::repeater_component::create_repeater_components(&doc.root_component);
passes::move_declarations::move_declarations(&doc.root_component); passes::move_declarations::move_declarations(&doc.root_component);
} }
mod library {
include!(env!("SIXTYFPS_WIDGETS_LIBRARY"));
}

View file

@ -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, /// 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. /// otherwise the diagnostics collected during the parsing are returned.
pub fn add_type_from_source<P: AsRef<std::path::Path>>( pub fn add_type_from_source<P: AsRef<std::path::Path>>(
registry: &Rc<RefCell<Self>>,
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, &registry);
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<P: AsRef<std::path::Path>>(
registry: &Rc<RefCell<Self>>, registry: &Rc<RefCell<Self>>,
path: P, path: P,
) -> std::io::Result<FileDiagnostics> { ) -> std::io::Result<FileDiagnostics> {
@ -482,7 +500,7 @@ impl TypeRegister {
} }
}) })
.map(|path| { .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)) .unwrap_or_else(|ioerr| FileDiagnostics::new_from_error(path, ioerr))
}) })
.collect()) .collect())
@ -528,7 +546,7 @@ fn test_extend_registry_from_source() {
} }
test_source_path.set_file_name("lib_test.60"); 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!(result.is_ok());
assert_ne!(local_types.borrow().lookup("PublicType"), Type::Invalid); assert_ne!(local_types.borrow().lookup("PublicType"), Type::Invalid);
@ -536,7 +554,7 @@ fn test_extend_registry_from_source() {
// Now try again. // Now try again.
test_source_path.set_file_name("lib_test2.60"); 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()); assert!(result.is_ok());
let diagnostics = result.unwrap(); let diagnostics = result.unwrap();

View file

@ -0,0 +1,2 @@
Button := Rectangle {}

View file

@ -1,2 +1,5 @@
//include_path: ../helper_components //include_path: ../helper_components
TestCase := TestButton {} TestCase := Rectangle {
TestButton {} // from external file
Button {} // from standard library
}