Initial Commit
This commit is contained in:
24 files changed
+4796
No files matched your search
@@ -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