Initial Commit
This commit is contained in:
27 files changed
+5466
No files matched your search
@@ -0,0 +1,198 @@
|
||||
#! @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 `sakurac`
|
||||
#! beside blossom, else the first `sakurac` on `PATH`.
|
||||
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 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> {
|
||||
return user_folder("APPDATA", "XDG_CONFIG_HOME", ".config");
|
||||
}
|
||||
|
||||
#! The folder blossom keeps its store of requirements in, which it can always
|
||||
#! clone again: `blossom` in `XDG_CACHE_HOME`, else in `~/.cache`, and on
|
||||
#! Windows in `LOCALAPPDATA`.
|
||||
#! @returns The folder, or `nothing` where the environment names no home.
|
||||
pub def cache_folder(): maybe<path> {
|
||||
return user_folder("LOCALAPPDATA", "XDG_CACHE_HOME", ".cache");
|
||||
}
|
||||
|
||||
# `blossom` in the folder a variable names: on Windows the first, elsewhere the
|
||||
# second, else the home's folder of the third name.
|
||||
def user_folder(on_windows: strview, elsewhere: strview, in_home: strview): maybe<path> {
|
||||
when os == windows {
|
||||
if process::variable(on_windows) is some(data) {
|
||||
if data.length != 0 {
|
||||
var at := path::parse(data) else (_) { return nothing {}; };
|
||||
return at.join("blossom");
|
||||
}
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
if process::variable(elsewhere) 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(in_home).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 `sakurac`
|
||||
#! beside blossom's executable, else the first `sakurac` 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("sakurac").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("sakurac").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 {});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user