Skip to content

Commit

Permalink
An option to keep extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
imago-storm committed Apr 9, 2019
1 parent 8d02c54 commit 7269708
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
19 changes: 16 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::io::prelude::*;
use getopts::Options;
use url::Url;
use notify::{RecommendedWatcher, Watcher, RecursiveMode, DebouncedEvent};
use rusty_sentry::updater::{PartialUpdate, guess_plugin_type, PluginType, PluginWizard, PluginGradle};
use rusty_sentry::updater::{PartialUpdate, guess_plugin_type, PluginType, PluginWizard, PluginGradle, UpdateOptions};
use rusty_sentry::ef_client::EFClient;
use serde_xml_rs::deserialize;
use shellexpand::tilde;
Expand All @@ -28,6 +28,7 @@ const USERNAME: &str = "u";
const PASSWORD: &str = "p";
const PATH: &str = "path";
const SID: &str = "sid";
const KEEP_EXTENSIONS: &str = "ke";

#[derive(Deserialize, Debug)]
struct Session {
Expand Down Expand Up @@ -126,6 +127,8 @@ fn main() {
opts.optopt("", PATH, "Provide path to the plugin folder", "PATH");
opts.optopt(PASSWORD, "password", "provide password for the server to connect", "PASSWORD");
opts.optopt("", SID, "provide session id to connect", "SID");
opts.optflag("k", "keep-extensions", "keeps file extensions");

let matches = match opts.parse(&args[1..]) {
Ok(m) => { m },
Err(f) => {
Expand Down Expand Up @@ -159,10 +162,20 @@ fn main() {
}
};

let ke: bool = if matches.opt_present("k") {
true
} else {
false
};

let options = UpdateOptions {
keep_extensions: ke
};

let plugin_type = guess_plugin_type(&path);
let result = match plugin_type {
Ok(PluginType::PluginWizard) => {
let updater = PluginWizard::build(&path, ef_client);
let updater = PluginWizard::build(&path, ef_client, options);
match updater {
Ok(upd) => watch(&path, &upd),
Err(e) => {
Expand All @@ -172,7 +185,7 @@ fn main() {
}
},
Ok(PluginType::Gradle) => {
let updater = PluginGradle::build(&path, ef_client);
let updater = PluginGradle::build(&path, ef_client, options);
match updater {
Ok(upd) => watch(&path, &upd),
Err(e) => {
Expand Down
28 changes: 22 additions & 6 deletions src/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,26 @@ pub struct PluginMeta {
pub struct PluginWizard {
meta: PluginMeta,
ef_client: EFClient,
update_options: UpdateOptions,
}

#[derive(Debug)]
pub struct PluginGradle {
meta: PluginMeta,
manifest_path: PathBuf,
ef_client: EFClient,
update_options: UpdateOptions,
}

#[derive(Debug)]
pub struct UpdateOptions {
pub keep_extensions: bool
}

pub trait PartialUpdate {
type PluginType;
fn update(&self, file: &PathBuf) -> Result<(), Error>;
fn build(plugin_folder: &PathBuf, ef_client: EFClient) -> Result<Self::PluginType, Error>;
fn build(plugin_folder: &PathBuf, ef_client: EFClient, options: UpdateOptions) -> Result<Self::PluginType, Error>;

fn get_file_content(&self, path: &Path, meta: &PluginMeta) -> Result<String, Error> {
let res = File::open(path);
Expand Down Expand Up @@ -109,7 +116,7 @@ impl PartialUpdate for PluginGradle {
Ok(())
}

fn build(folder: &PathBuf, ef_client: EFClient) -> Result<Self::PluginType, Error> {
fn build(folder: &PathBuf, ef_client: EFClient, options: UpdateOptions) -> Result<Self::PluginType, Error> {
println!("Reading gradle metadata\n");
let mut gradle_path = folder.clone();
gradle_path.push("build.gradle");
Expand Down Expand Up @@ -139,6 +146,7 @@ impl PartialUpdate for PluginGradle {
meta: metadata,
manifest_path,
ef_client,
update_options: options
})
}
}
Expand Down Expand Up @@ -238,14 +246,17 @@ impl PartialUpdate for PluginWizard {
Ok(())
}

fn build(folder: &PathBuf, ef_client: EFClient) -> Result<Self::PluginType, Error> {
fn build(folder: &PathBuf, ef_client: EFClient, options: UpdateOptions) -> Result<Self::PluginType, Error> {
let metadata_path = folder.join("META-INF").join("plugin.xml");
println!("Trying {}", metadata_path.to_str().unwrap());
let mut f = File::open(&metadata_path)?;
let mut contents = String::new();
f.read_to_string(&mut contents)?;
println!("Contents: {}", contents);
let plugin: Result<PluginMETAINF, serde_xml_rs::Error> = deserialize(contents.as_bytes());
if options.keep_extensions {
println!("Keeping files extensions in the property names");
}
match plugin {
Ok(p) => {
let metadata = PluginMeta{
Expand All @@ -255,7 +266,8 @@ impl PartialUpdate for PluginWizard {
};
Ok(PluginWizard{
meta: metadata,
ef_client
ef_client,
update_options: options
})
}
Err(error) => Err(Error::new(ErrorKind::Other, format!("Cannot parse {}: {}", metadata_path.display(), error)))
Expand All @@ -277,8 +289,12 @@ impl PluginWizard {
},
Ok(path) => path
};
let re = Regex::new("\\..+$").unwrap();
let mut property_name: String = String::from(re.replace_all(path.to_str().expect("Cannot remove file extension from property"), ""));
let mut property_name: String = String::from(path.to_str().expect("Cannot convert path to str"));
if !self.update_options.keep_extensions {
let re = Regex::new("\\..+$").unwrap();
property_name = String::from(re.replace_all(&property_name, ""))
}

let re = Regex::new("\\\\").expect("Cannot compile regexp");
property_name = String::from(re.replace_all(&property_name, "/"));
let plugin_name = &self.meta.key;
Expand Down

0 comments on commit 7269708

Please sign in to comment.