cmd_build.v

2.6 KB · raw

1//! `koh build` — build with `koh.build`, and upload nothing.2//!3//! Publishing is `koh offer --builds`; this is for the person who wants the4//! files, or who wants to know whether the build works at all.56module cli78import json29import src.build10import src.fmt1112pub fn cmd_build(raw []string) int {13	args := parse('build', raw, Options{14		values: ['platform']15		flags:  ['all', 'json']16	}) or { return usage_error(err.msg()) }17	// Two ways of naming platforms at once is a mistake in the command line, so18	// it is reported as one before anything is read.19	if args.flag('all') && args.values_of('platform').len > 0 {20		return usage_error('--all and --platform cannot be combined')21	}22	store := open_store() or { return refuse(args.json(), err.msg()) }23	defer {24		store.close()25	}26	config := build.read_config(store.root) or { return refuse(args.json(), err.msg()) }27	// Not `version`: that is the koh binary's own version, in this module.28	published_version := build.project_version(store.root) or {29		return refuse(args.json(), err.msg())30	}31	// The version comes before the platform list, so a project that builds for32	// another platform still says which version it declares.33	if !args.json() {34		note('version ${published_version}')35	}36	platforms := wanted_platforms(args, config) or { return refuse(args.json(), err.msg()) }37	mut built := []build.Built{}38	for platform in platforms {39		working('building ${platform}')40		result := build.build_one(store.root, config, platform, published_version) or {41			return refuse(args.json(), err.msg())42		}43		built << result44		if !args.json() {45			ok('${platform}  ${result.path}  (${fmt.human_bytes(result.size)})')46		}47	}48	if args.json() {49		emit_json(json2.encode(BuildJson{50			version: published_version51			files:   built.map(BuiltJson{52				platform: it.platform53				path:     it.path54				size:     it.size55			})56		}))57	}58	return 059}6061// wanted_platforms is what to build: every platform `koh.build` names with62// --all, the named ones with --platform, and this machine's own otherwise. A63// platform the file does not build for is refused rather than skipped.64fn wanted_platforms(args Args, config build.Config) ![]string {65	mut wanted := args.values_of('platform')66	if args.flag('all') {67		wanted = config.platforms.clone()68	} else if wanted.len == 0 {69		wanted = [build.this_machine()!]70	}71	for platform in wanted {72		if platform !in config.platforms {73			return error('${build.config_name} does not build for "${platform}" — it builds for ${config.platforms.join(', ')}')74		}75	}76	return wanted77}7879struct BuiltJson {80	platform string81	path     string82	size     i6483}8485struct BuildJson {86	version string87	files   []BuiltJson88}