From 9780e05ace2e7b5e171d95a09bac37b20bce3486 Mon Sep 17 00:00:00 2001 From: Arpita-Jaiswal Date: Tue, 19 Apr 2022 20:15:01 +0530 Subject: [PATCH 01/13] Initial commit --- ftd/fpm.ftd | 1 + src/commands/build.rs | 5 +++ src/config.rs | 97 ++++++++++++++++++++++++++++++++++++++++++- src/lib.rs | 1 + src/version.rs | 18 ++++++++ 5 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 src/version.rs diff --git a/ftd/fpm.ftd b/ftd/fpm.ftd index aa8a2170..ed740bd6 100644 --- a/ftd/fpm.ftd +++ b/ftd/fpm.ftd @@ -1,5 +1,6 @@ -- record package-data: caption name: +boolean versioned: false optional body about: optional string zip: optional string language: diff --git a/src/commands/build.rs b/src/commands/build.rs index 0415c59e..8702c383 100644 --- a/src/commands/build.rs +++ b/src/commands/build.rs @@ -46,6 +46,11 @@ pub async fn build( ); } + if config.package.versioned { + fpm::version::build_version(config, file, base_url, ignore_failed, &asset_documents) + .await?; + } + match ( config.package.translation_of.as_ref(), config.package.translations.has_elements(), diff --git a/src/config.rs b/src/config.rs index 87f84065..62f01147 100644 --- a/src/config.rs +++ b/src/config.rs @@ -334,6 +334,98 @@ impl Config { Ok(()) } + pub(crate) async fn get_versions( + &self, + package: &fpm::Package, + ) -> fpm::Result>> { + let path = if let Some(package_fpm_path) = &package.fpm_path { + // TODO: Unwrap? + package_fpm_path.parent().unwrap().to_owned() + } else if package.name.eq(&self.package.name) { + self.root.clone() + } else { + self.packages_root.clone().join(package.name.as_str()) + }; + let mut ignore_paths = ignore::WalkBuilder::new(&path); + ignore_paths.overrides(fpm::file::package_ignores(package, &path)?); + + let mut hash: std::collections::HashMap> = + std::collections::HashMap::new(); + + let all_files = ignore_paths + .build() + .into_iter() + .flatten() + .map(|x| camino::Utf8PathBuf::from_path_buf(x.into_path()).unwrap()) //todo: improve error message + .collect::>(); + + for file in all_files { + if file.is_dir() { + continue; + } + let version = if let Some(v) = get_version(&file, &path)? { + v + } else { + continue; + }; + let file = fpm::get_file(package.name.to_string(), &file, &path).await?; + if let Some(files) = hash.get_mut(&version) { + files.push(file) + } else { + hash.insert(version, vec![file]); + } + } + return Ok(hash); + + fn get_version( + x: &camino::Utf8PathBuf, + path: &camino::Utf8PathBuf, + ) -> fpm::Result> { + let id = match std::fs::canonicalize(x)?.to_str().unwrap().rsplit_once( + if path.as_str().ends_with(std::path::MAIN_SEPARATOR) { + path.as_str().to_string() + } else { + format!("{}{}", path, std::path::MAIN_SEPARATOR) + } + .as_str(), + ) { + Some((_, id)) => id.to_string(), + None => { + return Err(fpm::Error::UsageError { + message: format!("{:?} should be a file", x), + }); + } + }; + if id.eq("FPM.ftd") { + return Ok(None); + } + let v = if let Some((v, _)) = id.split_once('/') { + v + } else { + return Err(fpm::Error::UsageError { + message: format!("{:?} should be inside version folder", x), + }); + }; + + let number = if let Some(d) = v.strip_prefix('v') { + d + } else { + return Err(fpm::Error::UsageError { + message: format!("{:?} should be inside version folder", x), + }); + }; + number + .parse::() + .map(|v| Some(v)) + .map_err(|_| fpm::Error::UsageError { + message: format!( + "Folder should be structured as `v`, found: `{:?}`", + x + ), + }) + } + } + pub(crate) async fn get_files(&self, package: &fpm::Package) -> fpm::Result> { let path = if let Some(package_fpm_path) = &package.fpm_path { // TODO: Unwrap? @@ -473,6 +565,7 @@ pub(crate) fn find_root_for_file( #[derive(serde::Deserialize, Debug, Clone)] pub(crate) struct PackageTemp { pub name: String, + pub versioned: bool, #[serde(rename = "translation-of")] pub translation_of: Option, #[serde(rename = "translation")] @@ -502,6 +595,7 @@ impl PackageTemp { fpm::Package { name: self.name, + versioned: self.versioned, translation_of: Box::new(translation_of), translations, language: self.language, @@ -522,6 +616,7 @@ impl PackageTemp { #[derive(Debug, Clone)] pub struct Package { pub name: String, + pub versioned: bool, pub translation_of: Box>, pub translations: Vec, pub language: Option, @@ -541,7 +636,6 @@ pub struct Package { /// /// Note that this too is kind of bad design, we will move fonts to `fpm::Package` struct soon. pub fonts: Vec, - pub import_auto_imports_from_original: bool, } @@ -549,6 +643,7 @@ impl Package { pub fn new(name: &str) -> fpm::Package { fpm::Package { name: name.to_string(), + versioned: false, translation_of: Box::new(None), translations: vec![], language: None, diff --git a/src/lib.rs b/src/lib.rs index 49d71660..f6cead8d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,6 +19,7 @@ mod render; mod snapshot; mod tracker; mod translation; +mod version; pub(crate) use auto_import::AutoImport; pub(crate) use commands::build::process_file; diff --git a/src/version.rs b/src/version.rs new file mode 100644 index 00000000..622f5d8f --- /dev/null +++ b/src/version.rs @@ -0,0 +1,18 @@ +pub(crate) async fn build_version( + config: &fpm::Config, + file: Option<&str>, + base_url: &str, + skip_failed: bool, + asset_documents: &std::collections::HashMap, +) -> fpm::Result<()> { + // let documents = std::collections::BTreeMap::from_iter( + // config + // .get_files(&config.package) + // .await? + // .into_iter() + // .map(|v| (v.get_id(), v)), + // ); + let versioned_documents = config.get_versions(&config.package).await?; + dbg!(&versioned_documents); + Ok(()) +} From fc346fcae79ed36832c449b00e95fb20c4073afc Mon Sep 17 00:00:00 2001 From: Arpita-Jaiswal Date: Tue, 19 Apr 2022 20:47:06 +0530 Subject: [PATCH 02/13] Versions working --- src/commands/build.rs | 57 ++++++++++++++++++++++--------------------- src/config.rs | 15 ++++++++---- src/file.rs | 9 +++++++ src/version.rs | 51 +++++++++++++++++++++++++++++++------- 4 files changed, 90 insertions(+), 42 deletions(-) diff --git a/src/commands/build.rs b/src/commands/build.rs index 8702c383..8a3cc41e 100644 --- a/src/commands/build.rs +++ b/src/commands/build.rs @@ -49,36 +49,37 @@ pub async fn build( if config.package.versioned { fpm::version::build_version(config, file, base_url, ignore_failed, &asset_documents) .await?; + } else { + match ( + config.package.translation_of.as_ref(), + config.package.translations.has_elements(), + ) { + (Some(_), true) => { + // No package can be both a translation of something and has its own + // translations, when building `config` we ensured this was rejected + unreachable!() + } + (Some(original), false) => { + build_with_original( + config, + original, + file, + base_url, + ignore_failed, + &asset_documents, + ) + .await + } + (None, false) => { + build_simple(config, file, base_url, ignore_failed, &asset_documents).await + } + (None, true) => { + build_with_translations(config, file, base_url, ignore_failed, &asset_documents) + .await + } + }?; } - match ( - config.package.translation_of.as_ref(), - config.package.translations.has_elements(), - ) { - (Some(_), true) => { - // No package can be both a translation of something and has its own - // translations, when building `config` we ensured this was rejected - unreachable!() - } - (Some(original), false) => { - build_with_original( - config, - original, - file, - base_url, - ignore_failed, - &asset_documents, - ) - .await - } - (None, false) => { - build_simple(config, file, base_url, ignore_failed, &asset_documents).await - } - (None, true) => { - build_with_translations(config, file, base_url, ignore_failed, &asset_documents).await - } - }?; - for dep in dependencies { let static_files = std::collections::BTreeMap::from_iter( config diff --git a/src/config.rs b/src/config.rs index 62f01147..30efbd28 100644 --- a/src/config.rs +++ b/src/config.rs @@ -337,7 +337,7 @@ impl Config { pub(crate) async fn get_versions( &self, package: &fpm::Package, - ) -> fpm::Result>> { + ) -> fpm::Result>> { let path = if let Some(package_fpm_path) = &package.fpm_path { // TODO: Unwrap? package_fpm_path.parent().unwrap().to_owned() @@ -349,7 +349,7 @@ impl Config { let mut ignore_paths = ignore::WalkBuilder::new(&path); ignore_paths.overrides(fpm::file::package_ignores(package, &path)?); - let mut hash: std::collections::HashMap> = + let mut hash: std::collections::HashMap> = std::collections::HashMap::new(); let all_files = ignore_paths @@ -368,7 +368,12 @@ impl Config { } else { continue; }; - let file = fpm::get_file(package.name.to_string(), &file, &path).await?; + let file = fpm::get_file( + package.name.to_string(), + &file, + &path.join(format!("v{}", version)), + ) + .await?; if let Some(files) = hash.get_mut(&version) { files.push(file) } else { @@ -380,7 +385,7 @@ impl Config { fn get_version( x: &camino::Utf8PathBuf, path: &camino::Utf8PathBuf, - ) -> fpm::Result> { + ) -> fpm::Result> { let id = match std::fs::canonicalize(x)?.to_str().unwrap().rsplit_once( if path.as_str().ends_with(std::path::MAIN_SEPARATOR) { path.as_str().to_string() @@ -415,7 +420,7 @@ impl Config { }); }; number - .parse::() + .parse::() .map(|v| Some(v)) .map_err(|_| fpm::Error::UsageError { message: format!( diff --git a/src/file.rs b/src/file.rs index 6a446bde..dff0a65b 100644 --- a/src/file.rs +++ b/src/file.rs @@ -17,6 +17,15 @@ impl File { Self::Image(a) => a.id.clone(), } } + pub fn set_id(&mut self, new_id: &str) { + *(match self { + Self::Ftd(a) => &mut a.id, + Self::Static(a) => &mut a.id, + Self::Markdown(a) => &mut a.id, + Self::Code(a) => &mut a.id, + Self::Image(a) => &mut a.id, + }) = new_id.to_string(); + } pub fn get_base_path(&self) -> String { match self { Self::Ftd(a) => a.parent_path.to_string(), diff --git a/src/version.rs b/src/version.rs index 622f5d8f..18066fa6 100644 --- a/src/version.rs +++ b/src/version.rs @@ -1,18 +1,51 @@ pub(crate) async fn build_version( config: &fpm::Config, - file: Option<&str>, + _file: Option<&str>, base_url: &str, skip_failed: bool, asset_documents: &std::collections::HashMap, ) -> fpm::Result<()> { - // let documents = std::collections::BTreeMap::from_iter( - // config - // .get_files(&config.package) - // .await? - // .into_iter() - // .map(|v| (v.get_id(), v)), - // ); let versioned_documents = config.get_versions(&config.package).await?; - dbg!(&versioned_documents); + let mut index = 1; + let mut documents = std::collections::BTreeMap::new(); + loop { + if let Some(doc) = versioned_documents.get(&index) { + documents.extend(doc.into_iter().map(|v| (v.get_id(), v.to_owned()))); + } else { + break; + } + for doc in documents.values() { + let mut doc = doc.clone(); + let id = doc.get_id(); + doc.set_id(format!("v{}/{}", index, id).as_str()); + fpm::process_file( + config, + &config.package, + &doc, + None, + None, + Default::default(), + base_url, + skip_failed, + asset_documents, + ) + .await?; + } + index += 1; + } + for doc in documents.values() { + fpm::process_file( + config, + &config.package, + doc, + None, + None, + Default::default(), + base_url, + skip_failed, + asset_documents, + ) + .await?; + } Ok(()) } From 719a2c2f14b9ac9341dabe6df71266a557b69a3f Mon Sep 17 00:00:00 2001 From: Arpita-Jaiswal Date: Tue, 19 Apr 2022 21:37:44 +0530 Subject: [PATCH 03/13] Ignore the files not in version folder --- src/config.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/config.rs b/src/config.rs index 30efbd28..40e0e36e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -401,15 +401,11 @@ impl Config { }); } }; - if id.eq("FPM.ftd") { - return Ok(None); - } let v = if let Some((v, _)) = id.split_once('/') { v } else { - return Err(fpm::Error::UsageError { - message: format!("{:?} should be inside version folder", x), - }); + // ignoring the files not present in version folder + return Ok(None); }; let number = if let Some(d) = v.strip_prefix('v') { From fc0022e26e687c2e061bb1be88fce52980738386 Mon Sep 17 00:00:00 2001 From: Arpita-Jaiswal Date: Tue, 19 Apr 2022 22:34:10 +0530 Subject: [PATCH 04/13] Adding all the files in root level to all the versions and ignoring FPM.ftd --- src/commands/build.rs | 27 ++++++++++++++++------ src/config.rs | 52 +++++++++++++++++-------------------------- src/translation.rs | 1 + src/version.rs | 11 ++++++++- 4 files changed, 52 insertions(+), 39 deletions(-) diff --git a/src/commands/build.rs b/src/commands/build.rs index 8a3cc41e..8ff2afe4 100644 --- a/src/commands/build.rs +++ b/src/commands/build.rs @@ -168,6 +168,7 @@ async fn build_with_translations( base_url, skip_failed, asset_documents, + None, ) .await?; } @@ -322,6 +323,7 @@ async fn process_files( base_url, skip_failed, asset_documents, + None, ) .await? } @@ -339,6 +341,7 @@ pub(crate) async fn process_file( base_url: &str, skip_failed: bool, asset_documents: &std::collections::HashMap, + original_id: Option, ) -> fpm::Result<()> { use std::io::Write; @@ -374,7 +377,7 @@ pub(crate) async fn process_file( } } (fpm::File::Static(main_sa), fpm::File::Static(_)) => { - process_static(main_sa, &config.root, package).await? + process_static(main_sa, &config.root, package, original_id).await? } (fpm::File::Code(main_doc), fpm::File::Code(fallback_doc)) => { process_static( @@ -384,6 +387,7 @@ pub(crate) async fn process_file( }, &config.root, package, + original_id, ) .await?; let resp = process_code( @@ -408,7 +412,7 @@ pub(crate) async fn process_file( } } (fpm::File::Image(main_doc), fpm::File::Image(fallback_doc)) => { - process_static(main_doc, &config.root, package).await?; + process_static(main_doc, &config.root, package, original_id).await?; let resp = process_image( config, main_doc, @@ -497,7 +501,7 @@ pub(crate) async fn process_file( } } } - fpm::File::Static(sa) => process_static(sa, &config.root, package).await?, + fpm::File::Static(sa) => process_static(sa, &config.root, package, original_id).await?, fpm::File::Markdown(doc) => { let resp = process_markdown( config, @@ -521,7 +525,7 @@ pub(crate) async fn process_file( } } fpm::File::Image(main_doc) => { - process_static(main_doc, &config.root, package).await?; + process_static(main_doc, &config.root, package, original_id).await?; let resp = process_image( config, main_doc, @@ -552,6 +556,7 @@ pub(crate) async fn process_file( }, &config.root, package, + original_id, ) .await?; let resp = process_code( @@ -1224,10 +1229,11 @@ async fn process_static( sa: &fpm::Static, base_path: &camino::Utf8Path, package: &fpm::Package, + original_id: Option, ) -> fpm::Result<()> { - copy_to_build(sa, base_path, package)?; + copy_to_build(sa, base_path, package, &original_id)?; if let Some(original_package) = package.translation_of.as_ref() { - copy_to_build(sa, base_path, original_package)?; + copy_to_build(sa, base_path, original_package, &original_id)?; } return Ok(()); @@ -1235,17 +1241,24 @@ async fn process_static( sa: &fpm::Static, base_path: &camino::Utf8Path, package: &fpm::Package, + original_id: &Option, ) -> fpm::Result<()> { let build_path = base_path .join(".build") .join("-") .join(package.name.as_str()); + let original_id = if let Some(id) = original_id { + id.as_str() + } else { + sa.id.as_str() + }; + std::fs::create_dir_all(&build_path)?; if let Some((dir, _)) = sa.id.rsplit_once(std::path::MAIN_SEPARATOR) { std::fs::create_dir_all(&build_path.join(dir))?; } std::fs::copy( - sa.base_path.join(sa.id.as_str()), + sa.base_path.join(original_id), build_path.join(sa.id.as_str()), )?; diff --git a/src/config.rs b/src/config.rs index 40e0e36e..75f9f74e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -363,15 +363,15 @@ impl Config { if file.is_dir() { continue; } - let version = if let Some(v) = get_version(&file, &path)? { - v - } else { - continue; - }; + let version = get_version(&file, &path)?; let file = fpm::get_file( package.name.to_string(), &file, - &path.join(format!("v{}", version)), + &(if version.eq(&0) { + path.to_owned() + } else { + path.join(format!("v{}", version)) + }), ) .await?; if let Some(files) = hash.get_mut(&version) { @@ -382,10 +382,7 @@ impl Config { } return Ok(hash); - fn get_version( - x: &camino::Utf8PathBuf, - path: &camino::Utf8PathBuf, - ) -> fpm::Result> { + fn get_version(x: &camino::Utf8PathBuf, path: &camino::Utf8PathBuf) -> fpm::Result { let id = match std::fs::canonicalize(x)?.to_str().unwrap().rsplit_once( if path.as_str().ends_with(std::path::MAIN_SEPARATOR) { path.as_str().to_string() @@ -401,29 +398,22 @@ impl Config { }); } }; - let v = if let Some((v, _)) = id.split_once('/') { - v - } else { - // ignoring the files not present in version folder - return Ok(None); - }; - - let number = if let Some(d) = v.strip_prefix('v') { - d + let number = if let Some(number) = id + .split_once('/') + .map(|(v, _)| v.strip_prefix('v')) + .flatten() + { + number } else { - return Err(fpm::Error::UsageError { - message: format!("{:?} should be inside version folder", x), - }); + // 0 is base version + return Ok(0); }; - number - .parse::() - .map(|v| Some(v)) - .map_err(|_| fpm::Error::UsageError { - message: format!( - "Folder should be structured as `v`, found: `{:?}`", - x - ), - }) + number.parse::().map_err(|_| fpm::Error::UsageError { + message: format!( + "Folder should be structured as `v`, found: `{:?}`", + x + ), + }) } } diff --git a/src/translation.rs b/src/translation.rs index ae8c0340..a8c7f8f4 100644 --- a/src/translation.rs +++ b/src/translation.rs @@ -96,6 +96,7 @@ impl TranslatedDocument { base_url, skip_failed, asset_documents, + None, ) .await?; return Ok(()); diff --git a/src/version.rs b/src/version.rs index 18066fa6..386bba7b 100644 --- a/src/version.rs +++ b/src/version.rs @@ -6,7 +6,7 @@ pub(crate) async fn build_version( asset_documents: &std::collections::HashMap, ) -> fpm::Result<()> { let versioned_documents = config.get_versions(&config.package).await?; - let mut index = 1; + let mut index = 0; let mut documents = std::collections::BTreeMap::new(); loop { if let Some(doc) = versioned_documents.get(&index) { @@ -14,9 +14,16 @@ pub(crate) async fn build_version( } else { break; } + if index.eq(&0) { + index += 1; + continue; + } for doc in documents.values() { let mut doc = doc.clone(); let id = doc.get_id(); + if id.eq("FPM.ftd") { + continue; + } doc.set_id(format!("v{}/{}", index, id).as_str()); fpm::process_file( config, @@ -28,6 +35,7 @@ pub(crate) async fn build_version( base_url, skip_failed, asset_documents, + Some(id), ) .await?; } @@ -44,6 +52,7 @@ pub(crate) async fn build_version( base_url, skip_failed, asset_documents, + None, ) .await?; } From 4fd08aef4e0a819c558067a3a0a2049122355aa7 Mon Sep 17 00:00:00 2001 From: Arpita-Jaiswal Date: Wed, 20 Apr 2022 17:16:13 +0530 Subject: [PATCH 05/13] Added fpm.versions as toc-item --- Cargo.lock | 6 +-- ftd/fpm.ftd | 4 ++ src/library/get_version_data.rs | 94 +++++++++++++++++++++++++++++++++ src/library/mod.rs | 9 +++- src/version.rs | 8 +-- 5 files changed, 111 insertions(+), 10 deletions(-) create mode 100644 src/library/get_version_data.rs diff --git a/Cargo.lock b/Cargo.lock index 29dab5ca..6b37cbd0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2154,7 +2154,7 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" dependencies = [ - "semver 1.0.5", + "semver 1.0.7", ] [[package]] @@ -2252,9 +2252,9 @@ dependencies = [ [[package]] name = "semver" -version = "1.0.5" +version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0486718e92ec9a68fbed73bb5ef687d71103b142595b406835649bebd33f72c7" +checksum = "d65bd28f48be7196d222d95b9243287f48d27aca604e08497513019ff0502cc4" [[package]] name = "semver-parser" diff --git a/ftd/fpm.ftd b/ftd/fpm.ftd index ed740bd6..0f905635 100644 --- a/ftd/fpm.ftd +++ b/ftd/fpm.ftd @@ -158,6 +158,10 @@ toc-item list children: +-- toc-item list versions: + + + -- toc-item list language-toc: diff --git a/src/library/get_version_data.rs b/src/library/get_version_data.rs new file mode 100644 index 00000000..5b3f88a2 --- /dev/null +++ b/src/library/get_version_data.rs @@ -0,0 +1,94 @@ +pub fn processor( + section: &ftd::p1::Section, + doc: &ftd::p2::TDoc, + config: &fpm::Config, + document_id: &str, + base_url: &str, +) -> ftd::p1::Result { + let versions = + futures::executor::block_on(config.get_versions(&config.package)).map_err(|e| { + ftd::p1::Error::ParseError { + message: format!("Cant find versions: {:?}", e), + doc_id: doc.name.to_string(), + line_number: section.line_number, + } + })?; + + let version = if let Some(number) = document_id + .split_once('/') + .map(|(v, _)| v.strip_prefix('v')) + .flatten() + { + number + .parse::() + .map_err(|_| ftd::p1::Error::ParseError { + message: format!("Incorrect version `{:?}` in `{:?}`", number, document_id), + doc_id: doc.name.to_string(), + line_number: section.line_number, + })? + } else { + // 0 is base version + 0 + }; + + let doc_id = if let Some(doc) = document_id.split_once('/').map(|(_, v)| v) { + doc + } else { + document_id + } + .to_string(); + + let base_url = base_url + .trim_end_matches('/') + .trim_start_matches('/') + .to_string(); + let base_url = if !base_url.is_empty() { + format!("/{base_url}/") + } else { + String::from("/") + }; + + let url = match doc_id.as_str().rsplit_once('.') { + Some(("index", "ftd")) => { + // Index.ftd found. Return index.html + format!("{base_url}index.html") + } + Some((file_path, "ftd")) | Some((file_path, "md")) => { + format!("{base_url}{file_path}/index.html") + } + Some(_) | None => { + // Unknown file found, create URL + format!( + "{base_url}{file_path}/index.html", + file_path = doc_id.as_str() + ) + } + }; + + let mut version_toc = "".to_string(); + for (k, v) in versions { + if [version, 0].contains(&k) { + continue; + } + if v.iter() + .map(|v| v.get_id()) + .collect::>() + .contains(&doc_id) + { + version_toc = format!("{}- v{}: v{}{}", version_toc, k, k, url); + } + } + + let toc_items = fpm::library::toc::ToC::parse(version_toc.as_str(), doc.name) + .map_err(|e| ftd::p1::Error::ParseError { + message: format!("Cannot parse body: {:?}", e), + doc_id: doc.name.to_string(), + line_number: section.line_number, + })? + .items + .iter() + .map(|item| item.to_toc_item_compat()) + .collect::>(); + + doc.from_json(&toc_items, section) +} diff --git a/src/library/mod.rs b/src/library/mod.rs index bfa25a40..cde56e58 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -1,5 +1,6 @@ mod fpm_dot_ftd; mod get_data; +mod get_version_data; mod http; mod include; mod sqlite; @@ -14,7 +15,6 @@ pub struct Library { /// Hashmap that contains the information about the assets document for the current build /// It'll contain a map of corresponding to the asset doc for that package pub asset_documents: std::collections::HashMap, - pub base_url: String, } @@ -211,6 +211,13 @@ impl ftd::p2::Library for Library { "toc" => fpm::library::toc::processor(section, doc, &self.config), "include" => fpm::library::include::processor(section, doc, &self.config), "get-data" => fpm::library::get_data::processor(section, doc, &self.config), + "get-version-data" => fpm::library::get_version_data::processor( + section, + doc, + &self.config, + self.document_id.as_str(), + self.base_url.as_str(), + ), t => unimplemented!("$processor$: {} is not implemented yet", t), } } diff --git a/src/version.rs b/src/version.rs index 386bba7b..136bd199 100644 --- a/src/version.rs +++ b/src/version.rs @@ -8,12 +8,8 @@ pub(crate) async fn build_version( let versioned_documents = config.get_versions(&config.package).await?; let mut index = 0; let mut documents = std::collections::BTreeMap::new(); - loop { - if let Some(doc) = versioned_documents.get(&index) { - documents.extend(doc.into_iter().map(|v| (v.get_id(), v.to_owned()))); - } else { - break; - } + while let Some(doc) = versioned_documents.get(&index) { + documents.extend(doc.iter().map(|v| (v.get_id(), v.to_owned()))); if index.eq(&0) { index += 1; continue; From 566bf3f3a535c5e85bfe52b5ad02258b91e08d1f Mon Sep 17 00:00:00 2001 From: Arpita-Jaiswal Date: Wed, 20 Apr 2022 23:25:03 +0530 Subject: [PATCH 06/13] Clippy fixes --- src/library/get_version_data.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/library/get_version_data.rs b/src/library/get_version_data.rs index 5b3f88a2..8fa77389 100644 --- a/src/library/get_version_data.rs +++ b/src/library/get_version_data.rs @@ -70,11 +70,7 @@ pub fn processor( if [version, 0].contains(&k) { continue; } - if v.iter() - .map(|v| v.get_id()) - .collect::>() - .contains(&doc_id) - { + if v.iter().map(|v| v.get_id()).any(|x| x == doc_id) { version_toc = format!("{}- v{}: v{}{}", version_toc, k, k, url); } } From 2ca775addc5a75e8667fc2001aa8306301e6a6ba Mon Sep 17 00:00:00 2001 From: Arpita-Jaiswal Date: Wed, 20 Apr 2022 23:45:33 +0530 Subject: [PATCH 07/13] all succeeding versions contains file from specfic version --- src/library/get_version_data.rs | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/library/get_version_data.rs b/src/library/get_version_data.rs index 8fa77389..44d60108 100644 --- a/src/library/get_version_data.rs +++ b/src/library/get_version_data.rs @@ -14,7 +14,7 @@ pub fn processor( } })?; - let version = if let Some(number) = document_id + /*let version = if let Some(number) = document_id .split_once('/') .map(|(v, _)| v.strip_prefix('v')) .flatten() @@ -29,7 +29,7 @@ pub fn processor( } else { // 0 is base version 0 - }; + };*/ let doc_id = if let Some(doc) = document_id.split_once('/').map(|(_, v)| v) { doc @@ -51,28 +51,33 @@ pub fn processor( let url = match doc_id.as_str().rsplit_once('.') { Some(("index", "ftd")) => { // Index.ftd found. Return index.html - format!("{base_url}index.html") + format!("{base_url}") } Some((file_path, "ftd")) | Some((file_path, "md")) => { - format!("{base_url}{file_path}/index.html") + format!("{base_url}{file_path}/") } Some(_) | None => { // Unknown file found, create URL - format!( - "{base_url}{file_path}/index.html", - file_path = doc_id.as_str() - ) + format!("{base_url}{file_path}/", file_path = doc_id.as_str()) } }; let mut version_toc = "".to_string(); - for (k, v) in versions { - if [version, 0].contains(&k) { - continue; - } + let mut index = 0; + while let Some(v) = versions.get(&index) { if v.iter().map(|v| v.get_id()).any(|x| x == doc_id) { - version_toc = format!("{}- v{}: v{}{}", version_toc, k, k, url); + break; + } + index += 1; + } + + while versions.contains_key(&index) { + if index.eq(&0) { + index += 1; + continue; } + version_toc = format!("{}- v{}: v{}{}\n", version_toc, index, index, url); + index += 1; } let toc_items = fpm::library::toc::ToC::parse(version_toc.as_str(), doc.name) From cef9746aba831a93107d10510c87116e3309f479 Mon Sep 17 00:00:00 2001 From: Arpita-Jaiswal Date: Thu, 21 Apr 2022 00:01:14 +0530 Subject: [PATCH 08/13] set is-heading for current version --- src/library/get_version_data.rs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/library/get_version_data.rs b/src/library/get_version_data.rs index 44d60108..eae4d556 100644 --- a/src/library/get_version_data.rs +++ b/src/library/get_version_data.rs @@ -14,7 +14,7 @@ pub fn processor( } })?; - /*let version = if let Some(number) = document_id + let version = if let Some(number) = document_id .split_once('/') .map(|(v, _)| v.strip_prefix('v')) .flatten() @@ -29,7 +29,7 @@ pub fn processor( } else { // 0 is base version 0 - };*/ + }; let doc_id = if let Some(doc) = document_id.split_once('/').map(|(_, v)| v) { doc @@ -62,7 +62,6 @@ pub fn processor( } }; - let mut version_toc = "".to_string(); let mut index = 0; while let Some(v) = versions.get(&index) { if v.iter().map(|v| v.get_id()).any(|x| x == doc_id) { @@ -71,22 +70,27 @@ pub fn processor( index += 1; } + let mut version_toc = vec![]; while versions.contains_key(&index) { if index.eq(&0) { index += 1; continue; } - version_toc = format!("{}- v{}: v{}{}\n", version_toc, index, index, url); + version_toc.push(fpm::library::toc::TocItem { + id: None, + title: Some(format!("v{}", index)), + url: Some(format!("v{}{}", index, url)), + number: vec![], + is_heading: if version.eq(&index) { true } else { false }, + is_disabled: false, + img_src: None, + font_icon: None, + children: vec![], + }); index += 1; } - let toc_items = fpm::library::toc::ToC::parse(version_toc.as_str(), doc.name) - .map_err(|e| ftd::p1::Error::ParseError { - message: format!("Cannot parse body: {:?}", e), - doc_id: doc.name.to_string(), - line_number: section.line_number, - })? - .items + let toc_items = version_toc .iter() .map(|item| item.to_toc_item_compat()) .collect::>(); From 056ee22b7e51f56d548cea3961dea810cee5ed6b Mon Sep 17 00:00:00 2001 From: Arpita-Jaiswal Date: Thu, 21 Apr 2022 11:55:23 +0530 Subject: [PATCH 09/13] Using semantic versioning --- Cargo.lock | 1 + Cargo.toml | 1 + src/config.rs | 62 +++++++++++++++------------------ src/library/get_version_data.rs | 53 ++++++++++++++-------------- src/library/mod.rs | 8 +---- src/version.rs | 37 ++++++++++++++++++-- 6 files changed, 92 insertions(+), 70 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6b37cbd0..3f0aec7c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -800,6 +800,7 @@ dependencies = [ "reqwest", "rusqlite", "rusty-hook", + "semver 1.0.7", "serde", "serde_derive", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index d7cd9b5e..0e8b43d9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,6 +44,7 @@ intl-memoizer = "0.5.1" sha2 = "0.10" itertools = "0.10.3" mime_guess = "2" +semver = "1.0.7" [dev-dependencies] fbt-lib = "0.1.17" diff --git a/src/config.rs b/src/config.rs index 75f9f74e..40e388cc 100644 --- a/src/config.rs +++ b/src/config.rs @@ -337,19 +337,12 @@ impl Config { pub(crate) async fn get_versions( &self, package: &fpm::Package, - ) -> fpm::Result>> { - let path = if let Some(package_fpm_path) = &package.fpm_path { - // TODO: Unwrap? - package_fpm_path.parent().unwrap().to_owned() - } else if package.name.eq(&self.package.name) { - self.root.clone() - } else { - self.packages_root.clone().join(package.name.as_str()) - }; + ) -> fpm::Result)>> { + let path = self.get_root_for_package(package); let mut ignore_paths = ignore::WalkBuilder::new(&path); ignore_paths.overrides(fpm::file::package_ignores(package, &path)?); - let mut hash: std::collections::HashMap> = + let mut hash: std::collections::HashMap)> = std::collections::HashMap::new(); let all_files = ignore_paths @@ -363,26 +356,29 @@ impl Config { if file.is_dir() { continue; } - let version = get_version(&file, &path)?; + let (version_str, version) = get_version(&file, &path)?; let file = fpm::get_file( package.name.to_string(), &file, - &(if version.eq(&0) { + &(if version_str.eq("BASE_VERSION") { path.to_owned() } else { - path.join(format!("v{}", version)) + path.join(&version_str) }), ) .await?; - if let Some(files) = hash.get_mut(&version) { + if let Some((_, files)) = hash.get_mut(&version) { files.push(file) } else { - hash.insert(version, vec![file]); + hash.insert(version, (version_str, vec![file])); } } return Ok(hash); - fn get_version(x: &camino::Utf8PathBuf, path: &camino::Utf8PathBuf) -> fpm::Result { + fn get_version( + x: &camino::Utf8PathBuf, + path: &camino::Utf8PathBuf, + ) -> fpm::Result<(String, semver::Version)> { let id = match std::fs::canonicalize(x)?.to_str().unwrap().rsplit_once( if path.as_str().ends_with(std::path::MAIN_SEPARATOR) { path.as_str().to_string() @@ -398,34 +394,34 @@ impl Config { }); } }; - let number = if let Some(number) = id - .split_once('/') - .map(|(v, _)| v.strip_prefix('v')) - .flatten() - { - number + Ok(if let Some((v, _)) = id.split_once('/') { + ( + v.to_string(), + semver::Version::parse(v.strip_prefix('v').unwrap_or_else(|| v)).map_err( + |e| fpm::Error::UsageError { + message: format!("Invalid version number: `{}` Error:`{:?}`", v, e), + }, + )?, + ) } else { - // 0 is base version - return Ok(0); - }; - number.parse::().map_err(|_| fpm::Error::UsageError { - message: format!( - "Folder should be structured as `v`, found: `{:?}`", - x - ), + ("BASE_VERSION".to_string(), semver::Version::new(0, 0, 0)) }) } } - pub(crate) async fn get_files(&self, package: &fpm::Package) -> fpm::Result> { - let path = if let Some(package_fpm_path) = &package.fpm_path { + pub(crate) fn get_root_for_package(&self, package: &fpm::Package) -> camino::Utf8PathBuf { + if let Some(package_fpm_path) = &package.fpm_path { // TODO: Unwrap? package_fpm_path.parent().unwrap().to_owned() } else if package.name.eq(&self.package.name) { self.root.clone() } else { self.packages_root.clone().join(package.name.as_str()) - }; + } + } + + pub(crate) async fn get_files(&self, package: &fpm::Package) -> fpm::Result> { + let path = self.get_root_for_package(package); let mut ignore_paths = ignore::WalkBuilder::new(&path); // ignore_paths.hidden(false); // Allow the linux hidden files to be evaluated ignore_paths.overrides(fpm::file::package_ignores(package, &path)?); diff --git a/src/library/get_version_data.rs b/src/library/get_version_data.rs index eae4d556..be3b5691 100644 --- a/src/library/get_version_data.rs +++ b/src/library/get_version_data.rs @@ -1,3 +1,5 @@ +use itertools::Itertools; + pub fn processor( section: &ftd::p1::Section, doc: &ftd::p2::TDoc, @@ -14,21 +16,15 @@ pub fn processor( } })?; - let version = if let Some(number) = document_id - .split_once('/') - .map(|(v, _)| v.strip_prefix('v')) - .flatten() - { - number - .parse::() - .map_err(|_| ftd::p1::Error::ParseError { - message: format!("Incorrect version `{:?}` in `{:?}`", number, document_id), - doc_id: doc.name.to_string(), - line_number: section.line_number, - })? + let version = if let Some((v, _)) = document_id.split_once('/') { + let v = v.strip_prefix('v').unwrap_or_else(|| v); + semver::Version::parse(v).map_err(|e| ftd::p1::Error::ParseError { + message: format!("Invalid version number: `{}` Error:`{:?}`", v, e), + doc_id: doc.name.to_string(), + line_number: section.line_number, + })? } else { - // 0 is base version - 0 + semver::Version::new(0, 0, 0) }; let doc_id = if let Some(doc) = document_id.split_once('/').map(|(_, v)| v) { @@ -61,33 +57,36 @@ pub fn processor( format!("{base_url}{file_path}/", file_path = doc_id.as_str()) } }; - - let mut index = 0; - while let Some(v) = versions.get(&index) { - if v.iter().map(|v| v.get_id()).any(|x| x == doc_id) { - break; + let mut found = false; + if let Some((_, doc)) = versions.get(&semver::Version::new(0, 0, 0)) { + if doc.iter().map(|v| v.get_id()).any(|x| x == doc_id) { + found = true; } - index += 1; } let mut version_toc = vec![]; - while versions.contains_key(&index) { - if index.eq(&0) { - index += 1; + for key in versions.keys().sorted() { + if key.eq(&semver::Version::new(0, 0, 0)) { continue; } + let (version_str, doc) = versions[key].to_owned(); + if !found { + if !doc.iter().map(|v| v.get_id()).any(|x| x == doc_id) { + continue; + } + found = true; + } version_toc.push(fpm::library::toc::TocItem { id: None, - title: Some(format!("v{}", index)), - url: Some(format!("v{}{}", index, url)), + title: Some(format!("{}", version_str)), + url: Some(format!("{}{}", version_str, url)), number: vec![], - is_heading: if version.eq(&index) { true } else { false }, + is_heading: if version.eq(&key) { true } else { false }, is_disabled: false, img_src: None, font_icon: None, children: vec![], }); - index += 1; } let toc_items = version_toc diff --git a/src/library/mod.rs b/src/library/mod.rs index cde56e58..653f426c 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -170,13 +170,7 @@ impl ftd::p2::Library for Library { package: &fpm::Package, lib: &Library, ) -> Option { - let path = if let Some(package_fpm_path) = &package.fpm_path { - package_fpm_path.parent()?.to_owned() - } else if package.name.eq(&lib.config.package.name) { - lib.config.root.clone() - } else { - lib.config.packages_root.clone().join(package.name.as_str()) - }; + let path = lib.config.get_root_for_package(package); // Explicit check for the current package. if name.starts_with(&package.name.as_str()) { let new_name = name.replacen(&package.name.as_str(), "", 1); diff --git a/src/version.rs b/src/version.rs index 136bd199..b9c94c85 100644 --- a/src/version.rs +++ b/src/version.rs @@ -1,3 +1,5 @@ +use itertools::Itertools; + pub(crate) async fn build_version( config: &fpm::Config, _file: Option<&str>, @@ -6,9 +8,37 @@ pub(crate) async fn build_version( asset_documents: &std::collections::HashMap, ) -> fpm::Result<()> { let versioned_documents = config.get_versions(&config.package).await?; - let mut index = 0; let mut documents = std::collections::BTreeMap::new(); - while let Some(doc) = versioned_documents.get(&index) { + for key in versioned_documents.keys().sorted() { + let (version_str, doc) = versioned_documents[key].to_owned(); + documents.extend(doc.iter().map(|v| (v.get_id(), v.to_owned()))); + if key.eq(&semver::Version::new(0, 0, 0)) { + continue; + } + for doc in documents.values() { + let mut doc = doc.clone(); + let id = doc.get_id(); + if id.eq("FPM.ftd") { + continue; + } + doc.set_id(format!("{}/{}", version_str, id).as_str()); + fpm::process_file( + config, + &config.package, + &doc, + None, + None, + Default::default(), + base_url, + skip_failed, + asset_documents, + Some(id), + ) + .await?; + } + } + + /*while let Some(doc) = versioned_documents.get(&index) { documents.extend(doc.iter().map(|v| (v.get_id(), v.to_owned()))); if index.eq(&0) { index += 1; @@ -36,7 +66,8 @@ pub(crate) async fn build_version( .await?; } index += 1; - } + }*/ + for doc in documents.values() { fpm::process_file( config, From 6b69a6a7e7f0df613bc6eb1c646877ff1579372e Mon Sep 17 00:00:00 2001 From: Arpita-Jaiswal Date: Thu, 21 Apr 2022 13:18:36 +0530 Subject: [PATCH 10/13] Implemented versioning --- src/config.rs | 31 ++++------- src/lib.rs | 1 + src/library/get_version_data.rs | 17 +++--- src/version.rs | 99 ++++++++++++++++++++++----------- 4 files changed, 87 insertions(+), 61 deletions(-) diff --git a/src/config.rs b/src/config.rs index 40e388cc..0be23692 100644 --- a/src/config.rs +++ b/src/config.rs @@ -337,12 +337,12 @@ impl Config { pub(crate) async fn get_versions( &self, package: &fpm::Package, - ) -> fpm::Result)>> { + ) -> fpm::Result>> { let path = self.get_root_for_package(package); let mut ignore_paths = ignore::WalkBuilder::new(&path); ignore_paths.overrides(fpm::file::package_ignores(package, &path)?); - let mut hash: std::collections::HashMap)> = + let mut hash: std::collections::HashMap> = std::collections::HashMap::new(); let all_files = ignore_paths @@ -356,21 +356,21 @@ impl Config { if file.is_dir() { continue; } - let (version_str, version) = get_version(&file, &path)?; + let version = get_version(&file, &path)?; let file = fpm::get_file( package.name.to_string(), &file, - &(if version_str.eq("BASE_VERSION") { + &(if version.original.eq("BASE_VERSION") { path.to_owned() } else { - path.join(&version_str) + path.join(&version.original) }), ) .await?; - if let Some((_, files)) = hash.get_mut(&version) { + if let Some(files) = hash.get_mut(&version) { files.push(file) } else { - hash.insert(version, (version_str, vec![file])); + hash.insert(version, vec![file]); } } return Ok(hash); @@ -378,7 +378,7 @@ impl Config { fn get_version( x: &camino::Utf8PathBuf, path: &camino::Utf8PathBuf, - ) -> fpm::Result<(String, semver::Version)> { + ) -> fpm::Result { let id = match std::fs::canonicalize(x)?.to_str().unwrap().rsplit_once( if path.as_str().ends_with(std::path::MAIN_SEPARATOR) { path.as_str().to_string() @@ -394,18 +394,11 @@ impl Config { }); } }; - Ok(if let Some((v, _)) = id.split_once('/') { - ( - v.to_string(), - semver::Version::parse(v.strip_prefix('v').unwrap_or_else(|| v)).map_err( - |e| fpm::Error::UsageError { - message: format!("Invalid version number: `{}` Error:`{:?}`", v, e), - }, - )?, - ) + if let Some((v, _)) = id.split_once('/') { + fpm::Version::parse(v) } else { - ("BASE_VERSION".to_string(), semver::Version::new(0, 0, 0)) - }) + Ok(fpm::Version::base()) + } } } diff --git a/src/lib.rs b/src/lib.rs index f6cead8d..c5f4e3d9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,6 +39,7 @@ pub(crate) use snapshot::Snapshot; pub(crate) use tracker::Track; pub(crate) use translation::{TranslatedDocument, TranslationData}; pub(crate) use utils::{copy_dir_all, get_timestamp_nanosecond}; +pub(crate) use version::Version; pub const PACKAGE_INFO_INTERFACE: &str = "fifthtry.github.io/package-info"; pub const PACKAGE_THEME_INTERFACE: &str = "fifthtry.github.io/theme"; diff --git a/src/library/get_version_data.rs b/src/library/get_version_data.rs index be3b5691..ecc950dd 100644 --- a/src/library/get_version_data.rs +++ b/src/library/get_version_data.rs @@ -17,14 +17,13 @@ pub fn processor( })?; let version = if let Some((v, _)) = document_id.split_once('/') { - let v = v.strip_prefix('v').unwrap_or_else(|| v); - semver::Version::parse(v).map_err(|e| ftd::p1::Error::ParseError { - message: format!("Invalid version number: `{}` Error:`{:?}`", v, e), + fpm::Version::parse(v).map_err(|e| ftd::p1::Error::ParseError { + message: format!("{:?}", e), doc_id: doc.name.to_string(), line_number: section.line_number, })? } else { - semver::Version::new(0, 0, 0) + fpm::Version::base() }; let doc_id = if let Some(doc) = document_id.split_once('/').map(|(_, v)| v) { @@ -58,7 +57,7 @@ pub fn processor( } }; let mut found = false; - if let Some((_, doc)) = versions.get(&semver::Version::new(0, 0, 0)) { + if let Some(doc) = versions.get(&fpm::Version::base()) { if doc.iter().map(|v| v.get_id()).any(|x| x == doc_id) { found = true; } @@ -66,10 +65,10 @@ pub fn processor( let mut version_toc = vec![]; for key in versions.keys().sorted() { - if key.eq(&semver::Version::new(0, 0, 0)) { + if key.eq(&fpm::Version::base()) { continue; } - let (version_str, doc) = versions[key].to_owned(); + let doc = versions[key].to_owned(); if !found { if !doc.iter().map(|v| v.get_id()).any(|x| x == doc_id) { continue; @@ -78,8 +77,8 @@ pub fn processor( } version_toc.push(fpm::library::toc::TocItem { id: None, - title: Some(format!("{}", version_str)), - url: Some(format!("{}{}", version_str, url)), + title: Some(format!("{}", key.original)), + url: Some(format!("{}{}", key.original, url)), number: vec![], is_heading: if version.eq(&key) { true } else { false }, is_disabled: false, diff --git a/src/version.rs b/src/version.rs index b9c94c85..4f2bce6d 100644 --- a/src/version.rs +++ b/src/version.rs @@ -1,4 +1,67 @@ use itertools::Itertools; +use std::cmp::Ordering; + +#[derive(Clone, Eq, PartialEq, Hash, Debug)] +pub struct Version { + pub major: u64, + pub minor: Option, + pub original: String, +} + +impl Version { + pub(crate) fn base() -> fpm::Version { + fpm::Version { + major: 0, + minor: None, + original: "BASE_VERSION".to_string(), + } + } + + pub(crate) fn parse(s: &str) -> fpm::Result { + let v = s.strip_prefix(&['v', 'V']).unwrap_or_else(|| s); + let mut minor = None; + let major = if let Some((major, minor_)) = v.split_once('.') { + if minor_.contains('.') { + return Err(fpm::Error::UsageError { + message: format!("Cannot have more than one dots `.`, found: `{}`", s), + }); + } + let minor_ = minor_.parse::().map_err(|e| fpm::Error::UsageError { + message: format!("Invalid minor for `{}`: `{:?}`", s, e), + })?; + minor = Some(minor_); + major.parse::().map_err(|e| fpm::Error::UsageError { + message: format!("Invalid major for `{}`: `{:?}`", s, e), + })? + } else { + v.parse::().map_err(|e| fpm::Error::UsageError { + message: format!("Invalid major for `{}`: `{:?}`", s, e), + })? + }; + Ok(fpm::Version { + major, + minor, + original: s.to_string(), + }) + } +} + +impl PartialOrd for Version { + fn partial_cmp(&self, rhs: &Self) -> Option { + Some(Ord::cmp(self, rhs)) + } +} + +impl Ord for Version { + fn cmp(&self, rhs: &Self) -> std::cmp::Ordering { + if self.major.eq(&rhs.major) { + let lhs_minor = self.minor.unwrap_or(0); + let rhs_minor = rhs.minor.unwrap_or(0); + return lhs_minor.cmp(&rhs_minor); + } + self.major.cmp(&rhs.major) + } +} pub(crate) async fn build_version( config: &fpm::Config, @@ -10,9 +73,9 @@ pub(crate) async fn build_version( let versioned_documents = config.get_versions(&config.package).await?; let mut documents = std::collections::BTreeMap::new(); for key in versioned_documents.keys().sorted() { - let (version_str, doc) = versioned_documents[key].to_owned(); + let doc = versioned_documents[key].to_owned(); documents.extend(doc.iter().map(|v| (v.get_id(), v.to_owned()))); - if key.eq(&semver::Version::new(0, 0, 0)) { + if key.eq(&fpm::Version::base()) { continue; } for doc in documents.values() { @@ -21,7 +84,7 @@ pub(crate) async fn build_version( if id.eq("FPM.ftd") { continue; } - doc.set_id(format!("{}/{}", version_str, id).as_str()); + doc.set_id(format!("{}/{}", key.original, id).as_str()); fpm::process_file( config, &config.package, @@ -38,36 +101,6 @@ pub(crate) async fn build_version( } } - /*while let Some(doc) = versioned_documents.get(&index) { - documents.extend(doc.iter().map(|v| (v.get_id(), v.to_owned()))); - if index.eq(&0) { - index += 1; - continue; - } - for doc in documents.values() { - let mut doc = doc.clone(); - let id = doc.get_id(); - if id.eq("FPM.ftd") { - continue; - } - doc.set_id(format!("v{}/{}", index, id).as_str()); - fpm::process_file( - config, - &config.package, - &doc, - None, - None, - Default::default(), - base_url, - skip_failed, - asset_documents, - Some(id), - ) - .await?; - } - index += 1; - }*/ - for doc in documents.values() { fpm::process_file( config, From 8904202c7e2d76ff088044df29a0ecd69357a544 Mon Sep 17 00:00:00 2001 From: Arpita-Jaiswal Date: Thu, 21 Apr 2022 13:25:28 +0530 Subject: [PATCH 11/13] Clippy fix --- src/library/get_version_data.rs | 2 +- src/version.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/library/get_version_data.rs b/src/library/get_version_data.rs index ecc950dd..feb4c27e 100644 --- a/src/library/get_version_data.rs +++ b/src/library/get_version_data.rs @@ -80,7 +80,7 @@ pub fn processor( title: Some(format!("{}", key.original)), url: Some(format!("{}{}", key.original, url)), number: vec![], - is_heading: if version.eq(&key) { true } else { false }, + is_heading: if version.eq(key) { true } else { false }, is_disabled: false, img_src: None, font_icon: None, diff --git a/src/version.rs b/src/version.rs index 4f2bce6d..ff4eee32 100644 --- a/src/version.rs +++ b/src/version.rs @@ -18,7 +18,7 @@ impl Version { } pub(crate) fn parse(s: &str) -> fpm::Result { - let v = s.strip_prefix(&['v', 'V']).unwrap_or_else(|| s); + let v = s.strip_prefix(&['v', 'V']).unwrap_or(s); let mut minor = None; let major = if let Some((major, minor_)) = v.split_once('.') { if minor_.contains('.') { From 75d6f7f0ed396a71b09fd3a04d53d702d780771f Mon Sep 17 00:00:00 2001 From: Arpita-Jaiswal Date: Thu, 21 Apr 2022 15:03:25 +0530 Subject: [PATCH 12/13] Changing base_url --- src/version.rs | 50 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/src/version.rs b/src/version.rs index ff4eee32..56c8137a 100644 --- a/src/version.rs +++ b/src/version.rs @@ -1,5 +1,6 @@ use itertools::Itertools; use std::cmp::Ordering; +use std::io::Write; #[derive(Clone, Eq, PartialEq, Hash, Debug)] pub struct Version { @@ -74,17 +75,56 @@ pub(crate) async fn build_version( let mut documents = std::collections::BTreeMap::new(); for key in versioned_documents.keys().sorted() { let doc = versioned_documents[key].to_owned(); - documents.extend(doc.iter().map(|v| (v.get_id(), v.to_owned()))); + documents.extend( + doc.iter() + .map(|v| (v.get_id(), (key.original.to_string(), v.to_owned()))), + ); if key.eq(&fpm::Version::base()) { continue; } - for doc in documents.values() { + for (version, doc) in documents.values() { let mut doc = doc.clone(); let id = doc.get_id(); if id.eq("FPM.ftd") { continue; } - doc.set_id(format!("{}/{}", key.original, id).as_str()); + let new_id = format!("{}/{}", key.original, id); + if !key.original.eq(version) && !fpm::Version::base().original.eq(version) { + if let fpm::File::Ftd(_) = doc { + let original_id = format!("{}/{}", version, id); + let original_file_rel_path = if original_id.contains("index.ftd") { + original_id.replace("index.ftd", "index.html") + } else { + original_id.replace( + ".ftd", + format!("{}index.html", std::path::MAIN_SEPARATOR).as_str(), + ) + }; + let original_file_path = + config.root.join(".build").join(original_file_rel_path); + let file_rel_path = if new_id.contains("index.ftd") { + new_id.replace("index.ftd", "index.html") + } else { + new_id.replace( + ".ftd", + format!("{}index.html", std::path::MAIN_SEPARATOR).as_str(), + ) + }; + let new_file_path = config.root.join(".build").join(file_rel_path); + let original_content = std::fs::read_to_string(&original_file_path)?; + std::fs::create_dir_all(&new_file_path.as_str().replace("index.html", ""))?; + let mut f = std::fs::File::create(&new_file_path)?; + let from_pattern = format!("", base_url, version); + let to_pattern = format!("", base_url, key.original); + f.write_all( + original_content + .replace(from_pattern.as_str(), to_pattern.as_str()) + .as_bytes(), + )?; + continue; + } + } + doc.set_id(new_id.as_str()); fpm::process_file( config, &config.package, @@ -92,7 +132,7 @@ pub(crate) async fn build_version( None, None, Default::default(), - base_url, + format!("{}{}/", base_url, key.original).as_str(), skip_failed, asset_documents, Some(id), @@ -101,7 +141,7 @@ pub(crate) async fn build_version( } } - for doc in documents.values() { + for (_, doc) in documents.values() { fpm::process_file( config, &config.package, From eb6092f097447af6e67c1b098caa546b49d9da1d Mon Sep 17 00:00:00 2001 From: Arpita-Jaiswal Date: Thu, 21 Apr 2022 15:08:49 +0530 Subject: [PATCH 13/13] Clippy fixes --- src/library/get_version_data.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/library/get_version_data.rs b/src/library/get_version_data.rs index feb4c27e..342ffd17 100644 --- a/src/library/get_version_data.rs +++ b/src/library/get_version_data.rs @@ -44,10 +44,7 @@ pub fn processor( }; let url = match doc_id.as_str().rsplit_once('.') { - Some(("index", "ftd")) => { - // Index.ftd found. Return index.html - format!("{base_url}") - } + Some(("index", "ftd")) => base_url, Some((file_path, "ftd")) | Some((file_path, "md")) => { format!("{base_url}{file_path}/") } @@ -77,10 +74,10 @@ pub fn processor( } version_toc.push(fpm::library::toc::TocItem { id: None, - title: Some(format!("{}", key.original)), + title: Some(key.original.to_string()), url: Some(format!("{}{}", key.original, url)), number: vec![], - is_heading: if version.eq(key) { true } else { false }, + is_heading: version.eq(key), is_disabled: false, img_src: None, font_icon: None,