149 lines
5.3 KiB
Plaintext
149 lines
5.3 KiB
Plaintext
#! @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::io;
|
|
import system;
|
|
import std::io::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<vector<string>, failure> {
|
|
var made := vector::<string>::new();
|
|
for &written in each.srcs {
|
|
val pattern := glob::pattern::parse(written.span) else (e) {
|
|
return failure { status = 2, message = $"{each.file.span}:{each.line}: `{written.span}` in `{each.name.span}`'s `srcs`: {e.describe().span}" };
|
|
};
|
|
var found := vector::<path>::new();
|
|
if pattern.is_literal {
|
|
val named := path::parse(written.span) else (_) {
|
|
return failure { status = 2, message = $"{each.file.span}:{each.line}: `{written.span}` 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 = $"{each.file.span}:{each.line}: `{written.span}`, in `{each.name.span}`'s `srcs`, names no file." };
|
|
}
|
|
}
|
|
return made;
|
|
}
|
|
|
|
#! The files of a library, which `check` is given: every `.sakura` file within
|
|
#! its root, but those of another member's folder within it, and those its
|
|
#! member's tests and programs list in their `srcs`, which are theirs, so a
|
|
#! member's tests sit beside its library in its folder.
|
|
#! @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<vector<string>, failure> {
|
|
val root := path::parse(each.root.span) else (_) {
|
|
return failure { status = 2, message = $"{each.file.span}:{each.line}: `{each.root.span}` is no path." };
|
|
};
|
|
val folder := holder.folder.join(&root);
|
|
if !folder.is_directory {
|
|
return failure { status = 2, message = $"{each.file.span}:{each.line}: `{each.name.span}`'s folder, `{each.root.span}`, isn't there." };
|
|
}
|
|
var found := vector::<string>::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 found, &holder.folder, file);
|
|
}
|
|
}
|
|
|
|
# What the member's tests and programs list is theirs. One whose `srcs` name
|
|
# nothing says so as it's built, not here.
|
|
var theirs := vector::<string>::new();
|
|
for k in 0..holder.targets.length + holder.tests.length {
|
|
val other := &holder.at(k);
|
|
continue if other.kind != models::target_kind::program || other.member != each.member;
|
|
if sources_of(holder, other) is value(&listed) {
|
|
for &file in listed {
|
|
theirs.append(string::copy_of(file));
|
|
}
|
|
}
|
|
}
|
|
var made := vector::<string>::new();
|
|
for &mut file in found {
|
|
continue if holder.member_holding(file.span) != each.member.span;
|
|
var listed := false;
|
|
for &other in theirs {
|
|
listed = listed || same_file(other.span, file.span);
|
|
}
|
|
continue if listed;
|
|
made.append(move(file));
|
|
}
|
|
return made;
|
|
}
|
|
|
|
# Whether two paths relative to the project name one file, whichever of `/` and
|
|
# `\` is between their names.
|
|
def same_file(a: strview, b: strview): bool {
|
|
return false if a.length != b.length;
|
|
for k in 0..a.length {
|
|
val x := a[k] == '\\' ? '/' as u8 : a[k];
|
|
val y := b[k] == '\\' ? '/' as u8 : b[k];
|
|
return false if x != y;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
# Every `.sakura` file within a folder, at any depth, in order.
|
|
def sakura_files(folder: &path): result<vector<path>, glob::search_error> {
|
|
val all := glob::pattern::parse("**/*.sakura") else (_) { return vector::<path>::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<string>, folder: &path, file: &path): void {
|
|
var text := string::from(file.span, nothing {});
|
|
if file.relative_to(folder) is some(&relative) {
|
|
text = string::from(relative.span, nothing {});
|
|
}
|
|
for &prior in into {
|
|
return if prior == text;
|
|
}
|
|
into.append(move(text));
|
|
}
|