summaryrefslogtreecommitdiff
path: root/build.rs
blob: c986ca16207f3c88e0372af83fe6619a37d367f4 (plain)
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
use std::{io, process::Command};

fn main() -> Result<(), io::Error> {
    // trigger recompilation when a new migration is added
    println!("cargo::rerun-if-changed=migrations");

    // The following sections are intended to allow developers and packagers to create a working
    // `pilcrow` binary in one step in a clean source tree, using Cargo, even though Pilcrow is
    // written in two-and-a-half languages.
    //
    // The final binary embeds the Svelte UI. These steps build it. And, in service of that one-step
    // build idea, they also install the NPM dependencies needed to carry out that build. (Cargo
    // does this out of the box for Rust dependencies.)

    // rerun npm ci whenever packages or npm config are changed
    println!("cargo::rerun-if-changed=.npmrc");
    println!("cargo::rerun-if-changed=package.json");
    println!("cargo::rerun-if-changed=package-lock.json");
    // In theory, we should rerun `npm ci` if `node_modules` has changed since the last build, to
    // put it back into a known-good state. However, `npm ci` itself _always_ rewrites
    // `node_modules`, so asking Cargo to rerun this build script if `node_modules` changes leads to
    // Cargo always rerunning this build script. So, as a compromise, we assume that changes to
    // `package-lock.json` or `package.json` are sufficient justification to rerun `npm ci`, because
    // those are the most likely secondary indicators of changes to the installed packages.
    //
    // println!("cargo::rerun-if-changed=node_modules");
    let status = Command::new("npm").args(["ci"]).status()?;
    if !status.success() {
        return Err(io::Error::other(format!(
            "'npm ci' exited with status {status:?}"
        )));
    }

    // rerun `vite build` whenever the UI changes.
    //
    // `node_modules` is always touched if `npm install` runs, leading to spurious
    // rebuilds. (This duplicate is purely organizational; it reflects that the ui
    // depends on node_modules.)
    //
    // See: <https://github.com/npm/cli/issues/7874>
    // println!("cargo::rerun-if-changed=node_modules");
    println!("cargo::rerun-if-changed=postcss.config.js");
    println!("cargo::rerun-if-changed=svelte.config.js");
    println!("cargo::rerun-if-changed=vite.config.js");
    println!("cargo::rerun-if-changed=ui");
    let status = Command::new("npx").args(["vite", "build"]).status()?;
    if !status.success() {
        return Err(io::Error::other(format!(
            "'npx vite build' exited with status {status:?}"
        )));
    }

    Ok(())
}