Skip to content

Commit

Permalink
feat(bundler): add post/pre install/remove scripts for linux deb and …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
vlabo committed Mar 28, 2024
1 parent c33f6e6 commit 259d845
Show file tree
Hide file tree
Showing 9 changed files with 267 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changes/deb-rpm-post-pre-scripts-bundler.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tauri-bundler': 'minor:feat'
---

Add suport for include `preinstall`, `postinstall`, `preremove` and `postremove` scripts into Debian and RPM packages.
5 changes: 5 additions & 0 deletions .changes/deb-rpm-post-pre-scripts-config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tauri-utils': 'minor:feat'
---

Added `preInstallScript`, `postInstallScript`, `preRemoveScript` and `postRemoveScript` options for `bundler > deb` and `bundler > rpm` configs.
68 changes: 62 additions & 6 deletions core/tauri-config-schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2430,29 +2430,57 @@
"type": "string"
}
},
"section": {
"description": "Define the section in Debian Control file. See : https://www.debian.org/doc/debian-policy/ch-archive.html#s-subsections",
"type": [
"string",
"null"
]
},
"priority": {
"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`",
"type": [
"string",
"null"
]
},
"changelog": {
"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",
"type": [
"string",
"null"
]
},
"desktopTemplate": {
"description": "Path to a custom desktop file Handlebars template.\n\nAvailable variables: `categories`, `comment` (optional), `exec`, `icon` and `name`.",
"type": [
"string",
"null"
]
},
"section": {
"description": "Define the section in Debian Control file. See : https://www.debian.org/doc/debian-policy/ch-archive.html#s-subsections",
"preInstallScript": {
"description": "Path to script that will be executed before the package is unpacked. See https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html",
"type": [
"string",
"null"
]
},
"priority": {
"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`",
"postInstallScript": {
"description": "Path to script that will be executed after the package is unpacked. See https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html",
"type": [
"string",
"null"
]
},
"changelog": {
"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",
"preRemoveScript": {
"description": "Path to script that will be executed before the package is removed. See https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html",
"type": [
"string",
"null"
]
},
"postRemoveScript": {
"description": "Path to script that will be executed after the package is removed. See https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html",
"type": [
"string",
"null"
Expand Down Expand Up @@ -2501,6 +2529,34 @@
"string",
"null"
]
},
"preInstallScript": {
"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",
"type": [
"string",
"null"
]
},
"postInstallScript": {
"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",
"type": [
"string",
"null"
]
},
"preRemoveScript": {
"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",
"type": [
"string",
"null"
]
},
"postRemoveScript": {
"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",
"type": [
"string",
"null"
]
}
},
"additionalProperties": false
Expand Down
46 changes: 42 additions & 4 deletions core/tauri-utils/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,6 @@ pub struct DebConfig {
/// The files to include on the package.
#[serde(default)]
pub files: HashMap<PathBuf, PathBuf>,
/// Path to a custom desktop file Handlebars template.
///
/// Available variables: `categories`, `comment` (optional), `exec`, `icon` and `name`.
pub desktop_template: Option<PathBuf>,
/// Define the section in Debian Control file. See : https://www.debian.org/doc/debian-policy/ch-archive.html#s-subsections
pub section: Option<String>,
/// Change the priority of the Debian Package. By default, it is set to `optional`.
Expand All @@ -338,6 +334,27 @@ pub struct DebConfig {
/// 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
pub changelog: Option<PathBuf>,
/// Path to a custom desktop file Handlebars template.
///
/// Available variables: `categories`, `comment` (optional), `exec`, `icon` and `name`.
#[serde(alias = "desktop-template")]
pub desktop_template: Option<PathBuf>,
/// Path to script that will be executed before the package is unpacked. See
/// https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html
#[serde(alias = "pre-install-script")]
pub pre_install_script: Option<PathBuf>,
/// Path to script that will be executed after the package is unpacked. See
/// https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html
#[serde(alias = "post-install-script")]
pub post_install_script: Option<PathBuf>,
/// Path to script that will be executed before the package is removed. See
/// https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html
#[serde(alias = "pre-remove-script")]
pub pre_remove_script: Option<PathBuf>,
/// Path to script that will be executed after the package is removed. See
/// https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html
#[serde(alias = "post-remove-script")]
pub post_remove_script: Option<PathBuf>,
}

/// Configuration for Linux bundles.
Expand Down Expand Up @@ -379,7 +396,24 @@ pub struct RpmConfig {
/// Path to a custom desktop file Handlebars template.
///
/// Available variables: `categories`, `comment` (optional), `exec`, `icon` and `name`.
#[serde(alias = "desktop-template")]
pub desktop_template: Option<PathBuf>,
/// Path to script that will be executed before the package is unpacked. See
/// http://ftp.rpm.org/max-rpm/s1-rpm-inside-scripts.html
#[serde(alias = "pre-install-script")]
pub pre_install_script: Option<PathBuf>,
/// Path to script that will be executed after the package is unpacked. See
/// http://ftp.rpm.org/max-rpm/s1-rpm-inside-scripts.html
#[serde(alias = "post-install-script")]
pub post_install_script: Option<PathBuf>,
/// Path to script that will be executed before the package is removed. See
/// http://ftp.rpm.org/max-rpm/s1-rpm-inside-scripts.html
#[serde(alias = "pre-remove-script")]
pub pre_remove_script: Option<PathBuf>,
/// Path to script that will be executed after the package is removed. See
/// http://ftp.rpm.org/max-rpm/s1-rpm-inside-scripts.html
#[serde(alias = "post-remove-script")]
pub post_remove_script: Option<PathBuf>,
}

impl Default for RpmConfig {
Expand All @@ -390,6 +424,10 @@ impl Default for RpmConfig {
epoch: 0,
files: Default::default(),
desktop_template: None,
pre_install_script: None,
post_install_script: None,
pre_remove_script: None,
post_remove_script: None,
}
}
}
Expand Down
40 changes: 38 additions & 2 deletions tooling/bundler/src/bundle/linux/debian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ use tar::HeaderMode;
use walkdir::WalkDir;

use std::{
fs::{self, File},
fs::{self, File, OpenOptions},
io::{self, Write},
os::unix::fs::MetadataExt,
os::unix::fs::{MetadataExt, OpenOptionsExt},
path::{Path, PathBuf},
};

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

// Generate `debian-binary` file; see
Expand Down Expand Up @@ -202,6 +203,41 @@ fn generate_control_file(
Ok(())
}

fn generate_scripts(settings: &Settings, control_dir: &Path) -> crate::Result<()> {
if let Some(script_path) = &settings.deb().pre_install_script {
let dest_path = control_dir.join("preinst");
create_script_file_from_path(script_path, &dest_path)?
}

if let Some(script_path) = &settings.deb().post_install_script {
let dest_path = control_dir.join("postinst");
create_script_file_from_path(script_path, &dest_path)?
}

if let Some(script_path) = &settings.deb().pre_remove_script {
let dest_path = control_dir.join("prerm");
create_script_file_from_path(script_path, &dest_path)?
}

if let Some(script_path) = &settings.deb().post_remove_script {
let dest_path = control_dir.join("postrm");
create_script_file_from_path(script_path, &dest_path)?
}
Ok(())
}

fn create_script_file_from_path(from: &PathBuf, to: &PathBuf) -> crate::Result<()> {
let mut from = File::open(from)?;
let mut file = OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.mode(0o755)
.open(to)?;
std::io::copy(&mut from, &mut file)?;
Ok(())
}

/// Create an `md5sums` file in the `control_dir` containing the MD5 checksums
/// for each file within the `data_dir`.
fn generate_md5sums(control_dir: &Path, data_dir: &Path) -> crate::Result<()> {
Expand Down
21 changes: 21 additions & 0 deletions tooling/bundler/src/bundle/linux/rpm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,27 @@ pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<PathBuf>> {
builder = builder.with_file(&src, FileOptions::new(dest.to_string_lossy()))?;
}

// Add scripts
if let Some(script_path) = &settings.rpm().pre_install_script {
let script = fs::read_to_string(script_path)?;
builder = builder.pre_install_script(script);
}

if let Some(script_path) = &settings.rpm().post_install_script {
let script = fs::read_to_string(script_path)?;
builder = builder.post_install_script(script);
}

if let Some(script_path) = &settings.rpm().pre_remove_script {
let script = fs::read_to_string(script_path)?;
builder = builder.pre_uninstall_script(script);
}

if let Some(script_path) = &settings.rpm().post_remove_script {
let script = fs::read_to_string(script_path)?;
builder = builder.post_uninstall_script(script);
}

// Add resources
if settings.resource_files().count() > 0 {
let resource_dir = Path::new("/usr/lib").join(settings.main_binary_name());
Expand Down
24 changes: 24 additions & 0 deletions tooling/bundler/src/bundle/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,18 @@ pub struct DebianSettings {
/// 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
pub changelog: Option<PathBuf>,
/// Path to script that will be executed before the package is unpacked. See
/// https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html
pub pre_install_script: Option<PathBuf>,
/// Path to script that will be executed after the package is unpacked. See
/// https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html
pub post_install_script: Option<PathBuf>,
/// Path to script that will be executed before the package is removed. See
/// https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html
pub pre_remove_script: Option<PathBuf>,
/// Path to script that will be executed after the package is removed. See
/// https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html
pub post_remove_script: Option<PathBuf>,
}

/// The Linux AppImage bundle settings.
Expand Down Expand Up @@ -218,6 +230,18 @@ pub struct RpmSettings {
#[doc = include_str!("./linux/templates/main.desktop")]
/// ```
pub desktop_template: Option<PathBuf>,
/// Path to script that will be executed before the package is unpacked. See
/// http://ftp.rpm.org/max-rpm/s1-rpm-inside-scripts.html
pub pre_install_script: Option<PathBuf>,
/// Path to script that will be executed after the package is unpacked. See
/// http://ftp.rpm.org/max-rpm/s1-rpm-inside-scripts.html
pub post_install_script: Option<PathBuf>,
/// Path to script that will be executed before the package is removed. See
/// http://ftp.rpm.org/max-rpm/s1-rpm-inside-scripts.html
pub pre_remove_script: Option<PathBuf>,
/// Path to script that will be executed after the package is removed. See
/// http://ftp.rpm.org/max-rpm/s1-rpm-inside-scripts.html
pub post_remove_script: Option<PathBuf>,
}

/// Position coordinates struct.
Expand Down
Loading

0 comments on commit 259d845

Please sign in to comment.