#! @file entry.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains blossom's command line: `build`, `check`, `run`, `test`, `clean`, #! `install`, and `package`, each reading the blossomfile of a project and those #! of what it requires, then running the `sakura` it finds for each step; #! `pull`, which clones what a project requires; and `libraries`, which says what #! `--lib` each library the project reads is given, as an editor wants to know. #! #! It ends with 0 where everything worked, 1 where a build, a test, or a clone #! failed, and 2 where the command or a blossomfile is wrong. `run` ends with #! the status of the program it runs. import std::args; import std::io; import system; import blossom::models; import blossom::management; import blossom::pipeline; #! What every command takes. @group struct common_args { @option{ name = "option", short = 'o', value = "NAME=VALUE", help = "Gives the project's option a value; given again for each." } var options: vector; @option{ value = "TARGET", help = "Builds every target for `linux-x86_64` or `windows-x86_64`, whatever its own `target` says." } var target: strview; @option{ short = 'j', value = "N", help = "How many threads `sakura` compiles each program on." } var threads: maybe; @option{ short = 'v', help = "Prints each command run." } var verbose: bool; } @args::command struct build_args { @group var common: common_args; @argument{ value = "PROJECT", help = "The folder of the project's blossomfile, `.` unless given." } var project: maybe; } @args::command struct check_args { @group var common: common_args; @argument{ value = "PROJECT", help = "The folder of the project's blossomfile, `.` unless given." } var project: maybe; } @args::command struct run_args { @group var common: common_args; @argument{ value = "TARGET", help = "The program to run, unless the project has only one." } var name: maybe; @argument{ value = "ARGUMENT", rest = true, help = "What the program is given." } var arguments: vector; } @args::command struct test_args { @group var common: common_args; @option{ value = "NAME", help = "Runs only the suites, the project's `tests`, whose names hold the text." } var suite: strview; @option{ name = "test", value = "NAME", help = "Runs only the tests whose names hold the text, of each suite run, giving each suite `--only NAME`, as `std::testing` reads it." } var named: strview; @argument{ value = "PROJECT", help = "The folder of the project's blossomfile, `.` unless given." } var project: maybe; } @args::command struct clean_args { @group var common: common_args; @option{ help = "Removes the outputs of every set of option values, the whole `.blossom` folder." } var all: bool; @argument{ value = "PROJECT", help = "The folder of the project's blossomfile, `.` unless given." } var project: maybe; } @args::command struct install_args { @group var common: common_args; @option{ value = "DIR", help = "Where it's installed: `~/.local` unless given." } var prefix: strview; @option{ value = "DIR", help = "Puts what's installed under the folder, as DIR/PREFIX, for packaging." } var destdir: strview; @argument{ value = "PROJECT", help = "The folder of the project's blossomfile, `.` unless given." } var project: maybe; } @args::command struct pull_args { @group var common: common_args; @option{ value = "DIR", help = "The folder of the project's blossomfile, `.` unless given." } var project: strview; @option{ help = "Clones again what the store already holds, as a branch that moved wants." } var update: bool; @argument{ value = "REQUIREMENT", help = "What to pull: a remote or folder as the blossomfile writes it, or the last part of its path, as `binary`; everything the project requires unless given." } var names: vector; } @args::command struct libraries_args { @group var common: common_args; @argument{ value = "PROJECT", help = "The folder of the project's blossomfile, `.` unless given." } var project: maybe; } @args::command struct package_args { @group var common: common_args; @option{ name = "format", short = 'f', value = "FORMAT[,FORMAT]", help = "The packages written: `tar`, `zip`, `deb`, `arch`, and `rpm`; `tar` unless given, or `zip` for Windows." } var formats: vector; @option{ value = "DIR", help = "Where a system's package installs: `/usr` unless given." } var prefix: strview; @argument{ value = "PROJECT", help = "The folder of the project's blossomfile, `.` unless given." } var project: maybe; } @args::command{ name = "blossom", help = "Builds Sakura projects, as their blossomfiles say.", version = "blossom 1.0.0" } union commands { @args::command{ help = "Checks the project's libraries, then builds its programs into `.blossom`." } var build: build_args; @args::command{ help = "Checks each target of the project, writing nothing." } var check: check_args; @args::command{ help = "Runs a program of the project on the machine." } var run: run_args; @args::command{ help = "Builds the project, then runs its tests, showing what they write." } var test: test_args; @args::command{ help = "Removes the outputs of these option values, or with `--all`, of every set." } var clean: clean_args; @args::command{ help = "Builds the project, then installs its programs, exported libraries, and `install` steps." } var install: install_args; @args::command{ help = "Installs the project into its stage, then writes packages of it." } var package: package_args; @args::command{ help = "Clones what the project requires, or the requirements named, where the store is missing them." } var pull: pull_args; @args::command{ help = "Prints the `--lib` of each library the project holds or reads, its folder absolute, and `--missing` for each requirement the store is missing." } var libraries: libraries_args; } def main(args: span): void { val chosen := args::parse_or_exit::(args); match chosen { build(&b) => { var session := opened(&b.common, b.project); session.build(false, ""); exit(finished(&session, "Built")); } check(&c) => { var session := opened(&c.common, c.project); session.check(); exit(finished(&session, "Checked")); } run(&r) => { var session := opened(&r.common, nothing {}); var name := strview::blank(); if r.name is some(named) { name = named; } exit(session.run_program(name, r.arguments.span)); } test(&t) => { var session := opened(&t.common, t.project); session.build(true, t.suite, t.named); exit(finished(&session, "Tested")); } clean(&c) => exit(cleaned(&c)); install(&i) => { var session := opened(&i.common, i.project); exit(session.install_command(i.prefix, i.destdir)); } package(&p) => { var session := opened(&p.common, p.project); exit(session.package_command(p.formats.span, p.prefix)); } pull(&p) => exit(pulled(&p)); libraries(&l) => exit(listed(&l)); } } # Reads the project a command is given, and everything it requires, and finds # the `sakura` to run, stopping with what's wrong where any of that fails. def opened(common: &common_args, project: maybe): pipeline::session { var overrides := overrides_of(common); if common.target.length != 0 && !models::is_platform(common.target) { pipeline::fatal(2, $"`--target {common.target}` is no target: write `linux-x86_64` or `windows-x86_64`.".span); } var config := management::read_config(); if config is error(&problem) { pipeline::fatal(2, problem.describe().span); } var given := config.unwrap(); var sakura := management::find_sakura(&given) else { pipeline::fatal(2, "No `sakurac` is found: name a `sakura` as `sakura:` in blossom's config, or put `sakurac` beside blossom or on `PATH`."); return pipeline::session {}; }; var std := string::new(nothing {}); if given.std.length != 0 { val named := path::parse(given.std.span) else (_) { pipeline::fatal(2, $"The config's `std`, `{given.std.span}`, is no path.".span); return pipeline::session {}; }; val at := io::absolute(&named) else (_) { return pipeline::session {}; }; std = string::from(at.span, nothing {}); } var folder_text: strview = "."; if project is some(named) { folder_text = named; } val folder := path::parse(folder_text) else (_) { pipeline::fatal(2, "The project's folder is no path."); return pipeline::session {}; }; var space := management::open_workspace(&folder, overrides.span, common.target, common.verbose); if space is error(&why) { pipeline::fatal(why.status, why.message.span); } var made := pipeline::session { sakura = move(sakura), std = move(std) }; if common.threads is some(count) { made.threads = $"{count}"; } if space is value(&mut opened) { made.space = move(opened); } return made; } # The options `-o` gives, as `NAME=VALUE` each. def overrides_of(common: &common_args): vector { var made := vector::::new(); for &each in common.options { val equals := each.find("=") else { pipeline::fatal(2, $"`-o {each}` names no value: write `-o NAME=VALUE`.".span); return made; }; if equals == 0 { pipeline::fatal(2, $"`-o {each}` names no option: write `-o NAME=VALUE`.".span); } made.append(models::setting { name = string::from(each.substr(0, equals), nothing {}), value = string::from(each.substr(equals + 1, each.length - equals - 1), nothing {}) }); } return made; } # The status a command ends with, after its last line, as `Built # malunal/hello@1.0.0`. def finished(session: &pipeline::session, verb: strview): i32 { return 1 if session.failed; io::println($"{verb} {session.space.projects[0].identity.span}".span); return 0; } # Removes the outputs of these option values, or with `--all`, the whole # `.blossom` folder, reading only the project's own blossomfile and its # members'. def cleaned(given: &clean_args): i32 { var overrides := overrides_of(&given.common); var folder_text: strview = "."; if given.project is some(named) { folder_text = named; } val named := path::parse(folder_text) else (_) { pipeline::fatal(2, "The project's folder is no path."); return 2; }; # A member's folder cleans its project's outputs, which are its own. val place := management::project_of(&named); if place is error(&why) { pipeline::fatal(why.status, why.message.span); } if place is not value(&found) { return 2; } val folder := path::copy_of(&found.folder); val file := folder.join("blossomfile"); val text := io::read_file(&file) else (why) { pipeline::fatal(2, $"`{folder.span}` holds no blossomfile to read: {why}.".span); return 2; }; var read := models::read_project(text.span, &file, overrides.span, given.common.target); if read is error(&problem) { pipeline::fatal(2, problem.describe().span); } if read is not value(&project) { return 2; } var removed := project.output; if given.all { removed = folder.join(".blossom"); } if !io::exists(&removed) { io::println($"Nothing to clean for {project.identity.span}".span); return 0; } if management::remove_tree(&removed) is error(why) { pipeline::fatal(1, $"`{removed.span}` can't be removed: {why}.".span); } io::println($"Cleaned {project.identity.span} ({removed.span})".span); return 0; } # Clones what the project requires, or the requirements named, and what their # libraries read in turn, where the store is missing them, or with `--update` # where it holds them, then says where each is. No `sakura` is needed, since # none is run. def pulled(given: &pull_args): i32 { var overrides := overrides_of(&given.common); checked_target(&given.common); var folder_text: strview = "."; if given.project.length != 0 { folder_text = given.project; } val folder := project_folder(folder_text); var space := management::open_workspace_with(&folder, overrides.span, given.common.target, given.common.verbose, false, given.update, given.names.span); if space is error(&why) { pipeline::fatal(why.status, why.message.span); } if space is not value(&opened) { return 2; } val identity := opened.root().identity; val total := opened.found.length; if total == 0 { io::println($"Nothing to pull for {identity.span}".span); return 0; } for k in 0..total { val each := &opened.found[k]; if each.local { io::println($"[{k + 1}/{total}] Found {each.remote.span} ({each.folder.span})".span); continue; } val verb := each.updated ? "Updated" : each.cloned ? "Cloned" : "Present"; io::println($"[{k + 1}/{total}] {verb} {each.remote.span}@{each.branch.span} ({each.folder.span})".span); } io::println($"Pulled what {identity.span} requires".span); return 0; } # Prints the `--lib` of each library the project holds or reads, a line each, # its folder absolute: its own, then those its targets and tests read, and what # those read in turn. Nothing is cloned: each remote the store is missing is # printed as `--missing REMOTE@BRANCH` after them, for `pull` to clone, and the # libraries it would give are left out. No `sakura` is needed, since none is # run. def listed(given: &libraries_args): i32 { var overrides := overrides_of(&given.common); checked_target(&given.common); var folder_text: strview = "."; if given.project is some(named) { folder_text = named; } val folder := project_folder(folder_text); var space := management::open_workspace_with(&folder, overrides.span, given.common.target, given.common.verbose, true, false, span::::blank()); if space is error(&why) { pipeline::fatal(why.status, why.message.span); } if space is not value(&opened) { return 2; } var read := vector::::new(); var reached := opened.read_by(0, &mut read); if reached is error(&why) { pipeline::fatal(why.status, why.message.span); } for index in read { io::println($"--lib {opened.absolute_lib_option(index).span}".span); } for &each in opened.missing { io::println($"--missing {each.span}".span); } if opened.missing.length != 0 { val said := opened.missing.length == 1 ? string::from("blossom: 1 requirement isn't in the store, so what it holds is left out: `blossom pull` clones it.\n", nothing {}) : $"blossom: {opened.missing.length} requirements aren't in the store, so what they hold is left out: `blossom pull` clones them.\n"; system::fd::stderr.put(said.span); } return 0; } # Stops where `--target` names no target. def checked_target(common: &common_args): void { if common.target.length != 0 && !models::is_platform(common.target) { pipeline::fatal(2, $"`--target {common.target}` is no target: write `linux-x86_64` or `windows-x86_64`.".span); } } # The project's folder, stopping where the text is no path. def project_folder(text: strview): path { val folder := path::parse(text) else (_) { pipeline::fatal(2, "The project's folder is no path."); return path::new(); }; return folder; }