Skip to content

Commit

Permalink
fix(history): enable translated content history
Browse files Browse the repository at this point in the history
  • Loading branch information
fiji-flo committed Oct 14, 2024
1 parent 560f198 commit 7821bd2
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 19 deletions.
2 changes: 1 addition & 1 deletion crates/rari-cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ fn main() -> Result<(), Error> {
Commands::GitHistory => {
println!("Gathering history 📜");
let start = std::time::Instant::now();
gather_history();
gather_history()?;
println!("Took: {:?}", start.elapsed());
}
Commands::Popularities => {
Expand Down
6 changes: 1 addition & 5 deletions crates/rari-cli/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ struct AppError(anyhow::Error);

impl IntoResponse for AppError {
fn into_response(self) -> Response {
(
StatusCode::INTERNAL_SERVER_ERROR,
error!("🤷‍♂️: {}", self.0),
)
.into_response()
(StatusCode::INTERNAL_SERVER_ERROR, error!("🤷‍♂️: {}", self.0)).into_response()
}
}
impl<E> From<E> for AppError
Expand Down
1 change: 0 additions & 1 deletion crates/rari-doc/src/pages/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ fn build_doc(doc: &Doc) -> Result<BuiltDocy, DocError> {
},
history.map(|entry| entry.hash.as_str()).unwrap_or_default()
);

let popularity = popularities().popularities.get(doc.url()).cloned();
let other_translations = get_translations_for(doc.slug(), doc.locale())
.into_iter()
Expand Down
28 changes: 19 additions & 9 deletions crates/rari-tools/src/history.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
use std::collections::BTreeMap;
use std::fs::File;
use std::io::BufWriter;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::thread::spawn;

use rari_types::globals::content_root;
use rari_types::globals::{content_root, content_translated_root};
use rari_types::HistoryEntry;

pub fn gather_history() -> BTreeMap<PathBuf, HistoryEntry> {
modification_times()
use crate::error::ToolError;

pub fn gather_history() -> Result<(), ToolError> {
let hanlde = content_translated_root().map(|translated_root| {
spawn(|| {
modification_times(translated_root).unwrap();
})
});
modification_times(content_root())?;
if let Some(handle) = hanlde {
handle.join().expect("Unable to join history thread.");
}
Ok(())
}

fn modification_times(//path: &Path,
) -> BTreeMap<PathBuf, HistoryEntry> {
let path = content_root();
fn modification_times(path: &Path) -> Result<(), ToolError> {
let output = Command::new("git")
.args(["rev-parse", "--show-toplevel"])
.current_dir(path)
Expand Down Expand Up @@ -82,10 +92,10 @@ fn modification_times(//path: &Path,
})
.collect::<BTreeMap<PathBuf, HistoryEntry>>();

let out_file = path.join("en-US").join("_history.json");
let out_file = path.join("_git_history.json");
let file = File::create(out_file).unwrap();
let buffed = BufWriter::new(file);

serde_json::to_writer_pretty(buffed, &history).unwrap();
history
Ok(())
}
17 changes: 14 additions & 3 deletions crates/rari-types/src/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,30 @@ pub fn json_svg_data_lookup() -> &'static JsonSVGDataLookup {
}

pub static GIT_HISTORY: LazyLock<HashMap<PathBuf, HistoryEntry>> = LazyLock::new(|| {
let f = content_root().join("en-US").join("_history.json");
if let Ok(json_str) = fs::read_to_string(f) {
let f = content_root().join("_git_history.json");
let mut map = if let Ok(json_str) = fs::read_to_string(f) {
serde_json::from_str(&json_str).expect("unable to parse l10n json")
} else {
HashMap::new()
};
if let Some(translated_root) = content_translated_root() {
let f = translated_root.join("_git_history.json");
if let Ok(json_str) = fs::read_to_string(f) {
let translated: HashMap<PathBuf, HistoryEntry> =
serde_json::from_str(&json_str).expect("unable to parse l10n json");
map.extend(translated);
};
}
map
});
pub fn git_history() -> &'static HashMap<PathBuf, HistoryEntry> {
&GIT_HISTORY
}

pub static POPULARITIES: LazyLock<Popularities> = LazyLock::new(|| {
let f = content_root().join("en-US").join("popularities.json");
let f = content_root()
.join(Locale::EnUs.as_folder_str())
.join("popularities.json");
if let Ok(json_str) = fs::read_to_string(f) {
serde_json::from_str(&json_str).expect("unable to parse l10n json")
} else {
Expand Down

0 comments on commit 7821bd2

Please sign in to comment.