| 1 | #!/usr/bin/env -S v run |
| 2 | |
| 3 | import build |
| 4 | import time |
| 5 | |
| 6 | // Define variables that can be used to change tasks in the buildscript |
| 7 | const app_name = 'hello' |
| 8 | const program_args = 'World' |
| 9 | const build_dir = 'target' |
| 10 | |
| 11 | // Make the build context |
| 12 | mut 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 |
| 18 | context.task(name: 'doc', run: |self| system('echo "Nothing to do"')) |
| 19 | context.task(name: 'run', run: |self| system('v run . ${program_args}')) |
| 20 | context.task(name: 'build', run: |self| system('v .')) |
| 21 | context.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. |
| 25 | context.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 |
| 37 | context.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 |
| 46 | context.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 (-) |
| 58 | context.run() |
| 59 | |