You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
3.1 KiB
84 lines
3.1 KiB
use regex::Regex;
|
|
use std::error::Error;
|
|
use std::fmt;
|
|
use std::fs::File as FsFile;
|
|
use std::io::Read;
|
|
use std::ops::Deref;
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use command_runner::CommandRunner;
|
|
use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
|
|
use resources::Resource;
|
|
|
|
pub struct WordpressPlugin<'a, C, R> where C: Deref<Target=str> + fmt::Display, R: 'a + CommandRunner {
|
|
base: C,
|
|
name: C,
|
|
command_runner: &'a R
|
|
}
|
|
|
|
impl<'a, C, R> WordpressPlugin<'a, C, R> where C: Deref<Target=str> + fmt::Display, R: CommandRunner {
|
|
pub fn new(base: C, name: C, command_runner: &'a R) -> Self {
|
|
WordpressPlugin {
|
|
base: base,
|
|
name: name,
|
|
command_runner: command_runner
|
|
}
|
|
}
|
|
|
|
fn get_path(&self) -> PathBuf {
|
|
Path::new(&*self.base).join("wp-content/plugins").join(&*self.name)
|
|
}
|
|
}
|
|
|
|
impl<'a, C, R> Symbol for WordpressPlugin<'a, C, R> where C: Deref<Target=str> + fmt::Display, R: CommandRunner {
|
|
fn target_reached(&self) -> Result<bool, Box<Error>> {
|
|
if !self.get_path().exists() {
|
|
return Ok(false);
|
|
}
|
|
let path = self.get_path().join(self.name.to_string() + ".php");
|
|
let mut file = try!(FsFile::open(path));
|
|
let mut file_content = String::new();
|
|
try!(file.read_to_string(&mut file_content));
|
|
let mut version = String::new();
|
|
let mut plugin_uri = String::new();
|
|
let regex = try!(Regex::new("(?m)^(Plugin URI|Version): (.+)$"));
|
|
for matches in regex.captures_iter(&file_content) {
|
|
if &matches[1] == "Plugin URI" {
|
|
plugin_uri = matches[2].to_string();
|
|
} else {
|
|
version = matches[2].to_string();
|
|
}
|
|
}
|
|
let upstream = try!(self.command_runner.get_output("curl", &["--form", &format!(r###"plugins={{"plugins":{{"{0}/{0}.php":{{"Version":"{1}", "PluginURI":"{2}"}}}}}}"###, self.name, version, plugin_uri), "https://api.wordpress.org/plugins/update-check/1.1/"]));
|
|
Ok(try!(String::from_utf8(upstream)).contains(r###""plugins":[]"###))
|
|
}
|
|
|
|
fn execute(&self) -> Result<(), Box<Error>> {
|
|
let zip = format!("/tmp/{}.zip", self.name);
|
|
try!(self.command_runner.run_successfully("curl", &[&format!("https://downloads.wordpress.org/plugin/{}.zip", self.name), "-o", &zip]));
|
|
try!(self.command_runner.run_successfully("rm", &["-rf", &self.get_path().to_string_lossy()]));
|
|
self.command_runner.run_successfully("unzip", &[&zip, "-d", &Path::new(&*self.base).join("wp-content/plugins").to_string_lossy()])
|
|
}
|
|
|
|
fn get_prerequisites(&self) -> Vec<Resource> {
|
|
match self.get_path().parent() {
|
|
Some(p) => vec![ Resource::new("dir", p.to_string_lossy()) ],
|
|
None => vec![]
|
|
}
|
|
}
|
|
|
|
fn as_action<'b>(&'b self, runner: &'b SymbolRunner) -> Box<Action + 'b> {
|
|
Box::new(SymbolAction::new(runner, self))
|
|
}
|
|
|
|
fn into_action<'b>(self: Box<Self>, runner: &'b SymbolRunner) -> Box<Action + 'b> where Self: 'b {
|
|
Box::new(OwnedSymbolAction::new(runner, *self))
|
|
}
|
|
}
|
|
|
|
impl<'a, C, R> fmt::Display for WordpressPlugin<'a, C, R> where C: Deref<Target=str> + fmt::Display, R: CommandRunner {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{
|
|
write!(f, "WordpressPlugin {}", self.name)
|
|
}
|
|
}
|
|
|