Skip to content

Commit

Permalink
Merge pull request #127 from my4ng/var_bin_path
Browse files Browse the repository at this point in the history
Support variable substitutions in `bin_path_in_archive`
  • Loading branch information
jaemk committed Apr 11, 2024
2 parents e357529 + ea94348 commit ec8e626
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 50 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- `Release::asset_for` now searches for current `OS` and `ARCH` inside `asset.name` if `target` failed to match
- Update `reqwest` to `0.12.0`
- Update `hyper` to `1.2.0`
- Support variable substitutions in `bin_path_in_archive` at runtime
### Removed

## [0.39.0]
Expand Down
31 changes: 19 additions & 12 deletions src/backends/gitea.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ pub struct UpdateBuilder {
target: Option<String>,
bin_name: Option<String>,
bin_install_path: Option<PathBuf>,
bin_path_in_archive: Option<PathBuf>,
bin_path_in_archive: Option<String>,
show_download_progress: bool,
show_output: bool,
no_confirm: bool,
Expand Down Expand Up @@ -300,10 +300,10 @@ impl UpdateBuilder {
/// (see `std::env::consts::EXE_SUFFIX`) to the name if it's missing.
pub fn bin_name(&mut self, name: &str) -> &mut Self {
let raw_bin_name = format!("{}{}", name.trim_end_matches(EXE_SUFFIX), EXE_SUFFIX);
self.bin_name = Some(raw_bin_name.clone());
if self.bin_path_in_archive.is_none() {
self.bin_path_in_archive = Some(PathBuf::from(raw_bin_name));
self.bin_path_in_archive = Some(raw_bin_name.clone());
}
self.bin_name = Some(raw_bin_name);
self
}

Expand All @@ -321,29 +321,36 @@ impl UpdateBuilder {
/// the path to the binary (from the root of the tarball) is not equal to just
/// the `bin_name`.
///
/// This also supports variable paths:
/// - `{{ bin }}` is replaced with the value of `bin_name`
/// - `{{ target }}` is replaced with the value of `target`
/// - `{{ version }}` is replaced with the value of `target_version` if set,
/// otherwise the value of the latest available release version is used.
///
/// # Example
///
/// For a tarball `myapp.tar.gz` with the contents:
/// For a `myapp` binary with `windows` target and latest release version `1.2.3`,
/// the tarball `myapp.tar.gz` has the contents:
///
/// ```shell
/// myapp.tar/
/// |------- bin/
/// |------- windows-1.2.3-bin/
/// | |--- myapp # <-- executable
/// ```
///
/// The path provided should be:
///
/// ```
/// # use self_update::backends::gitea::Update;
/// # fn run() -> Result<(), Box< dyn ::std::error::Error>> {
/// # fn run() -> Result<(), Box<::std::error::Error>> {
/// Update::configure()
/// .bin_path_in_archive("bin/myapp")
/// .bin_path_in_archive("{{ target }}-{{ version }}-bin/{{ bin }}")
/// # .build()?;
/// # Ok(())
/// # }
/// ```
pub fn bin_path_in_archive(&mut self, bin_path: &str) -> &mut Self {
self.bin_path_in_archive = Some(PathBuf::from(bin_path));
self.bin_path_in_archive = Some(bin_path.to_owned());
self
}

Expand Down Expand Up @@ -438,8 +445,8 @@ impl UpdateBuilder {
bail!(Error::Config, "`bin_name` required")
},
bin_install_path,
bin_path_in_archive: if let Some(ref path) = self.bin_path_in_archive {
path.to_owned()
bin_path_in_archive: if let Some(ref bin_path) = self.bin_path_in_archive {
bin_path.to_owned()
} else {
bail!(Error::Config, "`bin_path_in_archive` required")
},
Expand Down Expand Up @@ -472,7 +479,7 @@ pub struct Update {
target_version: Option<String>,
bin_name: String,
bin_install_path: PathBuf,
bin_path_in_archive: PathBuf,
bin_path_in_archive: String,
show_download_progress: bool,
show_output: bool,
no_confirm: bool,
Expand Down Expand Up @@ -554,7 +561,7 @@ impl ReleaseUpdate for Update {
self.bin_install_path.clone()
}

fn bin_path_in_archive(&self) -> PathBuf {
fn bin_path_in_archive(&self) -> String {
self.bin_path_in_archive.clone()
}

Expand Down
29 changes: 18 additions & 11 deletions src/backends/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ pub struct UpdateBuilder {
identifier: Option<String>,
bin_name: Option<String>,
bin_install_path: Option<PathBuf>,
bin_path_in_archive: Option<PathBuf>,
bin_path_in_archive: Option<String>,
show_download_progress: bool,
show_output: bool,
no_confirm: bool,
Expand Down Expand Up @@ -313,10 +313,10 @@ impl UpdateBuilder {
/// (see `std::env::consts::EXE_SUFFIX`) to the name if it's missing.
pub fn bin_name(&mut self, name: &str) -> &mut Self {
let raw_bin_name = format!("{}{}", name.trim_end_matches(EXE_SUFFIX), EXE_SUFFIX);
self.bin_name = Some(raw_bin_name.clone());
if self.bin_path_in_archive.is_none() {
self.bin_path_in_archive = Some(PathBuf::from(raw_bin_name));
self.bin_path_in_archive = Some(raw_bin_name.clone());
}
self.bin_name = Some(raw_bin_name);
self
}

Expand All @@ -334,13 +334,20 @@ impl UpdateBuilder {
/// the path to the binary (from the root of the tarball) is not equal to just
/// the `bin_name`.
///
/// This also supports variable paths:
/// - `{{ bin }}` is replaced with the value of `bin_name`
/// - `{{ target }}` is replaced with the value of `target`
/// - `{{ version }}` is replaced with the value of `target_version` if set,
/// otherwise the value of the latest available release version is used.
///
/// # Example
///
/// For a tarball `myapp.tar.gz` with the contents:
/// For a `myapp` binary with `windows` target and latest release version `1.2.3`,
/// the tarball `myapp.tar.gz` has the contents:
///
/// ```shell
/// myapp.tar/
/// |------- bin/
/// |------- windows-1.2.3-bin/
/// | |--- myapp # <-- executable
/// ```
///
Expand All @@ -350,13 +357,13 @@ impl UpdateBuilder {
/// # use self_update::backends::github::Update;
/// # fn run() -> Result<(), Box<::std::error::Error>> {
/// Update::configure()
/// .bin_path_in_archive("bin/myapp")
/// .bin_path_in_archive("{{ target }}-{{ version }}-bin/{{ bin }}")
/// # .build()?;
/// # Ok(())
/// # }
/// ```
pub fn bin_path_in_archive(&mut self, bin_path: &str) -> &mut Self {
self.bin_path_in_archive = Some(PathBuf::from(bin_path));
self.bin_path_in_archive = Some(bin_path.to_owned());
self
}

Expand Down Expand Up @@ -447,8 +454,8 @@ impl UpdateBuilder {
bail!(Error::Config, "`bin_name` required")
},
bin_install_path,
bin_path_in_archive: if let Some(ref path) = self.bin_path_in_archive {
path.to_owned()
bin_path_in_archive: if let Some(ref bin_path) = self.bin_path_in_archive {
bin_path.to_owned()
} else {
bail!(Error::Config, "`bin_path_in_archive` required")
},
Expand Down Expand Up @@ -482,7 +489,7 @@ pub struct Update {
target_version: Option<String>,
bin_name: String,
bin_install_path: PathBuf,
bin_path_in_archive: PathBuf,
bin_path_in_archive: String,
show_download_progress: bool,
show_output: bool,
no_confirm: bool,
Expand Down Expand Up @@ -578,7 +585,7 @@ impl ReleaseUpdate for Update {
self.bin_install_path.clone()
}

fn bin_path_in_archive(&self) -> PathBuf {
fn bin_path_in_archive(&self) -> String {
self.bin_path_in_archive.clone()
}

Expand Down
31 changes: 19 additions & 12 deletions src/backends/gitlab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ pub struct UpdateBuilder {
target: Option<String>,
bin_name: Option<String>,
bin_install_path: Option<PathBuf>,
bin_path_in_archive: Option<PathBuf>,
bin_path_in_archive: Option<String>,
show_download_progress: bool,
show_output: bool,
no_confirm: bool,
Expand Down Expand Up @@ -297,10 +297,10 @@ impl UpdateBuilder {
/// (see `std::env::consts::EXE_SUFFIX`) to the name if it's missing.
pub fn bin_name(&mut self, name: &str) -> &mut Self {
let raw_bin_name = format!("{}{}", name.trim_end_matches(EXE_SUFFIX), EXE_SUFFIX);
self.bin_name = Some(raw_bin_name.clone());
if self.bin_path_in_archive.is_none() {
self.bin_path_in_archive = Some(PathBuf::from(raw_bin_name));
self.bin_path_in_archive = Some(raw_bin_name.to_owned());
}
self.bin_name = Some(raw_bin_name);
self
}

Expand All @@ -318,29 +318,36 @@ impl UpdateBuilder {
/// the path to the binary (from the root of the tarball) is not equal to just
/// the `bin_name`.
///
/// This also supports variable paths:
/// - `{{ bin }}` is replaced with the value of `bin_name`
/// - `{{ target }}` is replaced with the value of `target`
/// - `{{ version }}` is replaced with the value of `target_version` if set,
/// otherwise the value of the latest available release version is used.
///
/// # Example
///
/// For a tarball `myapp.tar.gz` with the contents:
/// For a `myapp` binary with `windows` target and latest release version `1.2.3`,
/// the tarball `myapp.tar.gz` has the contents:
///
/// ```shell
/// myapp.tar/
/// |------- bin/
/// |------- windows-1.2.3-bin/
/// | |--- myapp # <-- executable
/// ```
///
/// The path provided should be:
///
/// ```
/// # use self_update::backends::gitlab::Update;
/// # fn run() -> Result<(), Box< dyn ::std::error::Error>> {
/// # fn run() -> Result<(), Box<::std::error::Error>> {
/// Update::configure()
/// .bin_path_in_archive("bin/myapp")
/// .bin_path_in_archive("{{ target }}-{{ version }}-bin/{{ bin }}")
/// # .build()?;
/// # Ok(())
/// # }
/// ```
pub fn bin_path_in_archive(&mut self, bin_path: &str) -> &mut Self {
self.bin_path_in_archive = Some(PathBuf::from(bin_path));
self.bin_path_in_archive = Some(bin_path.to_owned());
self
}

Expand Down Expand Up @@ -431,8 +438,8 @@ impl UpdateBuilder {
bail!(Error::Config, "`bin_name` required")
},
bin_install_path,
bin_path_in_archive: if let Some(ref path) = self.bin_path_in_archive {
path.to_owned()
bin_path_in_archive: if let Some(ref bin_path) = self.bin_path_in_archive {
bin_path.to_owned()
} else {
bail!(Error::Config, "`bin_path_in_archive` required")
},
Expand Down Expand Up @@ -465,7 +472,7 @@ pub struct Update {
target_version: Option<String>,
bin_name: String,
bin_install_path: PathBuf,
bin_path_in_archive: PathBuf,
bin_path_in_archive: String,
show_download_progress: bool,
show_output: bool,
no_confirm: bool,
Expand Down Expand Up @@ -552,7 +559,7 @@ impl ReleaseUpdate for Update {
self.bin_install_path.clone()
}

fn bin_path_in_archive(&self) -> PathBuf {
fn bin_path_in_archive(&self) -> String {
self.bin_path_in_archive.clone()
}

Expand Down
29 changes: 18 additions & 11 deletions src/backends/s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ pub struct UpdateBuilder {
region: Option<String>,
bin_name: Option<String>,
bin_install_path: Option<PathBuf>,
bin_path_in_archive: Option<PathBuf>,
bin_path_in_archive: Option<String>,
show_download_progress: bool,
show_output: bool,
no_confirm: bool,
Expand Down Expand Up @@ -241,10 +241,10 @@ impl UpdateBuilder {
/// Set the exe's name. Also sets `bin_path_in_archive` if it hasn't already been set.
pub fn bin_name(&mut self, name: &str) -> &mut Self {
let raw_bin_name = format!("{}{}", name.trim_end_matches(EXE_SUFFIX), EXE_SUFFIX);
self.bin_name = Some(raw_bin_name.clone());
if self.bin_path_in_archive.is_none() {
self.bin_path_in_archive = Some(PathBuf::from(raw_bin_name));
self.bin_path_in_archive = Some(raw_bin_name.to_owned());
}
self.bin_name = Some(raw_bin_name);
self
}

Expand All @@ -262,13 +262,20 @@ impl UpdateBuilder {
/// the path to the binary (from the root of the tarball) is not equal to just
/// the `bin_name`.
///
/// This also supports variable paths:
/// - `{{ bin }}` is replaced with the value of `bin_name`
/// - `{{ target }}` is replaced with the value of `target`
/// - `{{ version }}` is replaced with the value of `target_version` if set,
/// otherwise the value of the latest available release version is used.
///
/// # Example
///
/// For a tarball `myapp.tar.gz` with the contents:
/// For a `myapp` binary with `windows` target and latest release version `1.2.3`,
/// the tarball `myapp.tar.gz` has the contents:
///
/// ```shell
/// myapp.tar/
/// |------- bin/
/// |------- windows-1.2.3-bin/
/// | |--- myapp # <-- executable
/// ```
///
Expand All @@ -278,13 +285,13 @@ impl UpdateBuilder {
/// # use self_update::backends::s3::Update;
/// # fn run() -> Result<(), Box<::std::error::Error>> {
/// Update::configure()
/// .bin_path_in_archive("bin/myapp")
/// .bin_path_in_archive("{{ target }}-{{ version }}-bin/{{ bin }}")
/// # .build()?;
/// # Ok(())
/// # }
/// ```
pub fn bin_path_in_archive(&mut self, bin_path: &str) -> &mut Self {
self.bin_path_in_archive = Some(PathBuf::from(bin_path));
self.bin_path_in_archive = Some(bin_path.to_owned());
self
}

Expand Down Expand Up @@ -366,8 +373,8 @@ impl UpdateBuilder {
bail!(Error::Config, "`bin_name` required")
},
bin_install_path,
bin_path_in_archive: if let Some(ref path) = self.bin_path_in_archive {
path.to_owned()
bin_path_in_archive: if let Some(ref bin_path) = self.bin_path_in_archive {
bin_path.to_owned()
} else {
bail!(Error::Config, "`bin_path_in_archive` required")
},
Expand Down Expand Up @@ -401,7 +408,7 @@ pub struct Update {
target_version: Option<String>,
bin_name: String,
bin_install_path: PathBuf,
bin_path_in_archive: PathBuf,
bin_path_in_archive: String,
show_download_progress: bool,
show_output: bool,
no_confirm: bool,
Expand Down Expand Up @@ -487,7 +494,7 @@ impl ReleaseUpdate for Update {
self.bin_install_path.clone()
}

fn bin_path_in_archive(&self) -> PathBuf {
fn bin_path_in_archive(&self) -> String {
self.bin_path_in_archive.clone()
}

Expand Down
Loading

0 comments on commit ec8e626

Please sign in to comment.