release.v

6.1 KB · raw

1//! Releases: the built files, the record that names them, and their keys in the2//! bucket.3//!4//! A version exists the moment its `release.json` lands (§11.2), so history goes5//! up first, then every file, then the record, and `releases/latest` moves last.6//! Kepr reads all of it back field by field, so this record and `ReleaseRecord`7//! in Kepr's `src/bucket/repo.ts` are one contract: a name here that Kepr8//! refuses would fail the whole repo's sync.910module release1112import crypto.sha25613import json214import os15import time16import src.build17import src.exchange18import src.history19import src.machine20import src.remote21import src.storage2223// The version a key is built from is validated where it is read24// (`build.project_version`), so it cannot carry a path anywhere.25pub fn release_key(repo string, version string) string {26	return '${repo}/releases/${version}/release.json'27}2829pub fn file_key(repo string, version string, platform string, name string) string {30	return '${repo}/releases/${version}/${platform}/${name}'31}3233pub fn latest_key(repo string) string {34	return '${repo}/releases/latest'35}3637// One built file, as Kepr reads it.38pub struct File {39pub:40	platform string41	name     string42	size     i6443	blake3   string44	sha256   string45}4647pub struct Record {48pub:49	format       int = 150	repo         string51	version      string52	save         string53	published_at i6454	machine      string55	notes        string56	files        []File57}5859pub struct Published {60pub:61	version string62	files   []File63}6465// publish builds every platform, then uploads the files, the record, and the66// new latest. Every build runs before the first upload: one platform failing67// must leave the bucket exactly as it was.68pub fn publish(store &storage.Store, mut backend remote.Backend, repo string,69	config build.Config, version string, notes string) !Published {70	head := history.head(store) or { return error(exchange.no_saves) }71	// Every build runs before the first upload, so two platforms landing on one72	// path would publish the second build's bytes for both. This is checked73	// before anything runs, so a collision builds nothing at all.74	mut paths := map[string]bool{}75	for platform in config.platforms {76		path := build.output_path(config, platform, version)77		if path in paths {78			return error('two platforms build to the same file, ${path} — name the platform in ${build.config_name}\'s output, as "out/demo-{platform}" does')79		}80		paths[path] = true81	}82	mut built := []build.Built{}83	for platform in config.platforms {84		built << build.build_one(store.root, config, platform, version)!85	}86	mut files := []File{}87	for artifact in built {88		// Read once: both hashes come from the same bytes, and a release file89		// can be tens of megabytes.90		bytes := os.read_file(os.join_path(store.root, artifact.path))!.bytes()91		file := describe(artifact, bytes)!92		upload(mut backend, repo, version, file, bytes)!93		files << file94	}95	record := Record{96		repo:         repo97		version:      version98		save:         head.id99		published_at: time.now().unix()100		machine:      machine.name()101		notes:        notes102		files:        files103	}104	// The record is what makes the version real. `put_if_absent`, so a record105	// already there stays: published versions never change. An identical record106	// is this publish retried and carries on; anything else is a version that107	// already exists with other files, refused as `upload` refuses a file.108	record_bytes := json2.encode(record).bytes()109	if !backend.put_if_absent(release_key(repo, version), record_bytes)! {110		existing := backend.get(release_key(repo, version))!111		if !existing.found || existing.body != record_bytes {112			return error('version ${version} is already published with different files — bump the version to publish again')113		}114	}115	move_latest(mut backend, repo, version)!116	return Published{117		version: version118		files:   files119	}120}121122// describe is one file's record, hashed from the bytes the upload sends.123fn describe(artifact build.Built, bytes []u8) !File {124	name := os.base(artifact.path)125	if !good_name(name) {126		return error('"${name}" cannot name a release file — no slash, backslash, quote or control character, and not "." or ".."')127	}128	return File{129		platform: artifact.platform130		name:     name131		size:     i64(bytes.len)132		blake3:   storage.hash_hex(bytes)133		sha256:   sha256.sum256(bytes).hex()134	}135}136137// good_name is Kepr's rule for a release file name: what Kepr refuses to read,138// Koh must not write.139fn good_name(name string) bool {140	if name == '' || name.len > 255 || name == '.' || name == '..' {141		return false142	}143	return !name.bytes().any(it == `/` || it == `\\` || it == `"` || it < 32 || it == 127)144}145146// upload puts one file at its key. A key that is already there is a retry of an147// interrupted publish when the bytes match, and a refusal when they do not:148// published versions never change (§11.2).149fn upload(mut backend remote.Backend, repo string, version string, file File, bytes []u8) ! {150	key := file_key(repo, version, file.platform, file.name)151	if backend.put_if_absent(key, bytes)! {152		return153	}154	existing := backend.get(key)!155	if !existing.found || storage.hash_hex(existing.body) != file.blake3 {156		return error('version ${version} already has a different ${file.name}')157	}158}159160// move_latest swaps the version pointer, which is the last step: until it moves,161// nothing about this version is visible.162fn move_latest(mut backend remote.Backend, repo string, version string) ! {163	current := backend.get(latest_key(repo))!164	if !backend.put_if_match(latest_key(repo), version.bytes(), current.tag)! {165		return error('${repo}/releases/latest moved while publishing — run it again')166	}167}168169// check_notes is Koh 2's rule for `--notes`, applied when one is given: a note170// that says nothing is worse than none, so a short one is refused. No notes at171// all is ordinary — the release then shows its save's message in Kepr.172pub fn check_notes(notes string) ! {173	trimmed := notes.trim_space()174	if trimmed == '' {175		return176	}177	words := trimmed.split_any(' \t\r\n').filter(it != '').len178	if trimmed.len < 40 || words < 12 {179		return error('--notes must be at least 40 characters and at least 12 words; that one is ${trimmed.len} characters and ${words} words')180	}181}