#! @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. #! #! 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; 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 tests whose names hold the text." } var only: 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 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 0.1.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; } 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::empty(); if r.name is some(named) { name = named; } exit(session.run_program(name, r.arguments.view())); } test(&t) => { var session := opened(&t.common, t.project); session.build(true, t.only); 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.view(), p.prefix)); } } } # 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`.".view()); } var config := management::read_config(); if config is error(&problem) { pipeline::fatal(2, problem.describe().view()); } var given := config.unwrap(); var sakura := management::find_sakura(&given) else { pipeline::fatal(2, "No `sakura` is found: name it as `sakura:` in blossom's config, put it beside blossom, or on `PATH`."); return pipeline::session {}; }; var std := string::new(nothing {}); if given.std.length != 0 { val named := path::parse(given.std.view()) else (_) { pipeline::fatal(2, $"The config's `std`, `{given.std.view()}`, is no path.".view()); return pipeline::session {}; }; val at := io::absolute(&named) else (_) { return pipeline::session {}; }; std = string::from(at.view(), 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.view(), common.target, common.verbose); if space is error(&why) { pipeline::fatal(why.status, why.message.view()); } 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`.".view()); return made; }; if equals == 0 { pipeline::fatal(2, $"`-o {each}` names no option: write `-o NAME=VALUE`.".view()); } 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.view()}".view()); return 0; } # Removes the outputs of these option values, or with `--all`, the whole # `.blossom` folder, reading only the project's own blossomfile. 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; }; val folder := io::absolute(&named) else (why) { pipeline::fatal(2, $"`{named.view()}` can't be found: {why}.".view()); return 2; }; val file := folder.join("blossomfile"); val text := io::read_file(&file) else (why) { pipeline::fatal(2, $"`{folder.view()}` holds no blossomfile to read: {why}.".view()); return 2; }; var read := models::read_project(text.view(), &file, overrides.view(), given.common.target); if read is error(&problem) { pipeline::fatal(2, problem.describe().view()); } 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.view()}".view()); return 0; } if management::remove_tree(&removed) is error(why) { pipeline::fatal(1, $"`{removed.view()}` can't be removed: {why}.".view()); } io::println($"Cleaned {project.identity.view()} ({removed.view()})".view()); return 0; }