Skip to content

Commit a3e3e87

Browse files
arglfiji-flo
andauthored
feature(cli): sync translated content (#24)
--------- Co-authored-by: Florian Dieminger <[email protected]>
1 parent 9c7baa4 commit a3e3e87

File tree

19 files changed

+750
-83
lines changed

19 files changed

+750
-83
lines changed

.cargo/config.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
TESTING_CONTENT_ROOT = { value = "tests/data/content/files", relative = true }
33
TESTING_CONTENT_TRANSLATED_ROOT = { value = "tests/data/translated_content/files", relative = true }
44
TESTING_CACHE_CONTENT = "0"
5+
TESTING_READER_IGNORES_GITIGNORE = "1"

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/rari-cli/main.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ use rari_tools::history::gather_history;
2323
use rari_tools::popularities::update_popularities;
2424
use rari_tools::r#move::r#move;
2525
use rari_tools::remove::remove;
26+
use rari_tools::sync_translated_content::sync_translated_content;
2627
use rari_types::globals::{build_out_root, content_root, content_translated_root, SETTINGS};
28+
use rari_types::locale::Locale;
2729
use rari_types::settings::Settings;
2830
use self_update::cargo_crate_version;
2931
use tabwriter::TabWriter;
@@ -68,21 +70,22 @@ enum ContentSubcommand {
6870
Move(MoveArgs),
6971
Delete(DeleteArgs),
7072
AddRedirect(AddRedirectArgs),
73+
SyncTranslatedContent(SyncTranslatedContentArgs),
7174
}
7275

7376
#[derive(Args)]
7477
struct MoveArgs {
7578
old_slug: String,
7679
new_slug: String,
77-
locale: Option<String>,
80+
locale: Option<Locale>,
7881
#[arg(short = 'y', long, help = "Assume yes to all prompts")]
7982
assume_yes: bool,
8083
}
8184

8285
#[derive(Args)]
8386
struct DeleteArgs {
8487
slug: String,
85-
locale: Option<String>,
88+
locale: Option<Locale>,
8689
#[arg(short, long, default_value_t = false)]
8790
recursive: bool,
8891
#[arg(long)]
@@ -97,6 +100,11 @@ struct AddRedirectArgs {
97100
to_url: String,
98101
}
99102

103+
#[derive(Args)]
104+
struct SyncTranslatedContentArgs {
105+
locales: Option<Vec<Locale>>,
106+
}
107+
100108
#[derive(Args)]
101109
struct UpdateArgs {
102110
#[arg(long)]
@@ -349,17 +357,12 @@ fn main() -> Result<(), Error> {
349357
}
350358
Commands::Content(content_subcommand) => match content_subcommand {
351359
ContentSubcommand::Move(args) => {
352-
r#move(
353-
&args.old_slug,
354-
&args.new_slug,
355-
args.locale.as_deref(),
356-
args.assume_yes,
357-
)?;
360+
r#move(&args.old_slug, &args.new_slug, args.locale, args.assume_yes)?;
358361
}
359362
ContentSubcommand::Delete(args) => {
360363
remove(
361364
&args.slug,
362-
args.locale.as_deref(),
365+
args.locale,
363366
args.recursive,
364367
args.redirect.as_deref(),
365368
args.assume_yes,
@@ -368,6 +371,10 @@ fn main() -> Result<(), Error> {
368371
ContentSubcommand::AddRedirect(args) => {
369372
add_redirect(&args.from_url, &args.to_url)?;
370373
}
374+
ContentSubcommand::SyncTranslatedContent(args) => {
375+
let locales = args.locales.as_deref().unwrap_or(Locale::translated());
376+
sync_translated_content(locales, cli.verbose.is_present())?;
377+
}
371378
},
372379
Commands::Update(args) => update(args.version)?,
373380
}

crates/rari-doc/src/cached_readers.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,17 +318,17 @@ pub fn read_and_cache_doc_pages() -> Result<Vec<Page>, DocError> {
318318
)
319319
.unwrap();
320320
if let Some(translated_root) = content_translated_root() {
321-
let transted_docs = read_docs_parallel::<Doc>(&[translated_root], None)?;
321+
let translated_docs = read_docs_parallel::<Doc>(&[translated_root], None)?;
322322
STATIC_DOC_PAGE_TRANSLATED_FILES
323323
.set(
324-
transted_docs
324+
translated_docs
325325
.iter()
326326
.cloned()
327327
.map(|doc| ((doc.locale(), Cow::Owned(doc.slug().to_string())), doc))
328328
.collect(),
329329
)
330330
.unwrap();
331-
docs.extend(transted_docs)
331+
docs.extend(translated_docs)
332332
}
333333
init_translations_from_static_docs();
334334
STATIC_DOC_PAGE_FILES_BY_PATH

crates/rari-doc/src/pages/page.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,26 @@ pub enum PageCategory {
4242

4343
impl Page {
4444
pub fn from_url(url: &str) -> Result<Self, DocError> {
45-
Self::from_url_with_other_locale_and_fallback(url, None)
45+
Self::internal_from_url_with_other_locale_and_fallback(url, None, true)
46+
}
47+
48+
pub fn from_url_no_fallback(url: &str) -> Result<Self, DocError> {
49+
Self::internal_from_url_with_other_locale_and_fallback(url, None, false)
4650
}
4751

4852
pub fn from_url_with_other_locale_and_fallback(
4953
url: &str,
5054
locale: Option<Locale>,
5155
) -> Result<Self, DocError> {
56+
Self::internal_from_url_with_other_locale_and_fallback(url, locale, true)
57+
}
58+
59+
fn internal_from_url_with_other_locale_and_fallback(
60+
url: &str,
61+
locale: Option<Locale>,
62+
fallback: bool,
63+
) -> Result<Self, DocError> {
64+
let url = &url[..url.find('#').unwrap_or(url.len())];
5265
let UrlMeta {
5366
folder_path,
5467
slug,
@@ -61,7 +74,7 @@ impl Page {
6174
.ok_or(DocError::PageNotFound(url.to_string(), PageCategory::SPA)),
6275
PageCategory::Doc => {
6376
let doc = Doc::page_from_slug_path(&folder_path, locale);
64-
if doc.is_err() && locale != Default::default() {
77+
if doc.is_err() && locale != Default::default() && fallback {
6578
Doc::page_from_slug_path(&folder_path, Default::default())
6679
} else {
6780
doc

crates/rari-doc/src/pages/types/doc.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ use crate::utils::{
3535
]
3636
*/
3737

38+
fn is_page_type_none(page_type: &PageType) -> bool {
39+
matches!(page_type, PageType::None)
40+
}
41+
3842
#[derive(Deserialize, Serialize, Clone, Debug, Default, Validate)]
3943
#[serde(default)]
4044
pub struct FrontMatter {
@@ -46,7 +50,7 @@ pub struct FrontMatter {
4650
#[serde(default, skip_serializing_if = "Vec::is_empty")]
4751
pub tags: Vec<String>,
4852
pub slug: String,
49-
#[serde(rename = "page-type")]
53+
#[serde(rename = "page-type", skip_serializing_if = "is_page_type_none")]
5054
pub page_type: PageType,
5155
#[serde(
5256
deserialize_with = "t_or_vec",

crates/rari-doc/src/walker.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::path::Path;
22

33
use ignore::types::TypesBuilder;
44
use ignore::WalkBuilder;
5-
use rari_types::globals::{content_root, content_translated_root};
5+
use rari_types::globals::{content_root, content_translated_root, settings};
66

77
pub fn walk_builder(
88
paths: &[impl AsRef<Path>],
@@ -25,6 +25,7 @@ pub fn walk_builder(
2525
}
2626
builder
2727
};
28+
builder.git_ignore(!settings().reader_ignores_gitignore);
2829
builder.types(types.build()?);
2930
Ok(builder)
3031
}

crates/rari-tools/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ rayon.workspace = true
2323
console = "0"
2424
dialoguer = "0"
2525
csv = "1"
26+
sha2 = "0.10"
2627

2728
[dev-dependencies]
2829
serial_test = { version = "3", features = ["file_locks"] }

crates/rari-tools/src/add_redirect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ mod test {
8686
use super::*;
8787
use crate::tests::fixtures::docs::DocFixtures;
8888
use crate::tests::fixtures::redirects::RedirectFixtures;
89-
use crate::utils::test_utils::get_redirects_map;
89+
use crate::utils::get_redirects_map;
9090

9191
#[test]
9292
fn test_add_redirect() {

crates/rari-tools/src/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ pub enum ToolError {
1212
InvalidSlug(Cow<'static, str>),
1313
#[error("Invalid url: {0}")]
1414
InvalidUrl(Cow<'static, str>),
15+
#[error("Invalid locale: {0}")]
16+
InvalidLocale(Cow<'static, str>),
17+
#[error("Orphaned doc exists: {0}")]
18+
OrphanedDocExists(Cow<'static, str>),
1519
#[error("Git error: {0}")]
1620
GitError(String),
1721

0 commit comments

Comments
 (0)