-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathproject.bri
168 lines (154 loc) · 4.67 KB
/
project.bri
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import * as std from "std";
import rust, { cargoBuild, vendorCrate } from "rust";
import curl from "curl";
import caCertificates from "ca_certificates";
import nodejs, { npmInstall } from "nodejs";
import { gitCheckout } from "git";
import openssl from "openssl";
/**
* Returns a recipe to build Brioche itself. This results in a packed build,
* which-- unlike a normal glibc-based build-- is portable and runs even
* if the host doesn't have glibc.
*/
export default function (): std.Recipe<std.Directory> {
let source = Brioche.glob(
// Cargo project files
"crates/**/*.rs",
"**/Cargo.toml",
"Cargo.lock",
// SQLx files
"crates/brioche-core/.sqlx",
"crates/brioche-core/migrations",
// JS runtime files
"crates/brioche-core/runtime/package.json",
"crates/brioche-core/runtime/package-lock.json",
"crates/brioche-core/runtime/tsconfig.json",
"crates/brioche-core/runtime/build.ts",
"crates/brioche-core/runtime/src",
"crates/brioche-core/runtime/tslib",
"crates/brioche-core/runtime/dist",
// Makefiles for miscellaneous tasks
"**/Makefile",
);
source = validateRuntime(source);
source = validateSqlxMetadata(source);
return cargoBuild({
source,
path: "crates/brioche",
runnable: "bin/brioche",
buildParams: {
// Disable self-updates. Currently, self updates aren't supported for
// packed builds.
defaultFeatures: false,
},
unsafe: {
// Network access is used for vendoring libraries (e.g. V8)
networking: true,
},
dependencies: [curl(), caCertificates()],
});
}
/**
* Use the SQLx CLI to validate that the prepared SQLx metadata is up-to-date
* with all of the database migrations and queries.
*/
function validateSqlxMetadata(
source: std.Recipe<std.Directory>,
): std.Recipe<std.Directory> {
const vendoredSource = vendorCrate({ source });
// Run `make check-db-schema` to ensure the database schema is up-to-date.
return validate({
recipe: source,
validation: std.runBash`
make check-db-schema
`
.workDir(vendoredSource)
.dependencies(
rust(),
sqlxCli(),
std.toolchain(),
curl(),
caCertificates(),
)
.unsafe({ networking: true })
.outputScaffold(std.directory())
.toDirectory(),
});
}
/**
* Validate that the source of the JS runtime project under
* `crates/brioche-core/runtime` matches the output `dist/` directory.
* This ensures the source and build don't go out of sync.
*/
function validateRuntime(
source: std.Recipe<std.Directory>,
): std.Recipe<std.Directory> {
return validate({
recipe: source,
validation: std.runBash`
if ! diff -r "$current_runtime" "$runtime"; then
echo "Error: 'crates/brioche-core/runtime/dist' does not match!" >&2
exit 1
fi
`
.env({
current_runtime: source.get("crates/brioche-core/runtime/dist"),
runtime: runtime(),
})
.outputScaffold(std.directory())
.toDirectory(),
});
}
/**
* Build the JS runtime project under `crates/brioche-core/runtime`. Returns
* the built directory, which is expected to be stored under the directory
* `crates/brioche-core/runtime/dist`.
*/
export function runtime(): std.Recipe<std.Directory> {
const source = Brioche.glob(
"crates/brioche-core/runtime/package.json",
"crates/brioche-core/runtime/package-lock.json",
"crates/brioche-core/runtime/tsconfig.json",
"crates/brioche-core/runtime/build.ts",
"crates/brioche-core/runtime/src",
"crates/brioche-core/runtime/tslib",
).peel(3);
const npmPackage = npmInstall({ source });
return std.runBash`
npm run build
mv dist "$BRIOCHE_OUTPUT"
`
.dependencies(nodejs())
.workDir(npmPackage)
.toDirectory();
}
export function sqlxCli() {
const source = gitCheckout(
Brioche.gitRef({
repository: "https://github.com/launchbadge/sqlx.git",
ref: "v0.7.4",
}),
);
return cargoBuild({
source,
path: "sqlx-cli",
dependencies: [openssl()],
runnable: "bin/sqlx",
});
}
interface ValidateOptions {
recipe: std.Recipe<std.Directory>;
validation: std.Recipe<std.Directory>;
}
/**
* Add a validation to a recipe. When the recipe is baked, the validation
* will also be baked, and will fail if the validation fails. The validation
* should be a process recipe that returns an empty directory on success.
*
* Internally, this just merges the validation and recipe results together.
* This results in a new recipe that depends on both the original recipe
* and the validation.
*/
function validate(options: ValidateOptions): std.Recipe<std.Directory> {
return std.merge(options.validation, options.recipe);
}