Skip to content

Commit

Permalink
feat: add ability to store different versions of plugin
Browse files Browse the repository at this point in the history
Closes #51, #34
  • Loading branch information
dodokek committed Feb 4, 2025
1 parent 64340fe commit 172d506
Showing 1 changed file with 57 additions and 10 deletions.
67 changes: 57 additions & 10 deletions src/helpers/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,60 @@ fn get_output_path() -> PathBuf {
pub struct Params {}

pub fn main(_params: &Params) {
let out_dir = get_output_path();
let out_manifest_path = Path::new(&out_dir).join("manifest.yaml");
let pkg_version = env::var("CARGO_PKG_VERSION").unwrap();
let pkg_name = env::var("CARGO_PKG_NAME").unwrap();
let plugin_path = out_dir.join(&pkg_name).join(&pkg_version);
let lib_name = format!("lib{}.{}", pkg_name.replace('-', "_"), LIB_EXT);

dir::remove(&plugin_path).unwrap();
fs::create_dir_all(&plugin_path).unwrap();

// Iterate through plugins version to find the latest
// then replace symlinks with actual files
for plugin_version_entry in fs::read_dir(out_dir.join(&pkg_name)).unwrap() {
let plugin_version_path = plugin_version_entry.unwrap().path();
if !plugin_version_path.is_dir() {
continue;
}

for plugin_artefact in fs::read_dir(&plugin_version_path).unwrap() {
let entry = plugin_artefact.unwrap();
if entry.file_type().unwrap().is_symlink() {
// Need to remove file before copying in order to properly replace symlink with file
if fs::exists(plugin_version_path.join(&lib_name)).unwrap() {
fs::remove_file(plugin_version_path.join(&lib_name)).unwrap();
}
fs::copy(out_dir.join(&lib_name), plugin_version_path.join(&lib_name)).unwrap();

// Copy manifest file
if fs::exists(plugin_version_path.join("manifest.yaml")).unwrap() {
fs::remove_file(plugin_version_path.join("manifest.yaml")).unwrap();
}
fs::copy(
&out_manifest_path,
plugin_version_path.join("manifest.yaml"),
)
.unwrap();

// Copy migrations folder
if fs::exists(plugin_version_path.join("migrations")).unwrap() {
fs::remove_file(plugin_version_path.join("migrations")).unwrap();
}
dir::copy(
out_dir.join("migrations"),
plugin_version_path,
&dir::CopyOptions::new(),
)
.unwrap();

break;
}
}
}

// Generate new manifest.yaml and migrations from template
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let crate_dir = Path::new(&crate_dir);

Expand Down Expand Up @@ -74,15 +128,11 @@ pub fn main(_params: &Params) {
Err(_) => Vec::new(),
};

let pkg_version = env::var("CARGO_PKG_VERSION").unwrap();

let template_ctx = liquid::object!({
"version": pkg_version,
"migrations": migrations,
});

let out_dir = get_output_path();
let out_manifest_path = Path::new(&out_dir).join("manifest.yaml");
fs::write(&out_manifest_path, template.render(&template_ctx).unwrap()).unwrap();

if !migrations.is_empty() {
Expand All @@ -91,13 +141,8 @@ pub fn main(_params: &Params) {
dir::copy(migrations_dir, &out_dir, &cp_opts).unwrap();
}

// create symbolic link
let pkg_name = env::var("CARGO_PKG_NAME").unwrap();
let plugin_path = out_dir.join(&pkg_name).join(pkg_version);
dir::remove(&plugin_path).unwrap();
fs::create_dir_all(&plugin_path).unwrap();
// Create symlinks for newest plugin version, which would be created after build.rs script
std::os::unix::fs::symlink(out_manifest_path, plugin_path.join("manifest.yaml")).unwrap();
let lib_name = format!("lib{pkg_name}.{LIB_EXT}");
std::os::unix::fs::symlink(out_dir.join(&lib_name), plugin_path.join(lib_name)).unwrap();

if !migrations.is_empty() {
Expand All @@ -109,5 +154,7 @@ pub fn main(_params: &Params) {
}
}

// When plugin version is changed you MUST run cargo update before further use
println!("cargo::rerun-if-changed=Cargo.toml");
println!("cargo::rerun-if-changed={MANIFEST_TEMPLATE_NAME}");
}

0 comments on commit 172d506

Please sign in to comment.