Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: Add support for tauri configuration files #1376

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
61 changes: 61 additions & 0 deletions crates/knope-versioning/src/versioned_file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use pubspec::PubSpec;
use pyproject::PyProject;
use relative_path::RelativePathBuf;
use serde::{Serialize, Serializer};
use tauri_conf_json::TauriConfJson;

use crate::{
Action,
Expand All @@ -24,6 +25,7 @@ mod maven_pom;
mod package_json;
mod pubspec;
mod pyproject;
mod tauri_conf_json;

#[derive(Clone, Debug)]
pub enum VersionedFile {
Expand All @@ -35,6 +37,10 @@ pub enum VersionedFile {
PackageJson(PackageJson),
PyProject(PyProject),
MavenPom(MavenPom),
TauriConf(TauriConfJson),
TauriMacosConf(TauriConfJson),
TauriWindowsConf(TauriConfJson),
TauriLinuxConf(TauriConfJson),
}

impl VersionedFile {
Expand Down Expand Up @@ -74,6 +80,18 @@ impl VersionedFile {
Format::MavenPom => MavenPom::new(config.as_path(), content)
.map(VersionedFile::MavenPom)
.map_err(Error::MavenPom),
Format::TauriConf => TauriConfJson::new(config.as_path(), content)
.map(VersionedFile::TauriConf)
.map_err(Error::TauriConfJson),
Format::TauriMacosConf => TauriConfJson::new(config.as_path(), content)
.map(VersionedFile::TauriMacosConf)
.map_err(Error::TauriConfJson),
Format::TauriWindowsConf => TauriConfJson::new(config.as_path(), content)
.map(VersionedFile::TauriWindowsConf)
.map_err(Error::TauriConfJson),
Format::TauriLinuxConf => TauriConfJson::new(config.as_path(), content)
.map(VersionedFile::TauriLinuxConf)
.map_err(Error::TauriConfJson),
}
}

Expand All @@ -88,6 +106,10 @@ impl VersionedFile {
VersionedFile::GoMod(gomod) => gomod.get_path(),
VersionedFile::PackageJson(package_json) => package_json.get_path(),
VersionedFile::MavenPom(maven_pom) => &maven_pom.path,
VersionedFile::TauriConf(tauri_conf) => &tauri_conf.get_path(),
VersionedFile::TauriMacosConf(tauri_conf) => &tauri_conf.get_path(),
VersionedFile::TauriWindowsConf(tauri_conf) => &tauri_conf.get_path(),
VersionedFile::TauriLinuxConf(tauri_conf) => &tauri_conf.get_path(),
}
}

Expand All @@ -106,6 +128,10 @@ impl VersionedFile {
VersionedFile::GoMod(gomod) => Ok(gomod.get_version().clone()),
VersionedFile::PackageJson(package_json) => Ok(package_json.get_version().clone()),
VersionedFile::MavenPom(maven_pom) => maven_pom.get_version().map_err(Error::MavenPom),
VersionedFile::TauriConf(tauri_conf) => Ok(tauri_conf.get_version().clone()),
VersionedFile::TauriMacosConf(tauri_conf) => Ok(tauri_conf.get_version().clone()),
VersionedFile::TauriWindowsConf(tauri_conf) => Ok(tauri_conf.get_version().clone()),
VersionedFile::TauriLinuxConf(tauri_conf) => Ok(tauri_conf.get_version().clone()),
}
}

Expand Down Expand Up @@ -144,6 +170,22 @@ impl VersionedFile {
.set_version(new_version)
.map_err(SetError::MavenPom)
.map(Self::MavenPom),
Self::TauriConf(tauri_conf) => tauri_conf
.set_version(new_version)
.map_err(SetError::Json)
.map(Self::TauriConf),
Self::TauriMacosConf(tauri_conf) => tauri_conf
.set_version(new_version)
.map_err(SetError::Json)
.map(Self::TauriMacosConf),
Self::TauriWindowsConf(tauri_conf) => tauri_conf
.set_version(new_version)
.map_err(SetError::Json)
.map(Self::TauriWindowsConf),
Self::TauriLinuxConf(tauri_conf) => tauri_conf
.set_version(new_version)
.map_err(SetError::Json)
.map(Self::TauriLinuxConf),
}
}

Expand All @@ -157,6 +199,10 @@ impl VersionedFile {
Self::GoMod(gomod) => gomod.write().map(Two),
Self::PackageJson(package_json) => package_json.write().map(Single),
Self::MavenPom(maven_pom) => maven_pom.write().map(Single),
Self::TauriConf(tauri_conf) => tauri_conf.write().map(Single),
Self::TauriMacosConf(tauri_conf) => tauri_conf.write().map(Single),
Self::TauriWindowsConf(tauri_conf) => tauri_conf.write().map(Single),
Self::TauriLinuxConf(tauri_conf) => tauri_conf.write().map(Single),
}
}
}
Expand Down Expand Up @@ -232,6 +278,9 @@ pub enum Error {
#[error(transparent)]
#[cfg_attr(feature = "miette", diagnostic(transparent))]
MavenPom(#[from] maven_pom::Error),
#[error(transparent)]
#[cfg_attr(feature = "miette", diagnostic(transparent))]
TauriConfJson(#[from] tauri_conf_json::Error),
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
Expand All @@ -244,6 +293,10 @@ pub(crate) enum Format {
GoMod,
PackageJson,
MavenPom,
TauriConf,
TauriMacosConf,
TauriWindowsConf,
TauriLinuxConf,
}

impl Format {
Expand All @@ -257,6 +310,10 @@ impl Format {
Format::GoMod => "go.mod",
Format::PackageJson => "package.json",
Format::MavenPom => "pom.xml",
Format::TauriConf => "tauri.conf.json",
Format::TauriMacosConf => "tauri.macos.conf.json",
Format::TauriWindowsConf => "tauri.windows.conf.json",
Format::TauriLinuxConf => "tauri.linux.conf.json",
}
}

Expand All @@ -270,6 +327,10 @@ impl Format {
"go.mod" => Some(Format::GoMod),
"package.json" => Some(Format::PackageJson),
"pom.xml" => Some(Format::MavenPom),
"tauri.conf.json" => Some(Format::TauriConf),
"tauri.macos.conf.json" => Some(Format::TauriMacosConf),
"tauri.windows.conf.json" => Some(Format::TauriWindowsConf),
"tauri.linux.conf.json" => Some(Format::TauriLinuxConf),
_ => None,
}
}
Expand Down
163 changes: 163 additions & 0 deletions crates/knope-versioning/src/versioned_file/tauri_conf_json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
#[cfg(feature = "miette")]
use miette::Diagnostic;
use relative_path::RelativePathBuf;
use serde::Deserialize;
use serde_json::{Map, Value};
use thiserror::Error;

use crate::{action::Action, semver::Version};

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TauriConfJson {
path: RelativePathBuf,
raw: String,
parsed: Json,
diff: Option<String>,
}

impl TauriConfJson {
pub(crate) fn new(path: RelativePathBuf, content: String) -> Result<Self, Error> {
match serde_json::from_str(&content) {
Ok(parsed) => Ok(TauriConfJson {
path,
raw: content,
parsed,
diff: None,
}),
Err(err) => Err(Error::Deserialize { path, source: err }),
}
}

pub(crate) fn get_version(&self) -> &Version {
&self.parsed.version
}

pub(crate) fn get_path(&self) -> &RelativePathBuf {
&self.path
}

pub(crate) fn set_version(mut self, new_version: &Version) -> serde_json::Result<Self> {
let mut json = serde_json::from_str::<Map<String, Value>>(&self.raw)?;
json.insert(
"version".to_string(),
Value::String(new_version.to_string()),
);
self.raw = serde_json::to_string_pretty(&json)?;
self.diff = Some(new_version.to_string());
Ok(self)
}

pub(crate) fn write(self) -> Option<Action> {
self.diff.map(|diff| Action::WriteToFile {
path: self.path,
content: self.raw,
diff,
})
}
}

#[derive(Debug, Error)]
#[cfg_attr(feature = "miette", derive(Diagnostic))]
pub enum Error {
#[error("Error deserializing {path}: {source}")]
#[cfg_attr(
feature = "miette",
diagnostic(
code(tauri_conf_json::deserialize),
help(
"knope expects the tauri.conf.json file to be an object with a top level `version` property"
),
url("https://knope.tech/reference/config-file/packages/#tauri-conf")
)
)]
Deserialize {
path: RelativePathBuf,
#[source]
source: serde_json::Error,
},
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
struct Json {
version: Version,
}

#[cfg(test)]
mod tests {
use std::str::FromStr;

use pretty_assertions::assert_eq;

use super::*;

#[test]
fn test_get_version() {
let content = r#"{
"productName": "tester",
"version": "0.1.0-rc.0"
}"#;

assert_eq!(
TauriConfJson::new(RelativePathBuf::new(), content.to_string())
.unwrap()
.get_version(),
&Version::from_str("0.1.0-rc.0").unwrap()
);
}

#[test]
fn test_set_version() {
let content = r#"{
"productName": "tester",
"version": "0.1.0-rc.0"
}"#;

let new = TauriConfJson::new(RelativePathBuf::new(), content.to_string())
.unwrap()
.set_version(&Version::from_str("1.2.3-rc.4").unwrap())
.unwrap()
.write()
.expect("diff to write");

let expected = r#"{
"productName": "tester",
"version": "1.2.3-rc.4"
}"#
.to_string();
let expected = Action::WriteToFile {
path: RelativePathBuf::new(),
content: expected,
diff: "1.2.3-rc.4".to_string(),
};
assert_eq!(new, expected);
}

#[test]
fn retain_property_order() {
let content = r#"{
"productName": "tester",
"version": "0.1.0-rc.0",
"identifier": "com.knope.tester"
}"#;

let new = TauriConfJson::new(RelativePathBuf::new(), content.to_string())
.unwrap()
.set_version(&Version::from_str("1.2.3-rc.4").unwrap())
.unwrap()
.write()
.expect("diff to write");

let expected = r#"{
"name": "tester",
"version": "1.2.3-rc.4",
"identifier": "com.knope.tester"
}"#
.to_string();
let expected = Action::WriteToFile {
path: RelativePathBuf::new(),
content: expected,
diff: "1.2.3-rc.4".to_string(),
};
assert_eq!(new, expected);
}
}
1 change: 1 addition & 0 deletions crates/knope/tests/prepare_release/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ mod pyproject_toml;
mod release_after_prerelease;
mod scopes;
mod second_prerelease;
mod tauri_conf_json;
mod unknown_versioned_file_format;
mod verbose;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Would add the following to Cargo.toml: version = 0.2.0
Would add the following to package.json: 0.2.0
Would add the following to tauri.conf.json: 0.2.0
Would add the following to tauri.macos.conf.json: 0.2.0
Would add files to git:
Cargo.toml
package.json
tauri.conf.json
tauri.macos.conf.json
24 changes: 24 additions & 0 deletions crates/knope/tests/prepare_release/tauri_conf_json/in/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "knope-test"
version = "0.1.0"
description = "A Tauri App"
authors = ["you"]
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
# The `_lib` suffix may seem redundant but it is necessary
# to make the lib name unique and wouldn't conflict with the bin name.
# This seems to be only an issue on Windows, see https://github.com/rust-lang/cargo/issues/8519
name = "knope_test_lib"
crate-type = ["staticlib", "cdylib", "rlib"]

[build-dependencies]
tauri-build = { version = "2", features = [] }

[dependencies]
tauri = { version = "2", features = [] }
tauri-plugin-opener = "2"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
13 changes: 13 additions & 0 deletions crates/knope/tests/prepare_release/tauri_conf_json/in/knope.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
versioned_files = [
"Cargo.toml",
"package.json",
"tauri.conf.json",
"tauri.macos.conf.json",
]

[[workflows]]
name = "release"

[[workflows.steps]]
type = "PrepareRelease"
21 changes: 21 additions & 0 deletions crates/knope/tests/prepare_release/tauri_conf_json/in/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "knope-test",
"private": true,
"version": "0.1.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview",
"tauri": "tauri"
},
"dependencies": {
"@tauri-apps/api": "^2",
"@tauri-apps/plugin-opener": "^2"
},
"devDependencies": {
"@tauri-apps/cli": "^2",
"vite": "^6.0.3",
"typescript": "~5.6.2"
}
}
Loading
Loading