#! @file session.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the session a command runs in: the workspace, the `sakura` it runs #! and what it's given, the steps counted and said as they're done, and what #! decides a step is done already: the command that made its output, kept #! beside it, and the files `sakura` said it read, through `--deps`, none newer #! than the output. import std::io; import std::process; import system; import blossom::models; import blossom::management; module blossom::pipeline; #! What a command runs with, and how its steps went. pub struct session { pub var space: management::workspace; pub var sakura: string; #! The `sakura` run, absolute. pub var std: string; #! The standard library it's given, or empty for the one it finds. pub var threads: string; #! What `-j` says, or empty. pub var step: u64; #! The step being done, counted from 1. pub var steps: u64; #! How many there are. pub var failed: bool; #! Whether a step failed. } #! Says a step is done, as `[2/3] Built hello`. #! @param this The session. #! @param what The step, as `Built`. #! @param name The target's name. #! @param more What follows, in parentheses, or empty. pub def session::said(&mut this, what: strview, name: strview, more: strview = ""): void { this.step++; if more.length != 0 { io::println($"[{this.step}/{this.steps}] {what} {name} ({more})".span); } else { io::println($"[{this.step}/{this.steps}] {what} {name}".span); } } #! Says a step failed, as `Failed hello`, and notes it, which the command's #! status says. #! @param this The session. #! @param name The target's name. pub def session::fail(&mut this, name: strview): void { this.step++; this.failed = true; io::println($"Failed {name}".span); } #! Says a step is skipped, as `Skipped hello`, since one it needs failed. #! @param this The session. #! @param name The target's name. pub def session::skip(&mut this, name: strview): void { this.step++; io::println($"Skipped {name}".span); } #! The words starting a `sakura` command: the program, the command, and the #! standard library where the config names one, as `--lib std=DIR`. #! @param this The session. #! @param command `check`, `run`, or `build`. #! @returns The words. pub def session::words(&this, command: strview): vector { var made := vector::::new(); made.append(string::copy_of(&this.sakura)); made.append(string::from(command, nothing {})); if this.std.length != 0 { made.append(string::from("--lib", nothing {})); made.append($"std={this.std}"); } return made; } #! Adds `-j N` where the command was given it. #! @param this The session. #! @param words The words to add to. pub def session::add_threads(&this, words: &mut vector): void { if this.threads.length != 0 { words.append(string::from("-j", nothing {})); words.append(string::copy_of(&this.threads)); } } #! Runs a command in a project's folder, its output and error going where #! blossom's go, printing it first where `-v` asks. #! @param this The session. #! @param words The program, then its arguments. #! @param cwd The folder it runs in. #! @returns How it stopped, or why it couldn't start. pub def session::run(&mut this, words: span, cwd: &path): result { var made := this.command(words, cwd); return made.run(); } #! A command, ready to run in a project's folder, with `SAKURA` set to the #! `sakura` blossom runs, so what it runs can run `sakura` as blossom does, #! printed first where `-v` asks. #! @param this The session. #! @param words The program, then its arguments. #! @param cwd The folder it runs in. #! @returns The command. pub def session::command(&mut this, words: span, cwd: &path): process::command { if this.space.verbose { io::println(spoken(words).span); } var made := process::command::new(words[0].span); for k in 1..words.length { made.argument(words[k].span); } made.directory(cwd); made.set_variable("SAKURA", this.sakura.span); return made; } #! A command as a shell would take it, each word quoted where it holds what a #! shell reads apart. #! @param words The words. #! @returns The line. pub def spoken(words: span): string { var made := string::new(nothing {}); for &each in words { if made.length != 0 { made.push(' '); } var plain := each.length != 0; for k in 0..each.length { val byte := each[k]; plain = plain && (byte.is_alnum || byte == '/' || byte == '.' || byte == '-' || byte == '_' || byte == '=' || byte == ':' || byte == '+' || byte == ',' || byte == '@'); } if plain { made.append(each.span); continue; } made.push('\''); for k in 0..each.length { if each[k] == '\'' { made.append("'\\''"); } else { made.push(each[k]); } } made.push('\''); } return made; } #! What's kept beside an output to say how it was made: the `sakura` that made #! it, when that executable was written, and the command's words, a line each. #! @param this The session. #! @param words The command. #! @returns The text. pub def session::made_by(&this, words: span): string { var made := string::copy_of(&this.sakura); var written: i64 = 0; if path::parse(this.sakura.span) is value(&at) { if io::info(&at) is value(&found) { written = found.modified; } } made.append($"\n{written}".span); for &each in words { made.push('\n'); made.append(each.span); } made.push('\n'); return made; } #! Whether an output is up to date: it's there, the command kept beside it is #! the one that would make it now, and no file `sakura` read making it, nor the #! blossomfile, nor a member's, is newer than it. #! @param output The output, as an executable or a stamp. #! @param kept The command kept beside it. #! @param now The command that would make it now, as `made_by` gives it. #! @param deps The files `sakura` read, as `--deps` wrote them, relative to #! the project's folder where they're not absolute. #! @param holder The project. #! @returns Whether it is. pub def is_fresh(output: &path, kept: &path, now: strview, deps: &path, holder: &models::project): bool { val made := io::info(output) else (_) { return false; }; val before := io::read_file(kept) else (_) { return false; }; return false if before.span != now; val read := io::read_file(deps) else (_) { return false; }; return false if newer(&holder.file, made.modified); for &each in holder.members { return false if newer(&each.file, made.modified); } for line in read.span.lines() { continue if line.length == 0; val named := path::parse(line) else (_) { return false; }; val file := holder.folder.join(&named); return false if newer(&file, made.modified); } return true; } # Whether a file was written after a time, or isn't there to say. def newer(at: &path, than: i64): bool { val found := io::info(at) else (_) { return true; }; return found.modified > than; } #! Writes a file, making the folders above it first. #! @param at The file. #! @param text What it holds. #! @returns Whether it was written. pub def written(at: &path, text: strview): bool { if at.parent() is some(&above) { _ = io::create_directories(above); } return io::write_file(at, text) is value; } #! A status as the code a program exits with: its own where it exited, and #! 128 and the signal's number where a signal stopped it, as shells give it. #! @param status How it stopped. #! @returns The code. pub def exit_code(status: &process::status): i32 { if status is exited(code) { return code; } if status is signaled(signal) { return 128 + signal; } return 1; }