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		result := build.build_one(store.root, config, platform, published_version) or {40			return refuse(args.json(), err.msg())41		}42		built << result43		if !args.json() {44			ok('${platform}  ${result.path}  (${fmt.human_bytes(result.size)})')45		}46	}47	if args.json() {48		emit_json(json2.encode(BuildJson{49			version: published_version50			files:   built.map(BuiltJson{51				platform: it.platform52				path:     it.path53				size:     it.size54			})55		}))56	}57	return 058}5960// wanted_platforms is what to build: every platform `koh.build` names with61// --all, the named ones with --platform, and this machine's own otherwise. A62// platform the file does not build for is refused rather than skipped.63fn wanted_platforms(args Args, config build.Config) ![]string {64	mut wanted := args.values_of('platform')65	if args.flag('all') {66		wanted = config.platforms.clone()67	} else if wanted.len == 0 {68		wanted = [build.this_machine()!]69	}70	for platform in wanted {71		if platform !in config.platforms {72			return error('${build.config_name} does not build for "${platform}" — it builds for ${config.platforms.join(', ')}')73		}74	}75	return wanted76}7778struct BuiltJson {79	platform string80	path     string81	size     i6482}8384struct BuildJson {85	version string86	files   []BuiltJson87}