Skip to content

Commit

Permalink
Lint more consistently and add alias
Browse files Browse the repository at this point in the history
  • Loading branch information
alexevanczuk committed Dec 23, 2024
1 parent 170a3cb commit 32c71d3
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[package]
name = "pks"
version = "0.2.20"
version = "0.2.21"
edition = "2021"
description = "Welcome! Please see https://github.com/alexevanczuk/packs for more information!"
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion src/packs/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ enum Command {
#[clap(about = "Add everything a pack depends on (may cause cycles)")]
AddDependencies { pack_name: String },

#[clap(about = "Lint package.yml files")]
#[clap(about = "Lint package.yml files", aliases = ["lint"])]
LintPackageYmlFiles,

#[clap(
Expand Down
83 changes: 78 additions & 5 deletions src/packs/pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,13 +399,62 @@ fn is_default_public_folder(value: &Option<PathBuf>) -> bool {
}
}

const KEY_SORT_ORDER: &[&str] = &[
"enforce_dependencies",
"enforce_privacy",
"enforce_layers",
"enforce_visibility",
"enforce_folder_privacy",
"enforce_folder_visibility",
"enforce_architecture",
"layer",
"public_path",
"dependencies",
"owner",
"private_constants",
"visible_to",
"enforcement_globs_ignore",
"metadata",
];

pub fn serialize_pack(pack: &Pack) -> String {
let serialized_pack = serde_yaml::to_string(&pack).unwrap();
if serialized_pack == "{}\n" {
"".to_owned()
} else {
serialized_pack
let serialized: Value = serde_yaml::to_value(pack).unwrap();
let mapping = serialized.as_mapping().unwrap();

// Prepare a Vec to preserve order
let mut ordered_map: Vec<(String, Value)> = Vec::new();

// Add keys from KEY_SORT_ORDER
for key in KEY_SORT_ORDER {
if let Some(value) = mapping.get(&Value::String(key.to_string())) {
ordered_map.push((key.to_string(), value.clone()));
}
}

// Add remaining keys not in KEY_SORT_ORDER
let mut added_keys: HashSet<String> =
ordered_map.iter().map(|(k, _)| k.clone()).collect();
for (key, value) in mapping {
if let Value::String(key_str) = key {
if !added_keys.contains(key_str) {
ordered_map.push((key_str.clone(), value.clone()));
added_keys.insert(key_str.clone());
}
}
}

// Convert the ordered map to a serde_yaml::Mapping
let mut sorted_mapping = serde_yaml::Mapping::new();
for (key, value) in ordered_map {
sorted_mapping.insert(Value::String(key), value);
}

// Serialize to YAML
let raw_yaml =
serde_yaml::to_string(&Value::Mapping(sorted_mapping)).unwrap();

// Remove YAML header (`---\n`) to match Ruby behavior
raw_yaml.trim_start_matches("---\n").to_string()
}

pub fn write_pack_to_disk(pack: &Pack) -> anyhow::Result<()> {
Expand Down Expand Up @@ -620,6 +669,30 @@ metadata:
assert_eq!(expected, actual)
}

#[test]
fn test_serde_with_many_fields() {
let pack_yml = r#"
enforce_dependencies: true
enforce_privacy: true
dependencies:
- packs/utilities
enforce_architecture: true
"#;

let actual = reserialize_pack(pack_yml);

let expected = r#"
enforce_dependencies: true
enforce_privacy: true
enforce_architecture: true
dependencies:
- packs/utilities
"#
.trim_start();

assert_eq!(expected, actual)
}

#[test]
fn test_serde_with_owner() {
let pack_yml = r#"
Expand Down

0 comments on commit 32c71d3

Please sign in to comment.