Anki/build/runner/src/yarn.rs
Kai Knoblich 42cc2c913c
Add support for offline builds (#2963)
* CONTRIBUTORS: Add myself to the contributors list

* Add support for offline builds

Downloading files during build time is a non-starter for FreeBSD ports
(and presumably for other *BSD ports and some Linux distros as well).

In order to still be able to build Anki successfully, two new
environment variables have been added that can be set accordingly:

* NO_VENV: If set, the Python system environment is used instead of
  a venv. This is necessary if there are no usable Python wheels for a
  platform, e.g. PyQt6.

* OFFLINE_BUILD: If set, the git repository synchronization (translation
  files, build hash, etc.) is skipped.

To successfully build Anki offline, following conditions must be met:

1. All required dependencies (node, Python, rust, yarn, etc.) must be
   present in the build environment.

2. The offline repositories for the translation files must be
   copied/linked to ftl/qt-repo and ftl/core-repo.

3. The Python pseudo venv needs to be setup:

   $ mkdir out/pyenv/bin
   $ ln -s /path/to/python out/pyenv/bin/python
   $ ln -s /path/to/protoc-gen-mypy out/pyenv/bin/protoc-gen-mypy

4. Create the offline cache for yarn and use its own environment
   variable YARN_CACHE_FOLDER to it:

   YARN_CACHE_FOLDER=/path/to/the/yarn/cache
   $ /path/to/yarn install --ignore-scripts

5. Build Anki:

   $ /path/to/cargo build --package runner --release --verbose --verbose
   $ OFFLINE_BUILD=1 \
     NO_VENV=1 \
     ${WRKSRC}/out/rust/release/runner build wheels
2024-01-31 09:13:46 +10:00

64 lines
1.9 KiB
Rust

// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
use std::env;
use std::path::Path;
use std::process::Command;
use clap::Args;
use crate::run::run_command;
#[derive(Args)]
pub struct YarnArgs {
yarn_bin: String,
stamp: String,
}
pub fn setup_yarn(args: YarnArgs) {
link_node_modules();
if env::var("OFFLINE_BUILD").is_ok() {
println!("OFFLINE_BUILD is set");
println!("Running yarn with '--offline' and '--ignore-scripts'.");
run_command(
Command::new(&args.yarn_bin)
.arg("install")
.arg("--offline")
.arg("--ignore-scripts"),
);
} else {
run_command(Command::new(&args.yarn_bin).arg("install"));
}
std::fs::write(args.stamp, b"").unwrap();
}
/// Unfortunately a lot of the node ecosystem expects the output folder to
/// reside in the repo root, so we need to link in our output folder.
#[cfg(not(windows))]
fn link_node_modules() {
let target = Path::new("node_modules");
if target.exists() {
if !target.is_symlink() {
panic!("please remove the node_modules folder from the repo root");
}
} else {
std::os::unix::fs::symlink("out/node_modules", target).unwrap();
}
}
/// Things are more complicated on Windows - having $root/node_modules point to
/// $root/out/node_modules breaks our globs for some reason, so we create the
/// junction in the opposite direction instead. Ninja will have already created
/// some empty folders based on our declared outputs, so we move the
/// created folder into the root.
#[cfg(windows)]
fn link_node_modules() {
let target = Path::new("out/node_modules");
let source = Path::new("node_modules");
if !source.exists() {
std::fs::rename(target, source).unwrap();
junction::create(source, target).unwrap()
}
}