Making WebIDL generation deterministic (#2101)

* Making webidl generation deterministic

* Fixing line endings

* Regenerating WebIDL

* Running rustfmt
This commit is contained in:
Pauan
2020-04-23 03:01:40 +02:00
committed by GitHub
parent 7bc9147258
commit a22bbca92c
3 changed files with 32 additions and 17 deletions

View File

@ -25,7 +25,7 @@ use crate::generator::{
use crate::idl_type::ToIdlType;
use crate::traverse::TraverseType;
use crate::util::{
camel_case_ident, is_structural, shouty_snake_case_ident, snake_case_ident, throws,
camel_case_ident, is_structural, read_dir, shouty_snake_case_ident, snake_case_ident, throws,
webidl_const_v_to_backend_const_v, TypePosition,
};
use anyhow::Context;
@ -749,11 +749,9 @@ pub fn generate(from: &Path, to: &Path, options: Options) -> Result<String> {
/// Read all WebIDL files in a directory into a single `SourceFile`
fn read_source_from_path(dir: &Path) -> Result<SourceFile> {
let entries = fs::read_dir(dir).context("reading webidls directory")?;
let entries = read_dir(dir).context("reading webidls directory")?;
let mut source = SourceFile::default();
for entry in entries {
let entry = entry.context(format!("getting {}/*.webidl entry", dir.display()))?;
let path = entry.path();
for path in entries {
if path.extension() != Some(OsStr::new("webidl")) {
continue;
}

View File

@ -1,5 +1,7 @@
use std::collections::BTreeSet;
use std::fs;
use std::iter::FromIterator;
use std::path::{Path, PathBuf};
use std::ptr;
use heck::{CamelCase, ShoutySnakeCase, SnakeCase};
@ -23,6 +25,21 @@ use crate::Options;
/// in their names, where `n` is this constant.
const MAX_VARIADIC_ARGUMENTS_COUNT: usize = 7;
/// Similar to std::fs::read_dir except it returns a sorted Vec,
/// which is important to make the code generation deterministic.
pub(crate) fn read_dir<P>(path: P) -> std::io::Result<Vec<PathBuf>>
where
P: AsRef<Path>,
{
let mut entries = fs::read_dir(path)?
.map(|entry| Ok(entry?.path()))
.collect::<std::io::Result<Vec<_>>>()?;
entries.sort();
Ok(entries)
}
/// Take a type and create an immutable shared reference to that type.
pub(crate) fn shared_ref(ty: syn::Type, mutable: bool) -> syn::Type {
syn::TypeReference {