abbaye/builders/
archive.rs1use std::{
2 fs::File,
3 path::{Path, PathBuf},
4};
5
6use flate2::{Compression, write::GzEncoder};
7use globset::{Glob, GlobSet, GlobSetBuilder};
8use ignore::WalkBuilder;
9use miette::{IntoDiagnostic, Result};
10use serde::{Deserialize, Serialize};
11
12use crate::builders::{ArtifactPath, Builder};
13
14fn default_ignore_patterns() -> Vec<String> {
15 vec![".git".to_owned(), "*.local".to_owned()]
16}
17
18#[derive(Debug, Clone, Deserialize, Serialize)]
20pub struct ArchiveBuilderConfig {
21 pub source_dir: Option<PathBuf>,
23
24 pub output: Option<PathBuf>,
27
28 pub prefix: Option<String>,
33
34 #[serde(default = "default_ignore_patterns")]
40 pub ignore_patterns: Vec<String>,
41}
42
43impl Default for ArchiveBuilderConfig {
44 fn default() -> Self {
45 Self {
46 source_dir: None,
47 output: None,
48 prefix: None,
49 ignore_patterns: default_ignore_patterns(),
50 }
51 }
52}
53
54pub struct ArchiveBuilder;
57
58impl Builder for ArchiveBuilder {
59 type ConfigType = ArchiveBuilderConfig;
60
61 async fn build(&self, config: Self::ConfigType) -> Result<Vec<ArtifactPath>> {
62 let source_dir = config
63 .source_dir
64 .unwrap_or_else(|| PathBuf::from("."))
65 .canonicalize()
66 .into_diagnostic()?;
67
68 let output = config
69 .output
70 .unwrap_or_else(|| PathBuf::from("../source.tar.gz"));
71
72 let prefix = config.prefix.unwrap_or_else(|| {
73 source_dir
74 .file_name()
75 .map(|n| n.to_string_lossy().into_owned())
76 .unwrap_or_else(|| "source".to_owned())
77 });
78
79 let ignore_set = build_ignore_set(&config.ignore_patterns)?;
80
81 let archive_path = tokio::task::spawn_blocking(move || {
82 create_archive(&source_dir, &output, &prefix, &ignore_set)
83 })
84 .await
85 .into_diagnostic()??;
86
87 let name = archive_path
88 .file_name()
89 .map(|n| n.to_string_lossy().into_owned())
90 .unwrap_or_default();
91
92 Ok(vec![ArtifactPath {
93 path: archive_path,
94 name,
95 hash: None,
96 }])
97 }
98}
99
100fn build_ignore_set(patterns: &[String]) -> Result<GlobSet> {
102 let mut builder = GlobSetBuilder::new();
103 for pattern in patterns {
104 builder.add(Glob::new(pattern).into_diagnostic()?);
105 }
106 builder.build().into_diagnostic()
107}
108
109fn create_archive(
114 source_dir: &Path,
115 output: &Path,
116 prefix: &str,
117 ignore_set: &GlobSet,
118) -> Result<PathBuf> {
119 let file = File::create(output).into_diagnostic()?;
120 let output_canonical = output.canonicalize().into_diagnostic()?;
123 let encoder = GzEncoder::new(file, Compression::default());
124 let mut archive = tar::Builder::new(encoder);
125
126 for result in WalkBuilder::new(source_dir)
127 .hidden(false) .build()
129 {
130 let entry = result.into_diagnostic()?;
131 let path = entry.path();
132
133 let relative = path.strip_prefix(source_dir).into_diagnostic()?;
134
135 if relative.components().any(|c| c.as_os_str() == ".git") {
137 continue;
138 }
139
140 if path == output_canonical {
142 continue;
143 }
144
145 if relative
147 .components()
148 .any(|c| ignore_set.is_match(Path::new(c.as_os_str())))
149 {
150 continue;
151 }
152
153 if !path.is_file() {
154 continue;
155 }
156
157 let entry_path = Path::new(prefix).join(relative);
158
159 archive
160 .append_path_with_name(path, &entry_path)
161 .into_diagnostic()?;
162 }
163
164 archive
166 .into_inner()
167 .into_diagnostic()?
168 .finish()
169 .into_diagnostic()?;
170
171 Ok(output.to_path_buf())
172}