Initial Commit
This commit is contained in:
24 files changed
+4796
No files matched your search
@@ -0,0 +1,186 @@
|
||||
#! @file config.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains where blossom keeps what it fetches and what it's told, and how it
|
||||
#! finds the `sakura` it runs: the one the config names, else the one beside
|
||||
#! blossom, else the first on `PATH`.
|
||||
import std;
|
||||
import std::io;
|
||||
import std::process;
|
||||
import std::yaml;
|
||||
import system;
|
||||
import blossom::models;
|
||||
|
||||
module blossom::management;
|
||||
|
||||
#! What blossom's config says, from `config` in its folder, where there's one.
|
||||
pub struct config {
|
||||
pub var sakura: string; #! The `sakura` to run, or empty to look for one.
|
||||
pub var std: string; #! The standard library `sakura` is given, or empty for the one it finds.
|
||||
}
|
||||
|
||||
#! The folder blossom keeps its config and its store of requirements in:
|
||||
#! `blossom` in `XDG_CONFIG_HOME`, else in `~/.config`, and on Windows in
|
||||
#! `APPDATA`.
|
||||
#! @returns The folder, or `nothing` where the environment names no home.
|
||||
pub def config_folder(): maybe<path> {
|
||||
when os == windows {
|
||||
if process::variable("APPDATA") is some(data) {
|
||||
if data.length != 0 {
|
||||
var at := path::parse(data) else (_) { return nothing {}; };
|
||||
return at.join("blossom");
|
||||
}
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
if process::variable("XDG_CONFIG_HOME") is some(named) {
|
||||
if named.length != 0 {
|
||||
var at := path::parse(named) else (_) { return nothing {}; };
|
||||
return at.join("blossom");
|
||||
}
|
||||
}
|
||||
val home := process::variable("HOME") else { return nothing {}; };
|
||||
var at := path::parse(home) else (_) { return nothing {}; };
|
||||
return at.join(".config").join("blossom");
|
||||
}
|
||||
|
||||
#! Reads blossom's config: `sakura` and `std`, each a path, both optional.
|
||||
#! @returns The config, which says nothing where there's no file, or why the
|
||||
#! file can't be read.
|
||||
pub def read_config(): result<config, models::problem> {
|
||||
var made := config {};
|
||||
val folder := config_folder() else { return made; };
|
||||
val file := folder.join("config");
|
||||
return made if !io::exists(&file);
|
||||
val text := io::read_file(&file) else (why) {
|
||||
return models::problem { file = string::from(file.view(), nothing {}), message = $"It can't be read: {why}." };
|
||||
};
|
||||
val doc := yaml::parse(text.view()) else (e) {
|
||||
return models::problem { file = string::from(file.view(), nothing {}), line = e.line, message = string::from(yaml::describe(e.kind), nothing {}) };
|
||||
};
|
||||
val root := doc.root;
|
||||
return made if root.is_null;
|
||||
if root.kind != yaml::kind::mapping {
|
||||
return models::problem { file = string::from(file.view(), nothing {}), line = 1, message = string::from("The config is a mapping of `sakura` and `std`.", nothing {}) };
|
||||
}
|
||||
for each in root.members() {
|
||||
val text := each.value.as_scalar() else (_) {
|
||||
return models::problem { file = string::from(file.view(), nothing {}), line = each.key.place.line, message = $"`{each.name}` is a path." };
|
||||
};
|
||||
if each.name == "sakura" {
|
||||
made.sakura = string::from(text, nothing {});
|
||||
} else if each.name == "std" {
|
||||
made.std = string::from(text, nothing {});
|
||||
} else {
|
||||
return models::problem { file = string::from(file.view(), nothing {}), line = each.key.place.line, message = $"`{each.name}` is no key of the config: it takes `sakura` and `std`." };
|
||||
}
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Finds the `sakura` blossom runs: the one the config names, else `sakura`
|
||||
#! beside blossom's executable, else the first `sakura` on `PATH`.
|
||||
#! @param given The config.
|
||||
#! @returns Its path, absolute, or `nothing` where there's none.
|
||||
pub def find_sakura(given: &config): maybe<string> {
|
||||
if given.sakura.length != 0 {
|
||||
val named := path::parse(given.sakura.view()) else (_) { return nothing {}; };
|
||||
val at := io::absolute(&named) else (_) { return nothing {}; };
|
||||
return io::exists(&at) ? string::from(at.view(), nothing {}) : nothing {};
|
||||
}
|
||||
|
||||
val own := executable_path();
|
||||
if own.length != 0 {
|
||||
if path::parse(own.view()) is value(&mine) {
|
||||
if mine.parent() is some(&beside) {
|
||||
val next := beside.join(program_file("sakura").view());
|
||||
if is_file(&next) {
|
||||
return string::from(next.view(), nothing {});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val searched := process::variable("PATH") else { return nothing {}; };
|
||||
for each in searched.split(path_separator) {
|
||||
continue if each.length == 0;
|
||||
val folder := path::parse(each) else (_) { continue; };
|
||||
val next := folder.join(program_file("sakura").view());
|
||||
if is_file(&next) {
|
||||
val at := io::absolute(&next) else (_) { continue; };
|
||||
return string::from(at.view(), nothing {});
|
||||
}
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! A program's file name on the system blossom runs on: as it is, or with
|
||||
#! `.exe` on Windows.
|
||||
#! @param name The program's name.
|
||||
#! @returns The file's name.
|
||||
pub def program_file(name: strview): string {
|
||||
when os == windows {
|
||||
return $"{name}.exe";
|
||||
}
|
||||
return string::from(name, nothing {});
|
||||
}
|
||||
|
||||
#! Whether a regular file is at a path, following links.
|
||||
#! @param at The path.
|
||||
#! @returns Whether one is.
|
||||
pub def is_file(at: &path): bool {
|
||||
return match io::info(at) {
|
||||
value(&found) => found.kind == file_kind::regular,
|
||||
error(_) => false,
|
||||
};
|
||||
}
|
||||
|
||||
when os == windows {
|
||||
# What separates the folders of `PATH`.
|
||||
val path_separator: u8 = ';';
|
||||
} else {
|
||||
# What separates the folders of `PATH`.
|
||||
val path_separator: u8 = ':';
|
||||
}
|
||||
|
||||
when os == linux {
|
||||
# The call reading where a link leads, as Linux numbers it on x86-64.
|
||||
val call_readlink: i64 = 89;
|
||||
|
||||
#! The path of blossom's own executable.
|
||||
#! @returns The path, or empty text where the system doesn't say.
|
||||
pub def executable_path(): string {
|
||||
val link: strview = "/proc/self/exe";
|
||||
var name: u8[16];
|
||||
for i in 0..link.length {
|
||||
name[i] = link[i];
|
||||
}
|
||||
name[link.length] = 0;
|
||||
|
||||
var text: u8[4096];
|
||||
val from := (&mut name) as *mut u8;
|
||||
val into := (&mut text) as *mut u8;
|
||||
val length := system::linux::syscall(call_readlink, from as u64, into as u64, 4095, 0, 0, 0);
|
||||
return string::new(nothing {}) if length <= 0;
|
||||
return string::from(span::<u8>::over(into as *u8, length as u64), nothing {});
|
||||
}
|
||||
} else when os == windows {
|
||||
# The code page of UTF-8, as Windows numbers it.
|
||||
val utf8_page: u32 = 65001;
|
||||
|
||||
#! The path of blossom's own executable, in UTF-8.
|
||||
#! @returns The path, or empty text where the system doesn't say.
|
||||
pub def executable_path(): string {
|
||||
var wide: u16[4096];
|
||||
var text: u8[16384];
|
||||
val units := (&mut wide) as *mut u16;
|
||||
val into := (&mut text) as *mut u8;
|
||||
val count := system::windows::GetModuleFileNameW(0, units, 4096);
|
||||
return string::new(nothing {}) if count == 0 || count >= 4096;
|
||||
val length := system::windows::WideCharToMultiByte(utf8_page, 0, units as *u16, count as i32, into, 16384, null, null);
|
||||
return string::new(nothing {}) if length <= 0;
|
||||
return string::from(span::<u8>::over(into as *u8, length as u64), nothing {});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
#! @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<vector<string>, failure> {
|
||||
var made := vector::<string>::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::<path>::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<vector<string>, 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::<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 made, &holder.folder, file);
|
||||
}
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
# 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.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));
|
||||
}
|
||||
@@ -0,0 +1,426 @@
|
||||
#! @file workspace.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the workspace: the project a command is given, and every project it
|
||||
#! requires, read depth first, each once, from a folder beside it or from the
|
||||
#! store, where a remote is cloned the first time it's required; and every
|
||||
#! library they hold, by name, which their programs read through `--lib`.
|
||||
import std;
|
||||
import std::io;
|
||||
import std::process;
|
||||
import system;
|
||||
import blossom::models;
|
||||
|
||||
module blossom::management;
|
||||
|
||||
#! Why a command can't go on, and the status it ends with: 1 where something
|
||||
#! failed, as a clone, and 2 where a blossomfile or the command is wrong.
|
||||
pub struct failure {
|
||||
pub var status: i32;
|
||||
pub var message: string;
|
||||
}
|
||||
|
||||
#! A library some project of the workspace holds.
|
||||
pub struct library {
|
||||
pub var name: string; #! Its name, as `hello.geometry`.
|
||||
pub var project: u64; #! The project holding it.
|
||||
pub var target: u64; #! The target it is, among the project's.
|
||||
}
|
||||
|
||||
#! The projects a command reads: the one given first, then those it requires.
|
||||
pub struct workspace {
|
||||
pub var projects: vector<models::project>;
|
||||
pub var libraries: vector<library>;
|
||||
pub var platform: string; #! What `--target` says, or empty.
|
||||
pub var verbose: bool; #! Whether each command run is printed.
|
||||
}
|
||||
|
||||
#! Reads a project and everything it requires, then indexes their libraries.
|
||||
#! @param folder The project's folder.
|
||||
#! @param overrides Values for its options, as `-o` gives them.
|
||||
#! @param platform What `--target` says, or empty.
|
||||
#! @param verbose Whether each command run is printed.
|
||||
#! @returns The workspace, whose first project is the one given, or why it
|
||||
#! can't be read.
|
||||
pub def open_workspace(
|
||||
folder: &path,
|
||||
overrides: span<models::setting>,
|
||||
platform: strview,
|
||||
verbose: bool
|
||||
): result<workspace, failure> {
|
||||
var made := workspace {
|
||||
projects = vector::<models::project>::new(),
|
||||
libraries = vector::<library>::new(),
|
||||
platform = string::from(platform, nothing {}),
|
||||
verbose = verbose
|
||||
};
|
||||
var loaded := made.load(folder, overrides, strview::empty(), true);
|
||||
if loaded is error(&mut why) {
|
||||
return move(why);
|
||||
}
|
||||
var indexed := made.index();
|
||||
if indexed is error(&mut why) {
|
||||
return move(why);
|
||||
}
|
||||
return move(made);
|
||||
}
|
||||
|
||||
#! The project the command was given.
|
||||
#! @param this The workspace.
|
||||
#! @returns It.
|
||||
pub def workspace::root(&this): &models::project {
|
||||
return &this.projects[0];
|
||||
}
|
||||
|
||||
# Reads the project in a folder, then each it requires, giving where it is
|
||||
# among the projects. One read before is given again, where its options are
|
||||
# the same. A project another requires reads what it requires only where its
|
||||
# libraries read what it doesn't hold itself, so what only its tests or
|
||||
# programs need is never fetched for those requiring it.
|
||||
def workspace::load(&mut this, folder: &path, overrides: span<models::setting>, by: strview, root: bool): result<u64, failure> {
|
||||
val absolute := io::absolute(folder) else (why) {
|
||||
return failure { status = 2, message = $"`{folder.view()}` can't be found: {why}." };
|
||||
};
|
||||
val file := absolute.join("blossomfile");
|
||||
val text := io::read_file(&file) else (why) {
|
||||
val whose := by.length != 0 ? $", which {by} requires," : string::new(nothing {});
|
||||
return failure { status = 2, message = $"`{absolute.view()}`{whose.view()} holds no blossomfile to read: {why}." };
|
||||
};
|
||||
var read := models::read_project(text.view(), &file, overrides, this.platform.view());
|
||||
if read is error(&problem) {
|
||||
return failure { status = 2, message = problem.describe() };
|
||||
}
|
||||
if read is not value(&mut made) {
|
||||
return failure { status = 2, message = string::from("The blossomfile couldn't be read.", nothing {}) };
|
||||
}
|
||||
|
||||
# A project is read once, and given one set of values for its options, since
|
||||
# each set builds its own outputs.
|
||||
for k in 0..this.projects.length {
|
||||
val prior := &this.projects[k];
|
||||
continue if prior.folder != made.folder;
|
||||
if !same_options(prior.options.view(), made.options.view()) {
|
||||
return failure { status = 2, message = $"{made.identity.view()} is required with two sets of option values: {described(prior.options.view()).view()}, and {described(made.options.view()).view()} by {by}." };
|
||||
}
|
||||
return k;
|
||||
}
|
||||
|
||||
val index := this.projects.length;
|
||||
this.projects.append(move(made));
|
||||
return index if !root && !needs_requirements(&this.projects[index]);
|
||||
|
||||
# Each requirement is taken out before the next is read, since reading one
|
||||
# adds projects, which may move those before it.
|
||||
val count := this.projects[index].requires.length;
|
||||
for k in 0..count {
|
||||
val required := &this.projects[index].requires[k];
|
||||
val remote := string::copy_of(&required.remote);
|
||||
val branch := string::copy_of(&required.branch);
|
||||
val local := required.local;
|
||||
val line := required.line;
|
||||
var options := vector::<models::setting>::new();
|
||||
for &each in required.options {
|
||||
options.append(models::setting { name = string::copy_of(&each.name), value = string::copy_of(&each.value) });
|
||||
}
|
||||
val requirer := this.projects[index].identity;
|
||||
|
||||
var found := path::new();
|
||||
if local {
|
||||
val named := path::parse(remote.view()) else (_) {
|
||||
return failure { status = 2, message = $"{this.projects[index].file.view()}:{line}: `{remote.view()}` is no folder." };
|
||||
};
|
||||
found = this.projects[index].folder.join(&named);
|
||||
} else {
|
||||
var fetched := this.fetch(remote.view(), branch.view());
|
||||
if fetched is error(&mut why) {
|
||||
return move(why);
|
||||
}
|
||||
found = fetched.unwrap();
|
||||
}
|
||||
var loaded := this.load(&found, options.view(), requirer.view(), false);
|
||||
if loaded is error(&mut why) {
|
||||
return move(why);
|
||||
}
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
# Finds a remote's clone in the store, cloning it where it's missing: into a
|
||||
# folder beside it first, renamed once the clone is whole, so one stopped
|
||||
# halfway is never taken for a clone.
|
||||
def workspace::fetch(&mut this, remote: strview, branch: strview): result<path, failure> {
|
||||
val folder := config_folder() else {
|
||||
return failure { status = 2, message = string::from("There's no store to clone into: neither `XDG_CONFIG_HOME` nor `HOME` is set.", nothing {}) };
|
||||
};
|
||||
val key := store_key(remote, branch);
|
||||
val stored := path::parse(key.view()) else (_) {
|
||||
return failure { status = 2, message = $"`{remote}` can't name a folder of the store." };
|
||||
};
|
||||
val into := folder.join(&stored);
|
||||
return into if io::exists(&into);
|
||||
|
||||
val partial := folder.join($"{key.view()}.partial".view());
|
||||
_ = remove_tree(&partial);
|
||||
if partial.parent() is some(&above) {
|
||||
_ = io::create_directories(above);
|
||||
}
|
||||
io::println($"Cloning {remote}@{branch}".view());
|
||||
var git := process::command::new("git");
|
||||
git.arguments(["clone", "--quiet", "-b", branch, "--depth", "1", remote, partial.view()]);
|
||||
if this.verbose {
|
||||
io::println($"git clone --quiet -b {branch} --depth 1 {remote} {partial.view()}".view());
|
||||
}
|
||||
val status := git.run() else (why) {
|
||||
return failure { status = 1, message = $"git can't be run, to clone `{remote}`: {why}." };
|
||||
};
|
||||
if !status.succeeded {
|
||||
_ = remove_tree(&partial);
|
||||
return failure { status = 1, message = $"Cloning {remote}@{branch} failed." };
|
||||
}
|
||||
if io::rename(&partial, &into) is error(why) {
|
||||
return failure { status = 1, message = $"The clone of `{remote}` can't be moved to `{into.view()}`: {why}." };
|
||||
}
|
||||
return into;
|
||||
}
|
||||
|
||||
# Whether a project's libraries read one it doesn't hold, which one it requires
|
||||
# must, so what it requires is read.
|
||||
def needs_requirements(holder: &models::project): bool {
|
||||
for &each in holder.targets {
|
||||
continue if each.kind != models::target_kind::library;
|
||||
for &name in each.deps {
|
||||
continue if name == "std";
|
||||
var own := false;
|
||||
for &other in holder.targets {
|
||||
own = own || (other.kind == models::target_kind::library && other.name == name);
|
||||
}
|
||||
return true if !own;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
# Indexes every library of the workspace's projects by name, refusing a name
|
||||
# two projects give.
|
||||
def workspace::index(&mut this): result<void, failure> {
|
||||
for p in 0..this.projects.length {
|
||||
val holder := &this.projects[p];
|
||||
for t in 0..holder.targets.length {
|
||||
val each := &holder.targets[t];
|
||||
continue if each.kind != models::target_kind::library;
|
||||
for &prior in this.libraries {
|
||||
if prior.name == each.name {
|
||||
return failure { status = 2, message = $"`{each.name.view()}` is a library of both {this.projects[prior.project].identity.view()} and {holder.identity.view()}." };
|
||||
}
|
||||
}
|
||||
this.libraries.append(library { name = string::copy_of(&each.name), project = p, target = t });
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
#! Finds a library by its name.
|
||||
#! @param this The workspace.
|
||||
#! @param name The name.
|
||||
#! @returns Where it is among the libraries, or `nothing`.
|
||||
pub def workspace::library_named(&this, name: strview): maybe<u64> {
|
||||
for k in 0..this.libraries.length {
|
||||
return k if this.libraries[k].name.view() == name;
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! Gathers every library a target reads, through the `deps` of each, each once:
|
||||
#! those of its own project, and those other projects export.
|
||||
#! @param this The workspace.
|
||||
#! @param project The project holding the target.
|
||||
#! @param deps The target's `deps`.
|
||||
#! @param who The target's name, for what's said where one is wrong.
|
||||
#! @param into Where each library's place among the libraries is added.
|
||||
#! @returns Why one can't be read, where one can't.
|
||||
pub def workspace::reach(
|
||||
&this,
|
||||
project: u64,
|
||||
deps: span<string>,
|
||||
who: strview,
|
||||
into: &mut vector<u64>
|
||||
): result<void, failure> {
|
||||
for &name in deps {
|
||||
continue if name == "std";
|
||||
val found := this.library_named(name.view()) else {
|
||||
return failure { status = 2, message = $"`{name.view()}`, which `{who}` reads, is no library of {this.projects[project].identity.view()} or of what it requires." };
|
||||
};
|
||||
val held := &this.libraries[found];
|
||||
if held.project != project && !this.projects[held.project].exported(name.view()) {
|
||||
return failure { status = 2, message = $"`{name.view()}`, which `{who}` reads, isn't exported by {this.projects[held.project].identity.view()}." };
|
||||
}
|
||||
var seen := false;
|
||||
for &prior in into {
|
||||
seen = seen || prior == found;
|
||||
}
|
||||
continue if seen;
|
||||
into.append(found);
|
||||
val target := &this.projects[held.project].targets[held.target];
|
||||
var reached := this.reach(held.project, target.deps.view(), name.view(), into);
|
||||
if reached is error(&mut why) {
|
||||
return move(why);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
#! The folder a library's modules are in.
|
||||
#! @param this The workspace.
|
||||
#! @param index Where the library is among the libraries.
|
||||
#! @param from The project whose folder `sakura` runs in, which its own
|
||||
#! libraries are written relative to.
|
||||
#! @returns The folder: relative to that project's where it's that project's
|
||||
#! own, and absolute otherwise.
|
||||
pub def workspace::library_folder(&this, index: u64, from: u64): path {
|
||||
val held := &this.libraries[index];
|
||||
val holder := &this.projects[held.project];
|
||||
val root := path::parse(holder.targets[held.target].root.view()) else (_) { return path::new(); };
|
||||
if held.project == from {
|
||||
return root;
|
||||
}
|
||||
return holder.folder.join(&root);
|
||||
}
|
||||
|
||||
#! The `--lib` a library is given by: its module, with `::` for `.`, and its
|
||||
#! folder.
|
||||
#! @param this The workspace.
|
||||
#! @param index Where the library is among the libraries.
|
||||
#! @param from The project whose folder `sakura` runs in.
|
||||
#! @returns As `hello::geometry=hello.geometry`.
|
||||
pub def workspace::lib_option(&this, index: u64, from: u64): string {
|
||||
var made := string::new(nothing {});
|
||||
for k in 0..this.libraries[index].name.length {
|
||||
val byte := this.libraries[index].name[k];
|
||||
if byte == '.' {
|
||||
made.append("::");
|
||||
} else {
|
||||
made.push(byte);
|
||||
}
|
||||
}
|
||||
made.push('=');
|
||||
made.append(this.library_folder(index, from).view());
|
||||
return made;
|
||||
}
|
||||
|
||||
#! The folder of the store a remote's branch is cloned into: its host and path,
|
||||
#! then `@` and the branch, as `git.malunal.studio/malunal/json@v1.0.0`, with
|
||||
#! any scheme, user, and `.git` left out, and anything that's no name's taken
|
||||
#! out, so no remote reaches outside the store.
|
||||
#! @param remote The remote, as `https://host/path`, `git@host:path`, or
|
||||
#! `file:///path`.
|
||||
#! @param branch The branch or tag.
|
||||
#! @returns The folder, relative to the store.
|
||||
pub def store_key(remote: strview, branch: strview): string {
|
||||
var rest := remote;
|
||||
var scheme := strview::empty();
|
||||
if rest.find("://") is some(at) {
|
||||
scheme = rest.substr(0, at);
|
||||
rest = rest.substr(at + 3, rest.length - at - 3);
|
||||
} else if rest.find(":") is some(at) {
|
||||
# `user@host:path`, as ssh writes it.
|
||||
var host := rest.substr(0, at);
|
||||
if host.find("@") is some(user) {
|
||||
host = host.substr(user + 1, host.length - user - 1);
|
||||
}
|
||||
var joined := string::from(host, nothing {});
|
||||
joined.push('/');
|
||||
joined.append(rest.substr(at + 1, rest.length - at - 1));
|
||||
return finished_key(joined.view(), strview::empty(), branch);
|
||||
}
|
||||
if scheme != "file" {
|
||||
val slash := rest.find("/") else { return finished_key(rest, scheme, branch); };
|
||||
var host := rest.substr(0, slash);
|
||||
if host.find("@") is some(user) {
|
||||
host = host.substr(user + 1, host.length - user - 1);
|
||||
var joined := string::from(host, nothing {});
|
||||
joined.append(rest.substr(slash, rest.length - slash));
|
||||
return finished_key(joined.view(), scheme, branch);
|
||||
}
|
||||
}
|
||||
return finished_key(rest, scheme, branch);
|
||||
}
|
||||
|
||||
# A store's folder from a remote's host and path: each segment a name, `.` and
|
||||
# `..` left out, and `.git` taken off the end, then `@` and the branch.
|
||||
def finished_key(text: strview, scheme: strview, branch: strview): string {
|
||||
var made := string::new(nothing {});
|
||||
if scheme == "file" {
|
||||
made.append("file");
|
||||
}
|
||||
for part in text.split('/') {
|
||||
continue if part.length == 0 || part == "." || part == "..";
|
||||
if made.length != 0 {
|
||||
made.push('/');
|
||||
}
|
||||
for k in 0..part.length {
|
||||
val byte := part[k];
|
||||
made.push(byte.is_alnum || byte == '.' || byte == '-' || byte == '_' || byte == '+' ? byte : '_');
|
||||
}
|
||||
}
|
||||
if made.view().ends_with(".git") {
|
||||
made.resize(made.length - 4);
|
||||
}
|
||||
made.push('@');
|
||||
for k in 0..branch.length {
|
||||
val byte := branch[k];
|
||||
made.push(byte.is_alnum || byte == '.' || byte == '-' || byte == '_' || byte == '+' ? byte : '_');
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
# Whether two projects' option values are the same.
|
||||
def same_options(a: span<models::setting>, b: span<models::setting>): bool {
|
||||
return false if a.length != b.length;
|
||||
for k in 0..a.length {
|
||||
return false if a[k].name != b[k].name || a[k].value != b[k].value;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
# Option values, as `level=2 debug=false`.
|
||||
def described(options: span<models::setting>): string {
|
||||
var made := string::new(nothing {});
|
||||
for &each in options {
|
||||
if made.length != 0 {
|
||||
made.push(' ');
|
||||
}
|
||||
made.append($"{each.name.view()}={each.value.view()}".view());
|
||||
}
|
||||
if made.length == 0 {
|
||||
made.append("none");
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Removes a file, or a folder and everything in it. Links are removed, not
|
||||
#! followed.
|
||||
#! @param at What to remove.
|
||||
#! @returns Why it couldn't all be removed, where it couldn't.
|
||||
pub def remove_tree(at: &path): result<void, io_error> {
|
||||
val found := io::info(at, false) else (why) {
|
||||
if why == io_error::not_found {
|
||||
return;
|
||||
}
|
||||
return why;
|
||||
};
|
||||
if found.kind == file_kind::directory {
|
||||
var names := vector::<path>::new();
|
||||
var listing := io::directory::open(at) else (why) { return why; };
|
||||
while listing.next() is some(entry) {
|
||||
val name := path::parse(entry.name) else (_) { return io_error::invalid; };
|
||||
names.append(at.join(&name));
|
||||
}
|
||||
for &each in names {
|
||||
var removed := remove_tree(each);
|
||||
return removed if removed is error;
|
||||
}
|
||||
}
|
||||
return io::remove(at);
|
||||
}
|
||||
Reference in new issue
Block a user