Initial Commit
This commit is contained in:
24 files changed
+4796
No files matched your search
@@ -0,0 +1,9 @@
|
|||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 2
|
||||||
|
end_of_line = lf
|
||||||
|
charset = utf-8
|
||||||
|
trim_trailing_whitespace = true
|
||||||
|
insert_final_newline = true
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
.blossom
|
||||||
@@ -0,0 +1,195 @@
|
|||||||
|
# blossom
|
||||||
|
|
||||||
|
blossom builds Sakura projects, as chinook builds C ones. A `blossomfile` in a project's
|
||||||
|
folder says who the project is, what it requires, and its targets and tests. blossom reads
|
||||||
|
it and those of what it requires, then runs `sakura` for each step: one command a program.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
blossom build # check the project's libraries, then build its programs
|
||||||
|
blossom check # check each target, writing nothing
|
||||||
|
blossom run -- 3 4 # run the project's program on the machine, given `3 4`
|
||||||
|
blossom test --only geometry # build, then run the tests whose names hold `geometry`
|
||||||
|
blossom clean --all # remove .blossom
|
||||||
|
blossom install --prefix ~/.local
|
||||||
|
blossom package -f tar,deb # write packages into .blossom/<key>/dist
|
||||||
|
```
|
||||||
|
|
||||||
|
## Building blossom
|
||||||
|
|
||||||
|
Until blossom has built itself once, the compiler builds it, given the libraries it reads:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
sakura build -O2 -o blossom \
|
||||||
|
--lib std::yaml=../sakura-yaml/std.yaml --lib std::args=../sakura-args/std.args \
|
||||||
|
--lib malunal::glob=../sakura-glob/malunal.glob \
|
||||||
|
entry.sakura models/*.sakura management/*.sakura pipeline/*.sakura
|
||||||
|
```
|
||||||
|
|
||||||
|
After that, `blossom build` builds it from its blossomfile, and `blossom test` runs its
|
||||||
|
tests: those of its parts, and those of its commands, which run the blossom just built on
|
||||||
|
projects made in a folder of each test's own. The command tests run `git`, `sh`, and
|
||||||
|
`tar`, so they run on Linux.
|
||||||
|
|
||||||
|
## Commands
|
||||||
|
|
||||||
|
| Command | Does |
|
||||||
|
|---|---|
|
||||||
|
| `build [PROJECT]` | Checks the project's libraries, then builds each program into `.blossom/<key>/bin`, where it isn't up to date |
|
||||||
|
| `check [PROJECT]` | Checks each target, libraries and programs, writing nothing |
|
||||||
|
| `run [TARGET] [-- ARG...]` | Runs a program on the machine, `sakura run`: the one named, or the project's only one |
|
||||||
|
| `test [PROJECT] [--only TEXT]` | Builds, then runs each test, or those whose names hold the text, showing what they write |
|
||||||
|
| `clean [PROJECT] [--all]` | Removes the outputs of these option values, `.blossom/<key>`, or with `--all`, all of `.blossom` |
|
||||||
|
| `install [PROJECT] [--prefix DIR] [--destdir DIR]` | Builds, then installs into `~/.local` or the prefix, under the destination folder where given |
|
||||||
|
| `package [PROJECT] [-f FORMAT[,FORMAT]] [--prefix DIR]` | Installs into `.blossom/<key>/stage`, then writes `tar`, `zip`, `deb`, `arch`, or `rpm` packages into `dist` |
|
||||||
|
|
||||||
|
Each takes:
|
||||||
|
- `-o NAME=VALUE`, giving the project's option a value;
|
||||||
|
- `--target linux-x86_64|windows-x86_64`, building every target for that system;
|
||||||
|
- `-j N`, given to `sakura`;
|
||||||
|
- `-v`, printing each command run.
|
||||||
|
|
||||||
|
Each step says how it went, a line each, with what `sakura` and the tests write passed on
|
||||||
|
as they write it:
|
||||||
|
|
||||||
|
```
|
||||||
|
[1/3] Checked hello.geometry
|
||||||
|
[2/3] Built hello (.blossom/3f2a9c01d4e5b677/bin/hello)
|
||||||
|
3 passed, 0 failed, 0 skipped
|
||||||
|
[3/3] Tested hello.tests (3 passed)
|
||||||
|
Tested malunal/hello@1.0.0
|
||||||
|
```
|
||||||
|
|
||||||
|
A step that fails says `Failed NAME`, and a program reading a library that failed to check
|
||||||
|
says `Skipped NAME`. blossom 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, with the file and the
|
||||||
|
line. `run` ends with the program's own status.
|
||||||
|
|
||||||
|
## The blossomfile
|
||||||
|
|
||||||
|
YAML, in the project's folder:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
name: hello # required; the identity is gpid/name@semv
|
||||||
|
gpid: malunal # required
|
||||||
|
semv: 1.0.0 # required; a version, as 1.2.0 or 1.2.0-alpha
|
||||||
|
|
||||||
|
options: # NAME: value, any scalar; ${NAME} anywhere after; -o NAME=VALUE
|
||||||
|
level: 2 # ${bin} is given too: this option set's .blossom/<key>/bin
|
||||||
|
debug: false
|
||||||
|
|
||||||
|
requires:
|
||||||
|
- https://git.malunal.studio/malunal/json@v1.0.0 # REMOTE@REF; `main` without a ref
|
||||||
|
- ../shapes # a folder, relative to this file
|
||||||
|
- remote: https://git.malunal.studio/malunal/yaml # a block, where it's given options
|
||||||
|
branch: v1.0.0
|
||||||
|
options:
|
||||||
|
strict: true
|
||||||
|
|
||||||
|
targets:
|
||||||
|
- name: hello # a program
|
||||||
|
type: program
|
||||||
|
level: ${level} # -O: 0, 1, or 2; 2 unless given
|
||||||
|
petal: ${debug} # -g, writing hello.petal beside it
|
||||||
|
target: windows-x86_64 # --target; the system blossom runs on unless given
|
||||||
|
deps: [std, malunal.json, hello.geometry] # libraries it reads; std is the compiler's
|
||||||
|
srcs: [main.sakura, util/**/*.sakura] # files, folders (each .sakura in them), globs
|
||||||
|
opts: [--inline, "24"] # given to `sakura build` as written
|
||||||
|
|
||||||
|
- name: hello.geometry # a library: the module hello::geometry
|
||||||
|
type: library
|
||||||
|
root: ./hello.geometry # ./<name> unless given
|
||||||
|
deps: [malunal.json] # libraries its modules import
|
||||||
|
|
||||||
|
tests:
|
||||||
|
- name: hello.tests # the keys of targets, and these
|
||||||
|
type: program
|
||||||
|
runs: machine # machine, by `sakura run`, unless native: built, then run
|
||||||
|
checks: true # `--checks`; on unless given, on the machine
|
||||||
|
deps: [hello.geometry]
|
||||||
|
srcs: [tests/**/*.sakura] # tests/**/*.sakura unless given
|
||||||
|
args: [--only, geometry] # what the test is given
|
||||||
|
|
||||||
|
exports: # the libraries other projects may read
|
||||||
|
- hello.geometry
|
||||||
|
|
||||||
|
install: # beside the programs and the exported libraries
|
||||||
|
- !include ./share # to <prefix>/share
|
||||||
|
- !include ./docs:share/doc/hello # to <prefix>/share/doc/hello
|
||||||
|
|
||||||
|
package:
|
||||||
|
lic: MIT
|
||||||
|
desc: Says hello.
|
||||||
|
auth: Me <me@example.com>
|
||||||
|
url: https://example.com/hello
|
||||||
|
```
|
||||||
|
|
||||||
|
- **Every key is checked:** one blossom doesn't know, or one written twice in a mapping,
|
||||||
|
which YAML would keep only one of, is refused by its line.
|
||||||
|
- **A library's name is its module's,** with `.` for `::`. Its folder is given to
|
||||||
|
`sakura` with `--lib`, as `--lib hello::geometry=hello.geometry`, and the compiler reads
|
||||||
|
only the modules a program imports. Two projects giving a library one name is refused,
|
||||||
|
as is a program reading a library another project doesn't export.
|
||||||
|
- **`install`'s steps** are each tagged with what they do. `!include SOURCE` copies a
|
||||||
|
file or folder to its own place under the prefix, and `!include SOURCE:DEST` to `DEST`.
|
||||||
|
A step without a tag, with one blossom doesn't know, or reaching outside the project or
|
||||||
|
the prefix, is refused.
|
||||||
|
|
||||||
|
## Requirements
|
||||||
|
|
||||||
|
- **A remote** is cloned once, `git clone -b REF --depth 1`, into the store:
|
||||||
|
`~/.config/blossom/<host>/<path>@<ref>`, or `$XDG_CONFIG_HOME/blossom/...`, and
|
||||||
|
`%APPDATA%\blossom\...` on Windows. A clone stopped halfway is never taken for one, and
|
||||||
|
where git fails, what it says is shown.
|
||||||
|
- **A folder** is read where it is, relative to the blossomfile naming it.
|
||||||
|
- **One project, two sets of option values,** is refused, since each set builds outputs of
|
||||||
|
its own.
|
||||||
|
- **What a requirement requires** is read only where its libraries read what it doesn't
|
||||||
|
hold itself, so what only its tests or programs need is never fetched for those
|
||||||
|
requiring it.
|
||||||
|
|
||||||
|
## Outputs and building again
|
||||||
|
|
||||||
|
Each project's outputs go to `.blossom/<key>`, the key being the hash of its identity, its
|
||||||
|
option values, and `--target`, so each set builds apart. A program is built again only
|
||||||
|
where:
|
||||||
|
- its executable is missing;
|
||||||
|
- the command building it changed, which is kept beside it in `<name>.command`, with the
|
||||||
|
`sakura` that ran it and when that was written;
|
||||||
|
- a file `sakura` read building it, as `--deps` lists them in `<name>.deps`, or the
|
||||||
|
blossomfile, is newer than it.
|
||||||
|
|
||||||
|
A library's check is kept the same way, in `.blossom/<key>/check`.
|
||||||
|
|
||||||
|
## Tests
|
||||||
|
|
||||||
|
A test runs on the machine, `sakura run --checks ... -- ARGS`, unless it says `runs:
|
||||||
|
native`, where it's built, then run. It passes where it ends with 0. Each runs in the
|
||||||
|
project's folder, with `SAKURA` naming the `sakura` blossom runs, and `SAKURA_STD` the
|
||||||
|
standard library the config names, so a test can run the compiler as blossom does.
|
||||||
|
|
||||||
|
## Installing and packaging
|
||||||
|
|
||||||
|
`install` builds, then copies each program to `<prefix>/bin`, with its `.petal`, each
|
||||||
|
exported library to `<prefix>/share/sakura/<name>`, as source another project's `--lib`
|
||||||
|
reads, then does each step of `install`.
|
||||||
|
|
||||||
|
`package` installs into `.blossom/<key>/stage`, then writes each package into `dist`,
|
||||||
|
dating every entry `SOURCE_DATE_EPOCH`, where it's set, so the same stage writes the same
|
||||||
|
bytes:
|
||||||
|
- `tar`, compressed by `gzip`, and `zip`, its files stored, each under a folder of the
|
||||||
|
project's name, version, and target. `tar` unless asked, or `zip` for Windows;
|
||||||
|
- `deb` and `arch`, under `--prefix`, `/usr` unless given, compressed by `gzip`, `xz`, and
|
||||||
|
`zstd`, with the digests each lists;
|
||||||
|
- `rpm`, built by `rpmbuild` from a spec blossom writes.
|
||||||
|
|
||||||
|
## The config
|
||||||
|
|
||||||
|
`config` in blossom's folder, `~/.config/blossom`, is optional:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
sakura: /opt/sakura/bin/sakura # the sakura to run
|
||||||
|
std: /opt/sakura/std # the standard library it's given, with --std
|
||||||
|
```
|
||||||
|
|
||||||
|
Without `sakura`, blossom runs the `sakura` beside it, or else the first on `PATH`.
|
||||||
|
Without `std`, `sakura` finds its own.
|
||||||
+52
@@ -0,0 +1,52 @@
|
|||||||
|
name: blossom
|
||||||
|
gpid: malunal
|
||||||
|
semv: 1.0.0
|
||||||
|
|
||||||
|
options:
|
||||||
|
level: 2
|
||||||
|
debug: false
|
||||||
|
|
||||||
|
# A requirement given no options is written on a line of its own: a block names
|
||||||
|
# one requirement, and is written where it gives options.
|
||||||
|
requires:
|
||||||
|
- https://git.malunal.studio/malunal/sakura-args@v1.0.0
|
||||||
|
- https://git.malunal.studio/malunal/sakura-glob@v1.0.0
|
||||||
|
- https://git.malunal.studio/malunal/sakura-yaml@v1.0.0
|
||||||
|
|
||||||
|
targets:
|
||||||
|
- name: blossom
|
||||||
|
type: program
|
||||||
|
level: ${level}
|
||||||
|
petal: ${debug}
|
||||||
|
deps:
|
||||||
|
- std # Provided by compiler
|
||||||
|
- std.args # Standard extension, in malunal/args
|
||||||
|
- malunal.glob # Standard extension
|
||||||
|
- std.yaml # Standard extension, in malunal/yaml
|
||||||
|
srcs:
|
||||||
|
- management/**/*.sakura
|
||||||
|
- models/**/*.sakura
|
||||||
|
- pipeline/**/*.sakura
|
||||||
|
- entry.sakura
|
||||||
|
|
||||||
|
tests:
|
||||||
|
- name: blossom.units
|
||||||
|
type: program
|
||||||
|
deps: [std.args, malunal.glob, std.yaml]
|
||||||
|
srcs:
|
||||||
|
- management/**/*.sakura
|
||||||
|
- models/**/*.sakura
|
||||||
|
- pipeline/**/*.sakura
|
||||||
|
- tests/units/**/*.sakura
|
||||||
|
|
||||||
|
- name: blossom.commands
|
||||||
|
type: program
|
||||||
|
srcs:
|
||||||
|
- tests/commands/**/*.sakura
|
||||||
|
args: ["${bin}/blossom"]
|
||||||
|
|
||||||
|
package:
|
||||||
|
lic: MIT
|
||||||
|
desc: Build tool for Sakura programming language.
|
||||||
|
auth: John Christman <sorakatadzuma@gmail.com>
|
||||||
|
url: https://git.malunal.studio/malunal/blossom
|
||||||
+277
@@ -0,0 +1,277 @@
|
|||||||
|
#! @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<strview>;
|
||||||
|
@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<u32>;
|
||||||
|
@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<strview>;
|
||||||
|
}
|
||||||
|
|
||||||
|
@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<strview>;
|
||||||
|
}
|
||||||
|
|
||||||
|
@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<strview>;
|
||||||
|
@argument{ value = "ARGUMENT", rest = true, help = "What the program is given." }
|
||||||
|
var arguments: vector<strview>;
|
||||||
|
}
|
||||||
|
|
||||||
|
@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<strview>;
|
||||||
|
}
|
||||||
|
|
||||||
|
@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<strview>;
|
||||||
|
}
|
||||||
|
|
||||||
|
@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<strview>;
|
||||||
|
}
|
||||||
|
|
||||||
|
@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<strview>;
|
||||||
|
@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<strview>;
|
||||||
|
}
|
||||||
|
|
||||||
|
@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<strview>): void {
|
||||||
|
val chosen := args::parse_or_exit::<commands>(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<strview>): 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<models::setting> {
|
||||||
|
var made := vector::<models::setting>::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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,186 @@
|
|||||||
|
#! @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 one beside
|
||||||
|
#! blossom, else the first on `PATH`.
|
||||||
|
import std;
|
||||||
|
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 and its store of requirements 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> {
|
||||||
|
when os == windows {
|
||||||
|
if process::variable("APPDATA") is some(data) {
|
||||||
|
if data.length != 0 {
|
||||||
|
var at := path::parse(data) else (_) { return nothing {}; };
|
||||||
|
return at.join("blossom");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nothing {};
|
||||||
|
}
|
||||||
|
if process::variable("XDG_CONFIG_HOME") 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(".config").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 `sakura`
|
||||||
|
#! beside blossom's executable, else the first `sakura` 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("sakura").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("sakura").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 {});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,111 @@
|
|||||||
|
#! @file sources.sakura
|
||||||
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||||
|
#! @copyright Malunal Studios, LLC.
|
||||||
|
#! @license MIT
|
||||||
|
#!
|
||||||
|
#! Contains how a target's `srcs` become the files `sakura` is given: a file as
|
||||||
|
#! it is, a folder as each `.sakura` file within it, and a glob as what it
|
||||||
|
#! matches, each relative to the project, in the order written, each once.
|
||||||
|
import std;
|
||||||
|
import std::io;
|
||||||
|
import system;
|
||||||
|
import malunal::glob;
|
||||||
|
import blossom::models;
|
||||||
|
|
||||||
|
module blossom::management;
|
||||||
|
|
||||||
|
#! The files a program target is built from.
|
||||||
|
#! @param holder The project.
|
||||||
|
#! @param each The target.
|
||||||
|
#! @returns The files, relative to the project's folder, or why one of its
|
||||||
|
#! `srcs` names none.
|
||||||
|
pub def sources_of(holder: &models::project, each: &models::target): result<vector<string>, failure> {
|
||||||
|
var made := vector::<string>::new();
|
||||||
|
for &written in each.srcs {
|
||||||
|
val pattern := glob::pattern::parse(written.view()) else (e) {
|
||||||
|
return failure { status = 2, message = $"{holder.file.view()}:{each.line}: `{written.view()}` in `{each.name.view()}`'s `srcs`: {e.describe().view()}" };
|
||||||
|
};
|
||||||
|
var found := vector::<path>::new();
|
||||||
|
if pattern.is_literal {
|
||||||
|
val named := path::parse(written.view()) else (_) {
|
||||||
|
return failure { status = 2, message = $"{holder.file.view()}:{each.line}: `{written.view()}` is no path." };
|
||||||
|
};
|
||||||
|
found.append(holder.folder.join(&named));
|
||||||
|
} else {
|
||||||
|
var matched := pattern.find(&holder.folder);
|
||||||
|
if matched is error(&why) {
|
||||||
|
return failure { status = 1, message = why.describe() };
|
||||||
|
}
|
||||||
|
if matched is value(&mut paths) {
|
||||||
|
found = move(paths);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var any := false;
|
||||||
|
for &at in found {
|
||||||
|
if at.is_directory {
|
||||||
|
var within := sakura_files(at);
|
||||||
|
if within is error(&why) {
|
||||||
|
return failure { status = 1, message = why.describe() };
|
||||||
|
}
|
||||||
|
if within is value(&files) {
|
||||||
|
for &file in files {
|
||||||
|
add_relative(&mut made, &holder.folder, file);
|
||||||
|
any = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if io::exists(at) {
|
||||||
|
add_relative(&mut made, &holder.folder, at);
|
||||||
|
any = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !any {
|
||||||
|
return failure { status = 2, message = $"{holder.file.view()}:{each.line}: `{written.view()}`, in `{each.name.view()}`'s `srcs`, names no file." };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return made;
|
||||||
|
}
|
||||||
|
|
||||||
|
#! The files of a library, which `check` is given.
|
||||||
|
#! @param holder The project.
|
||||||
|
#! @param each The library.
|
||||||
|
#! @returns The files, relative to the project's folder, or why its folder
|
||||||
|
#! can't be read.
|
||||||
|
pub def library_files(holder: &models::project, each: &models::target): result<vector<string>, failure> {
|
||||||
|
val root := path::parse(each.root.view()) else (_) {
|
||||||
|
return failure { status = 2, message = $"{holder.file.view()}:{each.line}: `{each.root.view()}` is no path." };
|
||||||
|
};
|
||||||
|
val folder := holder.folder.join(&root);
|
||||||
|
if !folder.is_directory {
|
||||||
|
return failure { status = 2, message = $"{holder.file.view()}:{each.line}: `{each.name.view()}`'s folder, `{each.root.view()}`, isn't there." };
|
||||||
|
}
|
||||||
|
var made := vector::<string>::new();
|
||||||
|
var within := sakura_files(&folder);
|
||||||
|
if within is error(&why) {
|
||||||
|
return failure { status = 1, message = why.describe() };
|
||||||
|
}
|
||||||
|
if within is value(&files) {
|
||||||
|
for &file in files {
|
||||||
|
add_relative(&mut made, &holder.folder, file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return made;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Every `.sakura` file within a folder, at any depth, in order.
|
||||||
|
def sakura_files(folder: &path): result<vector<path>, glob::search_error> {
|
||||||
|
val all := glob::pattern::parse("**/*.sakura") else (_) { return vector::<path>::new(); };
|
||||||
|
return all.find(folder);
|
||||||
|
}
|
||||||
|
|
||||||
|
# Adds a file, relative to the project's folder, unless it's there already.
|
||||||
|
def add_relative(into: &mut vector<string>, folder: &path, file: &path): void {
|
||||||
|
var text := string::from(file.view(), nothing {});
|
||||||
|
if file.relative_to(folder) is some(&relative) {
|
||||||
|
text = string::from(relative.view(), nothing {});
|
||||||
|
}
|
||||||
|
for &prior in into {
|
||||||
|
return if prior == text;
|
||||||
|
}
|
||||||
|
into.append(move(text));
|
||||||
|
}
|
||||||
@@ -0,0 +1,426 @@
|
|||||||
|
#! @file workspace.sakura
|
||||||
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||||
|
#! @copyright Malunal Studios, LLC.
|
||||||
|
#! @license MIT
|
||||||
|
#!
|
||||||
|
#! Contains the workspace: the project a command is given, and every project it
|
||||||
|
#! requires, read depth first, each once, from a folder beside it or from the
|
||||||
|
#! store, where a remote is cloned the first time it's required; and every
|
||||||
|
#! library they hold, by name, which their programs read through `--lib`.
|
||||||
|
import std;
|
||||||
|
import std::io;
|
||||||
|
import std::process;
|
||||||
|
import system;
|
||||||
|
import blossom::models;
|
||||||
|
|
||||||
|
module blossom::management;
|
||||||
|
|
||||||
|
#! Why a command can't go on, and the status it ends with: 1 where something
|
||||||
|
#! failed, as a clone, and 2 where a blossomfile or the command is wrong.
|
||||||
|
pub struct failure {
|
||||||
|
pub var status: i32;
|
||||||
|
pub var message: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
#! A library some project of the workspace holds.
|
||||||
|
pub struct library {
|
||||||
|
pub var name: string; #! Its name, as `hello.geometry`.
|
||||||
|
pub var project: u64; #! The project holding it.
|
||||||
|
pub var target: u64; #! The target it is, among the project's.
|
||||||
|
}
|
||||||
|
|
||||||
|
#! The projects a command reads: the one given first, then those it requires.
|
||||||
|
pub struct workspace {
|
||||||
|
pub var projects: vector<models::project>;
|
||||||
|
pub var libraries: vector<library>;
|
||||||
|
pub var platform: string; #! What `--target` says, or empty.
|
||||||
|
pub var verbose: bool; #! Whether each command run is printed.
|
||||||
|
}
|
||||||
|
|
||||||
|
#! Reads a project and everything it requires, then indexes their libraries.
|
||||||
|
#! @param folder The project's folder.
|
||||||
|
#! @param overrides Values for its options, as `-o` gives them.
|
||||||
|
#! @param platform What `--target` says, or empty.
|
||||||
|
#! @param verbose Whether each command run is printed.
|
||||||
|
#! @returns The workspace, whose first project is the one given, or why it
|
||||||
|
#! can't be read.
|
||||||
|
pub def open_workspace(
|
||||||
|
folder: &path,
|
||||||
|
overrides: span<models::setting>,
|
||||||
|
platform: strview,
|
||||||
|
verbose: bool
|
||||||
|
): result<workspace, failure> {
|
||||||
|
var made := workspace {
|
||||||
|
projects = vector::<models::project>::new(),
|
||||||
|
libraries = vector::<library>::new(),
|
||||||
|
platform = string::from(platform, nothing {}),
|
||||||
|
verbose = verbose
|
||||||
|
};
|
||||||
|
var loaded := made.load(folder, overrides, strview::empty(), true);
|
||||||
|
if loaded is error(&mut why) {
|
||||||
|
return move(why);
|
||||||
|
}
|
||||||
|
var indexed := made.index();
|
||||||
|
if indexed is error(&mut why) {
|
||||||
|
return move(why);
|
||||||
|
}
|
||||||
|
return move(made);
|
||||||
|
}
|
||||||
|
|
||||||
|
#! The project the command was given.
|
||||||
|
#! @param this The workspace.
|
||||||
|
#! @returns It.
|
||||||
|
pub def workspace::root(&this): &models::project {
|
||||||
|
return &this.projects[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
# Reads the project in a folder, then each it requires, giving where it is
|
||||||
|
# among the projects. One read before is given again, where its options are
|
||||||
|
# the same. A project another requires reads what it requires only where its
|
||||||
|
# libraries read what it doesn't hold itself, so what only its tests or
|
||||||
|
# programs need is never fetched for those requiring it.
|
||||||
|
def workspace::load(&mut this, folder: &path, overrides: span<models::setting>, by: strview, root: bool): result<u64, failure> {
|
||||||
|
val absolute := io::absolute(folder) else (why) {
|
||||||
|
return failure { status = 2, message = $"`{folder.view()}` can't be found: {why}." };
|
||||||
|
};
|
||||||
|
val file := absolute.join("blossomfile");
|
||||||
|
val text := io::read_file(&file) else (why) {
|
||||||
|
val whose := by.length != 0 ? $", which {by} requires," : string::new(nothing {});
|
||||||
|
return failure { status = 2, message = $"`{absolute.view()}`{whose.view()} holds no blossomfile to read: {why}." };
|
||||||
|
};
|
||||||
|
var read := models::read_project(text.view(), &file, overrides, this.platform.view());
|
||||||
|
if read is error(&problem) {
|
||||||
|
return failure { status = 2, message = problem.describe() };
|
||||||
|
}
|
||||||
|
if read is not value(&mut made) {
|
||||||
|
return failure { status = 2, message = string::from("The blossomfile couldn't be read.", nothing {}) };
|
||||||
|
}
|
||||||
|
|
||||||
|
# A project is read once, and given one set of values for its options, since
|
||||||
|
# each set builds its own outputs.
|
||||||
|
for k in 0..this.projects.length {
|
||||||
|
val prior := &this.projects[k];
|
||||||
|
continue if prior.folder != made.folder;
|
||||||
|
if !same_options(prior.options.view(), made.options.view()) {
|
||||||
|
return failure { status = 2, message = $"{made.identity.view()} is required with two sets of option values: {described(prior.options.view()).view()}, and {described(made.options.view()).view()} by {by}." };
|
||||||
|
}
|
||||||
|
return k;
|
||||||
|
}
|
||||||
|
|
||||||
|
val index := this.projects.length;
|
||||||
|
this.projects.append(move(made));
|
||||||
|
return index if !root && !needs_requirements(&this.projects[index]);
|
||||||
|
|
||||||
|
# Each requirement is taken out before the next is read, since reading one
|
||||||
|
# adds projects, which may move those before it.
|
||||||
|
val count := this.projects[index].requires.length;
|
||||||
|
for k in 0..count {
|
||||||
|
val required := &this.projects[index].requires[k];
|
||||||
|
val remote := string::copy_of(&required.remote);
|
||||||
|
val branch := string::copy_of(&required.branch);
|
||||||
|
val local := required.local;
|
||||||
|
val line := required.line;
|
||||||
|
var options := vector::<models::setting>::new();
|
||||||
|
for &each in required.options {
|
||||||
|
options.append(models::setting { name = string::copy_of(&each.name), value = string::copy_of(&each.value) });
|
||||||
|
}
|
||||||
|
val requirer := this.projects[index].identity;
|
||||||
|
|
||||||
|
var found := path::new();
|
||||||
|
if local {
|
||||||
|
val named := path::parse(remote.view()) else (_) {
|
||||||
|
return failure { status = 2, message = $"{this.projects[index].file.view()}:{line}: `{remote.view()}` is no folder." };
|
||||||
|
};
|
||||||
|
found = this.projects[index].folder.join(&named);
|
||||||
|
} else {
|
||||||
|
var fetched := this.fetch(remote.view(), branch.view());
|
||||||
|
if fetched is error(&mut why) {
|
||||||
|
return move(why);
|
||||||
|
}
|
||||||
|
found = fetched.unwrap();
|
||||||
|
}
|
||||||
|
var loaded := this.load(&found, options.view(), requirer.view(), false);
|
||||||
|
if loaded is error(&mut why) {
|
||||||
|
return move(why);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Finds a remote's clone in the store, cloning it where it's missing: into a
|
||||||
|
# folder beside it first, renamed once the clone is whole, so one stopped
|
||||||
|
# halfway is never taken for a clone.
|
||||||
|
def workspace::fetch(&mut this, remote: strview, branch: strview): result<path, failure> {
|
||||||
|
val folder := config_folder() else {
|
||||||
|
return failure { status = 2, message = string::from("There's no store to clone into: neither `XDG_CONFIG_HOME` nor `HOME` is set.", nothing {}) };
|
||||||
|
};
|
||||||
|
val key := store_key(remote, branch);
|
||||||
|
val stored := path::parse(key.view()) else (_) {
|
||||||
|
return failure { status = 2, message = $"`{remote}` can't name a folder of the store." };
|
||||||
|
};
|
||||||
|
val into := folder.join(&stored);
|
||||||
|
return into if io::exists(&into);
|
||||||
|
|
||||||
|
val partial := folder.join($"{key.view()}.partial".view());
|
||||||
|
_ = remove_tree(&partial);
|
||||||
|
if partial.parent() is some(&above) {
|
||||||
|
_ = io::create_directories(above);
|
||||||
|
}
|
||||||
|
io::println($"Cloning {remote}@{branch}".view());
|
||||||
|
var git := process::command::new("git");
|
||||||
|
git.arguments(["clone", "--quiet", "-b", branch, "--depth", "1", remote, partial.view()]);
|
||||||
|
if this.verbose {
|
||||||
|
io::println($"git clone --quiet -b {branch} --depth 1 {remote} {partial.view()}".view());
|
||||||
|
}
|
||||||
|
val status := git.run() else (why) {
|
||||||
|
return failure { status = 1, message = $"git can't be run, to clone `{remote}`: {why}." };
|
||||||
|
};
|
||||||
|
if !status.succeeded {
|
||||||
|
_ = remove_tree(&partial);
|
||||||
|
return failure { status = 1, message = $"Cloning {remote}@{branch} failed." };
|
||||||
|
}
|
||||||
|
if io::rename(&partial, &into) is error(why) {
|
||||||
|
return failure { status = 1, message = $"The clone of `{remote}` can't be moved to `{into.view()}`: {why}." };
|
||||||
|
}
|
||||||
|
return into;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Whether a project's libraries read one it doesn't hold, which one it requires
|
||||||
|
# must, so what it requires is read.
|
||||||
|
def needs_requirements(holder: &models::project): bool {
|
||||||
|
for &each in holder.targets {
|
||||||
|
continue if each.kind != models::target_kind::library;
|
||||||
|
for &name in each.deps {
|
||||||
|
continue if name == "std";
|
||||||
|
var own := false;
|
||||||
|
for &other in holder.targets {
|
||||||
|
own = own || (other.kind == models::target_kind::library && other.name == name);
|
||||||
|
}
|
||||||
|
return true if !own;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Indexes every library of the workspace's projects by name, refusing a name
|
||||||
|
# two projects give.
|
||||||
|
def workspace::index(&mut this): result<void, failure> {
|
||||||
|
for p in 0..this.projects.length {
|
||||||
|
val holder := &this.projects[p];
|
||||||
|
for t in 0..holder.targets.length {
|
||||||
|
val each := &holder.targets[t];
|
||||||
|
continue if each.kind != models::target_kind::library;
|
||||||
|
for &prior in this.libraries {
|
||||||
|
if prior.name == each.name {
|
||||||
|
return failure { status = 2, message = $"`{each.name.view()}` is a library of both {this.projects[prior.project].identity.view()} and {holder.identity.view()}." };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.libraries.append(library { name = string::copy_of(&each.name), project = p, target = t });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
#! Finds a library by its name.
|
||||||
|
#! @param this The workspace.
|
||||||
|
#! @param name The name.
|
||||||
|
#! @returns Where it is among the libraries, or `nothing`.
|
||||||
|
pub def workspace::library_named(&this, name: strview): maybe<u64> {
|
||||||
|
for k in 0..this.libraries.length {
|
||||||
|
return k if this.libraries[k].name.view() == name;
|
||||||
|
}
|
||||||
|
return nothing {};
|
||||||
|
}
|
||||||
|
|
||||||
|
#! Gathers every library a target reads, through the `deps` of each, each once:
|
||||||
|
#! those of its own project, and those other projects export.
|
||||||
|
#! @param this The workspace.
|
||||||
|
#! @param project The project holding the target.
|
||||||
|
#! @param deps The target's `deps`.
|
||||||
|
#! @param who The target's name, for what's said where one is wrong.
|
||||||
|
#! @param into Where each library's place among the libraries is added.
|
||||||
|
#! @returns Why one can't be read, where one can't.
|
||||||
|
pub def workspace::reach(
|
||||||
|
&this,
|
||||||
|
project: u64,
|
||||||
|
deps: span<string>,
|
||||||
|
who: strview,
|
||||||
|
into: &mut vector<u64>
|
||||||
|
): result<void, failure> {
|
||||||
|
for &name in deps {
|
||||||
|
continue if name == "std";
|
||||||
|
val found := this.library_named(name.view()) else {
|
||||||
|
return failure { status = 2, message = $"`{name.view()}`, which `{who}` reads, is no library of {this.projects[project].identity.view()} or of what it requires." };
|
||||||
|
};
|
||||||
|
val held := &this.libraries[found];
|
||||||
|
if held.project != project && !this.projects[held.project].exported(name.view()) {
|
||||||
|
return failure { status = 2, message = $"`{name.view()}`, which `{who}` reads, isn't exported by {this.projects[held.project].identity.view()}." };
|
||||||
|
}
|
||||||
|
var seen := false;
|
||||||
|
for &prior in into {
|
||||||
|
seen = seen || prior == found;
|
||||||
|
}
|
||||||
|
continue if seen;
|
||||||
|
into.append(found);
|
||||||
|
val target := &this.projects[held.project].targets[held.target];
|
||||||
|
var reached := this.reach(held.project, target.deps.view(), name.view(), into);
|
||||||
|
if reached is error(&mut why) {
|
||||||
|
return move(why);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
#! The folder a library's modules are in.
|
||||||
|
#! @param this The workspace.
|
||||||
|
#! @param index Where the library is among the libraries.
|
||||||
|
#! @param from The project whose folder `sakura` runs in, which its own
|
||||||
|
#! libraries are written relative to.
|
||||||
|
#! @returns The folder: relative to that project's where it's that project's
|
||||||
|
#! own, and absolute otherwise.
|
||||||
|
pub def workspace::library_folder(&this, index: u64, from: u64): path {
|
||||||
|
val held := &this.libraries[index];
|
||||||
|
val holder := &this.projects[held.project];
|
||||||
|
val root := path::parse(holder.targets[held.target].root.view()) else (_) { return path::new(); };
|
||||||
|
if held.project == from {
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
return holder.folder.join(&root);
|
||||||
|
}
|
||||||
|
|
||||||
|
#! The `--lib` a library is given by: its module, with `::` for `.`, and its
|
||||||
|
#! folder.
|
||||||
|
#! @param this The workspace.
|
||||||
|
#! @param index Where the library is among the libraries.
|
||||||
|
#! @param from The project whose folder `sakura` runs in.
|
||||||
|
#! @returns As `hello::geometry=hello.geometry`.
|
||||||
|
pub def workspace::lib_option(&this, index: u64, from: u64): string {
|
||||||
|
var made := string::new(nothing {});
|
||||||
|
for k in 0..this.libraries[index].name.length {
|
||||||
|
val byte := this.libraries[index].name[k];
|
||||||
|
if byte == '.' {
|
||||||
|
made.append("::");
|
||||||
|
} else {
|
||||||
|
made.push(byte);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
made.push('=');
|
||||||
|
made.append(this.library_folder(index, from).view());
|
||||||
|
return made;
|
||||||
|
}
|
||||||
|
|
||||||
|
#! The folder of the store a remote's branch is cloned into: its host and path,
|
||||||
|
#! then `@` and the branch, as `git.malunal.studio/malunal/json@v1.0.0`, with
|
||||||
|
#! any scheme, user, and `.git` left out, and anything that's no name's taken
|
||||||
|
#! out, so no remote reaches outside the store.
|
||||||
|
#! @param remote The remote, as `https://host/path`, `git@host:path`, or
|
||||||
|
#! `file:///path`.
|
||||||
|
#! @param branch The branch or tag.
|
||||||
|
#! @returns The folder, relative to the store.
|
||||||
|
pub def store_key(remote: strview, branch: strview): string {
|
||||||
|
var rest := remote;
|
||||||
|
var scheme := strview::empty();
|
||||||
|
if rest.find("://") is some(at) {
|
||||||
|
scheme = rest.substr(0, at);
|
||||||
|
rest = rest.substr(at + 3, rest.length - at - 3);
|
||||||
|
} else if rest.find(":") is some(at) {
|
||||||
|
# `user@host:path`, as ssh writes it.
|
||||||
|
var host := rest.substr(0, at);
|
||||||
|
if host.find("@") is some(user) {
|
||||||
|
host = host.substr(user + 1, host.length - user - 1);
|
||||||
|
}
|
||||||
|
var joined := string::from(host, nothing {});
|
||||||
|
joined.push('/');
|
||||||
|
joined.append(rest.substr(at + 1, rest.length - at - 1));
|
||||||
|
return finished_key(joined.view(), strview::empty(), branch);
|
||||||
|
}
|
||||||
|
if scheme != "file" {
|
||||||
|
val slash := rest.find("/") else { return finished_key(rest, scheme, branch); };
|
||||||
|
var host := rest.substr(0, slash);
|
||||||
|
if host.find("@") is some(user) {
|
||||||
|
host = host.substr(user + 1, host.length - user - 1);
|
||||||
|
var joined := string::from(host, nothing {});
|
||||||
|
joined.append(rest.substr(slash, rest.length - slash));
|
||||||
|
return finished_key(joined.view(), scheme, branch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return finished_key(rest, scheme, branch);
|
||||||
|
}
|
||||||
|
|
||||||
|
# A store's folder from a remote's host and path: each segment a name, `.` and
|
||||||
|
# `..` left out, and `.git` taken off the end, then `@` and the branch.
|
||||||
|
def finished_key(text: strview, scheme: strview, branch: strview): string {
|
||||||
|
var made := string::new(nothing {});
|
||||||
|
if scheme == "file" {
|
||||||
|
made.append("file");
|
||||||
|
}
|
||||||
|
for part in text.split('/') {
|
||||||
|
continue if part.length == 0 || part == "." || part == "..";
|
||||||
|
if made.length != 0 {
|
||||||
|
made.push('/');
|
||||||
|
}
|
||||||
|
for k in 0..part.length {
|
||||||
|
val byte := part[k];
|
||||||
|
made.push(byte.is_alnum || byte == '.' || byte == '-' || byte == '_' || byte == '+' ? byte : '_');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if made.view().ends_with(".git") {
|
||||||
|
made.resize(made.length - 4);
|
||||||
|
}
|
||||||
|
made.push('@');
|
||||||
|
for k in 0..branch.length {
|
||||||
|
val byte := branch[k];
|
||||||
|
made.push(byte.is_alnum || byte == '.' || byte == '-' || byte == '_' || byte == '+' ? byte : '_');
|
||||||
|
}
|
||||||
|
return made;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Whether two projects' option values are the same.
|
||||||
|
def same_options(a: span<models::setting>, b: span<models::setting>): bool {
|
||||||
|
return false if a.length != b.length;
|
||||||
|
for k in 0..a.length {
|
||||||
|
return false if a[k].name != b[k].name || a[k].value != b[k].value;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Option values, as `level=2 debug=false`.
|
||||||
|
def described(options: span<models::setting>): string {
|
||||||
|
var made := string::new(nothing {});
|
||||||
|
for &each in options {
|
||||||
|
if made.length != 0 {
|
||||||
|
made.push(' ');
|
||||||
|
}
|
||||||
|
made.append($"{each.name.view()}={each.value.view()}".view());
|
||||||
|
}
|
||||||
|
if made.length == 0 {
|
||||||
|
made.append("none");
|
||||||
|
}
|
||||||
|
return made;
|
||||||
|
}
|
||||||
|
|
||||||
|
#! Removes a file, or a folder and everything in it. Links are removed, not
|
||||||
|
#! followed.
|
||||||
|
#! @param at What to remove.
|
||||||
|
#! @returns Why it couldn't all be removed, where it couldn't.
|
||||||
|
pub def remove_tree(at: &path): result<void, io_error> {
|
||||||
|
val found := io::info(at, false) else (why) {
|
||||||
|
if why == io_error::not_found {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
return why;
|
||||||
|
};
|
||||||
|
if found.kind == file_kind::directory {
|
||||||
|
var names := vector::<path>::new();
|
||||||
|
var listing := io::directory::open(at) else (why) { return why; };
|
||||||
|
while listing.next() is some(entry) {
|
||||||
|
val name := path::parse(entry.name) else (_) { return io_error::invalid; };
|
||||||
|
names.append(at.join(&name));
|
||||||
|
}
|
||||||
|
for &each in names {
|
||||||
|
var removed := remove_tree(each);
|
||||||
|
return removed if removed is error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return io::remove(at);
|
||||||
|
}
|
||||||
@@ -0,0 +1,140 @@
|
|||||||
|
#! @file project.sakura
|
||||||
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||||
|
#! @copyright Malunal Studios, LLC.
|
||||||
|
#! @license MIT
|
||||||
|
#!
|
||||||
|
#! Contains what a blossomfile says of a project: who it is, its options, what
|
||||||
|
#! it requires, its targets and tests, what it exports, how it installs, and how
|
||||||
|
#! it is packaged.
|
||||||
|
import std;
|
||||||
|
import std::io;
|
||||||
|
|
||||||
|
module blossom::models;
|
||||||
|
|
||||||
|
#! An option of a project, and the value it has, as the text written.
|
||||||
|
pub struct setting {
|
||||||
|
pub var name: string;
|
||||||
|
pub var value: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
#! What a target is.
|
||||||
|
pub enum target_kind: u8 {
|
||||||
|
program, #! A program, built into an executable.
|
||||||
|
library, #! A folder of modules, which programs read through `--lib`.
|
||||||
|
}
|
||||||
|
|
||||||
|
#! Where a test runs.
|
||||||
|
pub enum runner: u8 {
|
||||||
|
machine, #! On the machine, by `sakura run`.
|
||||||
|
native, #! Built into an executable, which is then run.
|
||||||
|
}
|
||||||
|
|
||||||
|
#! A target or a test of a project.
|
||||||
|
pub struct target {
|
||||||
|
pub var name: string;
|
||||||
|
pub var kind: target_kind;
|
||||||
|
pub var level: u32 = 2; #! `-O`, from 0 to 2.
|
||||||
|
pub var petal: bool; #! Whether it's built with `-g`.
|
||||||
|
pub var system: string; #! What it's built for, as `windows-x86_64`, or empty for the system blossom runs on.
|
||||||
|
pub var deps: vector<string>; #! The libraries it reads, by name, `std` among them or not.
|
||||||
|
pub var srcs: vector<string>; #! Its files, folders, and globs, relative to the project.
|
||||||
|
pub var opts: vector<string>; #! What's given to `sakura` as written.
|
||||||
|
pub var root: string; #! A library's folder, relative to the project.
|
||||||
|
pub var runs: runner; #! Where a test runs.
|
||||||
|
pub var checks: bool; #! Whether a test's memory is checked as it runs.
|
||||||
|
pub var args: vector<string>; #! What a test is given.
|
||||||
|
pub var line: u32; #! Where it's written.
|
||||||
|
}
|
||||||
|
|
||||||
|
#! A project another requires.
|
||||||
|
pub struct requirement {
|
||||||
|
pub var remote: string; #! A remote's address, or a folder relative to the blossomfile.
|
||||||
|
pub var branch: string; #! The branch or tag cloned, `main` unless written.
|
||||||
|
pub var local: bool; #! Whether it's a folder rather than a remote.
|
||||||
|
pub var options: vector<setting>; #! The values it's given for its options.
|
||||||
|
pub var line: u32; #! Where it's written.
|
||||||
|
}
|
||||||
|
|
||||||
|
#! A step of installing, beside what's installed by default.
|
||||||
|
pub struct step {
|
||||||
|
pub var tag: string; #! What it does, as `!include`.
|
||||||
|
pub var source: string; #! What it takes, relative to the project.
|
||||||
|
pub var dest: string; #! Where it goes under the prefix, or empty for the source's own place.
|
||||||
|
pub var line: u32; #! Where it's written.
|
||||||
|
}
|
||||||
|
|
||||||
|
#! How a project is described in its packages.
|
||||||
|
pub struct package_info {
|
||||||
|
pub var lic: string; #! Its license, as `MIT`.
|
||||||
|
pub var desc: string; #! What it is, in a line.
|
||||||
|
pub var auth: string; #! Who made it, as `Name <mail>`.
|
||||||
|
pub var url: string; #! Where it lives.
|
||||||
|
}
|
||||||
|
|
||||||
|
#! A project, as its blossomfile says it.
|
||||||
|
pub struct project {
|
||||||
|
pub var name: string;
|
||||||
|
pub var gpid: string;
|
||||||
|
pub var semv: string;
|
||||||
|
pub var folder: path; #! The folder holding its blossomfile, absolute.
|
||||||
|
pub var file: path; #! Its blossomfile.
|
||||||
|
pub var options: vector<setting>; #! Its options' values, in the order declared.
|
||||||
|
pub var requires: vector<requirement>;
|
||||||
|
pub var targets: vector<target>;
|
||||||
|
pub var tests: vector<target>;
|
||||||
|
pub var exports: vector<string>; #! The libraries other projects may read.
|
||||||
|
pub var install: vector<step>;
|
||||||
|
pub var package: package_info;
|
||||||
|
pub var key: string; #! Its output folder's name in `.blossom`.
|
||||||
|
}
|
||||||
|
|
||||||
|
#! Who the project is, as `malunal/hello@1.0.0`.
|
||||||
|
#! @param this The project.
|
||||||
|
#! @returns Its group, name, and version.
|
||||||
|
pub val project::identity: string {
|
||||||
|
get => $"{this.gpid}/{this.name}@{this.semv}";
|
||||||
|
}
|
||||||
|
|
||||||
|
#! The folder its outputs for these option values go to.
|
||||||
|
#! @param this The project.
|
||||||
|
#! @returns `.blossom/<key>` within its folder.
|
||||||
|
pub val project::output: path {
|
||||||
|
get => this.folder.join(".blossom").join(this.key.view());
|
||||||
|
}
|
||||||
|
|
||||||
|
#! Finds a target or test by its name.
|
||||||
|
#! @param this The project.
|
||||||
|
#! @param name The name.
|
||||||
|
#! @returns Where it is among the targets, then the tests after them, or
|
||||||
|
#! `nothing` where neither has it.
|
||||||
|
pub def project::find(&this, name: strview): maybe<u64> {
|
||||||
|
for k in 0..this.targets.length {
|
||||||
|
return k if this.targets[k].name.view() == name;
|
||||||
|
}
|
||||||
|
for k in 0..this.tests.length {
|
||||||
|
return this.targets.length + k if this.tests[k].name.view() == name;
|
||||||
|
}
|
||||||
|
return nothing {};
|
||||||
|
}
|
||||||
|
|
||||||
|
#! A target, or a test after the targets, by where `find` says it is.
|
||||||
|
#! @param this The project.
|
||||||
|
#! @param index Where it is.
|
||||||
|
#! @returns It.
|
||||||
|
pub def project::at(&this, index: u64): &target {
|
||||||
|
if index < this.targets.length {
|
||||||
|
return &this.targets[index];
|
||||||
|
}
|
||||||
|
return &this.tests[index - this.targets.length];
|
||||||
|
}
|
||||||
|
|
||||||
|
#! Whether the project lets others read a library.
|
||||||
|
#! @param this The project.
|
||||||
|
#! @param name The library's name.
|
||||||
|
#! @returns Whether `exports` names it.
|
||||||
|
pub def project::exported(&this, name: strview): bool {
|
||||||
|
for &each in this.exports {
|
||||||
|
return true if each.view() == name;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
@@ -0,0 +1,638 @@
|
|||||||
|
#! @file reading.sakura
|
||||||
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||||
|
#! @copyright Malunal Studios, LLC.
|
||||||
|
#! @license MIT
|
||||||
|
#!
|
||||||
|
#! Contains how a blossomfile is read into a project: from YAML's document,
|
||||||
|
#! whose values keep their lines and tags, with every key checked against those
|
||||||
|
#! a mapping takes, a key written twice refused, and each `${NAME}` replaced by
|
||||||
|
#! the option's value as the text is read.
|
||||||
|
import std;
|
||||||
|
import std::io;
|
||||||
|
import std::yaml;
|
||||||
|
|
||||||
|
module blossom::models;
|
||||||
|
|
||||||
|
#! The systems a target may be built for, as `--target` names them.
|
||||||
|
pub val platforms: strview[2] = ["linux-x86_64", "windows-x86_64"];
|
||||||
|
|
||||||
|
#! Why a blossomfile couldn't be read: where, and what's wrong.
|
||||||
|
pub struct problem {
|
||||||
|
pub var file: string; #! The blossomfile, as its path is written.
|
||||||
|
pub var line: u32; #! The line, counted from 1, or 0 where it's no line's.
|
||||||
|
pub var message: string; #! What's wrong, in a sentence.
|
||||||
|
}
|
||||||
|
|
||||||
|
#! Says what's wrong, after where.
|
||||||
|
#! @param this The problem.
|
||||||
|
#! @returns The sentence, as `blossomfile:4: ...`.
|
||||||
|
pub def problem::describe(&this): string {
|
||||||
|
if this.line == 0 {
|
||||||
|
return $"{this.file.view()}: {this.message.view()}";
|
||||||
|
}
|
||||||
|
return $"{this.file.view()}:{this.line}: {this.message.view()}";
|
||||||
|
}
|
||||||
|
|
||||||
|
#! Reads a blossomfile.
|
||||||
|
#! @param text Its text.
|
||||||
|
#! @param file Its path, absolute.
|
||||||
|
#! @param overrides Values for its options, as `-o` gives them, which must
|
||||||
|
#! each name one it declares.
|
||||||
|
#! @param platform What every target is built for, as `--target` says, or
|
||||||
|
#! empty, which goes into its output folder's name.
|
||||||
|
#! @returns The project, or the first problem the file has.
|
||||||
|
pub def read_project(
|
||||||
|
text: strview,
|
||||||
|
file: &path,
|
||||||
|
overrides: span<setting>,
|
||||||
|
platform: strview
|
||||||
|
): result<project, problem> {
|
||||||
|
var at := reading { file = file.view(), options = vector::<setting>::new() };
|
||||||
|
var folder := file.parent() else {
|
||||||
|
return problem { file = string::from(file.view(), nothing {}), message = string::from("A blossomfile is in a folder.", nothing {}) };
|
||||||
|
};
|
||||||
|
var made := project { file = path::copy_of(file), folder = move(folder) };
|
||||||
|
at.read(text, &mut made, overrides, platform);
|
||||||
|
if at.failed {
|
||||||
|
return move(at.problem);
|
||||||
|
}
|
||||||
|
return move(made);
|
||||||
|
}
|
||||||
|
|
||||||
|
# What reading a blossomfile goes through: its file, the options so far, which
|
||||||
|
# `${NAME}` is replaced by, and the first problem found.
|
||||||
|
struct reading {
|
||||||
|
var file: strview;
|
||||||
|
var options: vector<setting>;
|
||||||
|
var failed: bool;
|
||||||
|
var problem: problem;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Notes the first problem, at a line.
|
||||||
|
def reading::fail(&mut this, line: u32, message: string): void {
|
||||||
|
return if this.failed;
|
||||||
|
this.failed = true;
|
||||||
|
this.problem = problem { file = string::from(this.file, nothing {}), line = line, message = move(message) };
|
||||||
|
}
|
||||||
|
|
||||||
|
# Reads the whole file into a project.
|
||||||
|
def reading::read(&mut this, text: strview, made: &mut project, overrides: span<setting>, platform: strview): void {
|
||||||
|
val doc := yaml::parse(text) else (e) {
|
||||||
|
this.fail(e.line, string::from(yaml::describe(e.kind), nothing {}));
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
val root := doc.root;
|
||||||
|
if root.is_null {
|
||||||
|
this.fail(0, string::from("The blossomfile is empty: it needs `name`, `gpid`, and `semv`.", nothing {}));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
return if !this.keys(&root, ["name", "gpid", "semv", "options", "requires", "targets", "tests", "exports", "install", "package"], "the blossomfile");
|
||||||
|
|
||||||
|
# Who it is comes first, then its options, which every text after may use.
|
||||||
|
made.name = this.needed(&root, "name");
|
||||||
|
made.gpid = this.needed(&root, "gpid");
|
||||||
|
made.semv = this.needed(&root, "semv");
|
||||||
|
return if this.failed;
|
||||||
|
if !is_version(made.semv.view()) {
|
||||||
|
this.fail(root.get("semv").some.place.line, $"`{made.semv.view()}` is not a version, as `1.2.0` or `1.2.0-alpha`.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if root.get("options") is some(options) {
|
||||||
|
return if !this.keys(&options, span::<strview>::empty(), "options");
|
||||||
|
for each in options.members() {
|
||||||
|
if each.name == "bin" {
|
||||||
|
this.fail(each.key.place.line, string::from("`bin` is blossom's: it names the folder of the programs built.", nothing {}));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var value := this.text(&each.value, "An option's value");
|
||||||
|
for &given in overrides {
|
||||||
|
if given.name.view() == each.name {
|
||||||
|
value = this.expanded(given.value.view(), 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.options.append(setting { name = string::from(each.name, nothing {}), value = move(value) });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for &given in overrides {
|
||||||
|
var declared := false;
|
||||||
|
for &each in this.options {
|
||||||
|
declared = declared || each.name == given.name;
|
||||||
|
}
|
||||||
|
if !declared {
|
||||||
|
this.fail(0, $"`{given.name.view()}`, given a value, names no option of {made.identity.view()}.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for &each in this.options {
|
||||||
|
made.options.append(setting { name = string::copy_of(&each.name), value = string::copy_of(&each.value) });
|
||||||
|
}
|
||||||
|
made.key = output_key(made.identity.view(), made.options.view(), platform);
|
||||||
|
val bin := made.output.join("bin");
|
||||||
|
this.options.append(setting { name = string::from("bin", nothing {}), value = string::from(bin.view(), nothing {}) });
|
||||||
|
return if this.failed;
|
||||||
|
|
||||||
|
if root.get("requires") is some(requires) {
|
||||||
|
for each in this.list(&requires, "`requires`") {
|
||||||
|
made.requires.append(this.requirement(&each));
|
||||||
|
return if this.failed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if root.get("targets") is some(targets) {
|
||||||
|
for each in this.list(&targets, "`targets`") {
|
||||||
|
made.targets.append(this.target(&each, false));
|
||||||
|
return if this.failed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if root.get("tests") is some(tests) {
|
||||||
|
for each in this.list(&tests, "`tests`") {
|
||||||
|
made.tests.append(this.target(&each, true));
|
||||||
|
return if this.failed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if root.get("exports") is some(exports) {
|
||||||
|
made.exports = this.texts(&exports, "`exports`");
|
||||||
|
}
|
||||||
|
if root.get("install") is some(install) {
|
||||||
|
for each in this.list(&install, "`install`") {
|
||||||
|
made.install.append(this.step(&each));
|
||||||
|
return if this.failed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if root.get("package") is some(package) {
|
||||||
|
return if !this.keys(&package, ["lic", "desc", "auth", "url"], "`package`");
|
||||||
|
made.package = package_info {
|
||||||
|
lic = this.optional(&package, "lic"),
|
||||||
|
desc = this.optional(&package, "desc"),
|
||||||
|
auth = this.optional(&package, "auth"),
|
||||||
|
url = this.optional(&package, "url")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return if this.failed;
|
||||||
|
this.validate(made, &root);
|
||||||
|
}
|
||||||
|
|
||||||
|
# Reads a requirement: its text alone, as `REMOTE@REF` or a folder, or the block
|
||||||
|
# of `remote`, `branch`, and `options`.
|
||||||
|
def reading::requirement(&mut this, value: &yaml::value): requirement {
|
||||||
|
val line := value.place.line;
|
||||||
|
if value.kind != yaml::kind::mapping {
|
||||||
|
val text := this.text(value, "A requirement");
|
||||||
|
var made := requirement { line = line };
|
||||||
|
if is_folder_text(text.view()) {
|
||||||
|
made.remote = string::copy_of(&text);
|
||||||
|
made.local = true;
|
||||||
|
return made;
|
||||||
|
}
|
||||||
|
val split := ref_at(text.view());
|
||||||
|
made.remote = string::from(text.view().substr(0, split), nothing {});
|
||||||
|
made.branch = split < text.length
|
||||||
|
? string::from(text.view().substr(split + 1, text.length - split - 1), nothing {})
|
||||||
|
: string::from("main", nothing {});
|
||||||
|
if made.remote.length == 0 || made.branch.length == 0 {
|
||||||
|
this.fail(line, $"`{text.view()}` is no requirement: write `REMOTE@REF` or a folder, as `../shapes`.");
|
||||||
|
}
|
||||||
|
return made;
|
||||||
|
}
|
||||||
|
|
||||||
|
var made := requirement { line = line };
|
||||||
|
return made if !this.keys(value, ["remote", "branch", "options"], "a requirement");
|
||||||
|
made.remote = this.needed(value, "remote");
|
||||||
|
made.local = is_folder_text(made.remote.view());
|
||||||
|
made.branch = string::from("main", nothing {});
|
||||||
|
if value.get("branch") is some(branch) {
|
||||||
|
made.branch = this.text(&branch, "`branch`");
|
||||||
|
if made.local {
|
||||||
|
this.fail(branch.place.line, string::from("A folder takes no `branch`.", nothing {}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if value.get("options") is some(options) {
|
||||||
|
return made if !this.keys(&options, span::<strview>::empty(), "a requirement's options");
|
||||||
|
for each in options.members() {
|
||||||
|
made.options.append(setting { name = string::from(each.name, nothing {}), value = this.text(&each.value, "An option's value") });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return made;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Reads a target, or a test, which takes the keys of tests too.
|
||||||
|
def reading::target(&mut this, value: &yaml::value, test: bool): target {
|
||||||
|
var made := target { line = value.place.line };
|
||||||
|
if test {
|
||||||
|
return made if !this.keys(value, ["name", "type", "level", "petal", "target", "deps", "srcs", "opts", "runs", "checks", "args"], "a test");
|
||||||
|
} else {
|
||||||
|
return made if !this.keys(value, ["name", "type", "level", "petal", "target", "deps", "srcs", "opts", "root"], "a target");
|
||||||
|
}
|
||||||
|
|
||||||
|
made.name = this.needed(value, "name");
|
||||||
|
val kind := this.needed(value, "type");
|
||||||
|
return made if this.failed;
|
||||||
|
if kind == "program" {
|
||||||
|
made.kind = target_kind::program;
|
||||||
|
} else if kind == "library" && !test {
|
||||||
|
made.kind = target_kind::library;
|
||||||
|
} else {
|
||||||
|
val line := value.get("type").some.place.line;
|
||||||
|
this.fail(line, test ? $"A test's `type` is `program`, not `{kind.view()}`." : $"`type` is `program` or `library`, not `{kind.view()}`.");
|
||||||
|
return made;
|
||||||
|
}
|
||||||
|
|
||||||
|
if made.kind == target_kind::library {
|
||||||
|
for key in ["level", "petal", "target", "srcs", "opts"] {
|
||||||
|
if value.get(key) is some(given) {
|
||||||
|
this.fail(given.place.line, $"A library takes no `{key}`: it's read by the programs that import it.");
|
||||||
|
return made;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !is_library_name(made.name.view()) {
|
||||||
|
this.fail(made.line, $"`{made.name.view()}` names no module: a library's name is its module's, with `.` for `::`, as `hello.geometry`.");
|
||||||
|
return made;
|
||||||
|
}
|
||||||
|
made.root = $"./{made.name.view()}";
|
||||||
|
if value.get("root") is some(root) {
|
||||||
|
made.root = this.text(&root, "`root`");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if value.get("root") is some(root) {
|
||||||
|
this.fail(root.place.line, string::from("Only a library takes a `root`.", nothing {}));
|
||||||
|
return made;
|
||||||
|
}
|
||||||
|
if !is_file_name(made.name.view()) {
|
||||||
|
this.fail(made.line, $"`{made.name.view()}` can't name a program's file: write letters, digits, `.`, `_`, or `-`.");
|
||||||
|
return made;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if value.get("level") is some(level) {
|
||||||
|
val text := this.text(&level, "`level`");
|
||||||
|
if text != "0" && text != "1" && text != "2" {
|
||||||
|
this.fail(level.place.line, $"`level` is 0, 1, or 2, not `{text.view()}`.");
|
||||||
|
return made;
|
||||||
|
}
|
||||||
|
made.level = (text[0] - '0') as u32;
|
||||||
|
}
|
||||||
|
if value.get("petal") is some(petal) {
|
||||||
|
made.petal = this.boolean(&petal, "`petal`");
|
||||||
|
}
|
||||||
|
if value.get("target") is some(system) {
|
||||||
|
made.system = this.text(&system, "`target`");
|
||||||
|
if !is_platform(made.system.view()) {
|
||||||
|
this.fail(system.place.line, $"`{made.system.view()}` is no target: write `linux-x86_64` or `windows-x86_64`.");
|
||||||
|
return made;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if value.get("deps") is some(deps) {
|
||||||
|
made.deps = this.texts(&deps, "`deps`");
|
||||||
|
}
|
||||||
|
if value.get("srcs") is some(srcs) {
|
||||||
|
made.srcs = this.texts(&srcs, "`srcs`");
|
||||||
|
}
|
||||||
|
if value.get("opts") is some(opts) {
|
||||||
|
made.opts = this.texts(&opts, "`opts`");
|
||||||
|
}
|
||||||
|
if value.get("args") is some(args) {
|
||||||
|
made.args = this.texts(&args, "`args`");
|
||||||
|
}
|
||||||
|
|
||||||
|
made.checks = true;
|
||||||
|
if value.get("runs") is some(runs) {
|
||||||
|
val text := this.text(&runs, "`runs`");
|
||||||
|
if text == "native" {
|
||||||
|
made.runs = runner::native;
|
||||||
|
made.checks = false;
|
||||||
|
} else if text != "machine" {
|
||||||
|
this.fail(runs.place.line, $"`runs` is `machine` or `native`, not `{text.view()}`.");
|
||||||
|
return made;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if value.get("checks") is some(checks) {
|
||||||
|
made.checks = this.boolean(&checks, "`checks`");
|
||||||
|
}
|
||||||
|
|
||||||
|
# A program needs files, which a test finds in `tests/` unless it says.
|
||||||
|
if made.kind == target_kind::program && made.srcs.length == 0 {
|
||||||
|
if !test {
|
||||||
|
this.fail(made.line, $"`{made.name.view()}` needs `srcs`: its files, folders, or globs.");
|
||||||
|
return made;
|
||||||
|
}
|
||||||
|
made.srcs.append(string::from("tests/**/*.sakura", nothing {}));
|
||||||
|
}
|
||||||
|
return made;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Reads a step of installing: a tag saying what it does, and its text.
|
||||||
|
def reading::step(&mut this, value: &yaml::value): step {
|
||||||
|
val line := value.place.line;
|
||||||
|
var made := step { line = line };
|
||||||
|
val tag := value.tag;
|
||||||
|
if tag is none {
|
||||||
|
this.fail(line, string::from("A step of `install` says what it does with a tag, as `!include ./share`.", nothing {}));
|
||||||
|
return made;
|
||||||
|
}
|
||||||
|
if tag.some != "!include" {
|
||||||
|
this.fail(line, $"`{tag.some}` is no step blossom knows: it knows `!include`.");
|
||||||
|
return made;
|
||||||
|
}
|
||||||
|
made.tag = string::from(tag.some, nothing {});
|
||||||
|
|
||||||
|
val text := this.text(value, "A step");
|
||||||
|
val split := text.view().find(":");
|
||||||
|
if split is some(at) {
|
||||||
|
made.source = string::from(text.view().substr(0, at), nothing {});
|
||||||
|
made.dest = string::from(text.view().substr(at + 1, text.length - at - 1), nothing {});
|
||||||
|
} else {
|
||||||
|
made.source = string::copy_of(&text);
|
||||||
|
}
|
||||||
|
if made.source.length == 0 || !is_within(made.source.view()) {
|
||||||
|
this.fail(line, $"`{made.source.view()}` is outside the project: a source is a path within it, as `./share`.");
|
||||||
|
} else if text.view().find(":") is some && (made.dest.length == 0 || !is_within(made.dest.view())) {
|
||||||
|
this.fail(line, $"`{made.dest.view()}` is outside the prefix: a destination is a path within it, as `share/doc`.");
|
||||||
|
}
|
||||||
|
return made;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Checks what reading each part couldn't: names given once, exports that are
|
||||||
|
# libraries, and libraries' roots within the project.
|
||||||
|
def reading::validate(&mut this, made: &mut project, root: &yaml::value): void {
|
||||||
|
val count := made.targets.length + made.tests.length;
|
||||||
|
for k in 0..count {
|
||||||
|
val each := &made.at(k);
|
||||||
|
for j in 0..k {
|
||||||
|
if made.at(j).name == each.name {
|
||||||
|
this.fail(each.line, $"`{each.name.view()}` names two targets.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if each.kind == target_kind::library && !is_within(each.root.view()) {
|
||||||
|
this.fail(each.line, $"`{each.root.view()}` is outside the project: a library's root is a folder within it.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for &name in made.exports {
|
||||||
|
val found := made.find(name.view());
|
||||||
|
if found is none || found.some >= made.targets.length || made.targets[found.some].kind != target_kind::library {
|
||||||
|
this.fail(root.get("exports").some.place.line, $"`{name.view()}` is exported, but it's no library of {made.identity.view()}.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Checks a mapping's keys: each one of those allowed, unless none are named,
|
||||||
|
# where any is, and none written twice, which YAML would keep only one of.
|
||||||
|
def reading::keys(&mut this, map: &yaml::value, allowed: span<strview>, what: strview): bool {
|
||||||
|
if map.kind != yaml::kind::mapping {
|
||||||
|
this.fail(map.place.line, $"{what} is a mapping of keys and values.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
var seen := vector::<strview>::new();
|
||||||
|
for each in map.members() {
|
||||||
|
val line := each.key.place.line;
|
||||||
|
var known := allowed.length == 0;
|
||||||
|
for &name in allowed {
|
||||||
|
known = known || name == each.name;
|
||||||
|
}
|
||||||
|
if !known {
|
||||||
|
this.fail(line, $"`{each.name}` is no key of {what}.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for &prior in seen {
|
||||||
|
if prior == each.name {
|
||||||
|
this.fail(line, $"`{each.name}` is written twice in {what}.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
seen.append(each.name);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
# The elements of a sequence, or none where the value is another kind, which
|
||||||
|
# is noted.
|
||||||
|
def reading::list(&mut this, value: &yaml::value, what: strview): yaml::value::element_cursor {
|
||||||
|
if value.kind != yaml::kind::sequence && !value.is_null {
|
||||||
|
this.fail(value.place.line, $"{what} is a list, of lines starting with `-`.");
|
||||||
|
}
|
||||||
|
return value.elements();
|
||||||
|
}
|
||||||
|
|
||||||
|
# A scalar's text, with options replaced.
|
||||||
|
def reading::text(&mut this, value: &yaml::value, what: strview): string {
|
||||||
|
val read := value.as_scalar() else (_) {
|
||||||
|
this.fail(value.place.line, $"{what} is a scalar, not a list or a mapping.");
|
||||||
|
return string::new(nothing {});
|
||||||
|
};
|
||||||
|
return this.expanded(read, value.place.line);
|
||||||
|
}
|
||||||
|
|
||||||
|
# The texts of a list of scalars.
|
||||||
|
def reading::texts(&mut this, value: &yaml::value, what: strview): vector<string> {
|
||||||
|
var made := vector::<string>::new();
|
||||||
|
for each in this.list(value, what) {
|
||||||
|
made.append(this.text(&each, $"Each of {what}".view()));
|
||||||
|
}
|
||||||
|
return made;
|
||||||
|
}
|
||||||
|
|
||||||
|
# A key's text, which must be written.
|
||||||
|
def reading::needed(&mut this, map: &yaml::value, key: strview): string {
|
||||||
|
if map.get(key) is some(value) {
|
||||||
|
var made := this.text(&value, $"`{key}`".view());
|
||||||
|
if made.length == 0 {
|
||||||
|
this.fail(value.place.line, $"`{key}` is empty.");
|
||||||
|
}
|
||||||
|
return made;
|
||||||
|
}
|
||||||
|
this.fail(map.place.line, $"`{key}` is needed.");
|
||||||
|
return string::new(nothing {});
|
||||||
|
}
|
||||||
|
|
||||||
|
# A key's text, or empty where it isn't written.
|
||||||
|
def reading::optional(&mut this, map: &yaml::value, key: strview): string {
|
||||||
|
if map.get(key) is some(value) {
|
||||||
|
return this.text(&value, $"`{key}`".view());
|
||||||
|
}
|
||||||
|
return string::new(nothing {});
|
||||||
|
}
|
||||||
|
|
||||||
|
# A scalar's truth, written `true` or `false`.
|
||||||
|
def reading::boolean(&mut this, value: &yaml::value, what: strview): bool {
|
||||||
|
val text := this.text(value, what);
|
||||||
|
if text != "true" && text != "false" {
|
||||||
|
this.fail(value.place.line, $"{what} is `true` or `false`, not `{text.view()}`.");
|
||||||
|
}
|
||||||
|
return text == "true";
|
||||||
|
}
|
||||||
|
|
||||||
|
# Text with each `${NAME}` replaced by the option's value, noting a name that's
|
||||||
|
# no option's.
|
||||||
|
def reading::expanded(&mut this, text: strview, line: u32): string {
|
||||||
|
var made := string::new(nothing {});
|
||||||
|
var at := 0u;
|
||||||
|
while at < text.length {
|
||||||
|
if text[at] == '$' && at + 1 < text.length && text[at + 1] == '{' {
|
||||||
|
val rest := text.substr(at + 2, text.length - at - 2);
|
||||||
|
if rest.find("}") is some(length) {
|
||||||
|
val name := rest.substr(0, length);
|
||||||
|
var found := false;
|
||||||
|
for &each in this.options {
|
||||||
|
if !found && each.name.view() == name {
|
||||||
|
made.append(each.value.view());
|
||||||
|
found = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
|
var message := string::from("`$", nothing {});
|
||||||
|
message.push('{');
|
||||||
|
message.append(name);
|
||||||
|
message.append("}` names no option declared before it.");
|
||||||
|
this.fail(line, move(message));
|
||||||
|
return made;
|
||||||
|
}
|
||||||
|
at += length + 3;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
made.push(text[at]);
|
||||||
|
at++;
|
||||||
|
}
|
||||||
|
return made;
|
||||||
|
}
|
||||||
|
|
||||||
|
#! The name of a project's output folder: the 64-bit hash of its identity, its
|
||||||
|
#! options' values, and what its targets are built for, in hex.
|
||||||
|
#! @param identity As `malunal/hello@1.0.0`.
|
||||||
|
#! @param options Its options' values.
|
||||||
|
#! @param platform What `--target` says, or empty.
|
||||||
|
#! @returns Sixteen hex digits.
|
||||||
|
pub def output_key(identity: strview, options: span<setting>, platform: strview): string {
|
||||||
|
var text := string::from(identity, nothing {});
|
||||||
|
# In order by name, so writing the options in another order changes nothing.
|
||||||
|
var order := vector::<u64>::new();
|
||||||
|
for k in 0..options.length {
|
||||||
|
var at := order.length;
|
||||||
|
while at > 0 && options[order[at - 1]].name.view().compare(options[k].name.view()) > 0 {
|
||||||
|
at--;
|
||||||
|
}
|
||||||
|
order.insert_at(at, k);
|
||||||
|
}
|
||||||
|
for k in order {
|
||||||
|
text.append($"\n{options[k].name.view()}={options[k].value.view()}".view());
|
||||||
|
}
|
||||||
|
text.append($"\ntarget={platform}".view());
|
||||||
|
return hex(hash(&text));
|
||||||
|
}
|
||||||
|
|
||||||
|
#! A number as sixteen hex digits.
|
||||||
|
#! @param value The number.
|
||||||
|
#! @returns Its digits, the highest first.
|
||||||
|
pub def hex(value: u64): string {
|
||||||
|
val digits: strview = "0123456789abcdef";
|
||||||
|
var made := string::new(nothing {});
|
||||||
|
var shift := 64u;
|
||||||
|
while shift > 0 {
|
||||||
|
shift -= 4;
|
||||||
|
made.push(digits[(value >> shift) & 15]);
|
||||||
|
}
|
||||||
|
return made;
|
||||||
|
}
|
||||||
|
|
||||||
|
#! Whether text is a version: three numbers, as `1.2.0`, then a `-` and what
|
||||||
|
#! comes before release, as `-alpha`, and a `+` and what it was built from, as
|
||||||
|
#! semantic versioning has them.
|
||||||
|
#! @param text The text.
|
||||||
|
#! @returns Whether it is one.
|
||||||
|
pub def is_version(text: strview): bool {
|
||||||
|
var at := 0u;
|
||||||
|
for part in 0..3 {
|
||||||
|
if part > 0 {
|
||||||
|
return false if at >= text.length || text[at] != '.';
|
||||||
|
at++;
|
||||||
|
}
|
||||||
|
val start := at;
|
||||||
|
while at < text.length && text[at].is_digit {
|
||||||
|
at++;
|
||||||
|
}
|
||||||
|
return false if at == start;
|
||||||
|
}
|
||||||
|
if at < text.length && (text[at] == '-' || text[at] == '+') {
|
||||||
|
at++;
|
||||||
|
return false if at == text.length;
|
||||||
|
while at < text.length {
|
||||||
|
val byte := text[at];
|
||||||
|
return false if !(byte.is_alnum || byte == '.' || byte == '-' || byte == '+');
|
||||||
|
at++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return at == text.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
#! Whether a requirement's text is a folder rather than a remote: a path
|
||||||
|
#! starting with `.` or a root.
|
||||||
|
#! @param text The text.
|
||||||
|
#! @returns Whether it is.
|
||||||
|
pub def is_folder_text(text: strview): bool {
|
||||||
|
return text.length != 0 && (text[0] == '.' || text[0] == '/' || text[0] == '\\' || (text.length > 1 && text[1] == ':' && text[0].is_alpha && text.find("://") is none));
|
||||||
|
}
|
||||||
|
|
||||||
|
# Where the `@` before a remote's ref is: the first after the remote's last `/`
|
||||||
|
# or `:`, or the text's length where there is none.
|
||||||
|
def ref_at(text: strview): u64 {
|
||||||
|
var from := 0u;
|
||||||
|
for k in 0..text.length {
|
||||||
|
if text[k] == '/' || text[k] == ':' {
|
||||||
|
from = k + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for k in from..text.length {
|
||||||
|
return k if text[k] == '@';
|
||||||
|
}
|
||||||
|
return text.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Whether a name is a module's, with `.` for `::`, as `hello.geometry`, and not
|
||||||
|
# one of the toolchain's own.
|
||||||
|
def is_library_name(name: strview): bool {
|
||||||
|
return false if name == "std" || name == "core" || name == "system" || name == "runtime";
|
||||||
|
var start := 0u;
|
||||||
|
for k in 0..=name.length {
|
||||||
|
if k == name.length || name[k] == '.' {
|
||||||
|
return false if k == start || name[start].is_digit;
|
||||||
|
start = k + 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
return false if !name[k].is_ident;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Whether a name can be a program's file.
|
||||||
|
def is_file_name(name: strview): bool {
|
||||||
|
return false if name.length == 0 || name == "." || name == "..";
|
||||||
|
for k in 0..name.length {
|
||||||
|
val byte := name[k];
|
||||||
|
return false if !(byte.is_alnum || byte == '.' || byte == '_' || byte == '-');
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
#! Whether `--target`, or a target's `target`, names a system blossom builds for.
|
||||||
|
#! @param text The name.
|
||||||
|
#! @returns Whether it does.
|
||||||
|
pub def is_platform(text: strview): bool {
|
||||||
|
for &each in platforms {
|
||||||
|
return true if each == text;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Whether a path stays within the folder it's taken from: relative, with no `..`.
|
||||||
|
def is_within(text: strview): bool {
|
||||||
|
return false if text.length == 0 || text[0] == '/' || text[0] == '\\' || (text.length > 1 && text[1] == ':');
|
||||||
|
for part in text.split('/') {
|
||||||
|
return false if part == "..";
|
||||||
|
for inner in part.split('\\') {
|
||||||
|
return false if inner == "..";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
@@ -0,0 +1,352 @@
|
|||||||
|
#! @file archives.sakura
|
||||||
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||||
|
#! @copyright Malunal Studios, LLC.
|
||||||
|
#! @license MIT
|
||||||
|
#!
|
||||||
|
#! Contains what packages are written as: a tar archive, a zip file holding
|
||||||
|
#! its files stored rather than compressed, and an `ar` archive, which a
|
||||||
|
#! Debian package is. Each is written whole into memory, every entry owned by
|
||||||
|
#! root and dated alike, so the same stage writes the same bytes. Compressing
|
||||||
|
#! them is the system's `gzip`, `xz`, and `zstd`.
|
||||||
|
import std;
|
||||||
|
import std::io;
|
||||||
|
import system;
|
||||||
|
|
||||||
|
module blossom::pipeline;
|
||||||
|
|
||||||
|
#! A file or folder of a package.
|
||||||
|
pub struct entry {
|
||||||
|
pub var name: string; #! Its path within the package, with `/`, without one at the end.
|
||||||
|
pub var source: path; #! The file it's read from, or empty for a folder.
|
||||||
|
pub var folder: bool;
|
||||||
|
pub var mode: u32; #! Who may read, write, and run it.
|
||||||
|
}
|
||||||
|
|
||||||
|
#! Lists what a stage holds, as entries under a root: the root's own folders
|
||||||
|
#! first, then each folder before what it holds, in order by name.
|
||||||
|
#! @param stage The stage.
|
||||||
|
#! @param root Where its files go in the package, as `usr` or
|
||||||
|
#! `hello-1.0.0-linux-x86_64`, or empty for its top.
|
||||||
|
#! @returns The entries.
|
||||||
|
pub def staged(stage: &path, root: strview): vector<entry> {
|
||||||
|
var made := vector::<entry>::new();
|
||||||
|
var above := string::new(nothing {});
|
||||||
|
for part in root.split('/') {
|
||||||
|
continue if part.length == 0;
|
||||||
|
if above.length != 0 {
|
||||||
|
above.push('/');
|
||||||
|
}
|
||||||
|
above.append(part);
|
||||||
|
made.append(entry { name = string::copy_of(&above), folder = true, mode = 0o755 });
|
||||||
|
}
|
||||||
|
walked(stage, above.view(), &mut made);
|
||||||
|
return made;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Adds what a folder holds, in order by name, each folder before what it holds.
|
||||||
|
def walked(folder: &path, under: strview, into: &mut vector<entry>): void {
|
||||||
|
var names := vector::<string>::new();
|
||||||
|
var listing := io::directory::open(folder) else (_) { return; };
|
||||||
|
while listing.next() is some(each) {
|
||||||
|
names.append(string::from(each.name, nothing {}));
|
||||||
|
}
|
||||||
|
names.sort_by(def(a: &string, b: &string): bool { return a.view().compare(b.view()) < 0; });
|
||||||
|
for &name in names {
|
||||||
|
val at := folder.join(name.view());
|
||||||
|
val text := under.length != 0 ? $"{under}/{name.view()}" : string::copy_of(name);
|
||||||
|
val found := io::info(&at) else (_) { continue; };
|
||||||
|
if found.kind == file_kind::directory {
|
||||||
|
into.append(entry { name = string::copy_of(&text), folder = true, mode = 0o755 });
|
||||||
|
walked(&at, text.view(), into);
|
||||||
|
} else {
|
||||||
|
into.append(entry {
|
||||||
|
name = string::copy_of(&text),
|
||||||
|
source = path::copy_of(&at),
|
||||||
|
mode = (found.permissions & 0o111) != 0 ? 0o755 : 0o644
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#! A tar archive, as POSIX's ustar writes one, with GNU's long names for a path
|
||||||
|
#! too long to split between a header's name and prefix.
|
||||||
|
pub struct tar {
|
||||||
|
pub var bytes: string;
|
||||||
|
pub var mtime: i64; #! When every entry was written, in seconds since 1970.
|
||||||
|
}
|
||||||
|
|
||||||
|
#! Adds a folder.
|
||||||
|
#! @param this The archive.
|
||||||
|
#! @param name Its path, which `/` is added to.
|
||||||
|
pub def tar::folder(&mut this, name: strview): void {
|
||||||
|
val named := $"{name}/";
|
||||||
|
this.header(named.view(), 0o755, 0, '5');
|
||||||
|
}
|
||||||
|
|
||||||
|
#! Adds a file.
|
||||||
|
#! @param this The archive.
|
||||||
|
#! @param name Its path.
|
||||||
|
#! @param data What it holds.
|
||||||
|
#! @param mode Who may read, write, and run it.
|
||||||
|
pub def tar::file(&mut this, name: strview, data: span<u8>, mode: u32): void {
|
||||||
|
this.header(name, mode, data.length, '0');
|
||||||
|
this.bytes.append(data);
|
||||||
|
this.pad();
|
||||||
|
}
|
||||||
|
|
||||||
|
#! Ends the archive with its two empty blocks.
|
||||||
|
#! @param this The archive.
|
||||||
|
#! @returns Its bytes.
|
||||||
|
pub def tar::finish(&mut this): strview {
|
||||||
|
for _ in 0..1024 {
|
||||||
|
this.bytes.push(0);
|
||||||
|
}
|
||||||
|
return this.bytes.view();
|
||||||
|
}
|
||||||
|
|
||||||
|
# Writes an entry's header, after a long name's own entry where it needs one.
|
||||||
|
def tar::header(&mut this, name: strview, mode: u32, size: u64, kind: u8): void {
|
||||||
|
var split := name.length;
|
||||||
|
if name.length > 100 {
|
||||||
|
# The last `/` leaving a name of 100 bytes or fewer, with 155 or fewer
|
||||||
|
# before it.
|
||||||
|
split = 0;
|
||||||
|
for k in 0..name.length {
|
||||||
|
if name[k] == '/' && name.length - k - 1 <= 100 && k <= 155 && k > 0 {
|
||||||
|
split = k;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if split == 0 {
|
||||||
|
val longer := $"{name}\0";
|
||||||
|
this.header("././@LongLink", 0o644, longer.length, 'L');
|
||||||
|
this.bytes.append(longer.view());
|
||||||
|
this.pad();
|
||||||
|
split = name.length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var block: u8[512];
|
||||||
|
for k in 0..512 {
|
||||||
|
block[k] = 0;
|
||||||
|
}
|
||||||
|
val base := split == name.length ? name : name.substr(split + 1, name.length - split - 1);
|
||||||
|
val prefix := split == name.length ? strview::empty() : name.substr(0, split);
|
||||||
|
put(&mut block, 0, base.substr(0, 100));
|
||||||
|
put_octal(&mut block, 100, 8, mode as u64);
|
||||||
|
put_octal(&mut block, 108, 8, 0);
|
||||||
|
put_octal(&mut block, 116, 8, 0);
|
||||||
|
put_octal(&mut block, 124, 12, size);
|
||||||
|
put_octal(&mut block, 136, 12, this.mtime as u64);
|
||||||
|
for k in 148..156 {
|
||||||
|
block[k] = ' ';
|
||||||
|
}
|
||||||
|
block[156] = kind;
|
||||||
|
put(&mut block, 257, "ustar");
|
||||||
|
put(&mut block, 263, "00");
|
||||||
|
put(&mut block, 265, "root");
|
||||||
|
put(&mut block, 297, "root");
|
||||||
|
put(&mut block, 345, prefix);
|
||||||
|
|
||||||
|
var sum := 0u;
|
||||||
|
for k in 0..512 {
|
||||||
|
sum += block[k] as u64;
|
||||||
|
}
|
||||||
|
put_octal(&mut block, 148, 7, sum);
|
||||||
|
block[155] = ' ';
|
||||||
|
this.bytes.append(strview::over((&block) as *u8, 512));
|
||||||
|
}
|
||||||
|
|
||||||
|
# Pads the archive to a whole block.
|
||||||
|
def tar::pad(&mut this): void {
|
||||||
|
while this.bytes.length % 512 != 0 {
|
||||||
|
this.bytes.push(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Writes text into a header from a byte.
|
||||||
|
def put(block: &mut u8[512], at: u64, text: strview): void {
|
||||||
|
for k in 0..text.length {
|
||||||
|
block[at + k] = text[k];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Writes a number in octal into a header, in a field of a width: zeros before
|
||||||
|
# it, and a zero byte after.
|
||||||
|
def put_octal(block: &mut u8[512], at: u64, width: u64, value: u64): void {
|
||||||
|
var left := value;
|
||||||
|
var k := width - 1;
|
||||||
|
block[at + k] = 0;
|
||||||
|
while k > 0 {
|
||||||
|
k--;
|
||||||
|
block[at + k] = ('0' + (left & 7) as u8);
|
||||||
|
left = left >> 3;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#! A zip file whose files are stored as they are, rather than compressed, with
|
||||||
|
#! who may run each in the Unix bits of its attributes.
|
||||||
|
pub struct zip {
|
||||||
|
pub var bytes: string;
|
||||||
|
pub var central: string; #! The central directory, written after the files.
|
||||||
|
pub var count: u64;
|
||||||
|
pub var mtime: i64; #! When every entry was written, in seconds since 1970.
|
||||||
|
}
|
||||||
|
|
||||||
|
#! Adds a folder.
|
||||||
|
#! @param this The zip file.
|
||||||
|
#! @param name Its path, which `/` is added to.
|
||||||
|
pub def zip::folder(&mut this, name: strview): void {
|
||||||
|
val named := $"{name}/";
|
||||||
|
this.add(named.view(), strview::empty(), 0o040000 | 0o755, 0x10);
|
||||||
|
}
|
||||||
|
|
||||||
|
#! Adds a file.
|
||||||
|
#! @param this The zip file.
|
||||||
|
#! @param name Its path.
|
||||||
|
#! @param data What it holds.
|
||||||
|
#! @param mode Who may read, write, and run it.
|
||||||
|
pub def zip::file(&mut this, name: strview, data: span<u8>, mode: u32): void {
|
||||||
|
this.add(name, data, 0o100000 | mode, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
# Adds an entry: its header, its bytes, and its record in the central directory.
|
||||||
|
def zip::add(&mut this, name: strview, data: span<u8>, unix: u32, dos: u32): void {
|
||||||
|
val offset := this.bytes.length;
|
||||||
|
val crc := crc32(data);
|
||||||
|
val stamp := dos_stamp(this.mtime);
|
||||||
|
put_u32(&mut this.bytes, 0x04034b50);
|
||||||
|
put_u16(&mut this.bytes, 20);
|
||||||
|
put_u16(&mut this.bytes, 0x0800); # Names are UTF-8.
|
||||||
|
put_u16(&mut this.bytes, 0);
|
||||||
|
put_u32(&mut this.bytes, stamp);
|
||||||
|
put_u32(&mut this.bytes, crc);
|
||||||
|
put_u32(&mut this.bytes, data.length as u32);
|
||||||
|
put_u32(&mut this.bytes, data.length as u32);
|
||||||
|
put_u16(&mut this.bytes, name.length as u16);
|
||||||
|
put_u16(&mut this.bytes, 0);
|
||||||
|
this.bytes.append(name);
|
||||||
|
this.bytes.append(data);
|
||||||
|
|
||||||
|
put_u32(&mut this.central, 0x02014b50);
|
||||||
|
put_u16(&mut this.central, (3 << 8) | 20); # Made on Unix.
|
||||||
|
put_u16(&mut this.central, 20);
|
||||||
|
put_u16(&mut this.central, 0x0800);
|
||||||
|
put_u16(&mut this.central, 0);
|
||||||
|
put_u32(&mut this.central, stamp);
|
||||||
|
put_u32(&mut this.central, crc);
|
||||||
|
put_u32(&mut this.central, data.length as u32);
|
||||||
|
put_u32(&mut this.central, data.length as u32);
|
||||||
|
put_u16(&mut this.central, name.length as u16);
|
||||||
|
put_u16(&mut this.central, 0);
|
||||||
|
put_u16(&mut this.central, 0);
|
||||||
|
put_u16(&mut this.central, 0);
|
||||||
|
put_u16(&mut this.central, 0);
|
||||||
|
put_u32(&mut this.central, (unix << 16) | dos);
|
||||||
|
put_u32(&mut this.central, offset as u32);
|
||||||
|
this.central.append(name);
|
||||||
|
this.count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
#! Ends the zip file with its central directory.
|
||||||
|
#! @param this The zip file.
|
||||||
|
#! @returns Its bytes.
|
||||||
|
pub def zip::finish(&mut this): strview {
|
||||||
|
val start := this.bytes.length;
|
||||||
|
this.bytes.append(this.central.view());
|
||||||
|
put_u32(&mut this.bytes, 0x06054b50);
|
||||||
|
put_u16(&mut this.bytes, 0);
|
||||||
|
put_u16(&mut this.bytes, 0);
|
||||||
|
put_u16(&mut this.bytes, this.count as u16);
|
||||||
|
put_u16(&mut this.bytes, this.count as u16);
|
||||||
|
put_u32(&mut this.bytes, this.central.length as u32);
|
||||||
|
put_u32(&mut this.bytes, start as u32);
|
||||||
|
put_u16(&mut this.bytes, 0);
|
||||||
|
return this.bytes.view();
|
||||||
|
}
|
||||||
|
|
||||||
|
# A time as MS-DOS keeps it, as zip files do: the date in the high half, the
|
||||||
|
# time in the low, in two-second steps, from 1980 on.
|
||||||
|
def dos_stamp(seconds: i64): u32 {
|
||||||
|
val at := seconds < 315532800 ? 315532800 : seconds;
|
||||||
|
val days := at / 86400;
|
||||||
|
val left := at % 86400;
|
||||||
|
val date := civil_of(days);
|
||||||
|
val day := ((date.year - 1980) as u32 << 9) | (date.month as u32 << 5) | date.day as u32;
|
||||||
|
val time := ((left / 3600) as u32 << 11) | (((left % 3600) / 60) as u32 << 5) | ((left % 60) / 2) as u32;
|
||||||
|
return (day << 16) | time;
|
||||||
|
}
|
||||||
|
|
||||||
|
# A day of the Gregorian calendar.
|
||||||
|
struct civil {
|
||||||
|
var year: i64;
|
||||||
|
var month: i64;
|
||||||
|
var day: i64;
|
||||||
|
}
|
||||||
|
|
||||||
|
# The date a number of days after 1970-01-01 is, as Howard Hinnant's
|
||||||
|
# `civil_from_days` works it out.
|
||||||
|
def civil_of(days: i64): civil {
|
||||||
|
val z := days + 719468;
|
||||||
|
val era := (z >= 0 ? z : z - 146096) / 146097;
|
||||||
|
val doe := z - era * 146097;
|
||||||
|
val yoe := (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
|
||||||
|
val doy := doe - (365 * yoe + yoe / 4 - yoe / 100);
|
||||||
|
val mp := (5 * doy + 2) / 153;
|
||||||
|
val day := doy - (153 * mp + 2) / 5 + 1;
|
||||||
|
val month := mp < 10 ? mp + 3 : mp - 9;
|
||||||
|
return civil { year = yoe + era * 400 + (month <= 2 ? 1 : 0), month = month, day = day };
|
||||||
|
}
|
||||||
|
|
||||||
|
#! An `ar` archive, as a Debian package is: its signature, then each member
|
||||||
|
#! with a header of 60 bytes.
|
||||||
|
pub struct ar {
|
||||||
|
pub var bytes: string;
|
||||||
|
pub var mtime: i64;
|
||||||
|
}
|
||||||
|
|
||||||
|
#! Starts an archive.
|
||||||
|
#! @param mtime When every member was written, in seconds since 1970.
|
||||||
|
#! @returns The archive, holding its signature.
|
||||||
|
pub def ar::new(mtime: i64): ar {
|
||||||
|
return ar { bytes = string::from("!<arch>\n", nothing {}), mtime = mtime };
|
||||||
|
}
|
||||||
|
|
||||||
|
#! Adds a member.
|
||||||
|
#! @param this The archive.
|
||||||
|
#! @param name Its name, of 16 bytes or fewer.
|
||||||
|
#! @param data What it holds.
|
||||||
|
pub def ar::member(&mut this, name: strview, data: span<u8>): void {
|
||||||
|
this.bytes.append(padded(name, 16).view());
|
||||||
|
this.bytes.append(padded($"{this.mtime}".view(), 12).view());
|
||||||
|
this.bytes.append(padded("0", 6).view());
|
||||||
|
this.bytes.append(padded("0", 6).view());
|
||||||
|
this.bytes.append(padded("100644", 8).view());
|
||||||
|
this.bytes.append(padded($"{data.length}".view(), 10).view());
|
||||||
|
this.bytes.append("`\n");
|
||||||
|
this.bytes.append(data);
|
||||||
|
if data.length % 2 != 0 {
|
||||||
|
this.bytes.push('\n');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Text with spaces after it, to a width.
|
||||||
|
def padded(text: strview, width: u64): string {
|
||||||
|
var made := string::from(text, nothing {});
|
||||||
|
while made.length < width {
|
||||||
|
made.push(' ');
|
||||||
|
}
|
||||||
|
return made;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Adds a number of two bytes, the low first.
|
||||||
|
def put_u16(into: &mut string, value: u32): void {
|
||||||
|
into.push((value & 0xff) as u8);
|
||||||
|
into.push(((value >> 8) & 0xff) as u8);
|
||||||
|
}
|
||||||
|
|
||||||
|
# Adds a number of four bytes, the low first.
|
||||||
|
def put_u32(into: &mut string, value: u32): void {
|
||||||
|
put_u16(into, value & 0xffff);
|
||||||
|
put_u16(into, value >> 16);
|
||||||
|
}
|
||||||
@@ -0,0 +1,255 @@
|
|||||||
|
#! @file digests.sakura
|
||||||
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||||
|
#! @copyright Malunal Studios, LLC.
|
||||||
|
#! @license MIT
|
||||||
|
#!
|
||||||
|
#! Contains the digests packages carry: MD5, which Debian's `md5sums` lists,
|
||||||
|
#! SHA-256, which Arch's `.MTREE` lists beside MD5, and CRC-32, which a zip
|
||||||
|
#! file keeps of each file it holds.
|
||||||
|
import std;
|
||||||
|
|
||||||
|
module blossom::pipeline;
|
||||||
|
|
||||||
|
#! The MD5 digest of bytes, as RFC 1321 gives it.
|
||||||
|
#! @param data The bytes.
|
||||||
|
#! @returns The digest, as 32 hex digits.
|
||||||
|
pub def md5(data: span<u8>): string {
|
||||||
|
var state: u32[4] = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476];
|
||||||
|
var block: u8[64];
|
||||||
|
val total := data.length;
|
||||||
|
var at := 0u;
|
||||||
|
while at + 64 <= total {
|
||||||
|
md5_block(&mut state, data.data + at);
|
||||||
|
at += 64;
|
||||||
|
}
|
||||||
|
|
||||||
|
# The last bytes, then `0x80`, then zeros, then the length in bits, low
|
||||||
|
# byte first, filling one block or two.
|
||||||
|
val left := total - at;
|
||||||
|
for k in 0..64 {
|
||||||
|
block[k] = 0;
|
||||||
|
}
|
||||||
|
for k in 0..left {
|
||||||
|
block[k] = data[at + k];
|
||||||
|
}
|
||||||
|
block[left] = 0x80;
|
||||||
|
if left >= 56 {
|
||||||
|
md5_block(&mut state, (&block) as *u8);
|
||||||
|
for k in 0..64 {
|
||||||
|
block[k] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val bits := total * 8;
|
||||||
|
for k in 0..8 {
|
||||||
|
block[56 + k] = ((bits >> (8 * k as u64)) & 0xff) as u8;
|
||||||
|
}
|
||||||
|
md5_block(&mut state, (&block) as *u8);
|
||||||
|
|
||||||
|
var made := string::new(nothing {});
|
||||||
|
for word in state {
|
||||||
|
for k in 0..4 {
|
||||||
|
append_hex(&mut made, ((word >> (8 * k as u32)) & 0xff) as u8);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return made;
|
||||||
|
}
|
||||||
|
|
||||||
|
# The amounts each round of MD5 turns its words left by.
|
||||||
|
val md5_turns: u32[16] = [7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23, 6, 10, 15, 21];
|
||||||
|
|
||||||
|
# Mixes a block of 64 bytes into MD5's state.
|
||||||
|
def md5_block(state: &mut u32[4], at: *u8): void {
|
||||||
|
var words: u32[16];
|
||||||
|
for k in 0..16 {
|
||||||
|
val from := at + 4 * k;
|
||||||
|
words[k] = from[0] as u32 | (from[1] as u32 << 8) | (from[2] as u32 << 16) | (from[3] as u32 << 24);
|
||||||
|
}
|
||||||
|
var a := state[0];
|
||||||
|
var b := state[1];
|
||||||
|
var c := state[2];
|
||||||
|
var d := state[3];
|
||||||
|
for i in 0u..64u {
|
||||||
|
var mixed: u32 = 0;
|
||||||
|
var index: u64 = 0;
|
||||||
|
if i < 16 {
|
||||||
|
mixed = (b & c) | (~b & d);
|
||||||
|
index = i;
|
||||||
|
} else if i < 32 {
|
||||||
|
mixed = (d & b) | (~d & c);
|
||||||
|
index = (5 * i + 1) % 16;
|
||||||
|
} else if i < 48 {
|
||||||
|
mixed = b ^ c ^ d;
|
||||||
|
index = (3 * i + 5) % 16;
|
||||||
|
} else {
|
||||||
|
mixed = c ^ (b | ~d);
|
||||||
|
index = (7 * i) % 16;
|
||||||
|
}
|
||||||
|
val sum := a + mixed + md5_constant(i) + words[index];
|
||||||
|
a = d;
|
||||||
|
d = c;
|
||||||
|
c = b;
|
||||||
|
b = b + turned_left(sum, md5_turns[(i / 16) * 4 + i % 4]);
|
||||||
|
}
|
||||||
|
state[0] = state[0] + a;
|
||||||
|
state[1] = state[1] + b;
|
||||||
|
state[2] = state[2] + c;
|
||||||
|
state[3] = state[3] + d;
|
||||||
|
}
|
||||||
|
|
||||||
|
# MD5's constant of each step: the whole part of 2^32 times the sine of the
|
||||||
|
# step's number, counted from 1.
|
||||||
|
def md5_constant(i: u64): u32 {
|
||||||
|
return md5_constants[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
val md5_constants: u32[64] = [
|
||||||
|
0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
|
||||||
|
0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
|
||||||
|
0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
|
||||||
|
0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
|
||||||
|
0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
|
||||||
|
0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
|
||||||
|
0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
|
||||||
|
0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391,
|
||||||
|
];
|
||||||
|
|
||||||
|
#! The SHA-256 digest of bytes, as FIPS 180-4 gives it.
|
||||||
|
#! @param data The bytes.
|
||||||
|
#! @returns The digest, as 64 hex digits.
|
||||||
|
pub def sha256(data: span<u8>): string {
|
||||||
|
var state: u32[8] = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19];
|
||||||
|
var block: u8[64];
|
||||||
|
val total := data.length;
|
||||||
|
var at := 0u;
|
||||||
|
while at + 64 <= total {
|
||||||
|
sha256_block(&mut state, data.data + at);
|
||||||
|
at += 64;
|
||||||
|
}
|
||||||
|
|
||||||
|
# As MD5 pads, but with the length's high byte first.
|
||||||
|
val left := total - at;
|
||||||
|
for k in 0..64 {
|
||||||
|
block[k] = 0;
|
||||||
|
}
|
||||||
|
for k in 0..left {
|
||||||
|
block[k] = data[at + k];
|
||||||
|
}
|
||||||
|
block[left] = 0x80;
|
||||||
|
if left >= 56 {
|
||||||
|
sha256_block(&mut state, (&block) as *u8);
|
||||||
|
for k in 0..64 {
|
||||||
|
block[k] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val bits := total * 8;
|
||||||
|
for k in 0..8 {
|
||||||
|
block[63 - k] = ((bits >> (8 * k as u64)) & 0xff) as u8;
|
||||||
|
}
|
||||||
|
sha256_block(&mut state, (&block) as *u8);
|
||||||
|
|
||||||
|
var made := string::new(nothing {});
|
||||||
|
for word in state {
|
||||||
|
for k in 0..4 {
|
||||||
|
append_hex(&mut made, ((word >> (24 - 8 * k as u32)) & 0xff) as u8);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return made;
|
||||||
|
}
|
||||||
|
|
||||||
|
# SHA-256's constants: the first 32 bits of the fractional parts of the cube
|
||||||
|
# roots of the first 64 primes.
|
||||||
|
val sha256_constants: u32[64] = [
|
||||||
|
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
||||||
|
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
||||||
|
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
||||||
|
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
||||||
|
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
||||||
|
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
||||||
|
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
||||||
|
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
||||||
|
];
|
||||||
|
|
||||||
|
# Mixes a block of 64 bytes into SHA-256's state.
|
||||||
|
def sha256_block(state: &mut u32[8], at: *u8): void {
|
||||||
|
var words: u32[64];
|
||||||
|
for k in 0..16 {
|
||||||
|
val from := at + 4 * k;
|
||||||
|
words[k] = (from[0] as u32 << 24) | (from[1] as u32 << 16) | (from[2] as u32 << 8) | from[3] as u32;
|
||||||
|
}
|
||||||
|
for k in 16..64 {
|
||||||
|
val low := words[k - 15];
|
||||||
|
val high := words[k - 2];
|
||||||
|
val small0 := turned_right(low, 7) ^ turned_right(low, 18) ^ (low >> 3);
|
||||||
|
val small1 := turned_right(high, 17) ^ turned_right(high, 19) ^ (high >> 10);
|
||||||
|
words[k] = words[k - 16] + small0 + words[k - 7] + small1;
|
||||||
|
}
|
||||||
|
|
||||||
|
var a := state[0];
|
||||||
|
var b := state[1];
|
||||||
|
var c := state[2];
|
||||||
|
var d := state[3];
|
||||||
|
var e := state[4];
|
||||||
|
var f := state[5];
|
||||||
|
var g := state[6];
|
||||||
|
var h := state[7];
|
||||||
|
for k in 0..64 {
|
||||||
|
val big1 := turned_right(e, 6) ^ turned_right(e, 11) ^ turned_right(e, 25);
|
||||||
|
val chosen := (e & f) ^ (~e & g);
|
||||||
|
val first := h + big1 + chosen + sha256_constants[k] + words[k];
|
||||||
|
val big0 := turned_right(a, 2) ^ turned_right(a, 13) ^ turned_right(a, 22);
|
||||||
|
val major := (a & b) ^ (a & c) ^ (b & c);
|
||||||
|
val second := big0 + major;
|
||||||
|
h = g;
|
||||||
|
g = f;
|
||||||
|
f = e;
|
||||||
|
e = d + first;
|
||||||
|
d = c;
|
||||||
|
c = b;
|
||||||
|
b = a;
|
||||||
|
a = first + second;
|
||||||
|
}
|
||||||
|
state[0] = state[0] + a;
|
||||||
|
state[1] = state[1] + b;
|
||||||
|
state[2] = state[2] + c;
|
||||||
|
state[3] = state[3] + d;
|
||||||
|
state[4] = state[4] + e;
|
||||||
|
state[5] = state[5] + f;
|
||||||
|
state[6] = state[6] + g;
|
||||||
|
state[7] = state[7] + h;
|
||||||
|
}
|
||||||
|
|
||||||
|
#! The CRC-32 of bytes, as zip files and gzip keep it: the polynomial
|
||||||
|
#! `0xEDB88320`, reflected, starting from and ending with every bit flipped.
|
||||||
|
#! @param data The bytes.
|
||||||
|
#! @returns The checksum.
|
||||||
|
pub def crc32(data: span<u8>): u32 {
|
||||||
|
var crc: u32 = 0xFFFFFFFF;
|
||||||
|
for k in 0..data.length {
|
||||||
|
crc = crc ^ data[k] as u32;
|
||||||
|
for _ in 0..8 {
|
||||||
|
val low := crc & 1;
|
||||||
|
crc = crc >> 1;
|
||||||
|
if low != 0 {
|
||||||
|
crc = crc ^ 0xEDB88320;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ~crc;
|
||||||
|
}
|
||||||
|
|
||||||
|
# A word turned left, its high bits coming round to the low.
|
||||||
|
def turned_left(word: u32, by: u32): u32 {
|
||||||
|
return (word << by) | (word >> (32 - by));
|
||||||
|
}
|
||||||
|
|
||||||
|
# A word turned right, its low bits coming round to the high.
|
||||||
|
def turned_right(word: u32, by: u32): u32 {
|
||||||
|
return (word >> by) | (word << (32 - by));
|
||||||
|
}
|
||||||
|
|
||||||
|
# Adds a byte as two hex digits.
|
||||||
|
def append_hex(into: &mut string, byte: u8): void {
|
||||||
|
val digits: strview = "0123456789abcdef";
|
||||||
|
into.push(digits[(byte >> 4) as u64]);
|
||||||
|
into.push(digits[(byte & 15) as u64]);
|
||||||
|
}
|
||||||
@@ -0,0 +1,169 @@
|
|||||||
|
#! @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;
|
||||||
|
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);
|
||||||
|
io::println($"Installed {this.space.projects[0].identity.view()} ({count} files) to {into.view()}".view());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#! Installs the project, already built, under a folder: its programs into
|
||||||
|
#! `bin`, its exported libraries into `share/sakura`, then each step.
|
||||||
|
#! @param this The session.
|
||||||
|
#! @param into The folder, which stands for the prefix.
|
||||||
|
#! @returns How many files were installed.
|
||||||
|
pub def session::install(&mut this, into: &path): 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.view(), system);
|
||||||
|
val built := holder.folder.join($"{output_text(holder).view()}/bin/{name.view()}".view());
|
||||||
|
count += copied(&built, &into.join("bin").join(name.view()));
|
||||||
|
val petal := holder.folder.join($"{output_text(holder).view()}/bin/{name.view()}.petal".view());
|
||||||
|
if io::exists(&petal) {
|
||||||
|
count += copied(&petal, &into.join("bin").join($"{name.view()}.petal".view()));
|
||||||
|
}
|
||||||
|
} else if holder.exported(each.name.view()) {
|
||||||
|
val root := path::parse(each.root.view()) else (_) { continue; };
|
||||||
|
count += copied(&holder.folder.join(&root), &into.join("share").join("sakura").join(each.name.view()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for &step in holder.install {
|
||||||
|
val source := path::parse(step.source.view()) else (_) {
|
||||||
|
fatal(2, $"{holder.file.view()}:{step.line}: `{step.source.view()}` is no path.");
|
||||||
|
return count;
|
||||||
|
};
|
||||||
|
val from := holder.folder.join(&source);
|
||||||
|
if !io::exists(&from) {
|
||||||
|
fatal(2, $"{holder.file.view()}:{step.line}: `{step.source.view()}` isn't there to install.");
|
||||||
|
}
|
||||||
|
val to := step.dest.length != 0 ? path::parse(step.dest.view()) : path::parse(step.source.view());
|
||||||
|
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.view()}` 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.view()}` 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.view().compare(b.view()) < 0; });
|
||||||
|
var count := 0u;
|
||||||
|
for &name in names {
|
||||||
|
count += copied(&from.join(name.view()), &to.join(name.view()));
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
if to.parent() is some(&above) {
|
||||||
|
_ = io::create_directories(above);
|
||||||
|
}
|
||||||
|
val bytes := io::read_file(from) else (why) {
|
||||||
|
fatal(1, $"`{from.view()}` can't be read: {why}.");
|
||||||
|
return 0;
|
||||||
|
};
|
||||||
|
if io::write_file(to, bytes.view()) is error(why) {
|
||||||
|
fatal(1, $"`{to.view()}` can't be written: {why}.");
|
||||||
|
}
|
||||||
|
_ = io::set_permissions(to, (found.permissions & 0o111) != 0 ? 0o755 : 0o644);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
# `.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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,478 @@
|
|||||||
|
#! @file package.sakura
|
||||||
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||||
|
#! @copyright Malunal Studios, LLC.
|
||||||
|
#! @license MIT
|
||||||
|
#!
|
||||||
|
#! Contains `package`: the project installed into its stage, then written as
|
||||||
|
#! each package asked for into its output folder's `dist`. tar and zip files
|
||||||
|
#! hold the stage under a folder of the project's name, version, and target;
|
||||||
|
#! Debian and Arch packages hold it under the prefix they install to, `/usr`
|
||||||
|
#! unless given; and an rpm package is built by `rpmbuild` from a spec written
|
||||||
|
#! for it. Every entry is dated `SOURCE_DATE_EPOCH`, where it's set, so a
|
||||||
|
#! package is written alike each time.
|
||||||
|
import std;
|
||||||
|
import std::io;
|
||||||
|
import std::process;
|
||||||
|
import std::time;
|
||||||
|
import system;
|
||||||
|
import blossom::models;
|
||||||
|
import blossom::management;
|
||||||
|
|
||||||
|
module blossom::pipeline;
|
||||||
|
|
||||||
|
#! The packages blossom writes.
|
||||||
|
pub val formats: strview[5] = ["tar", "zip", "deb", "arch", "rpm"];
|
||||||
|
|
||||||
|
#! Builds the project, installs it into its stage, then writes its packages.
|
||||||
|
#! @param this The session.
|
||||||
|
#! @param asked The formats asked for, each a format or several with `,`
|
||||||
|
#! between them, or none for `tar`, or `zip` for Windows.
|
||||||
|
#! @param prefix Where a Debian, Arch, or rpm package installs, or empty for
|
||||||
|
#! `/usr`.
|
||||||
|
#! @returns The status the command ends with.
|
||||||
|
pub def session::package_command(&mut this, asked: span<strview>, prefix: strview): i32 {
|
||||||
|
var chosen := vector::<strview>::new();
|
||||||
|
for &each in asked {
|
||||||
|
for format in each.split(',') {
|
||||||
|
continue if format.length == 0;
|
||||||
|
var known := false;
|
||||||
|
for &name in formats {
|
||||||
|
known = known || name == format;
|
||||||
|
}
|
||||||
|
if !known {
|
||||||
|
fatal(2, $"`{format}` is no format: write `tar`, `zip`, `deb`, `arch`, or `rpm`.".view());
|
||||||
|
}
|
||||||
|
chosen.append(format);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val platform := this.package_platform();
|
||||||
|
if chosen.length == 0 {
|
||||||
|
chosen.append(platform == "windows-x86_64" ? "zip" : "tar");
|
||||||
|
}
|
||||||
|
var installed_at: strview = "/usr";
|
||||||
|
if prefix.length != 0 {
|
||||||
|
installed_at = prefix;
|
||||||
|
}
|
||||||
|
|
||||||
|
# What a system's package can't hold is said before anything's built.
|
||||||
|
for &format in chosen {
|
||||||
|
if format == "deb" || format == "arch" || format == "rpm" {
|
||||||
|
_ = distro_arch(platform, format, "");
|
||||||
|
_ = distro_name(&this.space.projects[0]);
|
||||||
|
_ = distro_version(&this.space.projects[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.build(false, "");
|
||||||
|
return 1 if this.failed;
|
||||||
|
|
||||||
|
val holder := &this.space.projects[0];
|
||||||
|
val output := holder.output;
|
||||||
|
val stage := output.join("stage");
|
||||||
|
val dist := output.join("dist");
|
||||||
|
if management::remove_tree(&stage) is error(why) {
|
||||||
|
fatal(1, $"`{stage.view()}` can't be emptied: {why}.".view());
|
||||||
|
}
|
||||||
|
_ = io::create_directories(&stage);
|
||||||
|
if this.install(&stage) == 0 {
|
||||||
|
fatal(1, $"{holder.identity.view()} installs nothing, so there's nothing to package.".view());
|
||||||
|
}
|
||||||
|
_ = io::create_directories(&dist);
|
||||||
|
|
||||||
|
val mtime := build_time();
|
||||||
|
for &format in chosen {
|
||||||
|
var made := path::new();
|
||||||
|
if format == "tar" || format == "zip" {
|
||||||
|
made = this.archive(format, &stage, &dist, platform, mtime);
|
||||||
|
} else if format == "deb" {
|
||||||
|
made = this.debian(&stage, &dist, trimmed(installed_at), platform, mtime);
|
||||||
|
} else if format == "arch" {
|
||||||
|
made = this.arch_package(&stage, &dist, trimmed(installed_at), platform, mtime);
|
||||||
|
} else {
|
||||||
|
made = this.rpm(&stage, &dist, trimmed(installed_at), platform);
|
||||||
|
}
|
||||||
|
io::println($"Packaged {made.view()}".view());
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
# What the packages are for: what `--target` says, else what the first program
|
||||||
|
# that says is built for, else the system blossom runs on.
|
||||||
|
def session::package_platform(&this): strview {
|
||||||
|
if this.space.platform.length != 0 {
|
||||||
|
return this.space.platform.view();
|
||||||
|
}
|
||||||
|
for &each in this.space.projects[0].targets {
|
||||||
|
if each.kind == models::target_kind::program && each.system.length != 0 {
|
||||||
|
return each.system.view();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
when os == windows {
|
||||||
|
return "windows-x86_64";
|
||||||
|
}
|
||||||
|
return "linux-x86_64";
|
||||||
|
}
|
||||||
|
|
||||||
|
# Writes a tar archive, compressed by `gzip`, or a zip file, of the stage under
|
||||||
|
# a folder of the project's name, version, and target.
|
||||||
|
def session::archive(&mut this, format: strview, stage: &path, dist: &path, platform: strview, mtime: i64): path {
|
||||||
|
val holder := &this.space.projects[0];
|
||||||
|
val stem := $"{holder.name.view()}-{holder.semv.view()}-{platform}";
|
||||||
|
val items := staged(stage, stem.view());
|
||||||
|
if format == "zip" {
|
||||||
|
var made := zip { bytes = string::new(nothing {}), central = string::new(nothing {}), mtime = mtime };
|
||||||
|
for &item in items {
|
||||||
|
if item.folder {
|
||||||
|
made.folder(item.name.view());
|
||||||
|
} else {
|
||||||
|
val data := read_all(&item.source);
|
||||||
|
made.file(item.name.view(), data.view(), item.mode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val at := dist.join($"{stem.view()}.zip".view());
|
||||||
|
write_whole(&at, made.finish());
|
||||||
|
return at;
|
||||||
|
}
|
||||||
|
|
||||||
|
var made := tar { bytes = string::new(nothing {}), mtime = mtime };
|
||||||
|
add_items(&mut made, items.view(), strview::empty());
|
||||||
|
val at := dist.join($"{stem.view()}.tar".view());
|
||||||
|
write_whole(&at, made.finish());
|
||||||
|
return this.compressed(&at, "gzip", ".gz");
|
||||||
|
}
|
||||||
|
|
||||||
|
# Writes a Debian package: its control file and the digest of each file in a
|
||||||
|
# tar compressed by `gzip`, and the stage under the prefix in one compressed by
|
||||||
|
# `xz`, as members of an `ar` archive.
|
||||||
|
def session::debian(&mut this, stage: &path, dist: &path, prefix: strview, platform: strview, mtime: i64): path {
|
||||||
|
val holder := &this.space.projects[0];
|
||||||
|
val name := distro_name(holder);
|
||||||
|
val version := distro_version(holder);
|
||||||
|
val arch := distro_arch(platform, "deb", "amd64");
|
||||||
|
val items := staged(stage, prefix);
|
||||||
|
|
||||||
|
var sums := string::new(nothing {});
|
||||||
|
var size := 0u;
|
||||||
|
for &item in items {
|
||||||
|
continue if item.folder;
|
||||||
|
val data := read_all(&item.source);
|
||||||
|
size += data.length;
|
||||||
|
sums.append($"{md5(data.view()).view()} {item.name.view()}\n".view());
|
||||||
|
}
|
||||||
|
var control := string::new(nothing {});
|
||||||
|
control.append($"Package: {name.view()}\n".view());
|
||||||
|
control.append($"Version: {version.view()}-1\n".view());
|
||||||
|
control.append($"Architecture: {arch}\n".view());
|
||||||
|
control.append($"Maintainer: {or_else(holder.package.auth.view(), "Unknown <unknown@localhost>")}\n".view());
|
||||||
|
control.append($"Installed-Size: {(size + 1023) / 1024}\n".view());
|
||||||
|
control.append("Section: misc\nPriority: optional\n");
|
||||||
|
if holder.package.url.length != 0 {
|
||||||
|
control.append($"Homepage: {holder.package.url.view()}\n".view());
|
||||||
|
}
|
||||||
|
control.append($"Description: {or_else(holder.package.desc.view(), holder.name.view())}\n".view());
|
||||||
|
|
||||||
|
val work := dist.join("deb");
|
||||||
|
_ = management::remove_tree(&work);
|
||||||
|
_ = io::create_directories(&work);
|
||||||
|
|
||||||
|
var controls := tar { bytes = string::new(nothing {}), mtime = mtime };
|
||||||
|
controls.folder(".");
|
||||||
|
controls.file("./control", control.view(), 0o644);
|
||||||
|
controls.file("./md5sums", sums.view(), 0o644);
|
||||||
|
val control_at := work.join("control.tar");
|
||||||
|
write_whole(&control_at, controls.finish());
|
||||||
|
val control_gz := this.compressed(&control_at, "gzip", ".gz");
|
||||||
|
|
||||||
|
var data := tar { bytes = string::new(nothing {}), mtime = mtime };
|
||||||
|
data.folder(".");
|
||||||
|
add_items(&mut data, items.view(), "./");
|
||||||
|
val data_at := work.join("data.tar");
|
||||||
|
write_whole(&data_at, data.finish());
|
||||||
|
val data_xz := this.compressed(&data_at, "xz", ".xz");
|
||||||
|
|
||||||
|
var made := ar::new(mtime);
|
||||||
|
made.member("debian-binary", "2.0\n");
|
||||||
|
made.member("control.tar.gz", read_all(&control_gz).view());
|
||||||
|
made.member("data.tar.xz", read_all(&data_xz).view());
|
||||||
|
val at := dist.join($"{name.view()}_{version.view()}-1_{arch}.deb".view());
|
||||||
|
write_whole(&at, made.bytes.view());
|
||||||
|
_ = management::remove_tree(&work);
|
||||||
|
return at;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Writes an Arch package: its `.PKGINFO`, its `.MTREE` of each entry's
|
||||||
|
# digests, compressed by `gzip`, and the stage under the prefix, in a tar
|
||||||
|
# compressed by `zstd`.
|
||||||
|
def session::arch_package(&mut this, stage: &path, dist: &path, prefix: strview, platform: strview, mtime: i64): path {
|
||||||
|
val holder := &this.space.projects[0];
|
||||||
|
val name := distro_name(holder);
|
||||||
|
val version := distro_version(holder);
|
||||||
|
val arch := distro_arch(platform, "arch", "x86_64");
|
||||||
|
val items := staged(stage, prefix);
|
||||||
|
|
||||||
|
var size := 0u;
|
||||||
|
for &item in items {
|
||||||
|
continue if item.folder;
|
||||||
|
val found := io::info(&item.source) else (_) { continue; };
|
||||||
|
size += found.size;
|
||||||
|
}
|
||||||
|
var info := string::from("# Generated by blossom\n", nothing {});
|
||||||
|
info.append($"pkgname = {name.view()}\npkgbase = {name.view()}\nxdata = pkgtype=pkg\n".view());
|
||||||
|
info.append($"pkgver = {version.view()}-1\n".view());
|
||||||
|
info.append($"pkgdesc = {or_else(holder.package.desc.view(), holder.name.view())}\n".view());
|
||||||
|
if holder.package.url.length != 0 {
|
||||||
|
info.append($"url = {holder.package.url.view()}\n".view());
|
||||||
|
}
|
||||||
|
info.append($"builddate = {mtime}\n".view());
|
||||||
|
info.append($"packager = {or_else(holder.package.auth.view(), "Unknown Packager")}\n".view());
|
||||||
|
info.append($"size = {size}\narch = {arch}\n".view());
|
||||||
|
if holder.package.lic.length != 0 {
|
||||||
|
info.append($"license = {holder.package.lic.view()}\n".view());
|
||||||
|
}
|
||||||
|
|
||||||
|
var tree := string::from("#mtree\n/set type=file uid=0 gid=0 mode=644\n", nothing {});
|
||||||
|
tree.append($"./.PKGINFO time={mtime}.0 size={info.length} md5digest={md5(info.view()).view()} sha256digest={sha256(info.view()).view()}\n".view());
|
||||||
|
for &item in items {
|
||||||
|
val named := mtree_escaped(item.name.view());
|
||||||
|
if item.folder {
|
||||||
|
tree.append($"./{named.view()} time={mtime}.0 mode=755 type=dir\n".view());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
val data := read_all(&item.source);
|
||||||
|
var mode := string::new(nothing {});
|
||||||
|
if item.mode != 0o644 {
|
||||||
|
mode.append(" mode=");
|
||||||
|
mode.append(octal_text(item.mode).view());
|
||||||
|
}
|
||||||
|
tree.append($"./{named.view()} time={mtime}.0{mode.view()} size={data.length} md5digest={md5(data.view()).view()} sha256digest={sha256(data.view()).view()}\n".view());
|
||||||
|
}
|
||||||
|
|
||||||
|
val work := dist.join("arch");
|
||||||
|
_ = management::remove_tree(&work);
|
||||||
|
_ = io::create_directories(&work);
|
||||||
|
val tree_at := work.join("mtree");
|
||||||
|
write_whole(&tree_at, tree.view());
|
||||||
|
val tree_gz := this.compressed(&tree_at, "gzip", ".gz");
|
||||||
|
|
||||||
|
var made := tar { bytes = string::new(nothing {}), mtime = mtime };
|
||||||
|
made.file(".PKGINFO", info.view(), 0o644);
|
||||||
|
made.file(".MTREE", read_all(&tree_gz).view(), 0o644);
|
||||||
|
add_items(&mut made, items.view(), strview::empty());
|
||||||
|
val at := dist.join($"{name.view()}-{version.view()}-1-{arch}.pkg.tar".view());
|
||||||
|
write_whole(&at, made.finish());
|
||||||
|
_ = management::remove_tree(&work);
|
||||||
|
return this.compressed(&at, "zstd", ".zst");
|
||||||
|
}
|
||||||
|
|
||||||
|
# Builds an rpm package with `rpmbuild`, from a spec written for it that copies
|
||||||
|
# the stage under the prefix.
|
||||||
|
def session::rpm(&mut this, stage: &path, dist: &path, prefix: strview, platform: strview): path {
|
||||||
|
val holder := &this.space.projects[0];
|
||||||
|
val name := distro_name(holder);
|
||||||
|
val version := distro_version(holder);
|
||||||
|
val arch := distro_arch(platform, "rpm", "x86_64");
|
||||||
|
val summary := or_else(holder.package.desc.view(), holder.name.view());
|
||||||
|
|
||||||
|
var spec := string::from("%define debug_package %{nil}\n", nothing {});
|
||||||
|
spec.append($"Name: {name.view()}\nVersion: {version.view()}\nRelease: 1\nSummary: {summary}\n".view());
|
||||||
|
spec.append($"License: {or_else(holder.package.lic.view(), "Unspecified")}\n".view());
|
||||||
|
if holder.package.url.length != 0 {
|
||||||
|
spec.append($"URL: {holder.package.url.view()}\n".view());
|
||||||
|
}
|
||||||
|
spec.append($"BuildArch: {arch}\n\n%description\n{summary}\n\n%install\n".view());
|
||||||
|
spec.append("mkdir -p \"%{buildroot}/");
|
||||||
|
spec.append(prefix);
|
||||||
|
spec.append("\"\ncp -a \"");
|
||||||
|
spec.append(stage.view());
|
||||||
|
spec.append("/.\" \"%{buildroot}/");
|
||||||
|
spec.append(prefix);
|
||||||
|
spec.append("/\"\n\n%files\n%defattr(-,root,root,-)\n");
|
||||||
|
val items := staged(stage, prefix);
|
||||||
|
for &item in items {
|
||||||
|
continue if item.folder;
|
||||||
|
spec.append($"\"/{item.name.view()}\"\n".view());
|
||||||
|
}
|
||||||
|
|
||||||
|
val top := dist.join("rpmbuild");
|
||||||
|
val file := top.join("SPECS").join($"{name.view()}.spec".view());
|
||||||
|
if !written(&file, spec.view()) {
|
||||||
|
fatal(1, $"`{file.view()}` can't be written.".view());
|
||||||
|
}
|
||||||
|
var words := vector::<string>::new();
|
||||||
|
words.append(string::from("rpmbuild", nothing {}));
|
||||||
|
words.append(string::from("-bb", nothing {}));
|
||||||
|
words.append(string::from("--define", nothing {}));
|
||||||
|
words.append($"_topdir {top.view()}");
|
||||||
|
words.append(string::from(file.view(), nothing {}));
|
||||||
|
var building := this.command(words.view(), dist);
|
||||||
|
val status := building.run() else (_) {
|
||||||
|
fatal(1, $"rpm packages need `rpmbuild`, which isn't found; the spec is written to `{file.view()}`.".view());
|
||||||
|
return path::new();
|
||||||
|
};
|
||||||
|
if !status.succeeded {
|
||||||
|
fatal(1, "`rpmbuild` failed.");
|
||||||
|
}
|
||||||
|
|
||||||
|
# `rpmbuild` puts the package in a folder of its architecture.
|
||||||
|
val built := top.join("RPMS").join(arch).join($"{name.view()}-{version.view()}-1.{arch}.rpm".view());
|
||||||
|
val at := dist.join($"{name.view()}-{version.view()}-1.{arch}.rpm".view());
|
||||||
|
if io::rename(&built, &at) is error(why) {
|
||||||
|
fatal(1, $"`{built.view()}` can't be moved to `{at.view()}`: {why}.".view());
|
||||||
|
}
|
||||||
|
return at;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Adds entries to a tar archive, each name after a prefix, as `./`.
|
||||||
|
def add_items(made: &mut tar, items: span<entry>, before: strview): void {
|
||||||
|
for &item in items {
|
||||||
|
val name := $"{before}{item.name.view()}";
|
||||||
|
if item.folder {
|
||||||
|
made.folder(name.view());
|
||||||
|
} else {
|
||||||
|
val data := read_all(&item.source);
|
||||||
|
made.file(name.view(), data.view(), item.mode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Compresses a file with a program of the system, `gzip`, `xz`, or `zstd`,
|
||||||
|
# which replaces it with one named with a suffix, as `.gz`, giving its path.
|
||||||
|
def session::compressed(&mut this, at: &path, program: strview, suffix: strview): path {
|
||||||
|
var words := vector::<string>::new();
|
||||||
|
words.append(string::from(program, nothing {}));
|
||||||
|
if program == "gzip" {
|
||||||
|
words.append(string::from("-n", nothing {}));
|
||||||
|
words.append(string::from("-9", nothing {}));
|
||||||
|
} else if program == "zstd" {
|
||||||
|
words.append(string::from("-q", nothing {}));
|
||||||
|
words.append(string::from("--rm", nothing {}));
|
||||||
|
words.append(string::from("-19", nothing {}));
|
||||||
|
}
|
||||||
|
words.append(string::from("-f", nothing {}));
|
||||||
|
words.append(string::from(at.view(), nothing {}));
|
||||||
|
val folder := at.parent() else { return path::new(); };
|
||||||
|
var compressing := this.command(words.view(), &folder);
|
||||||
|
val status := compressing.run() else (why) {
|
||||||
|
fatal(1, $"`{program}` can't be run, to compress `{at.view()}`: {why}.".view());
|
||||||
|
return path::new();
|
||||||
|
};
|
||||||
|
if !status.succeeded {
|
||||||
|
fatal(1, $"`{program}` failed to compress `{at.view()}`.".view());
|
||||||
|
}
|
||||||
|
return path::parse($"{at.view()}{suffix}".view()) else (_) { return path::new(); };
|
||||||
|
}
|
||||||
|
|
||||||
|
# When a package's entries were written: `SOURCE_DATE_EPOCH`, where it's set,
|
||||||
|
# else now.
|
||||||
|
def build_time(): i64 {
|
||||||
|
if process::variable("SOURCE_DATE_EPOCH") is some(given) {
|
||||||
|
if integer::parse::<i64>(given) is value(seconds) {
|
||||||
|
return seconds;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return time::timestamp::now().unix;
|
||||||
|
}
|
||||||
|
|
||||||
|
# The project's name as a system's package names it: in lower case, with `-`
|
||||||
|
# for `_`, two or more letters, digits, `.`, `+`, or `-`, starting with a
|
||||||
|
# letter or digit.
|
||||||
|
def distro_name(holder: &models::project): string {
|
||||||
|
var made := holder.name.lower();
|
||||||
|
for k in 0..made.length {
|
||||||
|
if made[k] == '_' {
|
||||||
|
made[k] = '-';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var fits := made.length >= 2 && made[0].is_alnum;
|
||||||
|
for k in 0..made.length {
|
||||||
|
val byte := made[k];
|
||||||
|
fits = fits && (byte.is_alnum || byte == '.' || byte == '+' || byte == '-');
|
||||||
|
}
|
||||||
|
if !fits {
|
||||||
|
fatal(2, $"`{holder.name.view()}` can't name a system's package: it needs two or more letters, digits, `.`, `+`, or `-`.".view());
|
||||||
|
}
|
||||||
|
return made;
|
||||||
|
}
|
||||||
|
|
||||||
|
# The project's version as a system's package takes it: starting with a digit,
|
||||||
|
# with only letters, digits, `.`, `+`, or `~`.
|
||||||
|
def distro_version(holder: &models::project): string {
|
||||||
|
val text := holder.semv.view();
|
||||||
|
var fits := text.length != 0 && text[0].is_digit;
|
||||||
|
for k in 0..text.length {
|
||||||
|
val byte := text[k];
|
||||||
|
fits = fits && (byte.is_alnum || byte == '.' || byte == '+' || byte == '~');
|
||||||
|
}
|
||||||
|
if !fits {
|
||||||
|
fatal(2, $"{holder.identity.view()}'s version, `{text}`, can't be a system package's: it starts with a digit, and holds only letters, digits, `.`, `+`, or `~`.".view());
|
||||||
|
}
|
||||||
|
return string::from(text, nothing {});
|
||||||
|
}
|
||||||
|
|
||||||
|
# The architecture a system's packages name x86-64 by, where they're for Linux.
|
||||||
|
def distro_arch(platform: strview, format: strview, name: strview): strview {
|
||||||
|
if platform != "linux-x86_64" {
|
||||||
|
fatal(2, $"{format} packages are for Linux, not `{platform}`.".view());
|
||||||
|
}
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
# A path in `.MTREE`, each byte but letters, digits, and `._/+-` written as `\`
|
||||||
|
# and three octal digits.
|
||||||
|
def mtree_escaped(text: strview): string {
|
||||||
|
var made := string::new(nothing {});
|
||||||
|
for k in 0..text.length {
|
||||||
|
val byte := text[k];
|
||||||
|
if byte.is_alnum || byte == '.' || byte == '_' || byte == '/' || byte == '+' || byte == '-' {
|
||||||
|
made.push(byte);
|
||||||
|
} else {
|
||||||
|
made.push('\\');
|
||||||
|
made.push('0' + (byte >> 6));
|
||||||
|
made.push('0' + ((byte >> 3) & 7));
|
||||||
|
made.push('0' + (byte & 7));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return made;
|
||||||
|
}
|
||||||
|
|
||||||
|
# A mode in octal, as `755`.
|
||||||
|
def octal_text(mode: u32): string {
|
||||||
|
var made := string::new(nothing {});
|
||||||
|
made.push('0' + ((mode >> 6) & 7) as u8);
|
||||||
|
made.push('0' + ((mode >> 3) & 7) as u8);
|
||||||
|
made.push('0' + (mode & 7) as u8);
|
||||||
|
return made;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Text, or another where it's empty.
|
||||||
|
def or_else(text: strview, fallback: strview): strview {
|
||||||
|
return text.length != 0 ? text : fallback;
|
||||||
|
}
|
||||||
|
|
||||||
|
# The whole of a file, stopping the command where it can't be read.
|
||||||
|
def read_all(at: &path): string {
|
||||||
|
return io::read_file(at) else (why) {
|
||||||
|
fatal(1, $"`{at.view()}` can't be read: {why}.".view());
|
||||||
|
return string::new(nothing {});
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
# Writes a file whole, stopping the command where it can't be written.
|
||||||
|
def write_whole(at: &path, bytes: strview): void {
|
||||||
|
if !written(at, bytes) {
|
||||||
|
fatal(1, $"`{at.view()}` can't be written.".view());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# A prefix as a package holds it: without the `/` it starts or ends with.
|
||||||
|
def trimmed(text: strview): strview {
|
||||||
|
var start := 0u;
|
||||||
|
var end := text.length;
|
||||||
|
while start < end && text[start] == '/' {
|
||||||
|
start++;
|
||||||
|
}
|
||||||
|
while end > start && text[end - 1] == '/' {
|
||||||
|
end--;
|
||||||
|
}
|
||||||
|
return text.substr(start, end - start);
|
||||||
|
}
|
||||||
@@ -0,0 +1,232 @@
|
|||||||
|
#! @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;
|
||||||
|
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})".view());
|
||||||
|
} else {
|
||||||
|
io::println($"[{this.step}/{this.steps}] {what} {name}".view());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#! 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}".view());
|
||||||
|
}
|
||||||
|
|
||||||
|
#! 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}".view());
|
||||||
|
}
|
||||||
|
|
||||||
|
#! The words starting a `sakura` command: the program, the command, and the
|
||||||
|
#! standard library where the config names one.
|
||||||
|
#! @param this The session.
|
||||||
|
#! @param command `check`, `run`, or `build`.
|
||||||
|
#! @returns The words.
|
||||||
|
pub def session::words(&this, command: strview): vector<string> {
|
||||||
|
var made := vector::<string>::new();
|
||||||
|
made.append(string::copy_of(&this.sakura));
|
||||||
|
made.append(string::from(command, nothing {}));
|
||||||
|
if this.std.length != 0 {
|
||||||
|
made.append(string::from("--std", nothing {}));
|
||||||
|
made.append(string::copy_of(&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<string>): 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<string>, cwd: &path): result<process::status, io_error> {
|
||||||
|
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, and `SAKURA_STD` to the standard library the config
|
||||||
|
#! names, 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<string>, cwd: &path): process::command {
|
||||||
|
if this.space.verbose {
|
||||||
|
io::println(spoken(words).view());
|
||||||
|
}
|
||||||
|
var made := process::command::new(words[0].view());
|
||||||
|
for k in 1..words.length {
|
||||||
|
made.argument(words[k].view());
|
||||||
|
}
|
||||||
|
made.directory(cwd);
|
||||||
|
made.set_variable("SAKURA", this.sakura.view());
|
||||||
|
if this.std.length != 0 {
|
||||||
|
made.set_variable("SAKURA_STD", this.std.view());
|
||||||
|
}
|
||||||
|
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>): 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.view());
|
||||||
|
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>): string {
|
||||||
|
var made := string::copy_of(&this.sakura);
|
||||||
|
var written: i64 = 0;
|
||||||
|
if path::parse(this.sakura.view()) is value(&at) {
|
||||||
|
if io::info(&at) is value(&found) {
|
||||||
|
written = found.modified;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
made.append($"\n{written}".view());
|
||||||
|
for &each in words {
|
||||||
|
made.push('\n');
|
||||||
|
made.append(each.view());
|
||||||
|
}
|
||||||
|
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, 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.view() != now;
|
||||||
|
val read := io::read_file(deps) else (_) { return false; };
|
||||||
|
return false if newer(&holder.file, made.modified);
|
||||||
|
for line in read.view().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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,401 @@
|
|||||||
|
#! @file steps.sakura
|
||||||
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||||
|
#! @copyright Malunal Studios, LLC.
|
||||||
|
#! @license MIT
|
||||||
|
#!
|
||||||
|
#! Contains the steps of `check`, `build`, `test`, and `run`: a library checked,
|
||||||
|
#! a program built, a test run, and a program run on the machine, each one
|
||||||
|
#! `sakura` command run in the project's folder, with `--lib` for every library
|
||||||
|
#! its `deps` reach.
|
||||||
|
import std;
|
||||||
|
import std::io;
|
||||||
|
import std::process;
|
||||||
|
import system;
|
||||||
|
import blossom::models;
|
||||||
|
import blossom::management;
|
||||||
|
|
||||||
|
module blossom::pipeline;
|
||||||
|
|
||||||
|
# Bytes read at a time from a test's output, which is passed on as it comes.
|
||||||
|
const passing_bytes: u64 = 4096;
|
||||||
|
|
||||||
|
#! Checks the project's libraries, then builds its programs, each where it
|
||||||
|
#! isn't up to date, and, for `test`, runs its tests after.
|
||||||
|
#! @param this The session.
|
||||||
|
#! @param tests Whether the tests are run, as `test` asks.
|
||||||
|
#! @param only Text a test's name holds to be run, or empty for every test.
|
||||||
|
pub def session::build(&mut this, tests: bool, only: strview): void {
|
||||||
|
val root := &this.space.projects[0];
|
||||||
|
var chosen := vector::<u64>::new();
|
||||||
|
if tests {
|
||||||
|
for k in 0..root.tests.length {
|
||||||
|
if only.length == 0 || root.tests[k].name.view().find(only) is some {
|
||||||
|
chosen.append(k);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if chosen.length == 0 {
|
||||||
|
fatal(2, only.length == 0 ? $"{root.identity.view()} has no tests." : $"No test of {root.identity.view()} is named with `{only}`.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.steps = root.targets.length + chosen.length;
|
||||||
|
|
||||||
|
var broken := vector::<u64>::new(); # The libraries that failed to check.
|
||||||
|
for t in 0..root.targets.length {
|
||||||
|
continue if this.space.projects[0].targets[t].kind != models::target_kind::library;
|
||||||
|
if !this.check_library(t, true) {
|
||||||
|
val name := &this.space.projects[0].targets[t].name;
|
||||||
|
broken.append(this.space.library_named(name.view()).some);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for t in 0..root.targets.length {
|
||||||
|
continue if this.space.projects[0].targets[t].kind != models::target_kind::program;
|
||||||
|
_ = this.build_program(t, false, &broken);
|
||||||
|
}
|
||||||
|
for &k in chosen {
|
||||||
|
_ = this.run_test(k, &broken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#! Checks every target of the project, its libraries and programs, writing
|
||||||
|
#! nothing.
|
||||||
|
#! @param this The session.
|
||||||
|
pub def session::check(&mut this): void {
|
||||||
|
this.steps = this.space.projects[0].targets.length;
|
||||||
|
for t in 0..this.steps {
|
||||||
|
if this.space.projects[0].targets[t].kind == models::target_kind::library {
|
||||||
|
_ = this.check_library(t, false);
|
||||||
|
} else {
|
||||||
|
_ = this.check_program(t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#! Runs a program target on the machine, as `sakura run` does.
|
||||||
|
#! @param this The session.
|
||||||
|
#! @param name The target, or empty for the project's only program.
|
||||||
|
#! @param args What the program is given.
|
||||||
|
#! @returns The status it stopped with.
|
||||||
|
pub def session::run_program(&mut this, name: strview, args: span<strview>): i32 {
|
||||||
|
val root := &this.space.projects[0];
|
||||||
|
var chosen := root.targets.length;
|
||||||
|
for t in 0..root.targets.length {
|
||||||
|
continue if root.targets[t].kind != models::target_kind::program;
|
||||||
|
if name.length == 0 {
|
||||||
|
if chosen != root.targets.length {
|
||||||
|
fatal(2, $"{root.identity.view()} has more than one program: name the one to run.");
|
||||||
|
}
|
||||||
|
chosen = t;
|
||||||
|
} else if root.targets[t].name.view() == name {
|
||||||
|
chosen = t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if chosen == root.targets.length {
|
||||||
|
fatal(2, name.length == 0 ? $"{root.identity.view()} has no program to run." : $"`{name}` is no program of {root.identity.view()}.");
|
||||||
|
}
|
||||||
|
|
||||||
|
val each := &this.space.projects[0].targets[chosen];
|
||||||
|
var words := this.words("run");
|
||||||
|
this.add_libraries(each, &mut words);
|
||||||
|
this.add_threads(&mut words);
|
||||||
|
this.add_sources(each, &mut words);
|
||||||
|
if args.length != 0 {
|
||||||
|
words.append(string::from("--", nothing {}));
|
||||||
|
for &arg in args {
|
||||||
|
words.append(string::from(arg, nothing {}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val status := this.run(words.view(), &this.space.projects[0].folder) else (why) {
|
||||||
|
fatal(1, $"`{this.sakura.view()}` can't be run: {why}.");
|
||||||
|
return 1;
|
||||||
|
};
|
||||||
|
return exit_code(&status);
|
||||||
|
}
|
||||||
|
|
||||||
|
# Checks a library: its files, with `--lib` for those it reads. Where `fresh` is
|
||||||
|
# asked, a stamp kept with the command and the files read says it's checked, so
|
||||||
|
# it's checked again only where those change.
|
||||||
|
def session::check_library(&mut this, t: u64, fresh: bool): bool {
|
||||||
|
val holder := &this.space.projects[0];
|
||||||
|
val each := &holder.targets[t];
|
||||||
|
var files := management::library_files(holder, each);
|
||||||
|
if files is error(&why) {
|
||||||
|
fatal(why.status, why.message.view());
|
||||||
|
}
|
||||||
|
|
||||||
|
var words := this.words("check");
|
||||||
|
this.add_libraries(each, &mut words);
|
||||||
|
this.add_threads(&mut words);
|
||||||
|
val base := $"{output_text(holder).view()}/check/{each.name.view()}";
|
||||||
|
if fresh {
|
||||||
|
words.append(string::from("--deps", nothing {}));
|
||||||
|
words.append($"{base.view()}.deps");
|
||||||
|
}
|
||||||
|
if files is value(&listed) {
|
||||||
|
for &file in listed {
|
||||||
|
words.append(string::copy_of(file));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return this.made(t, words.view(), fresh ? base.view() : strview::empty(), "Checked", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
# Checks a program, as `check` does, writing nothing.
|
||||||
|
def session::check_program(&mut this, t: u64): bool {
|
||||||
|
val each := &this.space.projects[0].targets[t];
|
||||||
|
var words := this.words("check");
|
||||||
|
this.add_libraries(each, &mut words);
|
||||||
|
val system := this.platform_of(each);
|
||||||
|
if system.length != 0 {
|
||||||
|
words.append(string::from("--target", nothing {}));
|
||||||
|
words.append(string::from(system, nothing {}));
|
||||||
|
}
|
||||||
|
this.add_threads(&mut words);
|
||||||
|
this.add_sources(each, &mut words);
|
||||||
|
return this.made(t, words.view(), strview::empty(), "Checked", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
# Builds a program, or a test that runs natively, into the output folder's
|
||||||
|
# `bin`, unless it's up to date, or a library of the project it reads failed.
|
||||||
|
def session::build_program(&mut this, t: u64, test: bool, broken: &vector<u64>): bool {
|
||||||
|
val holder := &this.space.projects[0];
|
||||||
|
val index := test ? holder.targets.length + t : t;
|
||||||
|
val each := &holder.at(index);
|
||||||
|
if this.reaches_broken(each, broken) {
|
||||||
|
this.skip(each.name.view());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
val system := this.platform_of(each);
|
||||||
|
val output := $"{output_text(holder).view()}/bin/{output_name(each.name.view(), system).view()}";
|
||||||
|
var words := this.words("build");
|
||||||
|
this.add_libraries(each, &mut words);
|
||||||
|
words.append($"-O{each.level}");
|
||||||
|
if each.petal {
|
||||||
|
words.append(string::from("-g", nothing {}));
|
||||||
|
}
|
||||||
|
if system.length != 0 {
|
||||||
|
words.append(string::from("--target", nothing {}));
|
||||||
|
words.append(string::from(system, nothing {}));
|
||||||
|
}
|
||||||
|
if test && each.checks {
|
||||||
|
words.append(string::from("--checks", nothing {}));
|
||||||
|
}
|
||||||
|
this.add_threads(&mut words);
|
||||||
|
for &opt in each.opts {
|
||||||
|
words.append(string::copy_of(opt));
|
||||||
|
}
|
||||||
|
words.append(string::from("--deps", nothing {}));
|
||||||
|
words.append($"{output.view()}.deps");
|
||||||
|
words.append(string::from("-o", nothing {}));
|
||||||
|
words.append(string::copy_of(&output));
|
||||||
|
this.add_sources(each, &mut words);
|
||||||
|
return this.made(index, words.view(), output.view(), test ? "" : "Built", output.view());
|
||||||
|
}
|
||||||
|
|
||||||
|
# Runs a command that checks or builds a target, unless its output, where it
|
||||||
|
# has one, is up to date, and says how it went. A test built is said by its
|
||||||
|
# run, so its build is said only where it fails.
|
||||||
|
def session::made(&mut this, index: u64, words: span<string>, output: strview, verb: strview, more: strview): bool {
|
||||||
|
val holder := &this.space.projects[0];
|
||||||
|
val name := &holder.at(index).name;
|
||||||
|
val now := this.made_by(words);
|
||||||
|
if output.length != 0 {
|
||||||
|
val out := holder.folder.join(output);
|
||||||
|
val kept := holder.folder.join($"{output}.command".view());
|
||||||
|
val deps := holder.folder.join($"{output}.deps".view());
|
||||||
|
if is_fresh(&out, &kept, now.view(), &deps, holder) {
|
||||||
|
if verb.length != 0 {
|
||||||
|
this.said("Up to date", name.view());
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
_ = io::remove(&kept);
|
||||||
|
if out.parent() is some(&above) {
|
||||||
|
_ = io::create_directories(above);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val status := this.run(words, &holder.folder) else (why) {
|
||||||
|
fatal(1, $"`{this.sakura.view()}` can't be run: {why}.");
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
if !status.succeeded {
|
||||||
|
this.fail(name.view());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if output.length != 0 {
|
||||||
|
val kept := holder.folder.join($"{output}.command".view());
|
||||||
|
if verb == "Checked" {
|
||||||
|
_ = written(&holder.folder.join(output), "");
|
||||||
|
}
|
||||||
|
_ = written(&kept, now.view());
|
||||||
|
}
|
||||||
|
if verb.length != 0 {
|
||||||
|
this.said(verb, name.view(), more);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Runs a test: on the machine, as `sakura run`, with `--checks` unless it says
|
||||||
|
# otherwise, or built, then run. What it writes is passed on as it comes, and
|
||||||
|
# the count its last line gives, as `14 passed`, is said with it.
|
||||||
|
def session::run_test(&mut this, k: u64, broken: &vector<u64>): bool {
|
||||||
|
val holder := &this.space.projects[0];
|
||||||
|
val each := &holder.tests[k];
|
||||||
|
var words := vector::<string>::new();
|
||||||
|
if each.runs == models::runner::native {
|
||||||
|
return false if !this.build_program(k, true, broken);
|
||||||
|
val output := $"{output_text(holder).view()}/bin/{output_name(each.name.view(), this.platform_of(each)).view()}";
|
||||||
|
words.append(string::from(holder.folder.join(output.view()).view(), nothing {}));
|
||||||
|
for &arg in each.args {
|
||||||
|
words.append(string::copy_of(arg));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if this.reaches_broken(each, broken) {
|
||||||
|
this.skip(each.name.view());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
words = this.words("run");
|
||||||
|
this.add_libraries(each, &mut words);
|
||||||
|
if each.checks {
|
||||||
|
words.append(string::from("--checks", nothing {}));
|
||||||
|
}
|
||||||
|
this.add_threads(&mut words);
|
||||||
|
this.add_sources(each, &mut words);
|
||||||
|
if each.args.length != 0 {
|
||||||
|
words.append(string::from("--", nothing {}));
|
||||||
|
for &arg in each.args {
|
||||||
|
words.append(string::copy_of(arg));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var running := this.command(words.view(), &holder.folder);
|
||||||
|
running.output(process::stdio::piped);
|
||||||
|
var child := running.spawn() else (why) {
|
||||||
|
fatal(1, $"`{words[0].view()}` can't be run: {why}.");
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
# The output is passed on as it comes, keeping its last line.
|
||||||
|
var last := string::new(nothing {});
|
||||||
|
var chunk: u8[passing_bytes];
|
||||||
|
val into := slice::<u8>::over((&mut chunk) as *mut u8, passing_bytes);
|
||||||
|
while true {
|
||||||
|
val count := child.output().read(into) else (_) { break; };
|
||||||
|
break if count == 0;
|
||||||
|
val text := strview::over((&chunk) as *u8, count);
|
||||||
|
_ = system::fd::stdout.put(text);
|
||||||
|
last.append(text);
|
||||||
|
if last.length > 1024 {
|
||||||
|
val keep := last.view().substr(last.length - 512, 512);
|
||||||
|
last = string::from(keep, nothing {});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val status := child.wait() else (why) {
|
||||||
|
fatal(1, $"`{words[0].view()}` couldn't be waited for: {why}.");
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
if !status.succeeded {
|
||||||
|
this.fail(each.name.view());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
val counted := tally_of(last.view());
|
||||||
|
this.said("Tested", each.name.view(), counted);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
# The count a test's last line gives, as `14 passed` of `14 passed, 0 failed,
|
||||||
|
# 0 skipped`, or empty where it gives none.
|
||||||
|
def tally_of(text: strview): strview {
|
||||||
|
var end := text.length;
|
||||||
|
while end > 0 && (text[end - 1] == '\n' || text[end - 1] == '\r') {
|
||||||
|
end--;
|
||||||
|
}
|
||||||
|
var start := end;
|
||||||
|
while start > 0 && text[start - 1] != '\n' {
|
||||||
|
start--;
|
||||||
|
}
|
||||||
|
val line := text.substr(start, end - start);
|
||||||
|
val comma := line.find(",") else { return strview::empty(); };
|
||||||
|
val first := line.substr(0, comma);
|
||||||
|
return first.ends_with(" passed") ? first : strview::empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
# Adds `--lib` for every library a target's `deps` reach.
|
||||||
|
def session::add_libraries(&mut this, each: &models::target, words: &mut vector<string>): void {
|
||||||
|
var reached := vector::<u64>::new();
|
||||||
|
var found := this.space.reach(0, each.deps.view(), each.name.view(), &mut reached);
|
||||||
|
if found is error(&why) {
|
||||||
|
fatal(why.status, why.message.view());
|
||||||
|
}
|
||||||
|
for &library in reached {
|
||||||
|
words.append(string::from("--lib", nothing {}));
|
||||||
|
words.append(this.space.lib_option(library, 0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Adds a target's files, as its `srcs` give them.
|
||||||
|
def session::add_sources(&mut this, each: &models::target, words: &mut vector<string>): void {
|
||||||
|
var files := management::sources_of(&this.space.projects[0], each);
|
||||||
|
if files is error(&why) {
|
||||||
|
fatal(why.status, why.message.view());
|
||||||
|
}
|
||||||
|
if files is value(&mut listed) {
|
||||||
|
for &mut file in listed {
|
||||||
|
words.append(move(file));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Whether a target reads a library of the project that failed to check.
|
||||||
|
def session::reaches_broken(&mut this, each: &models::target, broken: &vector<u64>): bool {
|
||||||
|
return false if broken.length == 0;
|
||||||
|
var reached := vector::<u64>::new();
|
||||||
|
_ = this.space.reach(0, each.deps.view(), each.name.view(), &mut reached);
|
||||||
|
for &library in reached {
|
||||||
|
for &failed in broken {
|
||||||
|
return true if library == failed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
# What a target is built for: what `--target` says, else its own `target`,
|
||||||
|
# else nothing, for the system blossom runs on.
|
||||||
|
def session::platform_of(&this, each: &models::target): strview {
|
||||||
|
if this.space.platform.length != 0 {
|
||||||
|
return this.space.platform.view();
|
||||||
|
}
|
||||||
|
return each.system.view();
|
||||||
|
}
|
||||||
|
|
||||||
|
#! The file a program is built into: its name, with `.exe` where it's for
|
||||||
|
#! Windows, or blossom runs there and it's built for the system blossom runs on.
|
||||||
|
#! @param name The program's name.
|
||||||
|
#! @param platform What it's built for, or empty.
|
||||||
|
#! @returns The file's name.
|
||||||
|
pub def output_name(name: strview, platform: strview): string {
|
||||||
|
if platform == "windows-x86_64" {
|
||||||
|
return $"{name}.exe";
|
||||||
|
}
|
||||||
|
if platform.length == 0 {
|
||||||
|
return management::program_file(name);
|
||||||
|
}
|
||||||
|
return string::from(name, nothing {});
|
||||||
|
}
|
||||||
|
|
||||||
|
#! A project's output folder, relative to its own: `.blossom/` and its key.
|
||||||
|
#! @param holder The project.
|
||||||
|
#! @returns The folder, written with `/`.
|
||||||
|
pub def output_text(holder: &models::project): string {
|
||||||
|
return $".blossom/{holder.key.view()}";
|
||||||
|
}
|
||||||
|
|
||||||
|
#! Says why the command can't go on, and stops it with a status.
|
||||||
|
#! @param status 1 where something failed, and 2 where a blossomfile or the
|
||||||
|
#! command is wrong.
|
||||||
|
#! @param message What's wrong.
|
||||||
|
pub def fatal(status: i32, message: strview): void {
|
||||||
|
_ = system::fd::stderr.put($"blossom: {message}\n".view());
|
||||||
|
system::exit(status);
|
||||||
|
}
|
||||||
@@ -0,0 +1,129 @@
|
|||||||
|
# Tests of `build`, `check`, `run`, and `clean`: a program, a program with a
|
||||||
|
# library of its own, building only what changed, a check that fails, and
|
||||||
|
# building for Windows.
|
||||||
|
import std;
|
||||||
|
import std::io;
|
||||||
|
import std::testing;
|
||||||
|
|
||||||
|
@test def a_program_is_built_then_run(t: &mut tester, box: sandbox): void {
|
||||||
|
box.write("one/blossomfile", """
|
||||||
|
name: one
|
||||||
|
gpid: tests
|
||||||
|
semv: 0.1.0
|
||||||
|
|
||||||
|
targets:
|
||||||
|
- name: one
|
||||||
|
type: program
|
||||||
|
srcs: [main.sakura]
|
||||||
|
""");
|
||||||
|
box.write("one/main.sakura", """
|
||||||
|
import std;
|
||||||
|
import std::io;
|
||||||
|
|
||||||
|
def main(args: span<strview>): void {
|
||||||
|
io::println($"given {args.length}".view());
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
val built := box.blossom("one", ["build"]);
|
||||||
|
t.expects(built.status == 0, built.error.view());
|
||||||
|
t.expects(built.says("[1/1] Built one (.blossom/") && built.says("Built tests/one@0.1.0"));
|
||||||
|
|
||||||
|
# The executable is where blossom said.
|
||||||
|
val output := built_path(built.output.view(), "one");
|
||||||
|
val ran := box.run($"{box.at.view()}/one/{output}".view(), "one", ["a", "b"]);
|
||||||
|
t.expects(ran.says("given 2"));
|
||||||
|
|
||||||
|
val again := box.blossom("one", ["run", "--", "x", "y", "z"]);
|
||||||
|
t.expects(again.status == 0 && again.says("given 3"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@test def a_program_reads_a_library_of_its_project(t: &mut tester, box: sandbox): void {
|
||||||
|
box.hello("hello");
|
||||||
|
val built := box.blossom("hello", ["build"]);
|
||||||
|
t.expects(built.status == 0, built.error.view());
|
||||||
|
t.expects(built.says("[1/2] Checked hello.geometry") && built.says("[2/2] Built hello"));
|
||||||
|
val ran := box.blossom("hello", ["run", "--", "1"]);
|
||||||
|
t.expects(ran.says("area 12 cube 8 given 1"));
|
||||||
|
|
||||||
|
# `check` checks each target and writes nothing.
|
||||||
|
box.hello("other");
|
||||||
|
val checked := box.blossom("other", ["check"]);
|
||||||
|
t.expects(checked.status == 0 && checked.says("[1/2] Checked hello") && checked.says("[2/2] Checked hello.geometry"));
|
||||||
|
t.expects(!box.has("other/.blossom"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@test def only_what_changed_is_built_again(t: &mut tester, box: sandbox): void {
|
||||||
|
box.hello("hello");
|
||||||
|
t.expects(box.blossom("hello", ["build"]).status == 0);
|
||||||
|
|
||||||
|
# Nothing changed: `sakura` isn't run.
|
||||||
|
val again := box.blossom("hello", ["build", "-v"]);
|
||||||
|
t.expects(again.says("[1/2] Up to date hello.geometry") && again.says("[2/2] Up to date hello"));
|
||||||
|
t.expects(!again.says("sakura"));
|
||||||
|
|
||||||
|
# A file of the library: it's checked again, and the program built again.
|
||||||
|
box.write("hello/hello.geometry/solids/solids.sakura", """
|
||||||
|
module hello::geometry::solids;
|
||||||
|
|
||||||
|
pub def cube(side: i64): i64 { return side * side * side + 0; }
|
||||||
|
""");
|
||||||
|
val touched := box.blossom("hello", ["build"]);
|
||||||
|
t.expects(touched.says("[1/2] Checked hello.geometry") && touched.says("[2/2] Built hello"));
|
||||||
|
|
||||||
|
# The program's own file: only it's built again.
|
||||||
|
box.write("hello/main.sakura", box.read("hello/main.sakura").view());
|
||||||
|
val program := box.blossom("hello", ["build"]);
|
||||||
|
t.expects(program.says("[1/2] Up to date hello.geometry") && program.says("[2/2] Built hello"));
|
||||||
|
|
||||||
|
# Another option value builds in a folder of its own, and the first stays up
|
||||||
|
# to date.
|
||||||
|
val other := box.blossom("hello", ["build", "-o", "level=0"]);
|
||||||
|
t.expects(other.says("[2/2] Built hello") && built_path(other.output.view(), "hello") != built_path(program.output.view(), "hello"));
|
||||||
|
t.expects(box.blossom("hello", ["build"]).says("[2/2] Up to date hello"));
|
||||||
|
|
||||||
|
# Changing the blossomfile builds again.
|
||||||
|
val text := box.read("hello/blossomfile");
|
||||||
|
box.write("hello/blossomfile", $"{text.view()}\n".view());
|
||||||
|
t.expects(box.blossom("hello", ["build"]).says("[2/2] Built hello"));
|
||||||
|
|
||||||
|
# `clean` removes this option set's outputs, and `--all` every set's.
|
||||||
|
val cleaned := box.blossom("hello", ["clean"]);
|
||||||
|
t.expects(cleaned.status == 0 && cleaned.says("Cleaned tests/hello@1.0.0"));
|
||||||
|
t.expects(box.has("hello/.blossom") && box.blossom("hello", ["clean"]).says("Nothing to clean"));
|
||||||
|
t.expects(box.blossom("hello", ["clean", "--all"]).status == 0 && !box.has("hello/.blossom"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@test def a_program_that_fails_to_check_fails_the_build(t: &mut tester, box: sandbox): void {
|
||||||
|
box.hello("hello");
|
||||||
|
box.write("hello/main.sakura", """
|
||||||
|
def main(): void {
|
||||||
|
val wrong: i64 = "text";
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
val built := box.blossom("hello", ["build"]);
|
||||||
|
t.expects(built.status == 1 && built.says("Failed hello") && !built.says("Built tests/hello"));
|
||||||
|
t.expects(built.error.view().find("main.sakura:2") is some, built.error.view());
|
||||||
|
|
||||||
|
# A library that fails to check skips the programs reading it.
|
||||||
|
box.hello("broken");
|
||||||
|
box.write("broken/hello.geometry/shapes.sakura", """
|
||||||
|
module hello::geometry;
|
||||||
|
|
||||||
|
pub def area(w: i64, h: i64): i64 { return "no"; }
|
||||||
|
""");
|
||||||
|
val skipped := box.blossom("broken", ["build"]);
|
||||||
|
t.expects(skipped.status == 1 && skipped.says("Failed hello.geometry") && skipped.says("Skipped hello"));
|
||||||
|
|
||||||
|
# `check` checks every target, so the program fails there too.
|
||||||
|
val checked := box.blossom("broken", ["check"]);
|
||||||
|
t.expects(checked.status == 1 && checked.says("Failed hello\n") && checked.says("Failed hello.geometry\n"), checked.output.view());
|
||||||
|
}
|
||||||
|
|
||||||
|
@test def a_target_builds_an_executable_for_windows(t: &mut tester, box: sandbox): void {
|
||||||
|
box.hello("hello");
|
||||||
|
val built := box.blossom("hello", ["build", "--target", "windows-x86_64"]);
|
||||||
|
t.expects(built.status == 0, built.error.view());
|
||||||
|
val output := built_path(built.output.view(), "hello");
|
||||||
|
t.expects(output.ends_with("/bin/hello.exe"), output);
|
||||||
|
t.expects(box.read($"hello/{output}".view()).view().starts_with("MZ"));
|
||||||
|
}
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
# Tests of `test`, `install`, and `package`: tests passing, failing, run
|
||||||
|
# natively, and chosen by name; installing into a prefix and under a folder;
|
||||||
|
# and packages of tar and zip.
|
||||||
|
import std;
|
||||||
|
import std::io;
|
||||||
|
import std::testing;
|
||||||
|
|
||||||
|
@test def tests_run_and_say_how_many_passed(t: &mut tester, box: sandbox): void {
|
||||||
|
box.hello("hello");
|
||||||
|
val tested := box.blossom("hello", ["test"]);
|
||||||
|
t.expects(tested.status == 0 && tested.says("[3/3] Tested hello.tests (2 passed)") && tested.says("[ OK ] cubes_cube"), tested.error.view());
|
||||||
|
t.expects(tested.says("Tested tests/hello@1.0.0"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@test def a_test_that_fails_fails_the_command(t: &mut tester, box: sandbox): void {
|
||||||
|
box.hello("hello");
|
||||||
|
box.write("hello/tests/wrong.sakura", """
|
||||||
|
import std;
|
||||||
|
import std::testing;
|
||||||
|
|
||||||
|
@test def nothing_is_something(t: &mut tester): void { t.expects(1 == 2); }
|
||||||
|
""");
|
||||||
|
val tested := box.blossom("hello", ["test"]);
|
||||||
|
t.expects(tested.status == 1 && tested.says("Failed hello.tests") && !tested.says("Tested tests/hello"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@test def tests_run_natively_by_name_with_what_theyre_given(t: &mut tester, box: sandbox): void {
|
||||||
|
box.hello("hello", """
|
||||||
|
- name: given.tests
|
||||||
|
type: program
|
||||||
|
runs: native
|
||||||
|
srcs: [given/main.sakura]
|
||||||
|
args: ["${bin}", second]
|
||||||
|
""");
|
||||||
|
box.write("hello/given/main.sakura", """
|
||||||
|
import std;
|
||||||
|
import std::io;
|
||||||
|
import system;
|
||||||
|
|
||||||
|
def main(args: span<strview>): void {
|
||||||
|
io::println($"given {args[0]} {args[1]}".view());
|
||||||
|
exit(args[0].ends_with("/bin") && args[1] == "second" ? 0 : 1);
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
val tested := box.blossom("hello", ["test", "--only", "given"]);
|
||||||
|
t.expects(tested.status == 0 && tested.says("[3/3] Tested given.tests") && !tested.says("hello.tests"), tested.output.view());
|
||||||
|
t.expects(tested.says($"given {box.at.view()}/hello/.blossom/".view()));
|
||||||
|
t.expects(box.blossom("hello", ["test", "--only", "none"]).status == 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@test def install_puts_programs_libraries_and_steps_in_the_prefix(t: &mut tester, box: sandbox): void {
|
||||||
|
box.hello("hello", """
|
||||||
|
exports:
|
||||||
|
- hello.geometry
|
||||||
|
|
||||||
|
install:
|
||||||
|
- !include ./share
|
||||||
|
- !include ./docs:share/doc/hello
|
||||||
|
- !include ./notes.txt
|
||||||
|
- !include ./notes.txt:etc/hello.txt
|
||||||
|
""");
|
||||||
|
box.write("hello/share/data.txt", "data\n");
|
||||||
|
box.write("hello/docs/README.md", "# Hello\n");
|
||||||
|
box.write("hello/notes.txt", "notes\n");
|
||||||
|
val installed := box.blossom("hello", ["install", "--prefix", $"{box.at.view()}/prefix".view()]);
|
||||||
|
t.expects(installed.status == 0 && installed.says("Installed tests/hello@1.0.0 (7 files)"), installed.error.view());
|
||||||
|
for name in ["prefix/bin/hello", "prefix/share/data.txt", "prefix/share/doc/hello/README.md", "prefix/notes.txt", "prefix/etc/hello.txt",
|
||||||
|
"prefix/share/sakura/hello.geometry/shapes.sakura", "prefix/share/sakura/hello.geometry/solids/solids.sakura"] {
|
||||||
|
t.expects(box.has(name), name);
|
||||||
|
}
|
||||||
|
val ran := box.run($"{box.at.view()}/prefix/bin/hello".view(), ".", span::<strview>::empty());
|
||||||
|
t.expects(ran.says("area 12"));
|
||||||
|
|
||||||
|
# Under a folder, as a package is staged.
|
||||||
|
val staged := box.blossom("hello", ["install", "--prefix", "/usr/local", "--destdir", $"{box.at.view()}/stage".view()]);
|
||||||
|
t.expects(staged.status == 0 && box.has("stage/usr/local/bin/hello"), staged.error.view());
|
||||||
|
}
|
||||||
|
|
||||||
|
@test def packages_hold_what_installing_puts_in_the_prefix(t: &mut tester, box: sandbox): void {
|
||||||
|
box.hello("hello", "install:\n- !include ./notes.txt\n");
|
||||||
|
box.write("hello/notes.txt", "notes\n");
|
||||||
|
val packaged := box.blossom("hello", ["package", "-f", "tar,zip"]);
|
||||||
|
t.expects(packaged.status == 0 && packaged.says("hello-1.0.0-linux-x86_64.tar.gz") && packaged.says("hello-1.0.0-linux-x86_64.zip"), packaged.error.view());
|
||||||
|
|
||||||
|
val listed := box.run("sh", "hello", ["-c", "tar -tzf .blossom/*/dist/hello-1.0.0-linux-x86_64.tar.gz"]);
|
||||||
|
t.expects(listed.output.view() == "hello-1.0.0-linux-x86_64/\nhello-1.0.0-linux-x86_64/bin/\nhello-1.0.0-linux-x86_64/bin/hello\nhello-1.0.0-linux-x86_64/notes.txt\n", listed.output.view());
|
||||||
|
val zip := box.run("sh", "hello", ["-c", "head -c 2 .blossom/*/dist/hello-1.0.0-linux-x86_64.zip"]);
|
||||||
|
t.expects(zip.output.view() == "PK");
|
||||||
|
|
||||||
|
# The same stage makes the same bytes.
|
||||||
|
val first := box.run("sh", "hello", ["-c", "md5sum .blossom/*/dist/*"]);
|
||||||
|
_ = box.blossom("hello", ["package", "-f", "tar,zip"]);
|
||||||
|
val second := box.run("sh", "hello", ["-c", "md5sum .blossom/*/dist/*"]);
|
||||||
|
t.expects(first.output.length != 0 && first.output == second.output);
|
||||||
|
|
||||||
|
# A system's package is refused for Windows before anything's built.
|
||||||
|
val windows := box.blossom("hello", ["package", "-f", "deb", "--target", "windows-x86_64"]);
|
||||||
|
t.expects(windows.status == 2 && windows.warns("deb packages are for Linux") && !windows.says("Built"));
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
# Runs blossom's tests of its commands. Each makes projects in a folder of its
|
||||||
|
# own, runs the blossom it's given first on them, and compares what it says and
|
||||||
|
# what it makes with what's expected. blossom's config in that folder names the
|
||||||
|
# `sakura` that `SAKURA` names, and the standard library `SAKURA_STD` names,
|
||||||
|
# where it's set, as `blossom test` sets them:
|
||||||
|
#
|
||||||
|
# blossom test --only blossom.commands
|
||||||
|
#
|
||||||
|
# or, from the root of the repository, with a blossom built:
|
||||||
|
#
|
||||||
|
# SAKURA=SAKURA sakura run tests/commands/*.sakura -- BLOSSOM
|
||||||
|
#
|
||||||
|
# Add `--only TEXT` after BLOSSOM to run those whose name holds the text, and
|
||||||
|
# `--here` to run them in one program, as a debugger wants. They run `git`, `sh`,
|
||||||
|
# and `tar`, so they run on Linux.
|
||||||
|
import std;
|
||||||
|
import std::io;
|
||||||
|
import std::process;
|
||||||
|
import system;
|
||||||
|
import std::testing;
|
||||||
|
|
||||||
|
def main(args: span<strview>): void {
|
||||||
|
if args.length == 0 || args[0].starts_with("-") {
|
||||||
|
io::println("blossom's tests are given the blossom they run first, as `-- .blossom/KEY/bin/blossom`.");
|
||||||
|
exit(2);
|
||||||
|
}
|
||||||
|
if process::variable("SAKURA") is none {
|
||||||
|
io::println("blossom's tests run the `sakura` that `SAKURA` names, as `blossom test` sets it.");
|
||||||
|
exit(2);
|
||||||
|
}
|
||||||
|
blossom_at = args[0];
|
||||||
|
exit(tester::run(args));
|
||||||
|
}
|
||||||
@@ -0,0 +1,94 @@
|
|||||||
|
# Tests of what blossom refuses, each with status 2 and a line saying what's
|
||||||
|
# wrong and where: a key written twice, keys and options it doesn't know, a
|
||||||
|
# step of `install` without a tag it knows, and a folder with no blossomfile.
|
||||||
|
import std;
|
||||||
|
import std::testing;
|
||||||
|
|
||||||
|
# Writes a project whose blossomfile is the text given, with a program to build.
|
||||||
|
def sandbox::project(&this, folder: strview, blossomfile: strview): void {
|
||||||
|
this.write($"{folder}/blossomfile".view(), blossomfile);
|
||||||
|
this.write($"{folder}/main.sakura".view(), "def main(): void {}\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
@test def a_key_written_twice_is_refused_by_its_line(t: &mut tester, box: sandbox): void {
|
||||||
|
box.project("twice", """
|
||||||
|
name: twice
|
||||||
|
gpid: tests
|
||||||
|
semv: 1.0.0
|
||||||
|
|
||||||
|
requires:
|
||||||
|
- remote: https://example.com/one
|
||||||
|
branch: v1.0.0
|
||||||
|
remote: https://example.com/two
|
||||||
|
branch: v1.0.0
|
||||||
|
""");
|
||||||
|
val built := box.blossom("twice", ["build"]);
|
||||||
|
t.expects(built.status == 2 && built.warns("blossomfile:8: `remote` is written twice in a requirement."), built.error.view());
|
||||||
|
}
|
||||||
|
|
||||||
|
@test def keys_options_and_targets_blossom_doesnt_know_are_refused(t: &mut tester, box: sandbox): void {
|
||||||
|
box.project("keys", """
|
||||||
|
name: keys
|
||||||
|
gpid: tests
|
||||||
|
semv: 1.0.0
|
||||||
|
|
||||||
|
targets:
|
||||||
|
- name: keys
|
||||||
|
type: program
|
||||||
|
srcs: [main.sakura]
|
||||||
|
optlevel: 2
|
||||||
|
""");
|
||||||
|
t.expects(box.blossom("keys", ["build"]).warns("blossomfile:9: `optlevel` is no key of a target."));
|
||||||
|
|
||||||
|
box.project("options", """
|
||||||
|
name: options
|
||||||
|
gpid: tests
|
||||||
|
semv: 1.0.0
|
||||||
|
|
||||||
|
options:
|
||||||
|
level: 2
|
||||||
|
|
||||||
|
targets:
|
||||||
|
- name: options
|
||||||
|
type: program
|
||||||
|
level: ${nope}
|
||||||
|
srcs: [main.sakura]
|
||||||
|
""");
|
||||||
|
val unknown := box.blossom("options", ["build"]);
|
||||||
|
t.expects(unknown.status == 2 && unknown.warns("blossomfile:11: `${nope}` names no option declared before it."), unknown.error.view());
|
||||||
|
|
||||||
|
val bad := box.blossom("keys", ["build", "-o", "nope=1"]);
|
||||||
|
t.expects(bad.status == 2 && bad.warns("`nope`, given a value, names no option of tests/keys@1.0.0."));
|
||||||
|
t.expects(box.blossom("keys", ["build", "--target", "mars"]).status == 2);
|
||||||
|
t.expects(box.blossom("keys", ["frobnicate"]).status == 2);
|
||||||
|
t.expects(box.blossom("keys", ["build", "--no-such-option"]).status == 2);
|
||||||
|
|
||||||
|
box.project("version", "name: version\ngpid: tests\nsemv: one\n");
|
||||||
|
t.expects(box.blossom("version", ["build"]).warns("`one` is not a version"));
|
||||||
|
|
||||||
|
box.project("level", "name: level\ngpid: tests\nsemv: 1.0.0\ntargets:\n- name: level\n type: program\n level: 3\n srcs: [main.sakura]\n");
|
||||||
|
t.expects(box.blossom("level", ["build"]).warns("blossomfile:7: `level` is 0, 1, or 2, not `3`."));
|
||||||
|
|
||||||
|
box.project("missing", "name: missing\ngpid: tests\nsemv: 1.0.0\ntargets:\n- name: missing\n type: program\n srcs: [nothing.sakura, '*.c']\n");
|
||||||
|
val missing := box.blossom("missing", ["build"]);
|
||||||
|
t.expects(missing.status == 2 && missing.warns("`nothing.sakura`, in `missing`'s `srcs`, names no file."), missing.error.view());
|
||||||
|
}
|
||||||
|
|
||||||
|
@test def a_step_of_install_says_what_it_does_by_a_tag_blossom_knows(t: &mut tester, box: sandbox): void {
|
||||||
|
box.project("untagged", "name: untagged\ngpid: tests\nsemv: 1.0.0\ninstall:\n- ./share\n");
|
||||||
|
val untagged := box.blossom("untagged", ["install", "--prefix", "prefix"]);
|
||||||
|
t.expects(untagged.status == 2 && untagged.warns("blossomfile:5: A step of `install` says what it does with a tag"), untagged.error.view());
|
||||||
|
|
||||||
|
box.project("unknown", "name: unknown\ngpid: tests\nsemv: 1.0.0\ninstall:\n- !program ./share\n");
|
||||||
|
val unknown := box.blossom("unknown", ["install", "--prefix", "prefix"]);
|
||||||
|
t.expects(unknown.status == 2 && unknown.warns("`!program` is no step blossom knows"), unknown.error.view());
|
||||||
|
|
||||||
|
box.project("outside", "name: outside\ngpid: tests\nsemv: 1.0.0\ninstall:\n- !include ../elsewhere\n- !include ./a:/etc\n");
|
||||||
|
t.expects(box.blossom("outside", ["install", "--prefix", "prefix"]).warns("`../elsewhere` is outside the project"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@test def a_folder_without_a_blossomfile_is_refused(t: &mut tester, box: sandbox): void {
|
||||||
|
box.write("empty/main.sakura", "def main(): void {}\n");
|
||||||
|
val built := box.blossom("empty", ["build"]);
|
||||||
|
t.expects(built.status == 2 && built.warns("holds no blossomfile"), built.error.view());
|
||||||
|
}
|
||||||
@@ -0,0 +1,175 @@
|
|||||||
|
# Tests of requirements: a folder beside the project, a remote cloned once into
|
||||||
|
# the store, from a bare git repository in the test's folder, written short with
|
||||||
|
# a ref and without, and as a block giving options, and what's refused: one
|
||||||
|
# project with two sets of options, a library not exported, a name two
|
||||||
|
# projects give, and a clone that fails.
|
||||||
|
import std;
|
||||||
|
import std::io;
|
||||||
|
import std::testing;
|
||||||
|
|
||||||
|
# Writes a project holding a library, `shapes.basic`, whose `side` gives the
|
||||||
|
# text the option `flavor` names a folder of, `plain` unless given.
|
||||||
|
def sandbox::shapes(&this, folder: strview, exported: bool = true): void {
|
||||||
|
var exports := string::new(nothing {});
|
||||||
|
if exported {
|
||||||
|
exports.append("exports:\n- shapes.basic\n");
|
||||||
|
}
|
||||||
|
this.write($"{folder}/blossomfile".view(), $"""
|
||||||
|
name: shapes
|
||||||
|
gpid: tests
|
||||||
|
semv: 1.0.0
|
||||||
|
|
||||||
|
options:
|
||||||
|
flavor: plain
|
||||||
|
|
||||||
|
targets:
|
||||||
|
- name: shapes.basic
|
||||||
|
type: library
|
||||||
|
root: ./$\{flavor\}
|
||||||
|
|
||||||
|
{exports.view()}
|
||||||
|
""".view());
|
||||||
|
this.write($"{folder}/plain/basic.sakura".view(), "module shapes::basic;\n\npub def side(): strview { return \"plain\"; }\n");
|
||||||
|
this.write($"{folder}/fancy/basic.sakura".view(), "module shapes::basic;\n\npub def side(): strview { return \"fancy\"; }\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
# Writes a program, `app`, reading `shapes.basic`, which its blossomfile's
|
||||||
|
# `requires` names.
|
||||||
|
def sandbox::app(&this, folder: strview, requires: strview): void {
|
||||||
|
this.write($"{folder}/blossomfile".view(), $"""
|
||||||
|
name: app
|
||||||
|
gpid: tests
|
||||||
|
semv: 1.0.0
|
||||||
|
|
||||||
|
requires:
|
||||||
|
{requires}
|
||||||
|
|
||||||
|
targets:
|
||||||
|
- name: app
|
||||||
|
type: program
|
||||||
|
deps: [shapes.basic]
|
||||||
|
srcs: [main.sakura]
|
||||||
|
""".view());
|
||||||
|
this.write($"{folder}/main.sakura".view(), """
|
||||||
|
import std;
|
||||||
|
import std::io;
|
||||||
|
import shapes::basic;
|
||||||
|
|
||||||
|
def main(): void {
|
||||||
|
io::println($"side {basic::side()}".view());
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
}
|
||||||
|
|
||||||
|
# Makes a bare git repository of a project, as a remote is, with a commit on
|
||||||
|
# `main` and a tag, `v1.0.0`.
|
||||||
|
def sandbox::remote(&this, from: strview, bare: strview): bool {
|
||||||
|
val made := this.run("sh", from, [
|
||||||
|
"-c",
|
||||||
|
$"git init -q -b main . && git add -A && git -c user.name=t -c user.email=t@t commit -q -m one && git tag v1.0.0 && git clone -q --bare . {this.at.view()}/{bare}".view()
|
||||||
|
]);
|
||||||
|
return made.status == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@test def a_folder_beside_the_project_is_required_by_its_path(t: &mut tester, box: sandbox): void {
|
||||||
|
box.shapes("shapes");
|
||||||
|
box.app("app", "- ../shapes");
|
||||||
|
val ran := box.blossom("app", ["run"]);
|
||||||
|
t.expects(ran.status == 0 && ran.says("side plain"), ran.error.view());
|
||||||
|
}
|
||||||
|
|
||||||
|
@test def a_block_gives_a_requirement_values_for_its_options(t: &mut tester, box: sandbox): void {
|
||||||
|
box.shapes("shapes");
|
||||||
|
box.app("app", "- remote: ../shapes\n options:\n flavor: fancy");
|
||||||
|
val ran := box.blossom("app", ["run"]);
|
||||||
|
t.expects(ran.status == 0 && ran.says("side fancy"), ran.error.view());
|
||||||
|
}
|
||||||
|
|
||||||
|
@test def a_remote_is_cloned_once_into_the_store(t: &mut tester, box: sandbox): void {
|
||||||
|
box.shapes("source");
|
||||||
|
t.expects(box.remote("source", "shapes.git"));
|
||||||
|
box.app("app", $"- file://{box.at.view()}/shapes.git@v1.0.0".view());
|
||||||
|
val first := box.blossom("app", ["build"]);
|
||||||
|
t.expects(first.status == 0 && first.says("Cloning file://") && first.says("[1/1] Built app"), first.error.view());
|
||||||
|
t.expects(box.has($"config/blossom/file{box.at.view()}/shapes@v1.0.0/blossomfile".view()));
|
||||||
|
|
||||||
|
# Cloned, it's taken from the store.
|
||||||
|
val second := box.blossom("app", ["build"]);
|
||||||
|
t.expects(second.status == 0 && !second.says("Cloning"));
|
||||||
|
|
||||||
|
# Without a ref, `main` is cloned.
|
||||||
|
box.app("main", $"- file://{box.at.view()}/shapes.git".view());
|
||||||
|
val main := box.blossom("main", ["run"]);
|
||||||
|
t.expects(main.status == 0 && main.says("side plain"), main.error.view());
|
||||||
|
t.expects(box.has($"config/blossom/file{box.at.view()}/shapes@main".view()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@test def a_clone_that_fails_says_why(t: &mut tester, box: sandbox): void {
|
||||||
|
box.write("bin/git", "#!/bin/sh\necho 'fatal: the remote hung up' >&2\nexit 128\n");
|
||||||
|
val git := box.at.join("bin/git");
|
||||||
|
_ = io::set_permissions(&git, 0o755);
|
||||||
|
box.app("app", "- https://example.com/shapes@v1.0.0");
|
||||||
|
val built := box.blossom("app", ["build"], $"{box.at.view()}/bin:/usr/bin:/bin".view());
|
||||||
|
t.expects(built.status == 1 && built.warns("fatal: the remote hung up") && built.warns("Cloning https://example.com/shapes@v1.0.0 failed."), built.error.view());
|
||||||
|
t.expects(!box.has("config/blossom/example.com/shapes@v1.0.0"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@test def requirements_that_dont_fit_are_refused(t: &mut tester, box: sandbox): void {
|
||||||
|
# One project, with two sets of option values.
|
||||||
|
box.shapes("shapes");
|
||||||
|
box.write("other/blossomfile", "name: other\ngpid: tests\nsemv: 1.0.0\nrequires:\n- ../shapes\ntargets:\n- name: other.lib\n type: library\n deps: [shapes.basic]\n");
|
||||||
|
box.app("app", "- ../other\n- remote: ../shapes\n options:\n flavor: fancy");
|
||||||
|
val twice := box.blossom("app", ["build"]);
|
||||||
|
t.expects(twice.status == 2 && twice.warns("tests/shapes@1.0.0 is required with two sets of option values"), twice.error.view());
|
||||||
|
|
||||||
|
# A library its project doesn't export.
|
||||||
|
box.shapes("hidden", false);
|
||||||
|
box.app("private", "- ../hidden");
|
||||||
|
val private := box.blossom("private", ["build"]);
|
||||||
|
t.expects(private.status == 2 && private.warns("`shapes.basic`, which `app` reads, isn't exported by tests/shapes@1.0.0."), private.error.view());
|
||||||
|
|
||||||
|
# One name, two projects.
|
||||||
|
box.write("copy/blossomfile", "name: copy\ngpid: tests\nsemv: 1.0.0\ntargets:\n- name: shapes.basic\n type: library\n root: ./plain\nexports:\n- shapes.basic\n");
|
||||||
|
box.write("copy/plain/basic.sakura", "module shapes::basic;\n\npub def side(): strview { return \"copy\"; }\n");
|
||||||
|
box.app("both", "- ../shapes\n- ../copy");
|
||||||
|
val clash := box.blossom("both", ["build"]);
|
||||||
|
t.expects(clash.status == 2 && clash.warns("`shapes.basic` is a library of both"), clash.error.view());
|
||||||
|
|
||||||
|
# A library no project has.
|
||||||
|
box.app("none", " []");
|
||||||
|
t.expects(box.blossom("none", ["build"]).warns("`shapes.basic`, which `app` reads, is no library"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@test def a_requirement_reads_what_it_requires_only_where_its_libraries_need_it(t: &mut tester, box: sandbox): void {
|
||||||
|
# What `shapes` requires for itself alone, which can't be fetched, isn't.
|
||||||
|
box.shapes("shapes");
|
||||||
|
val text := box.read("shapes/blossomfile");
|
||||||
|
box.write("shapes/blossomfile", $"{text.view()}\nrequires:\n- https://example.invalid/never@v1.0.0\n".view());
|
||||||
|
box.app("app", "- ../shapes");
|
||||||
|
val ran := box.blossom("app", ["run"]);
|
||||||
|
t.expects(ran.status == 0 && ran.says("side plain") && !ran.says("Cloning"), ran.error.view());
|
||||||
|
|
||||||
|
# A library reading one of what its project requires brings that along.
|
||||||
|
box.shapes("inner");
|
||||||
|
box.write("outer/blossomfile", """
|
||||||
|
name: outer
|
||||||
|
gpid: tests
|
||||||
|
semv: 1.0.0
|
||||||
|
|
||||||
|
requires:
|
||||||
|
- ../inner
|
||||||
|
|
||||||
|
targets:
|
||||||
|
- name: outer.lib
|
||||||
|
type: library
|
||||||
|
deps: [shapes.basic]
|
||||||
|
|
||||||
|
exports:
|
||||||
|
- outer.lib
|
||||||
|
""");
|
||||||
|
box.write("outer/outer.lib/lib.sakura", "import shapes::basic;\n\nmodule outer::lib;\n\npub def says(): strview { return basic::side(); }\n");
|
||||||
|
box.write("user/blossomfile", "name: user\ngpid: tests\nsemv: 1.0.0\nrequires:\n- ../outer\ntargets:\n- name: user\n type: program\n deps: [outer.lib]\n srcs: [main.sakura]\n");
|
||||||
|
box.write("user/main.sakura", "import std;\nimport std::io;\nimport outer::lib;\n\ndef main(): void { io::println($\"says {lib::says()}\".view()); }\n");
|
||||||
|
val through := box.blossom("user", ["run"]);
|
||||||
|
t.expects(through.status == 0 && through.says("says plain"), through.error.view());
|
||||||
|
}
|
||||||
@@ -0,0 +1,213 @@
|
|||||||
|
# What blossom's tests of its commands share: the blossom they run, a folder of
|
||||||
|
# each test's own, writing projects into it, and running blossom there.
|
||||||
|
import std;
|
||||||
|
import std::io;
|
||||||
|
import std::process;
|
||||||
|
import system;
|
||||||
|
import std::testing;
|
||||||
|
|
||||||
|
#! The blossom the tests run, which `main` is given first.
|
||||||
|
var blossom_at: strview = "";
|
||||||
|
|
||||||
|
#! A folder of a test's own, holding the projects it makes, and blossom's
|
||||||
|
#! config and store, in `config`, removed with all it holds where the test
|
||||||
|
#! returns.
|
||||||
|
struct sandbox : fixture<sandbox>, owning {
|
||||||
|
var at: path;
|
||||||
|
}
|
||||||
|
|
||||||
|
def sandbox::setup(t: &mut tester): sandbox {
|
||||||
|
for n in 0..10000 {
|
||||||
|
var at := path::parse($"/tmp/blossom-tests-{n}".view()) else (_) { break; };
|
||||||
|
if io::create_directory(&at) is error(_) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
var made := sandbox { at = move(at) };
|
||||||
|
var config := string::new(nothing {});
|
||||||
|
if process::variable("SAKURA") is some(sakura) {
|
||||||
|
config.append($"sakura: {sakura}\n".view());
|
||||||
|
}
|
||||||
|
if process::variable("SAKURA_STD") is some(std) {
|
||||||
|
config.append($"std: {std}\n".view());
|
||||||
|
}
|
||||||
|
made.write("config/blossom/config", config.view());
|
||||||
|
return made;
|
||||||
|
}
|
||||||
|
system::abort();
|
||||||
|
return sandbox {};
|
||||||
|
}
|
||||||
|
|
||||||
|
def sandbox::close(&mut this): void {
|
||||||
|
var remove := process::command::new("rm");
|
||||||
|
remove.arguments(["-rf", this.at.view()]);
|
||||||
|
_ = remove.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
#! Writes a file in the sandbox, making the folders above it.
|
||||||
|
#! @param this The sandbox.
|
||||||
|
#! @param name The file, relative to the sandbox.
|
||||||
|
#! @param text What it holds.
|
||||||
|
def sandbox::write(&this, name: strview, text: strview): void {
|
||||||
|
val at := this.at.join(name);
|
||||||
|
if at.parent() is some(&above) {
|
||||||
|
_ = io::create_directories(above);
|
||||||
|
}
|
||||||
|
_ = io::write_file(&at, text);
|
||||||
|
}
|
||||||
|
|
||||||
|
#! Reads a file of the sandbox.
|
||||||
|
#! @param this The sandbox.
|
||||||
|
#! @param name The file, relative to the sandbox.
|
||||||
|
#! @returns What it holds, or empty text where it can't be read.
|
||||||
|
def sandbox::read(&this, name: strview): string {
|
||||||
|
val at := this.at.join(name);
|
||||||
|
return io::read_file(&at) else (_) { return string::new(nothing {}); };
|
||||||
|
}
|
||||||
|
|
||||||
|
#! Whether a file or folder is in the sandbox.
|
||||||
|
#! @param this The sandbox.
|
||||||
|
#! @param name Its path, relative to the sandbox.
|
||||||
|
#! @returns Whether it is.
|
||||||
|
def sandbox::has(&this, name: strview): bool {
|
||||||
|
val at := this.at.join(name);
|
||||||
|
return io::exists(&at);
|
||||||
|
}
|
||||||
|
|
||||||
|
#! What a command said, and the status it ended with.
|
||||||
|
struct said {
|
||||||
|
var status: i32;
|
||||||
|
var output: string;
|
||||||
|
var error: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
#! Whether what a command wrote to its output holds some text.
|
||||||
|
#! @param this What it said.
|
||||||
|
#! @param text The text.
|
||||||
|
#! @returns Whether it does.
|
||||||
|
def said::says(&this, text: strview): bool {
|
||||||
|
return this.output.view().find(text) is some;
|
||||||
|
}
|
||||||
|
|
||||||
|
#! Whether what a command wrote to its error holds some text.
|
||||||
|
#! @param this What it said.
|
||||||
|
#! @param text The text.
|
||||||
|
#! @returns Whether it does.
|
||||||
|
def said::warns(&this, text: strview): bool {
|
||||||
|
return this.error.view().find(text) is some;
|
||||||
|
}
|
||||||
|
|
||||||
|
#! Runs blossom in a folder of the sandbox, its config and store the sandbox's.
|
||||||
|
#! @param this The sandbox.
|
||||||
|
#! @param folder The folder, relative to the sandbox.
|
||||||
|
#! @param words What blossom is given.
|
||||||
|
#! @param path What `PATH` is, or empty for the tests' own.
|
||||||
|
#! @returns What it said.
|
||||||
|
def sandbox::blossom(&this, folder: strview, words: span<strview>, path: strview = ""): said {
|
||||||
|
return this.run(blossom_at, folder, words, path);
|
||||||
|
}
|
||||||
|
|
||||||
|
#! Runs a program in a folder of the sandbox, as `blossom` does.
|
||||||
|
#! @param this The sandbox.
|
||||||
|
#! @param program The program.
|
||||||
|
#! @param folder The folder, relative to the sandbox.
|
||||||
|
#! @param words What it's given.
|
||||||
|
#! @param path What `PATH` is, or empty for the tests' own.
|
||||||
|
#! @returns What it said.
|
||||||
|
def sandbox::run(&this, program: strview, folder: strview, words: span<strview>, path: strview = ""): said {
|
||||||
|
var command := process::command::new(program);
|
||||||
|
command.arguments(words);
|
||||||
|
val at := this.at.join(folder);
|
||||||
|
command.directory(&at);
|
||||||
|
command.set_variable("XDG_CONFIG_HOME", $"{this.at.view()}/config".view());
|
||||||
|
command.set_variable("SOURCE_DATE_EPOCH", "1700000000");
|
||||||
|
if path.length != 0 {
|
||||||
|
command.set_variable("PATH", path);
|
||||||
|
}
|
||||||
|
var got := command.capture() else (why) {
|
||||||
|
return said { status = -1, output = string::new(nothing {}), error = $"`{program}` can't be run: {why}" };
|
||||||
|
};
|
||||||
|
var code: i32 = -1;
|
||||||
|
if got.status is exited(exited) {
|
||||||
|
code = exited;
|
||||||
|
}
|
||||||
|
return said { status = code, output = move(got.output), error = move(got.error) };
|
||||||
|
}
|
||||||
|
|
||||||
|
#! The path a line of blossom's output gives in parentheses, as the program of
|
||||||
|
#! `[2/2] Built hello (.blossom/KEY/bin/hello)`, relative to the project.
|
||||||
|
#! @param text What blossom said.
|
||||||
|
#! @param name The target.
|
||||||
|
#! @returns The path, or empty text.
|
||||||
|
def built_path(text: strview, name: strview): strview {
|
||||||
|
val marker := $"Built {name} (";
|
||||||
|
val at := text.find(marker.view()) else { return strview::empty(); };
|
||||||
|
val from := at + marker.length;
|
||||||
|
val rest := text.substr(from, text.length - from);
|
||||||
|
val end := rest.find(")") else { return strview::empty(); };
|
||||||
|
return rest.substr(0, end);
|
||||||
|
}
|
||||||
|
|
||||||
|
#! Writes a project, `hello`, in a folder of the sandbox: a program reading a
|
||||||
|
#! library of its own, `hello.geometry`, which holds a module within it, and
|
||||||
|
#! tests of the library, as the plan's "Using it" has it.
|
||||||
|
#! @param this The sandbox.
|
||||||
|
#! @param folder The project's folder.
|
||||||
|
#! @param more What's added to its blossomfile.
|
||||||
|
def sandbox::hello(&this, folder: strview, more: strview = ""): void {
|
||||||
|
this.write($"{folder}/blossomfile".view(), $"""
|
||||||
|
name: hello
|
||||||
|
gpid: tests
|
||||||
|
semv: 1.0.0
|
||||||
|
|
||||||
|
options:
|
||||||
|
level: 2
|
||||||
|
|
||||||
|
targets:
|
||||||
|
- name: hello
|
||||||
|
type: program
|
||||||
|
level: $\{level\}
|
||||||
|
deps: [std, hello.geometry]
|
||||||
|
srcs: [main.sakura]
|
||||||
|
|
||||||
|
- name: hello.geometry
|
||||||
|
type: library
|
||||||
|
|
||||||
|
tests:
|
||||||
|
- name: hello.tests
|
||||||
|
type: program
|
||||||
|
deps: [hello.geometry]
|
||||||
|
{more}
|
||||||
|
""".view());
|
||||||
|
this.write($"{folder}/main.sakura".view(), """
|
||||||
|
import std;
|
||||||
|
import std::io;
|
||||||
|
import hello::geometry;
|
||||||
|
import hello::geometry::solids;
|
||||||
|
|
||||||
|
def main(args: span<strview>): void {
|
||||||
|
io::println($"area {geometry::area(3, 4)} cube {solids::cube(2)} given {args.length}".view());
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
this.write($"{folder}/hello.geometry/shapes.sakura".view(), """
|
||||||
|
module hello::geometry;
|
||||||
|
|
||||||
|
pub def area(w: i64, h: i64): i64 { return w * h; }
|
||||||
|
""");
|
||||||
|
this.write($"{folder}/hello.geometry/solids/solids.sakura".view(), """
|
||||||
|
module hello::geometry::solids;
|
||||||
|
|
||||||
|
pub def cube(side: i64): i64 { return side * side * side; }
|
||||||
|
""");
|
||||||
|
this.write($"{folder}/tests/main.sakura".view(), """
|
||||||
|
import std;
|
||||||
|
import system;
|
||||||
|
import std::testing;
|
||||||
|
import hello::geometry;
|
||||||
|
import hello::geometry::solids;
|
||||||
|
|
||||||
|
def main(args: span<strview>): void { exit(tester::run(args)); }
|
||||||
|
|
||||||
|
@test def areas_multiply(t: &mut tester): void { t.expects(geometry::area(2, 5) == 10); }
|
||||||
|
@test def cubes_cube(t: &mut tester): void { t.expects(solids::cube(3) == 27); }
|
||||||
|
""");
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
# Runs blossom's tests of its parts, which read its own files:
|
||||||
|
#
|
||||||
|
# blossom test --only blossom.units
|
||||||
|
#
|
||||||
|
# or, from the root of the repository, with the libraries blossom reads:
|
||||||
|
#
|
||||||
|
# sakura run --lib std::yaml=... --lib std::args=... --lib malunal::glob=...
|
||||||
|
# models/*.sakura management/*.sakura pipeline/*.sakura tests/units/*.sakura
|
||||||
|
import std;
|
||||||
|
import system;
|
||||||
|
import std::testing;
|
||||||
|
|
||||||
|
def main(args: span<strview>): void {
|
||||||
|
exit(tester::run(args));
|
||||||
|
}
|
||||||
@@ -0,0 +1,116 @@
|
|||||||
|
# Tests of blossom's parts: reading blossomfiles, the store's folders, output
|
||||||
|
# folders' names, versions, the digests packages carry, and commands as `-v`
|
||||||
|
# prints them.
|
||||||
|
import std;
|
||||||
|
import std::io;
|
||||||
|
import std::testing;
|
||||||
|
import blossom::models;
|
||||||
|
import blossom::management;
|
||||||
|
import blossom::pipeline;
|
||||||
|
|
||||||
|
# Reads a blossomfile's text, giving what's wrong, or empty text where nothing
|
||||||
|
# is.
|
||||||
|
def problem_in(text: strview): string {
|
||||||
|
val file := path::parse("/projects/tests/blossomfile") else (_) { return string::new(nothing {}); };
|
||||||
|
var overrides := vector::<models::setting>::new();
|
||||||
|
var read := models::read_project(text, &file, overrides.view(), "");
|
||||||
|
if read is error(&problem) {
|
||||||
|
return problem.describe();
|
||||||
|
}
|
||||||
|
return string::new(nothing {});
|
||||||
|
}
|
||||||
|
|
||||||
|
@test def digests_are_those_their_standards_give(t: &mut tester): void {
|
||||||
|
t.expects(pipeline::md5("") == "d41d8cd98f00b204e9800998ecf8427e");
|
||||||
|
t.expects(pipeline::md5("abc") == "900150983cd24fb0d6963f7d28e17f72");
|
||||||
|
t.expects(pipeline::md5("12345678901234567890123456789012345678901234567890123456789012345678901234567890") == "57edf4a22be3c955ac49da2e2107b67a");
|
||||||
|
t.expects(pipeline::sha256("") == "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
|
||||||
|
t.expects(pipeline::sha256("abc") == "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
|
||||||
|
t.expects(pipeline::sha256("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") == "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1");
|
||||||
|
t.expects(pipeline::crc32("123456789") == 0xCBF43926 && pipeline::crc32("") == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@test def remotes_are_stored_by_their_host_path_and_ref(t: &mut tester): void {
|
||||||
|
t.expects(management::store_key("https://git.malunal.studio/malunal/json", "v1.0.0") == "git.malunal.studio/malunal/json@v1.0.0");
|
||||||
|
t.expects(management::store_key("https://me@host.com/a/b.git", "main") == "host.com/a/b@main");
|
||||||
|
t.expects(management::store_key("git@github.com:malunal/json.git", "main") == "github.com/malunal/json@main");
|
||||||
|
t.expects(management::store_key("file:///tmp/x/repo.git", "v2") == "file/tmp/x/repo@v2");
|
||||||
|
|
||||||
|
# Nothing leads out of the store.
|
||||||
|
t.expects(management::store_key("https://host/../../etc", "main") == "host/etc@main");
|
||||||
|
t.expects(management::store_key("https://host/a", "../../x") == "host/a@.._.._x");
|
||||||
|
}
|
||||||
|
|
||||||
|
@test def output_folders_are_named_by_identity_options_and_target(t: &mut tester): void {
|
||||||
|
var options := vector::<models::setting>::new();
|
||||||
|
options.append(models::setting { name = string::from("level", nothing {}), value = string::from("2", nothing {}) });
|
||||||
|
options.append(models::setting { name = string::from("debug", nothing {}), value = string::from("false", nothing {}) });
|
||||||
|
val first := models::output_key("malunal/hello@1.0.0", options.view(), "");
|
||||||
|
t.expects(first.length == 16);
|
||||||
|
|
||||||
|
# In any order, the same options name the same folder.
|
||||||
|
var reordered := vector::<models::setting>::new();
|
||||||
|
reordered.append(models::setting { name = string::from("debug", nothing {}), value = string::from("false", nothing {}) });
|
||||||
|
reordered.append(models::setting { name = string::from("level", nothing {}), value = string::from("2", nothing {}) });
|
||||||
|
t.expects(models::output_key("malunal/hello@1.0.0", reordered.view(), "") == first);
|
||||||
|
t.expects(models::output_key("malunal/hello@1.0.0", options.view(), "windows-x86_64") != first);
|
||||||
|
t.expects(models::output_key("malunal/hello@1.0.1", options.view(), "") != first);
|
||||||
|
t.expects(models::hex(0x0123456789abcdef) == "0123456789abcdef");
|
||||||
|
}
|
||||||
|
|
||||||
|
@test def versions_are_semantic(t: &mut tester): void {
|
||||||
|
for text in ["1.2.0", "0.0.1", "10.20.30", "1.2.0-alpha", "1.2.0-alpha.1", "1.2.0+build.5"] {
|
||||||
|
t.expects(models::is_version(text), text);
|
||||||
|
}
|
||||||
|
for text in ["1", "1.2", "1.2.x", "v1.2.0", "1.2.0-", "1..0", ""] {
|
||||||
|
t.expects(!models::is_version(text), text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@test def blossomfiles_are_read_with_their_mistakes_found(t: &mut tester): void {
|
||||||
|
t.expects(problem_in("name: a\ngpid: b\nsemv: 1.0.0\n").length == 0);
|
||||||
|
t.expects(problem_in("") == "/projects/tests/blossomfile: The blossomfile is empty: it needs `name`, `gpid`, and `semv`.");
|
||||||
|
t.expects(problem_in("name: a\ngpid: b\n") == "/projects/tests/blossomfile:1: `semv` is needed.");
|
||||||
|
t.expects(problem_in("name: a\ngpid: b\nsemv: 1.0.0\nname: c\n") == "/projects/tests/blossomfile:4: `name` is written twice in the blossomfile.");
|
||||||
|
t.expects(problem_in("name: a\ngpid: b\nsemv: 1.0.0\noptions:\n bin: x\n").view().find("`bin` is blossom's") is some);
|
||||||
|
t.expects(problem_in("name: a\ngpid: b\nsemv: 1.0.0\nexports: [nope]\n").view().find("`nope` is exported, but it's no library") is some);
|
||||||
|
t.expects(problem_in("name: a\ngpid: b\nsemv: 1.0.0\ntargets:\n- name: x.y\n type: library\n- name: x.y\n type: library\n").view().find("`x.y` names two targets.") is some);
|
||||||
|
t.expects(problem_in("name: a\ngpid: b\nsemv: 1.0.0\ntargets:\n- name: 9lives\n type: library\n").view().find("names no module") is some);
|
||||||
|
t.expects(problem_in("name: a\ngpid: b\nsemv: 1.0.0\ntargets:\n- name: p\n type: program\n").view().find("`p` needs `srcs`") is some);
|
||||||
|
t.expects(problem_in("name: a\ngpid: b\nsemv: 1.0.0\nrequires:\n- ../x@v1\n- remote: ../y\n branch: v1\n").view().find("A folder takes no `branch`.") is some);
|
||||||
|
t.expects(problem_in("name: a\ngpid: b\nsemv: 1.0.0\ntargets:\n- name: l\n type: library\n srcs: [a.sakura]\n").view().find("A library takes no `srcs`") is some);
|
||||||
|
}
|
||||||
|
|
||||||
|
@test def requirements_are_read_short_and_as_blocks(t: &mut tester): void {
|
||||||
|
val file := path::parse("/projects/tests/blossomfile") else (_) { return; };
|
||||||
|
var overrides := vector::<models::setting>::new();
|
||||||
|
var read := models::read_project("""
|
||||||
|
name: a
|
||||||
|
gpid: b
|
||||||
|
semv: 1.0.0
|
||||||
|
requires:
|
||||||
|
- https://host/json@v1.0.0
|
||||||
|
- git@host:malunal/yaml
|
||||||
|
- ../shapes
|
||||||
|
- remote: https://host/args
|
||||||
|
options:
|
||||||
|
strict: true
|
||||||
|
""", &file, overrides.view(), "");
|
||||||
|
if read is not value(&made) {
|
||||||
|
t.expects(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
t.expects(made.requires.length == 4);
|
||||||
|
t.expects(made.requires[0].remote == "https://host/json" && made.requires[0].branch == "v1.0.0" && !made.requires[0].local);
|
||||||
|
t.expects(made.requires[1].remote == "git@host:malunal/yaml" && made.requires[1].branch == "main");
|
||||||
|
t.expects(made.requires[2].remote == "../shapes" && made.requires[2].local);
|
||||||
|
t.expects(made.requires[3].branch == "main" && made.requires[3].options.length == 1 && made.requires[3].options[0].value == "true");
|
||||||
|
}
|
||||||
|
|
||||||
|
@test def commands_are_printed_as_a_shell_takes_them(t: &mut tester): void {
|
||||||
|
var words := vector::<string>::new();
|
||||||
|
for each in ["sakura", "build", "--lib", "a::b=./x", "my file.sakura", "it's"] {
|
||||||
|
words.append(string::from(each, nothing {}));
|
||||||
|
}
|
||||||
|
t.expects(pipeline::spoken(words.view()) == "sakura build --lib a::b=./x 'my file.sakura' 'it'\\''s'");
|
||||||
|
}
|
||||||
Reference in new issue
Block a user