Files
blossom/pipeline/install.sakura
T

169 lines
5.8 KiB
Plaintext
Raw Normal View History

2026-10-24 12:13:52 -05:00
#! @file install.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains `install`: the project built, then its programs put in the prefix's
#! `bin`, its exported libraries in `share/sakura`, as source another project's
#! `--lib` reads, and each `install` step done in turn.
import std;
import std::io;
import std::process;
import system;
import blossom::models;
import blossom::management;
module blossom::pipeline;
#! Builds the project, then installs it.
#! @param this The session.
#! @param prefix Where it's installed, or empty for `~/.local`.
#! @param destdir A folder to put what's installed under, as DESTDIR/PREFIX,
#! or empty.
#! @returns The status the command ends with.
pub def session::install_command(&mut this, prefix: strview, destdir: strview): i32 {
var into := path::new();
if prefix.length != 0 {
val named := path::parse(prefix) else (_) {
fatal(2, $"`--prefix {prefix}` is no path.");
return 2;
};
into = io::absolute(&named) else (why) {
fatal(2, $"`--prefix {prefix}` can't be found: {why}.");
return 2;
};
} else {
val home := process::variable("HOME") else {
when os == windows {
if process::variable("USERPROFILE") is some(profile) {
return this.install_into(prefix_under(profile), destdir);
}
}
fatal(2, "`HOME` isn't set, so the prefix must be given, with `--prefix`.");
return 2;
};
into = prefix_under(home);
}
return this.install_into(move(into), destdir);
}
# Installs into a prefix, under a destination folder where one's given.
def session::install_into(&mut this, prefix: path, destdir: strview): i32 {
var into := path::copy_of(&prefix);
if destdir.length != 0 {
val named := path::parse(destdir) else (_) {
fatal(2, $"`--destdir {destdir}` is no path.");
return 2;
};
val stage := io::absolute(&named) else (why) {
fatal(2, $"`--destdir {destdir}` can't be found: {why}.");
return 2;
};
into = under_root(&stage, &prefix);
}
this.build(false, "");
if this.failed {
return 1;
}
val count := this.install(&into);
io::println($"Installed {this.space.projects[0].identity.view()} ({count} files) to {into.view()}".view());
return 0;
}
#! Installs the project, already built, under a folder: its programs into
#! `bin`, its exported libraries into `share/sakura`, then each step.
#! @param this The session.
#! @param into The folder, which stands for the prefix.
#! @returns How many files were installed.
pub def session::install(&mut this, into: &path): u64 {
val holder := &this.space.projects[0];
var count := 0u;
for &each in holder.targets {
if each.kind == models::target_kind::program {
val system := this.platform_of(&each);
val name := output_name(each.name.view(), system);
val built := holder.folder.join($"{output_text(holder).view()}/bin/{name.view()}".view());
count += copied(&built, &into.join("bin").join(name.view()));
val petal := holder.folder.join($"{output_text(holder).view()}/bin/{name.view()}.petal".view());
if io::exists(&petal) {
count += copied(&petal, &into.join("bin").join($"{name.view()}.petal".view()));
}
} else if holder.exported(each.name.view()) {
val root := path::parse(each.root.view()) else (_) { continue; };
count += copied(&holder.folder.join(&root), &into.join("share").join("sakura").join(each.name.view()));
}
}
for &step in holder.install {
val source := path::parse(step.source.view()) else (_) {
fatal(2, $"{holder.file.view()}:{step.line}: `{step.source.view()}` is no path.");
return count;
};
val from := holder.folder.join(&source);
if !io::exists(&from) {
fatal(2, $"{holder.file.view()}:{step.line}: `{step.source.view()}` isn't there to install.");
}
val to := step.dest.length != 0 ? path::parse(step.dest.view()) : path::parse(step.source.view());
if to is value(&place) {
count += copied(&from, &into.join(place));
}
}
return count;
}
# Copies a file, or a folder and everything in it, making the folders above
# it, keeping who may run each, and giving how many files were copied. A copy
# that fails stops the command.
def copied(from: &path, to: &path): u64 {
val found := io::info(from) else (why) {
fatal(1, $"`{from.view()}` can't be read: {why}.");
return 0;
};
if found.kind == file_kind::directory {
_ = io::create_directories(to);
var names := vector::<string>::new();
var listing := io::directory::open(from) else (why) {
fatal(1, $"`{from.view()}` can't be read: {why}.");
return 0;
};
while listing.next() is some(entry) {
names.append(string::from(entry.name, nothing {}));
}
names.sort_by(def(a: &string, b: &string): bool { return a.view().compare(b.view()) < 0; });
var count := 0u;
for &name in names {
count += copied(&from.join(name.view()), &to.join(name.view()));
}
return count;
}
if to.parent() is some(&above) {
_ = io::create_directories(above);
}
val bytes := io::read_file(from) else (why) {
fatal(1, $"`{from.view()}` can't be read: {why}.");
return 0;
};
if io::write_file(to, bytes.view()) is error(why) {
fatal(1, $"`{to.view()}` can't be written: {why}.");
}
_ = io::set_permissions(to, (found.permissions & 0o111) != 0 ? 0o755 : 0o644);
return 1;
}
# `.local` in a home folder, the prefix unless one's given.
def prefix_under(home: strview): path {
val at := path::parse(home) else (_) { return path::new(); };
return at.join(".local");
}
# A path put under a folder, as `/stage` and `/usr/local` make
# `/stage/usr/local`.
def under_root(stage: &path, at: &path): path {
var made := path::copy_of(stage);
for part in at.segments() {
made = made.join(part);
}
return made;
}