#! @file sources.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains how a target's `srcs` become the files `sakura` is given: a file as #! it is, a folder as each `.sakura` file within it, and a glob as what it #! matches, each relative to the project, in the order written, each once. import std; import std::io; import system; import malunal::glob; import blossom::models; module blossom::management; #! The files a program target is built from. #! @param holder The project. #! @param each The target. #! @returns The files, relative to the project's folder, or why one of its #! `srcs` names none. pub def sources_of(holder: &models::project, each: &models::target): result, failure> { var made := vector::::new(); for &written in each.srcs { val pattern := glob::pattern::parse(written.view()) else (e) { return failure { status = 2, message = $"{holder.file.view()}:{each.line}: `{written.view()}` in `{each.name.view()}`'s `srcs`: {e.describe().view()}" }; }; var found := vector::::new(); if pattern.is_literal { val named := path::parse(written.view()) else (_) { return failure { status = 2, message = $"{holder.file.view()}:{each.line}: `{written.view()}` is no path." }; }; found.append(holder.folder.join(&named)); } else { var matched := pattern.find(&holder.folder); if matched is error(&why) { return failure { status = 1, message = why.describe() }; } if matched is value(&mut paths) { found = move(paths); } } var any := false; for &at in found { if at.is_directory { var within := sakura_files(at); if within is error(&why) { return failure { status = 1, message = why.describe() }; } if within is value(&files) { for &file in files { add_relative(&mut made, &holder.folder, file); any = true; } } } else if io::exists(at) { add_relative(&mut made, &holder.folder, at); any = true; } } if !any { return failure { status = 2, message = $"{holder.file.view()}:{each.line}: `{written.view()}`, in `{each.name.view()}`'s `srcs`, names no file." }; } } return made; } #! The files of a library, which `check` is given. #! @param holder The project. #! @param each The library. #! @returns The files, relative to the project's folder, or why its folder #! can't be read. pub def library_files(holder: &models::project, each: &models::target): result, failure> { val root := path::parse(each.root.view()) else (_) { return failure { status = 2, message = $"{holder.file.view()}:{each.line}: `{each.root.view()}` is no path." }; }; val folder := holder.folder.join(&root); if !folder.is_directory { return failure { status = 2, message = $"{holder.file.view()}:{each.line}: `{each.name.view()}`'s folder, `{each.root.view()}`, isn't there." }; } var made := vector::::new(); var within := sakura_files(&folder); if within is error(&why) { return failure { status = 1, message = why.describe() }; } if within is value(&files) { for &file in files { add_relative(&mut made, &holder.folder, file); } } return made; } # Every `.sakura` file within a folder, at any depth, in order. def sakura_files(folder: &path): result, glob::search_error> { val all := glob::pattern::parse("**/*.sakura") else (_) { return vector::::new(); }; return all.find(folder); } # Adds a file, relative to the project's folder, unless it's there already. def add_relative(into: &mut vector, folder: &path, file: &path): void { var text := string::from(file.view(), nothing {}); if file.relative_to(folder) is some(&relative) { text = string::from(relative.view(), nothing {}); } for &prior in into { return if prior == text; } into.append(move(text)); }