Skip to content

Commit 17d002e

Browse files
committed
remap POST requests url
allows using `ankidroid.postBaseUrl` as the base url for POST requests
1 parent 6620d90 commit 17d002e

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

build_rust/src/main.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ use anki_io::{copy_file, create_dir_all, read_dir_files};
22
use anki_process::CommandExt;
33
use anyhow::Result;
44
use camino::{Utf8Path, Utf8PathBuf};
5-
use std::env;
65
use std::env::consts::OS;
7-
use std::path::Path;
6+
use std::fs::File;
7+
use std::io::{BufRead, BufReader, BufWriter, Write};
8+
use std::path::{Path, PathBuf};
89
use std::process::Command;
10+
use std::{env, io};
911

1012
const ANDROID_OUT_DIR: &str = "rsdroid/build/generated/jniLibs";
1113
const ROBOLECTRIC_OUT_DIR: &str = "rsdroid-testing/build/generated/jniLibs";
@@ -109,6 +111,16 @@ fn build_web_artifacts() -> Result<()> {
109111
"anki/ts/licenses.json",
110112
artifacts_dir.join("web/licenses-ts.json"),
111113
)?;
114+
let _ = replace_string_in_file(
115+
&artifacts_dir.join("web/reviewer.js"),
116+
"\"/_anki/\"",
117+
"`${ankidroid.postBaseUrl}_anki/`",
118+
);
119+
let _ = replace_string_in_file(
120+
&artifacts_dir.join("web/reviewer_extras_bundle.js"),
121+
"\"/_anki/\"",
122+
"`${ankidroid.postBaseUrl}_anki/`",
123+
);
112124
Ok(())
113125
}
114126

@@ -282,3 +294,24 @@ fn build_rsdroid(is_release: bool, target_arch: &str, target_dir: &Utf8Path) ->
282294
command.ensure_success()?;
283295
Ok(())
284296
}
297+
298+
fn replace_string_in_file(file_path: &PathBuf, old_word: &str, new_word: &str) -> io::Result<()> {
299+
let input_file = File::open(file_path)?;
300+
let reader = BufReader::new(input_file);
301+
302+
let temp_file_path = file_path.with_extension("tmp");
303+
let temp_file = File::create(&temp_file_path)?;
304+
let mut writer = BufWriter::new(temp_file);
305+
306+
for line in reader.lines() {
307+
let line = line?;
308+
let modified_line = line.replace(old_word, new_word);
309+
writeln!(writer, "{}", modified_line)?;
310+
}
311+
312+
writer.flush()?;
313+
314+
std::fs::rename(&temp_file_path, file_path)?;
315+
316+
Ok(())
317+
}

0 commit comments

Comments
 (0)