v / examples / build_system / build.vsh
58 lines · 50 sloc · 1.78 KB · e972860fc97ccecbe45560e7c22d2ba65d67783f
Raw
1#!/usr/bin/env -S v run
2
3import build
4import time
5
6// Define variables that can be used to change tasks in the buildscript
7const app_name = 'hello'
8const program_args = 'World'
9const build_dir = 'target'
10
11// Make the build context
12mut context := build.context(
13 // Set the default task to `release` when no arguments are provided
14 default: 'release'
15)
16
17// Add a few simple tasks
18context.task(name: 'doc', run: |self| system('echo "Nothing to do"'))
19context.task(name: 'run', run: |self| system('v run . ${program_args}'))
20context.task(name: 'build', run: |self| system('v .'))
21context.task(name: 'build.prod', run: |self| system('v -prod -o ${app_name} .'))
22
23// `_` to denote "private" tasks. Nothing stops the user from using it, but
24// this tells them that the task is not meant to be used by them.
25context.task(
26 name: '_mkdirs'
27 // The `help` field is displayed in `--tasks` to give a short summary of what the task does.
28 help: 'Makes the directories used by the application'
29 run: fn (self build.Task) ! {
30 if !exists(build_dir) {
31 mkdir_all(build_dir) or { panic(err) }
32 }
33 }
34)
35
36// This task will only run when the `test.txt` file is outdated
37context.artifact(
38 name: 'test.txt'
39 help: 'Generate test.txt'
40 run: fn (self build.Task) ! {
41 write_file('test.txt', time.now().str())!
42 }
43)
44
45// Add a more complex task
46context.task(
47 name: 'release'
48 help: 'Build the app in production mode, generates documentation, and releases the build on Git'
49 depends: ['_mkdirs', 'doc', 'test.txt']
50 run: fn (self build.Task) ! {
51 system('v -prod -o ${build_dir}/${app_name} .')
52 // Pretend we are using Git to publish the built file as a release here.
53 }
54)
55
56// Run the build context. This will iterate over os.args and each corresponding
57// task, skipping any arguments that start with a hyphen (-)
58context.run()
59