Skip to content

Commit 9c9e1fd

Browse files
committed
no-bug: A few quality-of-life tweaks
package.json: Invoke cargo(1) through a Python script that honors the CARGO environment variable if set, and pass arguments to ffprefs using the new convention (see below) run_cargo.py: Script to invoke cargo(1), as a workaround for reading an environment variable in a package.json script in a cross-platform manner update_service_dumps.py: Allow specifying DUMPS_FOLDER and ENGINE_DUMPS_FOLDER on the command line ffprefs/src/main.rs: Allow specifying the prefs and engine dirs on the command line instead of hard-coding their locations relative to a common root dir
1 parent 8f0edbd commit 9c9e1fd

4 files changed

Lines changed: 42 additions & 29 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"surfer": "surfer",
2828
"test": "python3 scripts/run_tests.py",
2929
"test:dbg": "python3 scripts/run_tests.py --jsdebugger --debug-on-failure",
30-
"ffprefs": "cd tools/ffprefs && cargo run --bin ffprefs -- ../../",
30+
"ffprefs": "python3 scripts/run_cargo.py run --manifest-path tools/ffprefs/Cargo.toml --bin ffprefs -- prefs engine",
3131
"lc": "surfer license-check",
3232
"lc:fix": "surfer license-check --fix",
3333
"use-moz-src": "cd engine && ./mach use-moz-src",

scripts/run_cargo.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# This Source Code Form is subject to the terms of the Mozilla Public
2+
# License, v. 2.0. If a copy of the MPL was not distributed with this
3+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
5+
import os
6+
import sys
7+
8+
cargo = os.environ.get('CARGO', 'cargo')
9+
10+
command = [cargo] + sys.argv[1:]
11+
12+
try:
13+
os.execvp(command[0], command)
14+
except OSError as e:
15+
print(f"{sys.argv[0]}: '{command[0]}': {e}", file=sys.stderr)
16+
if 'CARGO' in os.environ:
17+
print('(CARGO environment variable is set)', file=sys.stderr)
18+
sys.exit(1)

scripts/update_service_dumps.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,7 @@ def main():
6060

6161

6262
if __name__ == "__main__":
63+
import sys
64+
if len(sys.argv) == 3:
65+
_, DUMPS_FOLDER, ENGINE_DUMPS_FOLDER = sys.argv
6366
main()

tools/ffprefs/src/main.rs

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ use std::env;
107107
use std::fs;
108108
use std::path::PathBuf;
109109

110-
const STATIC_PREFS: &str = "../engine/modules/libpref/init/zen-static-prefs.inc";
111-
const FIREFOX_PREFS: &str = "../engine/browser/app/profile/firefox.js";
112-
const DYNAMIC_PREFS: &str = "../engine/browser/app/profile/zen.js";
110+
const STATIC_PREFS: &str = "modules/libpref/init/zen-static-prefs.inc";
111+
const FIREFOX_PREFS: &str = "browser/app/profile/firefox.js";
112+
const DYNAMIC_PREFS: &str = "browser/app/profile/zen.js";
113113

114114
#[derive(Serialize, Deserialize, PartialEq, Debug)]
115115
struct Preference {
@@ -124,12 +124,6 @@ struct Preference {
124124
sticky: Option<bool>,
125125
}
126126

127-
fn get_config_path() -> PathBuf {
128-
let mut path = env::current_dir().expect("Failed to get current directory");
129-
path.push("prefs");
130-
path
131-
}
132-
133127
fn ordered_prefs(mut prefs: Vec<Preference>) -> Vec<Preference> {
134128
// Sort preferences by name
135129
prefs.sort_by(|a, b| a.name.cmp(&b.name));
@@ -153,11 +147,10 @@ fn get_prefs_files_recursively(dir: &PathBuf, files: &mut Vec<PathBuf>) {
153147
}
154148
}
155149

156-
fn load_preferences() -> Vec<Preference> {
150+
fn load_preferences(prefs_path: &PathBuf) -> Vec<Preference> {
157151
let mut prefs = Vec::new();
158-
let config_path = get_config_path();
159152
let mut pref_files = Vec::new();
160-
get_prefs_files_recursively(&config_path, &mut pref_files);
153+
get_prefs_files_recursively(&prefs_path, &mut pref_files);
161154
for file_path in pref_files {
162155
let content = fs::read_to_string(&file_path).expect("Failed to read file");
163156
let mut parsed_prefs: Vec<Preference> =
@@ -263,14 +256,9 @@ fn get_value(pref: &Preference) -> String {
263256
}
264257
}
265258

266-
fn write_preferences(prefs: &[Preference]) {
267-
let config_path = get_config_path();
268-
if !config_path.exists() {
269-
fs::create_dir_all(&config_path).expect("Failed to create prefs directory");
270-
}
271-
272-
let static_prefs_path = config_path.join(STATIC_PREFS);
273-
let dynamic_prefs_path = config_path.join(DYNAMIC_PREFS);
259+
fn write_preferences(engine_path: &PathBuf, prefs: &[Preference]) {
260+
let static_prefs_path = engine_path.join(STATIC_PREFS);
261+
let dynamic_prefs_path = engine_path.join(DYNAMIC_PREFS);
274262
println!(
275263
"Writing preferences to:\n Static: {}\n Dynamic: {}",
276264
static_prefs_path.display(),
@@ -300,10 +288,10 @@ fn write_preferences(prefs: &[Preference]) {
300288
fs::write(&dynamic_prefs_path, dynamic_content).expect("Failed to write dynamic prefs");
301289
}
302290

303-
fn prepare_zen_prefs() {
291+
fn prepare_zen_prefs(engine_path: &PathBuf) {
304292
// Add `#include zen.js` to the bottom of the firefox.js file if it doesn't exist
305293
let line = "#include zen.js";
306-
let firefox_prefs_path = get_config_path().join(FIREFOX_PREFS);
294+
let firefox_prefs_path = engine_path.join(FIREFOX_PREFS);
307295
if let Ok(mut content) = fs::read_to_string(&firefox_prefs_path) {
308296
if !content.contains(line) {
309297
content.push_str(format!("\n{}\n", line).as_str());
@@ -351,15 +339,19 @@ fn expand_pref_values(prefs: &mut [Preference]) {
351339

352340
fn main() {
353341
let args: Vec<String> = env::args().collect();
354-
let root_path = if args.len() > 1 {
342+
let prefs_path = if args.len() > 1 {
355343
PathBuf::from(&args[1])
356344
} else {
357-
env::current_dir().expect("Failed to get current directory")
345+
PathBuf::from("prefs")
346+
};
347+
let engine_path = if args.len() > 2 {
348+
PathBuf::from(&args[2])
349+
} else {
350+
PathBuf::from("engine")
358351
};
359-
env::set_current_dir(&root_path).expect("Failed to change directory");
360352

361-
prepare_zen_prefs();
362-
let mut preferences = load_preferences();
353+
prepare_zen_prefs(&engine_path);
354+
let mut preferences = load_preferences(&prefs_path);
363355
expand_pref_values(&mut preferences);
364-
write_preferences(&preferences);
356+
write_preferences(&engine_path, &preferences);
365357
}

0 commit comments

Comments
 (0)