Files

285 lines
10 KiB
Plaintext
Raw Permalink 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::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, prefix.span);
val files: strview = count == 1 ? "file" : "files";
io::println($"Installed {this.space.projects[0].identity.span} ({count} {files}) to {into.span}".span);
return 0;
}
#! Installs the project, already built, under a folder: its programs into
#! `bin`, each shown as a desktop's program for Linux with its desktop entry and
#! its icons, its exported libraries into `share/sakura`, then each step.
#! @param this The session.
#! @param into The folder, which stands for the prefix.
#! @param prefix The prefix itself, where a desktop entry finds its program.
#! @returns How many files were installed.
pub def session::install(&mut this, into: &path, prefix: strview): 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.span, system);
val built := holder.folder.join($"{output_text(holder).span}/bin/{name.span}".span);
count += copied(&built, &into.join("bin").join(name.span));
val petal := holder.folder.join($"{output_text(holder).span}/bin/{name.span}.petal".span);
if io::exists(&petal) {
count += copied(&petal, &into.join("bin").join($"{name.span}.petal".span));
}
if each.shown && !for_windows(system) {
count += install_desktop_entry(holder, &each, into, prefix);
}
} else if holder.exported(each.name.span) {
val root := path::parse(each.root.span) else (_) { continue; };
count += copied(&holder.folder.join(&root), &into.join("share").join("sakura").join(each.name.span));
}
}
for &step in holder.install {
val source := path::parse(step.source.span) else (_) {
fatal(2, $"{holder.file.span}:{step.line}: `{step.source.span}` is no path.");
return count;
};
val from := holder.folder.join(&source);
if !io::exists(&from) {
fatal(2, $"{holder.file.span}:{step.line}: `{step.source.span}` isn't there to install.");
}
val to := step.dest.length != 0 ? path::parse(step.dest.span) : path::parse(step.source.span);
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.span}` 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.span}` 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.span.compare(b.span) < 0; });
var count := 0u;
for &name in names {
count += copied(&from.join(name.span), &to.join(name.span));
}
return count;
}
if to.parent() is some(&above) {
_ = io::create_directories(above);
}
val bytes := io::read_file(from) else (why) {
fatal(1, $"`{from.span}` can't be read: {why}.");
return 0;
};
if io::write_file(to, bytes.span) is error(why) {
fatal(1, $"`{to.span}` can't be written: {why}.");
}
_ = io::set_permissions(to, (found.permissions & 0o111) != 0 ? 0o755 : 0o644);
return 1;
}
# Installs what Linux's desktops show a program by, as the freedesktop.org
# specifications have it, its file's name its own: each PNG of its `icon` into
# the icon theme every desktop reads, `share/icons/hicolor/WxH/apps`, by the
# size its header says, an SVG into its `scalable/apps`, and its desktop entry,
# `share/applications/NAME.desktop`, naming it by its title, its program where
# the prefix holds it, and its icon. An icon file holds Windows' icons alone,
# so it's left out here. Gives how many files were installed.
def install_desktop_entry(holder: &models::project, each: &models::target, into: &path, prefix: strview): u64 {
val name := each.name.span;
val icons := into.join("share/icons/hicolor");
var count := 0u;
var iconic := false;
for &file in each.icons {
val named := path::parse(file.span) else (_) {
fatal(2, $"{each.file.span}:{each.line}: `{file.span}` is no path.");
return count;
};
val from := holder.folder.join(&named);
if models::ends_with_any_case(file.span, ".svg") {
count += copied(&from, &icons.join($"scalable/apps/{name}.svg".span));
iconic = true;
} else if models::ends_with_any_case(file.span, ".png") {
val bytes := io::read_file(&from) else (why) {
fatal(1, $"`{from.span}` can't be read: {why}.");
return count;
};
val size := png_size(bytes.span);
if size[0] == 0 || size[1] == 0 {
fatal(2, $"{each.file.span}:{each.line}: `{file.span}` is no PNG.");
return count;
}
count += copied(&from, &icons.join($"{size[0]}x{size[1]}/apps/{name}.png".span));
iconic = true;
}
}
var entry := string::from("[Desktop Entry]\nType=Application\n", nothing {});
entry.append($"Name={one_line(each.title.length != 0 ? each.title.span : name).span}\n".span);
entry.append($"Exec={exec_text(prefix, name).span}\n".span);
if iconic {
entry.append($"Icon={name}\n".span);
}
entry.append(each.windowed ? "Terminal=false\n" : "Terminal=true\n");
entry.append($"StartupWMClass={name}\n".span);
val desktop := into.join($"share/applications/{name}.desktop".span);
if !written(&desktop, entry.span) {
fatal(1, $"`{desktop.span}` can't be written.");
}
_ = io::set_permissions(&desktop, 0o644);
return count + 1;
}
#! The size of a PNG, as its header says it.
#! @param bytes The file.
#! @returns Its width and height, or zeros where it's no PNG or is cut short.
pub def png_size(bytes: strview): u64[2] {
var made: u64[2];
return made if bytes.length < 24 || !bytes.starts_with("\x89PNG\r\n\x1A\n") || bytes.substr(12, 4) != "IHDR";
for k in 0..2u {
val at := 16 + 4 * k;
made[k] = (bytes[at] as u64 << 24) | (bytes[at + 1] as u64 << 16) | (bytes[at + 2] as u64 << 8) | bytes[at + 3] as u64;
}
return made;
}
#! What a desktop entry's `Exec` runs: the program in the prefix's `bin`, its
#! path's `\` written `/`, quoted where it holds what a desktop would split it
#! at, or read as its own, as the specification quotes it.
#! @param prefix The prefix.
#! @param name The program.
#! @returns The text.
pub def exec_text(prefix: strview, name: strview): string {
var end := prefix.length;
while end > 1 && (prefix[end - 1] == '/' || prefix[end - 1] == '\\') {
end--;
}
var whole := string::new(nothing {});
for k in 0..end {
whole.push(prefix[k] == '\\' ? '/' as u8 : prefix[k]);
}
whole.append($"/bin/{name}".span);
var plain := true;
for k in 0..whole.length {
val byte := whole[k];
plain = plain && !(byte == ' ' || byte == '\t' || byte == '"' || byte == '\'' || byte == '\\' || byte == '`' || byte == '$' || byte == '%');
}
return whole if plain;
var made := string::from("\"", nothing {});
for k in 0..whole.length {
val byte := whole[k];
if byte == '"' || byte == '`' || byte == '$' || byte == '\\' {
made.push('\\' as u8);
}
if byte == '%' {
made.push('%' as u8);
}
made.push(byte);
}
made.push('"' as u8);
return made;
}
# Text on one line, as a desktop entry's value is: each line break a space.
def one_line(text: strview): string {
var made := string::new(nothing {});
for k in 0..text.length {
made.push(text[k] == '\n' || text[k] == '\r' ? ' ' as u8 : text[k]);
}
return made;
}
# `.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;
}