Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-11-14 15:31:15 -06:00
commit ab8208d6c6
28 files changed
+6914

No files matched your search

+889
View File
@@ -0,0 +1,889 @@
#! @file reading.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains how a blossomfile is read into a project: from YAML's document,
#! whose values keep their lines and tags, with every key checked against those
#! a mapping takes, a key written twice refused, and each `${NAME}` replaced by
#! the option's value as the text is read.
import std::io;
import std::yaml;
module blossom::models;
#! The systems a target may be built for, as `--target` names them.
pub val platforms: strview[2] = ["linux-x86_64", "windows-x86_64"];
#! Why a blossomfile couldn't be read: where, and what's wrong.
pub struct problem {
pub var file: string; #! The blossomfile, as its path is written.
pub var line: u32; #! The line, counted from 1, or 0 where it's no line's.
pub var message: string; #! What's wrong, in a sentence.
}
#! Says what's wrong, after where.
#! @param this The problem.
#! @returns The sentence, as `blossomfile:4: ...`.
pub def problem::describe(&this): string {
if this.line == 0 {
return $"{this.file.span}: {this.message.span}";
}
return $"{this.file.span}:{this.line}: {this.message.span}";
}
#! Reads a blossomfile.
#! @param text Its text.
#! @param file Its path, absolute.
#! @param overrides Values for its options, as `-o` gives them, which must
#! each name one it declares.
#! @param platform What every target is built for, as `--target` says, or
#! empty, which goes into its output folder's name.
#! @returns The project, or the first problem the file has.
pub def read_project(
text: strview,
file: &path,
overrides: span<setting>,
platform: strview
): result<project, problem> {
var at := reading { file = file.span, at = path::copy_of(file), options = vector::<setting>::new() };
var folder := file.parent() else {
return problem { file = string::from(file.span, nothing {}), message = string::from("A blossomfile is in a folder.", nothing {}) };
};
var made := project { file = path::copy_of(file), folder = move(folder) };
at.read(text, &mut made, overrides, platform);
if at.failed {
return move(at.problem);
}
return move(made);
}
# What reading a blossomfile goes through: its file, as text and as a path, the
# options so far, which `${NAME}` is replaced by, and the first problem found.
struct reading {
var file: strview;
var at: path;
var options: vector<setting>;
var failed: bool;
var problem: problem;
}
# Notes the first problem, at a line of the file being read.
def reading::fail(&mut this, line: u32, message: string): void {
this.fail_in(this.file, line, move(message));
}
# Notes the first problem, at a line of a file: the project's, or a member's.
def reading::fail_in(&mut this, file: strview, line: u32, message: string): void {
return if this.failed;
this.failed = true;
this.problem = problem { file = string::from(file, nothing {}), line = line, message = move(message) };
}
# Reads the whole file into a project.
def reading::read(&mut this, text: strview, made: &mut project, overrides: span<setting>, platform: strview): void {
val doc := yaml::parse(text) else (e) {
this.fail(e.line, string::from(yaml::describe(e.kind), nothing {}));
return;
};
val root := doc.root;
if root.is_null {
this.fail(0, string::from("The blossomfile is empty: it needs `name`, `gpid`, and `semv`.", nothing {}));
return;
}
if root.kind == yaml::kind::mapping && root.get("member") is some(of) {
this.fail(of.place.line, string::from("This blossomfile is a member's, of the project whose blossomfile lists its folder in `members`: blossom reads it with that project, run there or here.", nothing {}));
return;
}
return if !this.keys(&root, ["name", "gpid", "semv", "options", "members", "requires", "targets", "tests", "exports", "install", "package"], "the blossomfile");
# Who it is comes first, then its options, which every text after may use.
made.name = this.needed(&root, "name");
made.gpid = this.needed(&root, "gpid");
made.semv = this.needed(&root, "semv");
return if this.failed;
if !is_version(made.semv.span) {
this.fail(root.get("semv").some.place.line, $"`{made.semv.span}` is not a version, as `1.2.0` or `1.2.0-alpha`.");
return;
}
if root.get("options") is some(options) {
return if !this.keys(&options, span::<strview>::blank(), "options");
for each in options.members() {
if each.name == "bin" {
this.fail(each.key.place.line, string::from("`bin` is blossom's: it names the folder of the programs built.", nothing {}));
return;
}
var value := this.text(&each.value, "An option's value");
for &given in overrides {
if given.name.span == each.name {
value = this.expanded(given.value.span, 0);
}
}
this.options.append(setting { name = string::from(each.name, nothing {}), value = move(value) });
}
}
for &given in overrides {
var declared := false;
for &each in this.options {
declared = declared || each.name == given.name;
}
if !declared {
this.fail(0, $"`{given.name.span}`, given a value, names no option of {made.identity.span}.");
return;
}
}
for &each in this.options {
made.options.append(setting { name = string::copy_of(&each.name), value = string::copy_of(&each.value) });
}
made.key = output_key(made.identity.span, made.options.span, platform);
val bin := made.output.join("bin");
this.options.append(setting { name = string::from("bin", nothing {}), value = string::from(bin.span, nothing {}) });
return if this.failed;
if root.get("requires") is some(requires) {
for each in this.list(&requires, "`requires`") {
made.requires.append(this.requirement(&each));
return if this.failed;
}
}
if root.get("targets") is some(targets) {
for each in this.list(&targets, "`targets`") {
made.targets.append(this.target(&each, false));
return if this.failed;
}
}
if root.get("tests") is some(tests) {
for each in this.list(&tests, "`tests`") {
made.tests.append(this.target(&each, true));
return if this.failed;
}
}
if root.get("members") is some(members) {
this.members(&members, made);
return if this.failed;
}
if root.get("exports") is some(exports) {
made.exports = this.texts(&exports, "`exports`");
}
if root.get("install") is some(install) {
for each in this.list(&install, "`install`") {
made.install.append(this.step(&each));
return if this.failed;
}
}
if root.get("package") is some(package) {
return if !this.keys(&package, ["lic", "desc", "auth", "url"], "`package`");
made.package = package_info {
lic = this.optional(&package, "lic"),
desc = this.optional(&package, "desc"),
auth = this.optional(&package, "auth"),
url = this.optional(&package, "url")
};
}
return if this.failed;
this.validate(made, &root);
}
# Reads the project's members: each folder `members` lists, within the project,
# holding a blossomfile that says it's the project's member, whose targets,
# tests, and requirements are the project's, their paths taken from the
# member's folder.
def reading::members(&mut this, list: &yaml::value, made: &mut project): void {
for each in this.list(list, "`members`") {
val written := this.text(&each, "A member");
return if this.failed;
val folder := member_folder(written.span);
if folder.length == 0 || !is_within(folder.span) {
this.fail(each.place.line, $"`{written.span}` is outside the project: a member is a folder within it, as `engine`.");
return;
}
for &prior in made.members {
if prior.folder == folder {
this.fail(each.place.line, $"`{folder.span}` is listed twice in `members`.");
return;
}
}
val named := path::parse(folder.span) else (_) {
this.fail(each.place.line, $"`{written.span}` is no folder.");
return;
};
val file := made.folder.join(&named).join("blossomfile");
val text := io::read_file(&file) else (why) {
this.fail(each.place.line, $"The member `{folder.span}` holds no blossomfile to read: {why}.");
return;
};
made.members.append(member { folder = string::copy_of(&folder), file = path::copy_of(&file) });
var inner := reading { file = file.span, at = path::copy_of(&file), options = vector::<setting>::new() };
for &option in this.options {
inner.options.append(setting { name = string::copy_of(&option.name), value = string::copy_of(&option.value) });
}
inner.member(text.span, made, folder.span);
if inner.failed {
this.failed = true;
this.problem = move(inner.problem);
return;
}
}
}
# Reads a member's blossomfile into its project: `member`, naming the project,
# then its requirements, its targets, and its tests, which take the project's
# options, each path they write taken from the member's folder.
def reading::member(&mut this, text: strview, made: &mut project, folder: strview): void {
val doc := yaml::parse(text) else (e) {
this.fail(e.line, string::from(yaml::describe(e.kind), nothing {}));
return;
};
val root := doc.root;
if root.is_null {
this.fail(0, $"A member's blossomfile is empty: it needs `member: {made.name.span}`, naming the project it's of.");
return;
}
return if !this.keys(&root, ["member", "requires", "targets", "tests"], "a member's blossomfile");
val of := this.needed(&root, "member");
return if this.failed;
if of != made.name {
this.fail(root.get("member").some.place.line, $"This blossomfile says it's a member of `{of.span}`, but `{made.name.span}` lists it in its `members`.");
return;
}
if root.get("requires") is some(requires) {
for each in this.list(&requires, "`requires`") {
var required := this.requirement(&each);
return if this.failed;
if required.local {
required.remote = placed(folder, required.remote.span);
}
made.requires.append(move(required));
}
}
for key in ["targets", "tests"] {
if root.get(key) is some(listed) {
for each in this.list(&listed, $"`{key}`".span) {
var read := this.target(&each, key == "tests");
return if this.failed;
this.place_target(&mut read, folder);
return if this.failed;
if key == "tests" {
made.tests.append(move(read));
} else {
made.targets.append(move(read));
}
}
}
}
}
# Takes a member's target's paths from the member's folder: a library's root,
# which stays within the member, its files, and its icons.
def reading::place_target(&mut this, read: &mut target, folder: strview): void {
read.member = string::from(folder, nothing {});
if read.kind == target_kind::library {
if !is_within(read.root.span) {
this.fail(read.line, $"`{read.root.span}` is outside the member: a library's root is a folder within its member's.");
return;
}
read.root = placed(folder, read.root.span);
}
for &mut each in read.srcs {
each = placed(folder, each.span);
}
for &mut each in read.icons {
each = placed(folder, each.span);
}
}
# A member's folder as `members` writes it, with `/` between its names, and
# neither `./` before it nor `/` after it.
def member_folder(text: strview): string {
var made := string::new(nothing {});
for k in 0..text.length {
made.push(text[k] == '\\' ? '/' as u8 : text[k]);
}
while made.span.starts_with("./") {
made = string::from(made.span.substr(2, made.length - 2), nothing {});
}
while made.length != 0 && made[made.length - 1] == '/' {
made = string::from(made.span.substr(0, made.length - 1), nothing {});
}
return made;
}
# A path a member writes, relative to its folder, as the project reads it,
# relative to its own: the member's folder before it, unless it's absolute.
def placed(folder: strview, text: strview): string {
if text.length != 0 && (text[0] == '/' || text[0] == '\\' || (text.length > 1 && text[1] == ':')) {
return string::from(text, nothing {});
}
var rest := text;
while rest.starts_with("./") || rest.starts_with(".\\") {
rest = rest.substr(2, rest.length - 2);
}
if rest.length == 0 || rest == "." {
return string::from(folder, nothing {});
}
return $"{folder}/{rest}";
}
#! The project a blossomfile says it's a member of, by its `member`, or none
#! where it says none, or isn't YAML to say.
#! @param text The blossomfile's text.
#! @returns The project's name.
pub def member_of(text: strview): maybe<string> {
val doc := yaml::parse(text) else (_) { return nothing {}; };
val root := doc.root;
return nothing {} if root.kind != yaml::kind::mapping;
val of := root.get("member") else { return nothing {}; };
val name := of.as_scalar() else (_) { return nothing {}; };
return string::from(name, nothing {});
}
#! Whether a blossomfile lists a folder among its `members`.
#! @param text The blossomfile's text.
#! @param folder The folder, relative to the blossomfile's, with `/` between
#! its names.
#! @returns Whether it does.
pub def lists_member(text: strview, folder: strview): bool {
val doc := yaml::parse(text) else (_) { return false; };
val root := doc.root;
return false if root.kind != yaml::kind::mapping;
val members := root.get("members") else { return false; };
return false if members.kind != yaml::kind::sequence;
for each in members.elements() {
val written := each.as_scalar() else (_) { continue; };
return true if member_folder(written).span == folder;
}
return false;
}
# Reads a requirement: its text alone, as `REMOTE@REF` or a folder, or the block
# of `remote`, `branch`, and `options`.
def reading::requirement(&mut this, value: &yaml::value): requirement {
val line := value.place.line;
if value.kind != yaml::kind::mapping {
val text := this.text(value, "A requirement");
var made := requirement { line = line, file = path::copy_of(&this.at) };
if is_folder_text(text.span) {
made.remote = string::copy_of(&text);
made.local = true;
return made;
}
val split := ref_at(text.span);
made.remote = string::from(text.span.substr(0, split), nothing {});
made.branch = split < text.length
? string::from(text.span.substr(split + 1, text.length - split - 1), nothing {})
: string::from("main", nothing {});
if made.remote.length == 0 || made.branch.length == 0 {
this.fail(line, $"`{text.span}` is no requirement: write `REMOTE@REF` or a folder, as `../shapes`.");
}
return made;
}
var made := requirement { line = line, file = path::copy_of(&this.at) };
return made if !this.keys(value, ["remote", "branch", "options"], "a requirement");
made.remote = this.needed(value, "remote");
made.local = is_folder_text(made.remote.span);
made.branch = string::from("main", nothing {});
if value.get("branch") is some(branch) {
made.branch = this.text(&branch, "`branch`");
if made.local {
this.fail(branch.place.line, string::from("A folder takes no `branch`.", nothing {}));
}
}
if value.get("options") is some(options) {
return made if !this.keys(&options, span::<strview>::blank(), "a requirement's options");
for each in options.members() {
made.options.append(setting { name = string::from(each.name, nothing {}), value = this.text(&each.value, "An option's value") });
}
}
return made;
}
# Reads a target, or a test, which takes the keys of tests too.
def reading::target(&mut this, value: &yaml::value, test: bool): target {
var made := target { line = value.place.line, file = path::copy_of(&this.at) };
if test {
return made if !this.keys(value, ["name", "type", "level", "petal", "target", "deps", "srcs", "opts", "runs", "checks", "args"], "a test");
} else {
return made if !this.keys(value, ["name", "type", "level", "petal", "target", "deps", "srcs", "opts", "root", "title", "icon", "windowed"], "a target");
}
made.name = this.needed(value, "name");
val kind := this.needed(value, "type");
return made if this.failed;
if kind == "program" {
made.kind = target_kind::program;
} else if kind == "library" && !test {
made.kind = target_kind::library;
} else {
val line := value.get("type").some.place.line;
this.fail(line, test ? $"A test's `type` is `program`, not `{kind.span}`." : $"`type` is `program` or `library`, not `{kind.span}`.");
return made;
}
if made.kind == target_kind::library {
for key in ["level", "petal", "target", "srcs", "opts", "title", "icon", "windowed"] {
if value.get(key) is some(given) {
this.fail(given.place.line, $"A library takes no `{key}`: it's read by the programs that import it.");
return made;
}
}
if !is_library_name(made.name.span) {
this.fail(made.line, $"`{made.name.span}` names no module: a library's name is its module's, with `.` for `::`, as `hello.geometry`.");
return made;
}
made.root = $"./{made.name.span}";
if value.get("root") is some(root) {
made.root = this.text(&root, "`root`");
}
} else {
if value.get("root") is some(root) {
this.fail(root.place.line, string::from("Only a library takes a `root`.", nothing {}));
return made;
}
if !is_file_name(made.name.span) {
this.fail(made.line, $"`{made.name.span}` can't name a program's file: write letters, digits, `.`, `_`, or `-`.");
return made;
}
}
if value.get("level") is some(level) {
val text := this.text(&level, "`level`");
if text != "0" && text != "1" && text != "2" {
this.fail(level.place.line, $"`level` is 0, 1, or 2, not `{text.span}`.");
return made;
}
made.level = (text[0] - '0') as u32;
}
if value.get("petal") is some(petal) {
made.petal = this.boolean(&petal, "`petal`");
}
if value.get("target") is some(system) {
made.system = this.text(&system, "`target`");
if !is_platform(made.system.span) {
this.fail(system.place.line, $"`{made.system.span}` is no target: write `linux-x86_64` or `windows-x86_64`.");
return made;
}
}
if value.get("deps") is some(deps) {
made.deps = this.texts(&deps, "`deps`");
}
if value.get("srcs") is some(srcs) {
made.srcs = this.texts(&srcs, "`srcs`");
}
if value.get("opts") is some(opts) {
made.opts = this.texts(&opts, "`opts`");
}
if value.get("args") is some(args) {
made.args = this.texts(&args, "`args`");
}
# How a program is shown: its title, its icon's files, and whether it's
# windowed, each of which may be an option's value, as an editor building a
# game gives them with `-o`: an empty title or icon is none.
if value.get("title") is some(title) {
made.title = this.text(&title, "`title`");
}
if value.get("icon") is some(icon) {
made.icons = this.icon_files(&icon);
return made if this.failed;
}
if value.get("windowed") is some(windowed) {
made.windowed = this.boolean(&windowed, "`windowed`");
}
made.checks = true;
if value.get("runs") is some(runs) {
val text := this.text(&runs, "`runs`");
if text == "native" {
made.runs = runner::native;
made.checks = false;
} else if text != "machine" {
this.fail(runs.place.line, $"`runs` is `machine` or `native`, not `{text.span}`.");
return made;
}
}
if value.get("checks") is some(checks) {
made.checks = this.boolean(&checks, "`checks`");
}
# A program needs files, which a test finds in `tests/` unless it says.
if made.kind == target_kind::program && made.srcs.length == 0 {
if !test {
this.fail(made.line, $"`{made.name.span}` needs `srcs`: its files, folders, or globs.");
return made;
}
made.srcs.append(string::from("tests/**/*.sakura", nothing {}));
}
return made;
}
# Reads a step of installing: a tag saying what it does, and its text.
def reading::step(&mut this, value: &yaml::value): step {
val line := value.place.line;
var made := step { line = line };
val tag := value.tag;
if tag is none {
this.fail(line, string::from("A step of `install` says what it does with a tag, as `!include ./share`.", nothing {}));
return made;
}
if tag.some != "!include" {
this.fail(line, $"`{tag.some}` is no step blossom knows: it knows `!include`.");
return made;
}
made.tag = string::from(tag.some, nothing {});
val text := this.text(value, "A step");
val split := text.span.find(":");
if split is some(at) {
made.source = string::from(text.span.substr(0, at), nothing {});
made.dest = string::from(text.span.substr(at + 1, text.length - at - 1), nothing {});
} else {
made.source = string::copy_of(&text);
}
if made.source.length == 0 || !is_within(made.source.span) {
this.fail(line, $"`{made.source.span}` is outside the project: a source is a path within it, as `./share`.");
} else if text.span.find(":") is some && (made.dest.length == 0 || !is_within(made.dest.span)) {
this.fail(line, $"`{made.dest.span}` is outside the prefix: a destination is a path within it, as `share/doc`.");
}
return made;
}
# Checks what reading each part couldn't: names given once, exports that are
# libraries, and libraries' roots within the project.
def reading::validate(&mut this, made: &mut project, root: &yaml::value): void {
val count := made.targets.length + made.tests.length;
for k in 0..count {
val each := &made.at(k);
for j in 0..k {
if made.at(j).name == each.name {
val first := &made.at(j);
val earlier := first.file == each.file ? $"line {first.line}" : $"{first.file.span}:{first.line}";
this.fail_in(each.file.span, each.line, $"`{each.name.span}` names two targets: this, and that of {earlier.span}.");
return;
}
}
if each.kind == target_kind::library && !is_within(each.root.span) {
this.fail_in(each.file.span, each.line, $"`{each.root.span}` is outside the project: a library's root is a folder within it.");
return;
}
}
for &name in made.exports {
val found := made.find(name.span);
if found is none || found.some >= made.targets.length || made.targets[found.some].kind != target_kind::library {
this.fail(root.get("exports").some.place.line, $"`{name.span}` is exported, but it's no library of {made.identity.span}.");
return;
}
}
}
# Checks a mapping's keys: each one of those allowed, unless none are named,
# where any is, and none written twice, which YAML would keep only one of.
def reading::keys(&mut this, map: &yaml::value, allowed: span<strview>, what: strview): bool {
if map.kind != yaml::kind::mapping {
this.fail(map.place.line, $"{what} is a mapping of keys and values.");
return false;
}
var seen := vector::<strview>::new();
for each in map.members() {
val line := each.key.place.line;
var known := allowed.length == 0;
for &name in allowed {
known = known || name == each.name;
}
if !known {
this.fail(line, $"`{each.name}` is no key of {what}.");
return false;
}
for &prior in seen {
if prior == each.name {
this.fail(line, $"`{each.name}` is written twice in {what}.");
return false;
}
}
seen.append(each.name);
}
return true;
}
# The elements of a sequence, or none where the value is another kind, which
# is noted.
def reading::list(&mut this, value: &yaml::value, what: strview): yaml::value::element_cursor {
if value.kind != yaml::kind::sequence && !value.is_null {
this.fail(value.place.line, $"{what} is a list, of lines starting with `-`.");
}
return value.elements();
}
# A scalar's text, with options replaced.
def reading::text(&mut this, value: &yaml::value, what: strview): string {
val read := value.as_scalar() else (_) {
this.fail(value.place.line, $"{what} is a scalar, not a list or a mapping.");
return string::new(nothing {});
};
return this.expanded(read, value.place.line);
}
# The texts of a list of scalars.
def reading::texts(&mut this, value: &yaml::value, what: strview): vector<string> {
var made := vector::<string>::new();
for each in this.list(value, what) {
made.append(this.text(&each, $"Each of {what}".span));
}
return made;
}
# The files an `icon` names: a file, or a list of them, each text naming any
# number with `,` between them, as an option's value names several, and none
# where it's empty. Each is a PNG, an SVG, or an icon file, by its ending.
def reading::icon_files(&mut this, value: &yaml::value): vector<string> {
var texts := vector::<string>::new();
if value.kind == yaml::kind::sequence {
texts = this.texts(value, "`icon`");
} else if !value.is_null {
texts.append(this.text(value, "`icon`"));
}
var made := vector::<string>::new();
for &text in texts {
for part in text.span.split(',') {
val file := part.trim();
continue if file.length == 0;
if !is_icon_file(file) {
this.fail(value.place.line, $"`{file}` is no icon blossom knows: write a `.png`, an `.svg`, or an `.ico`.");
return made;
}
made.append(string::from(file, nothing {}));
}
}
return made;
}
# A key's text, which must be written.
def reading::needed(&mut this, map: &yaml::value, key: strview): string {
if map.get(key) is some(value) {
var made := this.text(&value, $"`{key}`".span);
if made.length == 0 {
this.fail(value.place.line, $"`{key}` is empty.");
}
return made;
}
this.fail(map.place.line, $"`{key}` is needed.");
return string::new(nothing {});
}
# A key's text, or empty where it isn't written.
def reading::optional(&mut this, map: &yaml::value, key: strview): string {
if map.get(key) is some(value) {
return this.text(&value, $"`{key}`".span);
}
return string::new(nothing {});
}
# A scalar's truth, written `true` or `false`.
def reading::boolean(&mut this, value: &yaml::value, what: strview): bool {
val text := this.text(value, what);
if text != "true" && text != "false" {
this.fail(value.place.line, $"{what} is `true` or `false`, not `{text.span}`.");
}
return text == "true";
}
# Text with each `${NAME}` replaced by the option's value, noting a name that's
# no option's.
def reading::expanded(&mut this, text: strview, line: u32): string {
var made := string::new(nothing {});
var at := 0u;
while at < text.length {
if text[at] == '$' && at + 1 < text.length && text[at + 1] == '{' {
val rest := text.substr(at + 2, text.length - at - 2);
if rest.find("}") is some(length) {
val name := rest.substr(0, length);
var found := false;
for &each in this.options {
if !found && each.name.span == name {
made.append(each.value.span);
found = true;
}
}
if !found {
var message := string::from("`$", nothing {});
message.push('{');
message.append(name);
message.append("}` names no option declared before it.");
this.fail(line, move(message));
return made;
}
at += length + 3;
continue;
}
}
made.push(text[at]);
at++;
}
return made;
}
#! The name of a project's output folder: the 64-bit hash of its identity, its
#! options' values, and what its targets are built for, in hex.
#! @param identity As `malunal/hello@1.0.0`.
#! @param options Its options' values.
#! @param platform What `--target` says, or empty.
#! @returns Sixteen hex digits.
pub def output_key(identity: strview, options: span<setting>, platform: strview): string {
var text := string::from(identity, nothing {});
# In order by name, so writing the options in another order changes nothing.
var order := vector::<u64>::new();
for k in 0..options.length {
var at := order.length;
while at > 0 && options[order[at - 1]].name.span.compare(options[k].name.span) > 0 {
at--;
}
order.insert_at(at, k);
}
for k in order {
text.append($"\n{options[k].name.span}={options[k].value.span}".span);
}
text.append($"\ntarget={platform}".span);
return hex(hash(&text));
}
#! A number as sixteen hex digits.
#! @param value The number.
#! @returns Its digits, the highest first.
pub def hex(value: u64): string {
val digits: strview = "0123456789abcdef";
var made := string::new(nothing {});
var shift := 64u;
while shift > 0 {
shift -= 4;
made.push(digits[(value >> shift) & 15]);
}
return made;
}
#! Whether text is a version: three numbers, as `1.2.0`, then a `-` and what
#! comes before release, as `-alpha`, and a `+` and what it was built from, as
#! semantic versioning has them.
#! @param text The text.
#! @returns Whether it is one.
pub def is_version(text: strview): bool {
var at := 0u;
for part in 0..3 {
if part > 0 {
return false if at >= text.length || text[at] != '.';
at++;
}
val start := at;
while at < text.length && text[at].is_digit {
at++;
}
return false if at == start;
}
if at < text.length && (text[at] == '-' || text[at] == '+') {
at++;
return false if at == text.length;
while at < text.length {
val byte := text[at];
return false if !(byte.is_alnum || byte == '.' || byte == '-' || byte == '+');
at++;
}
}
return at == text.length;
}
#! Whether a requirement's text is a folder rather than a remote: a path
#! starting with `.` or a root.
#! @param text The text.
#! @returns Whether it is.
pub def is_folder_text(text: strview): bool {
return text.length != 0 && (text[0] == '.' || text[0] == '/' || text[0] == '\\' || (text.length > 1 && text[1] == ':' && text[0].is_alpha && text.find("://") is none));
}
# Where the `@` before a remote's ref is: the first after the remote's last `/`
# or `:`, or the text's length where there is none.
def ref_at(text: strview): u64 {
var from := 0u;
for k in 0..text.length {
if text[k] == '/' || text[k] == ':' {
from = k + 1;
}
}
for k in from..text.length {
return k if text[k] == '@';
}
return text.length;
}
# Whether a name is a module's, with `.` for `::`, as `hello.geometry`, and not
# one of the toolchain's own.
def is_library_name(name: strview): bool {
return false if name == "std" || name == "core" || name == "system" || name == "runtime";
var start := 0u;
for k in 0..=name.length {
if k == name.length || name[k] == '.' {
return false if k == start || name[start].is_digit;
start = k + 1;
continue;
}
return false if !name[k].is_ident;
}
return true;
}
# Whether a name can be a program's file.
def is_file_name(name: strview): bool {
return false if name.length == 0 || name == "." || name == "..";
for k in 0..name.length {
val byte := name[k];
return false if !(byte.is_alnum || byte == '.' || byte == '_' || byte == '-');
}
return true;
}
#! Whether `--target`, or a target's `target`, names a system blossom builds for.
#! @param text The name.
#! @returns Whether it does.
pub def is_platform(text: strview): bool {
for &each in platforms {
return true if each == text;
}
return false;
}
#! Whether a file is one an `icon` may name, by its ending, in any case: a PNG,
#! an SVG, or an icon file.
#! @param file The file.
#! @returns Whether it is.
pub def is_icon_file(file: strview): bool {
return ends_with_any_case(file, ".png") || ends_with_any_case(file, ".svg") || ends_with_any_case(file, ".ico");
}
#! Whether text ends with an ending, written in lower case, in any case.
#! @param text The text.
#! @param ending The ending.
#! @returns Whether it does.
pub def ends_with_any_case(text: strview, ending: strview): bool {
return false if text.length < ending.length;
val from := text.length - ending.length;
for k in 0..ending.length {
var byte := text[from + k];
if byte >= 'A' as u8 && byte <= 'Z' as u8 {
byte = byte + 32;
}
return false if byte != ending[k];
}
return true;
}
# Whether a path stays within the folder it's taken from: relative, with no `..`.
def is_within(text: strview): bool {
return false if text.length == 0 || text[0] == '/' || text[0] == '\\' || (text.length > 1 && text[1] == ':');
for part in text.split('/') {
return false if part == "..";
for inner in part.split('\\') {
return false if inner == "..";
}
}
return true;
}