Skip to content

Commit 259d845

Browse files
authored
feat(bundler): add post/pre install/remove scripts for linux deb and rpm packages (#9209)
* feat(cli/add): add post/pre install/remove scripts for linux * fix(cli): clippy errors * fix(cli): remove script struct from deb/rpm bundle * change files and aliases
1 parent c33f6e6 commit 259d845

File tree

9 files changed

+267
-18
lines changed

9 files changed

+267
-18
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'tauri-bundler': 'minor:feat'
3+
---
4+
5+
Add suport for include `preinstall`, `postinstall`, `preremove` and `postremove` scripts into Debian and RPM packages.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'tauri-utils': 'minor:feat'
3+
---
4+
5+
Added `preInstallScript`, `postInstallScript`, `preRemoveScript` and `postRemoveScript` options for `bundler > deb` and `bundler > rpm` configs.

core/tauri-config-schema/schema.json

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2430,29 +2430,57 @@
24302430
"type": "string"
24312431
}
24322432
},
2433+
"section": {
2434+
"description": "Define the section in Debian Control file. See : https://www.debian.org/doc/debian-policy/ch-archive.html#s-subsections",
2435+
"type": [
2436+
"string",
2437+
"null"
2438+
]
2439+
},
2440+
"priority": {
2441+
"description": "Change the priority of the Debian Package. By default, it is set to `optional`. Recognized Priorities as of now are : `required`, `important`, `standard`, `optional`, `extra`",
2442+
"type": [
2443+
"string",
2444+
"null"
2445+
]
2446+
},
2447+
"changelog": {
2448+
"description": "Path of the uncompressed Changelog file, to be stored at /usr/share/doc/package-name/changelog.gz. See https://www.debian.org/doc/debian-policy/ch-docs.html#changelog-files-and-release-notes",
2449+
"type": [
2450+
"string",
2451+
"null"
2452+
]
2453+
},
24332454
"desktopTemplate": {
24342455
"description": "Path to a custom desktop file Handlebars template.\n\nAvailable variables: `categories`, `comment` (optional), `exec`, `icon` and `name`.",
24352456
"type": [
24362457
"string",
24372458
"null"
24382459
]
24392460
},
2440-
"section": {
2441-
"description": "Define the section in Debian Control file. See : https://www.debian.org/doc/debian-policy/ch-archive.html#s-subsections",
2461+
"preInstallScript": {
2462+
"description": "Path to script that will be executed before the package is unpacked. See https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html",
24422463
"type": [
24432464
"string",
24442465
"null"
24452466
]
24462467
},
2447-
"priority": {
2448-
"description": "Change the priority of the Debian Package. By default, it is set to `optional`. Recognized Priorities as of now are : `required`, `important`, `standard`, `optional`, `extra`",
2468+
"postInstallScript": {
2469+
"description": "Path to script that will be executed after the package is unpacked. See https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html",
24492470
"type": [
24502471
"string",
24512472
"null"
24522473
]
24532474
},
2454-
"changelog": {
2455-
"description": "Path of the uncompressed Changelog file, to be stored at /usr/share/doc/package-name/changelog.gz. See https://www.debian.org/doc/debian-policy/ch-docs.html#changelog-files-and-release-notes",
2475+
"preRemoveScript": {
2476+
"description": "Path to script that will be executed before the package is removed. See https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html",
2477+
"type": [
2478+
"string",
2479+
"null"
2480+
]
2481+
},
2482+
"postRemoveScript": {
2483+
"description": "Path to script that will be executed after the package is removed. See https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html",
24562484
"type": [
24572485
"string",
24582486
"null"
@@ -2501,6 +2529,34 @@
25012529
"string",
25022530
"null"
25032531
]
2532+
},
2533+
"preInstallScript": {
2534+
"description": "Path to script that will be executed before the package is unpacked. See http://ftp.rpm.org/max-rpm/s1-rpm-inside-scripts.html",
2535+
"type": [
2536+
"string",
2537+
"null"
2538+
]
2539+
},
2540+
"postInstallScript": {
2541+
"description": "Path to script that will be executed after the package is unpacked. See http://ftp.rpm.org/max-rpm/s1-rpm-inside-scripts.html",
2542+
"type": [
2543+
"string",
2544+
"null"
2545+
]
2546+
},
2547+
"preRemoveScript": {
2548+
"description": "Path to script that will be executed before the package is removed. See http://ftp.rpm.org/max-rpm/s1-rpm-inside-scripts.html",
2549+
"type": [
2550+
"string",
2551+
"null"
2552+
]
2553+
},
2554+
"postRemoveScript": {
2555+
"description": "Path to script that will be executed after the package is removed. See http://ftp.rpm.org/max-rpm/s1-rpm-inside-scripts.html",
2556+
"type": [
2557+
"string",
2558+
"null"
2559+
]
25042560
}
25052561
},
25062562
"additionalProperties": false

core/tauri-utils/src/config.rs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,10 +326,6 @@ pub struct DebConfig {
326326
/// The files to include on the package.
327327
#[serde(default)]
328328
pub files: HashMap<PathBuf, PathBuf>,
329-
/// Path to a custom desktop file Handlebars template.
330-
///
331-
/// Available variables: `categories`, `comment` (optional), `exec`, `icon` and `name`.
332-
pub desktop_template: Option<PathBuf>,
333329
/// Define the section in Debian Control file. See : https://www.debian.org/doc/debian-policy/ch-archive.html#s-subsections
334330
pub section: Option<String>,
335331
/// Change the priority of the Debian Package. By default, it is set to `optional`.
@@ -338,6 +334,27 @@ pub struct DebConfig {
338334
/// Path of the uncompressed Changelog file, to be stored at /usr/share/doc/package-name/changelog.gz. See
339335
/// https://www.debian.org/doc/debian-policy/ch-docs.html#changelog-files-and-release-notes
340336
pub changelog: Option<PathBuf>,
337+
/// Path to a custom desktop file Handlebars template.
338+
///
339+
/// Available variables: `categories`, `comment` (optional), `exec`, `icon` and `name`.
340+
#[serde(alias = "desktop-template")]
341+
pub desktop_template: Option<PathBuf>,
342+
/// Path to script that will be executed before the package is unpacked. See
343+
/// https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html
344+
#[serde(alias = "pre-install-script")]
345+
pub pre_install_script: Option<PathBuf>,
346+
/// Path to script that will be executed after the package is unpacked. See
347+
/// https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html
348+
#[serde(alias = "post-install-script")]
349+
pub post_install_script: Option<PathBuf>,
350+
/// Path to script that will be executed before the package is removed. See
351+
/// https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html
352+
#[serde(alias = "pre-remove-script")]
353+
pub pre_remove_script: Option<PathBuf>,
354+
/// Path to script that will be executed after the package is removed. See
355+
/// https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html
356+
#[serde(alias = "post-remove-script")]
357+
pub post_remove_script: Option<PathBuf>,
341358
}
342359

343360
/// Configuration for Linux bundles.
@@ -379,7 +396,24 @@ pub struct RpmConfig {
379396
/// Path to a custom desktop file Handlebars template.
380397
///
381398
/// Available variables: `categories`, `comment` (optional), `exec`, `icon` and `name`.
399+
#[serde(alias = "desktop-template")]
382400
pub desktop_template: Option<PathBuf>,
401+
/// Path to script that will be executed before the package is unpacked. See
402+
/// http://ftp.rpm.org/max-rpm/s1-rpm-inside-scripts.html
403+
#[serde(alias = "pre-install-script")]
404+
pub pre_install_script: Option<PathBuf>,
405+
/// Path to script that will be executed after the package is unpacked. See
406+
/// http://ftp.rpm.org/max-rpm/s1-rpm-inside-scripts.html
407+
#[serde(alias = "post-install-script")]
408+
pub post_install_script: Option<PathBuf>,
409+
/// Path to script that will be executed before the package is removed. See
410+
/// http://ftp.rpm.org/max-rpm/s1-rpm-inside-scripts.html
411+
#[serde(alias = "pre-remove-script")]
412+
pub pre_remove_script: Option<PathBuf>,
413+
/// Path to script that will be executed after the package is removed. See
414+
/// http://ftp.rpm.org/max-rpm/s1-rpm-inside-scripts.html
415+
#[serde(alias = "post-remove-script")]
416+
pub post_remove_script: Option<PathBuf>,
383417
}
384418

385419
impl Default for RpmConfig {
@@ -390,6 +424,10 @@ impl Default for RpmConfig {
390424
epoch: 0,
391425
files: Default::default(),
392426
desktop_template: None,
427+
pre_install_script: None,
428+
post_install_script: None,
429+
pre_remove_script: None,
430+
post_remove_script: None,
393431
}
394432
}
395433
}

tooling/bundler/src/bundle/linux/debian.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ use tar::HeaderMode;
3232
use walkdir::WalkDir;
3333

3434
use std::{
35-
fs::{self, File},
35+
fs::{self, File, OpenOptions},
3636
io::{self, Write},
37-
os::unix::fs::MetadataExt,
37+
os::unix::fs::{MetadataExt, OpenOptionsExt},
3838
path::{Path, PathBuf},
3939
};
4040

@@ -76,6 +76,7 @@ pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<PathBuf>> {
7676
let control_dir = package_dir.join("control");
7777
generate_control_file(settings, arch, &control_dir, &data_dir)
7878
.with_context(|| "Failed to create control file")?;
79+
generate_scripts(settings, &control_dir).with_context(|| "Failed to create control scripts")?;
7980
generate_md5sums(&control_dir, &data_dir).with_context(|| "Failed to create md5sums file")?;
8081

8182
// Generate `debian-binary` file; see
@@ -202,6 +203,41 @@ fn generate_control_file(
202203
Ok(())
203204
}
204205

206+
fn generate_scripts(settings: &Settings, control_dir: &Path) -> crate::Result<()> {
207+
if let Some(script_path) = &settings.deb().pre_install_script {
208+
let dest_path = control_dir.join("preinst");
209+
create_script_file_from_path(script_path, &dest_path)?
210+
}
211+
212+
if let Some(script_path) = &settings.deb().post_install_script {
213+
let dest_path = control_dir.join("postinst");
214+
create_script_file_from_path(script_path, &dest_path)?
215+
}
216+
217+
if let Some(script_path) = &settings.deb().pre_remove_script {
218+
let dest_path = control_dir.join("prerm");
219+
create_script_file_from_path(script_path, &dest_path)?
220+
}
221+
222+
if let Some(script_path) = &settings.deb().post_remove_script {
223+
let dest_path = control_dir.join("postrm");
224+
create_script_file_from_path(script_path, &dest_path)?
225+
}
226+
Ok(())
227+
}
228+
229+
fn create_script_file_from_path(from: &PathBuf, to: &PathBuf) -> crate::Result<()> {
230+
let mut from = File::open(from)?;
231+
let mut file = OpenOptions::new()
232+
.create(true)
233+
.truncate(true)
234+
.write(true)
235+
.mode(0o755)
236+
.open(to)?;
237+
std::io::copy(&mut from, &mut file)?;
238+
Ok(())
239+
}
240+
205241
/// Create an `md5sums` file in the `control_dir` containing the MD5 checksums
206242
/// for each file within the `data_dir`.
207243
fn generate_md5sums(control_dir: &Path, data_dir: &Path) -> crate::Result<()> {

tooling/bundler/src/bundle/linux/rpm.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,27 @@ pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<PathBuf>> {
7878
builder = builder.with_file(&src, FileOptions::new(dest.to_string_lossy()))?;
7979
}
8080

81+
// Add scripts
82+
if let Some(script_path) = &settings.rpm().pre_install_script {
83+
let script = fs::read_to_string(script_path)?;
84+
builder = builder.pre_install_script(script);
85+
}
86+
87+
if let Some(script_path) = &settings.rpm().post_install_script {
88+
let script = fs::read_to_string(script_path)?;
89+
builder = builder.post_install_script(script);
90+
}
91+
92+
if let Some(script_path) = &settings.rpm().pre_remove_script {
93+
let script = fs::read_to_string(script_path)?;
94+
builder = builder.pre_uninstall_script(script);
95+
}
96+
97+
if let Some(script_path) = &settings.rpm().post_remove_script {
98+
let script = fs::read_to_string(script_path)?;
99+
builder = builder.post_uninstall_script(script);
100+
}
101+
81102
// Add resources
82103
if settings.resource_files().count() > 0 {
83104
let resource_dir = Path::new("/usr/lib").join(settings.main_binary_name());

tooling/bundler/src/bundle/settings.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,18 @@ pub struct DebianSettings {
188188
/// Path of the uncompressed Changelog file, to be stored at /usr/share/doc/package-name/changelog.gz. See
189189
/// https://www.debian.org/doc/debian-policy/ch-docs.html#changelog-files-and-release-notes
190190
pub changelog: Option<PathBuf>,
191+
/// Path to script that will be executed before the package is unpacked. See
192+
/// https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html
193+
pub pre_install_script: Option<PathBuf>,
194+
/// Path to script that will be executed after the package is unpacked. See
195+
/// https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html
196+
pub post_install_script: Option<PathBuf>,
197+
/// Path to script that will be executed before the package is removed. See
198+
/// https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html
199+
pub pre_remove_script: Option<PathBuf>,
200+
/// Path to script that will be executed after the package is removed. See
201+
/// https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html
202+
pub post_remove_script: Option<PathBuf>,
191203
}
192204

193205
/// The Linux AppImage bundle settings.
@@ -218,6 +230,18 @@ pub struct RpmSettings {
218230
#[doc = include_str!("./linux/templates/main.desktop")]
219231
/// ```
220232
pub desktop_template: Option<PathBuf>,
233+
/// Path to script that will be executed before the package is unpacked. See
234+
/// http://ftp.rpm.org/max-rpm/s1-rpm-inside-scripts.html
235+
pub pre_install_script: Option<PathBuf>,
236+
/// Path to script that will be executed after the package is unpacked. See
237+
/// http://ftp.rpm.org/max-rpm/s1-rpm-inside-scripts.html
238+
pub post_install_script: Option<PathBuf>,
239+
/// Path to script that will be executed before the package is removed. See
240+
/// http://ftp.rpm.org/max-rpm/s1-rpm-inside-scripts.html
241+
pub pre_remove_script: Option<PathBuf>,
242+
/// Path to script that will be executed after the package is removed. See
243+
/// http://ftp.rpm.org/max-rpm/s1-rpm-inside-scripts.html
244+
pub post_remove_script: Option<PathBuf>,
221245
}
222246

223247
/// Position coordinates struct.

0 commit comments

Comments
 (0)