Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Refactor script create_package_xml. #67

Merged
merged 2 commits into from
Apr 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.tpl.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ limitations under the License.
</notes>
<contents>
<dir name="/">
{% for file in files %}{{ file.path|file_filter }}
{% for file in files %}<file name="{{ file.name }}" role="{{ file.role }}" />
{% endfor %}
</dir>
</contents>
Expand Down
42 changes: 22 additions & 20 deletions scripts/src/command/create_package_xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
use chrono::{DateTime, Local};
use clap::Parser;
use serde::Serialize;
use std::{collections::HashMap, fs, path::PathBuf, process::Command, time::SystemTime};
use tera::{Context, Result, Tera, Value};
use std::{fs, path::PathBuf, process::Command, time::SystemTime};
use tera::{Context, Tera};
use tracing::info;

/// Create package.xml from template file.
Expand Down Expand Up @@ -50,20 +50,28 @@ pub struct CreatePackageXmlCommand {

#[derive(Serialize)]
struct File {
path: String,
name: String,
role: String,
}

pub fn file_filter(value: &Value, _: &HashMap<String, Value>) -> Result<Value> {
let mut role = "src";
let path = value.to_string().trim_matches('"').to_string();
if path.ends_with(".md") || path == "LICENSE" || path == "NOTICE" {
role = "doc"
impl File {
fn new(path: &str) -> Self {
let path = path.trim_matches('"');
let role = if path.ends_with(".md")
|| path.starts_with("docs/")
|| path.starts_with("dist-material/")
|| ["LICENSE", "NOTICE"].contains(&path)
{
"doc"
} else {
"src"
};

Self {
name: path.to_owned(),
role: role.to_owned(),
}
}

Ok(Value::String(format!(
"<file name=\"{}\" role=\"{}\"/>",
path, role
)))
}

impl CreatePackageXmlCommand {
Expand All @@ -78,7 +86,6 @@ impl CreatePackageXmlCommand {
context.insert("files", &self.get_git_files()?);

let mut tera = Tera::default();
tera.register_filter("file_filter", file_filter);
let contents = tera.render_str(&tpl, &context)?;

info!(target_path = ?&self.target_path, "write target content");
Expand All @@ -102,11 +109,6 @@ impl CreatePackageXmlCommand {
.args(["ls-tree", "-r", "HEAD", "--name-only"])
.output()?;
let content = String::from_utf8(output.stdout)?;
Ok(content
.split_whitespace()
.map(|path| File {
path: path.to_owned(),
})
.collect())
Ok(content.split_whitespace().map(File::new).collect())
}
}