diff options
Diffstat (limited to 'crates/shirabe/src/package/version')
6 files changed, 82 insertions, 58 deletions
diff --git a/crates/shirabe/src/package/version/mod.rs b/crates/shirabe/src/package/version/mod.rs new file mode 100644 index 0000000..a734e23 --- /dev/null +++ b/crates/shirabe/src/package/version/mod.rs @@ -0,0 +1,5 @@ +pub mod stability_filter; +pub mod version_bumper; +pub mod version_guesser; +pub mod version_parser; +pub mod version_selector; diff --git a/crates/shirabe/src/package/version/stability_filter.rs b/crates/shirabe/src/package/version/stability_filter.rs index 65f024f..d08492c 100644 --- a/crates/shirabe/src/package/version/stability_filter.rs +++ b/crates/shirabe/src/package/version/stability_filter.rs @@ -1,7 +1,7 @@ //! ref: composer/src/Composer/Package/Version/StabilityFilter.php -use indexmap::IndexMap; use crate::package::base_package::BasePackage; +use indexmap::IndexMap; pub struct StabilityFilter; diff --git a/crates/shirabe/src/package/version/version_bumper.rs b/crates/shirabe/src/package/version/version_bumper.rs index 8717d19..5911458 100644 --- a/crates/shirabe/src/package/version/version_bumper.rs +++ b/crates/shirabe/src/package/version/version_bumper.rs @@ -1,15 +1,15 @@ //! ref: composer/src/Composer/Package/Version/VersionBumper.php -use anyhow::Result; -use indexmap::IndexMap; -use shirabe_external_packages::composer::pcre::preg::Preg; -use shirabe_semver::constraint::constraint_interface::ConstraintInterface; -use shirabe_semver::intervals::Intervals; use crate::package::dumper::array_dumper::ArrayDumper; use crate::package::loader::array_loader::ArrayLoader; use crate::package::package_interface::PackageInterface; use crate::package::version::version_parser::VersionParser; use crate::util::platform::Platform; +use anyhow::Result; +use indexmap::IndexMap; +use shirabe_external_packages::composer::pcre::preg::Preg; +use shirabe_semver::constraint::constraint_interface::ConstraintInterface; +use shirabe_semver::intervals::Intervals; #[derive(Debug)] pub struct VersionBumper; @@ -46,7 +46,8 @@ impl VersionBumper { } let major = Preg::replace(r"{^([1-9][0-9]*|0\.\d+).*}", "$1", version.clone())?; - let version_without_suffix = Preg::replace(r"{(?:\.(?:0|9999999))+(-dev)?$}", "", version.clone())?; + let version_without_suffix = + Preg::replace(r"{(?:\.(?:0|9999999))+(-dev)?$}", "", version.clone())?; let new_pretty_constraint = format!("^{}", version_without_suffix); if !Preg::is_match(r"{^\^\d+(\.\d+)*$}", &new_pretty_constraint)? { @@ -82,23 +83,27 @@ impl VersionBumper { } else { "" }; - let replacement = if match_str.starts_with('~') && match_str.matches('.').count() != 1 { - let mut version_bits: Vec<String> = - version_without_suffix.split('.').map(String::from).collect(); - let needed_len = match_str.matches('.').count() + 1; - while version_bits.len() < needed_len { - version_bits.push("0".to_string()); - } - let dots_in_match = match_str.matches('.').count(); - format!("~{}", version_bits[..dots_in_match + 1].join(".")) - } else if match_str == "*" || match_str.starts_with(">=") { - format!(">={}{}", version_without_suffix, suffix) - } else { - format!("{}{}", new_pretty_constraint, suffix) - }; + let replacement = + if match_str.starts_with('~') && match_str.matches('.').count() != 1 { + let mut version_bits: Vec<String> = version_without_suffix + .split('.') + .map(String::from) + .collect(); + let needed_len = match_str.matches('.').count() + 1; + while version_bits.len() < needed_len { + version_bits.push("0".to_string()); + } + let dots_in_match = match_str.matches('.').count(); + format!("~{}", version_bits[..dots_in_match + 1].join(".")) + } else if match_str == "*" || match_str.starts_with(">=") { + format!(">={}{}", version_without_suffix, suffix) + } else { + format!("{}{}", new_pretty_constraint, suffix) + }; let offset = match_offset as usize; let length = Platform::strlen(match_str) as usize; - modified = shirabe_php_shim::substr_replace(&modified, &replacement, offset, length); + modified = + shirabe_php_shim::substr_replace(&modified, &replacement, offset, length); } let new_constraint = parser.parse_constraints(&modified)?; diff --git a/crates/shirabe/src/package/version/version_guesser.rs b/crates/shirabe/src/package/version/version_guesser.rs index a97095d..c4e25e0 100644 --- a/crates/shirabe/src/package/version/version_guesser.rs +++ b/crates/shirabe/src/package/version/version_guesser.rs @@ -5,9 +5,9 @@ use indexmap::IndexMap; use shirabe_external_packages::composer::pcre::preg::Preg; use shirabe_external_packages::symfony::component::process::process::Process; use shirabe_php_shim::{ - array_keys, array_map, array_merge, empty, function_exists, implode, is_string, json_encode, - preg_quote, str_replace, strlen, strnatcasecmp, strpos, substr, trim, usort, PhpMixed, - RuntimeException, PHP_INT_MAX, + PHP_INT_MAX, PhpMixed, RuntimeException, array_keys, array_map, array_merge, empty, + function_exists, implode, is_string, json_encode, preg_quote, str_replace, strlen, + strnatcasecmp, strpos, substr, trim, usort, }; use shirabe_semver::version_parser::VersionParser as SemverVersionParser; @@ -144,7 +144,12 @@ impl VersionGuesser { .map(|fv| !fv.is_empty()) .unwrap_or(false); if feature_non_empty - && "-dev" == substr(version_data.feature_version.as_deref().unwrap_or(""), -4, None) + && "-dev" + == substr( + version_data.feature_version.as_deref().unwrap_or(""), + -4, + None, + ) && Preg::is_match( r"{\.9{7}}", version_data.feature_version.as_deref().unwrap_or(""), @@ -296,7 +301,11 @@ impl VersionGuesser { &GitUtil::parse_rev_list_output(&command_output, &self.process), None, ); - commit = if parsed.is_empty() { None } else { Some(parsed) }; + commit = if parsed.is_empty() { + None + } else { + Some(parsed) + }; } } @@ -386,10 +395,8 @@ impl VersionGuesser { // TODO(phase-b): clone ProcessExecutor todo!("self.process.clone()"), ); - let branches: Vec<String> = array_map( - |k: &String| k.clone(), - &array_keys(driver.get_branches()), - ); + let branches: Vec<String> = + array_map(|k: &String| k.clone(), &array_keys(driver.get_branches())); // try to find the best (nearest) version branch to assume this feature's version let mut result = self.guess_feature_version( @@ -597,11 +604,7 @@ impl VersionGuesser { // try to fetch current version from fossil tags let mut output = String::new(); if 0 == self.process.execute( - &[ - "fossil".to_string(), - "tag".to_string(), - "list".to_string(), - ], + &["fossil".to_string(), "tag".to_string(), "list".to_string()], &mut output, Some(path.to_string()), ) { @@ -637,11 +640,7 @@ impl VersionGuesser { // try to fetch current version from svn let mut output = String::new(); if 0 == self.process.execute( - &[ - "svn".to_string(), - "info".to_string(), - "--xml".to_string(), - ], + &["svn".to_string(), "info".to_string(), "--xml".to_string()], &mut output, Some(path.to_string()), ) { @@ -670,7 +669,8 @@ impl VersionGuesser { let m1 = matches.get(1).cloned().unwrap_or_default(); let m2 = matches.get(2).cloned(); let m3 = matches.get(3).cloned(); - if m2.is_some() && m3.is_some() + if m2.is_some() + && m3.is_some() && (branches_path == *m2.as_ref().unwrap() || tags_path == *m2.as_ref().unwrap()) { diff --git a/crates/shirabe/src/package/version/version_parser.rs b/crates/shirabe/src/package/version/version_parser.rs index dbaa1c9..6bb1004 100644 --- a/crates/shirabe/src/package/version/version_parser.rs +++ b/crates/shirabe/src/package/version/version_parser.rs @@ -21,7 +21,10 @@ pub struct VersionParser { impl VersionParser { pub const DEFAULT_BRANCH_ALIAS: &'static str = "9999999-dev"; - pub fn parse_constraints(&self, constraints: &str) -> anyhow::Result<Arc<dyn ConstraintInterface + Send + Sync>> { + pub fn parse_constraints( + &self, + constraints: &str, + ) -> anyhow::Result<Arc<dyn ConstraintInterface + Send + Sync>> { let mut cache = CONSTRAINTS.lock().unwrap(); if !cache.contains_key(constraints) { let parsed = self.inner.parse_constraints(constraints)?; @@ -30,13 +33,20 @@ impl VersionParser { Ok(Arc::clone(cache.get(constraints).unwrap())) } - pub fn parse_name_version_pairs(&self, pairs: Vec<String>) -> anyhow::Result<Vec<IndexMap<String, String>>> { + pub fn parse_name_version_pairs( + &self, + pairs: Vec<String>, + ) -> anyhow::Result<Vec<IndexMap<String, String>>> { let pairs: Vec<String> = pairs; let mut result: Vec<IndexMap<String, String>> = Vec::new(); let count = pairs.len(); let mut i = 0_usize; while i < count { - let mut pair = Preg::replace(r"{^([^=: ]+)[=: ](.*)$}", "$1 $2", pairs[i].trim().to_string())?; + let mut pair = Preg::replace( + r"{^([^=: ]+)[=: ](.*)$}", + "$1 $2", + pairs[i].trim().to_string(), + )?; if !pair.contains(' ') && i + 1 < count && !pairs[i + 1].contains('/') diff --git a/crates/shirabe/src/package/version/version_selector.rs b/crates/shirabe/src/package/version/version_selector.rs index 85ce71d..f665320 100644 --- a/crates/shirabe/src/package/version/version_selector.rs +++ b/crates/shirabe/src/package/version/version_selector.rs @@ -5,7 +5,7 @@ use std::any::Any; use indexmap::IndexMap; use shirabe_external_packages::composer::pcre::preg::Preg; use shirabe_php_shim::{ - strtolower, version_compare, PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION, + PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION, strtolower, version_compare, }; use shirabe_semver::constraint::constraint::Constraint; use shirabe_semver::constraint::constraint_interface::ConstraintInterface; @@ -20,9 +20,9 @@ use crate::package::base_package::BasePackage; use crate::package::dumper::array_dumper::ArrayDumper; use crate::package::loader::array_loader::ArrayLoader; use crate::package::package_interface::PackageInterface; +use crate::package::version::version_parser::VersionParser; use crate::repository::platform_repository::PlatformRepository; use crate::repository::repository_set::RepositorySet; -use crate::package::version::version_parser::VersionParser; #[derive(Debug)] pub struct VersionSelector { @@ -140,7 +140,8 @@ impl VersionSelector { if link.get_constraint().matches(provided_constraint.as_ref()) { continue 'reqs; } - let list_filter_opt = (platform_requirement_filter.as_ref() as &dyn Any) + let list_filter_opt = (platform_requirement_filter.as_ref() + as &dyn Any) .downcast_ref::<IgnoreListPlatformRequirementFilter>(); if let Some(list_filter) = list_filter_opt { if list_filter.is_upper_bound_ignored(name) { @@ -168,8 +169,7 @@ impl VersionSelector { _ => true, }; if should_warn { - let warn_key = - format!("{}/{}", pkg.get_name(), link.get_target()); + let warn_key = format!("{}/{}", pkg.get_name(), link.get_target()); let is_first_warning = !already_warned_names.contains_key(&warn_key); already_warned_names.insert(warn_key, true); let latest = if is_latest_version { @@ -222,15 +222,16 @@ impl VersionSelector { Some(p) => p, }; - let package = if let Some(alias) = (package.as_ref() as &dyn Any).downcast_ref::<AliasPackage>() { - if alias.get_version() == VersionParser::DEFAULT_BRANCH_ALIAS { - alias.get_alias_of() + let package = + if let Some(alias) = (package.as_ref() as &dyn Any).downcast_ref::<AliasPackage>() { + if alias.get_version() == VersionParser::DEFAULT_BRANCH_ALIAS { + alias.get_alias_of() + } else { + package + } } else { package - } - } else { - package - }; + }; Ok(Some(package)) } @@ -288,7 +289,10 @@ impl VersionSelector { if semantic_version_parts.len() == 4 && Preg::is_match(r"{^\d+\D?}", semantic_version_parts[3]).unwrap_or(false) { - let mut parts: Vec<String> = semantic_version_parts.iter().map(|s| s.to_string()).collect(); + let mut parts: Vec<String> = semantic_version_parts + .iter() + .map(|s| s.to_string()) + .collect(); let version = if parts[0] == "0" { parts.truncate(3); parts.join(".") |
