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.
98 lines
3.4 KiB
98 lines
3.4 KiB
use regex::Regex;
|
|
use std::error::Error;
|
|
use std::fmt;
|
|
use std::fs::File as FsFile;
|
|
use std::io;
|
|
use std::io::{BufRead, BufReader};
|
|
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 version = String::new();
|
|
let mut plugin_uri = String::new();
|
|
match FsFile::open(path) {
|
|
Err(e) => {
|
|
// Check if file exists
|
|
return if e.kind() == io::ErrorKind::NotFound {
|
|
Ok(false)
|
|
} else {
|
|
Err(Box::new(e))
|
|
};
|
|
},
|
|
Ok(file) => {
|
|
let mut reader = BufReader::new(file);
|
|
let regex = Regex::new("(?m)^(Plugin URI|Version): (.+)$")?;
|
|
for content in reader.lines() {
|
|
for matches in regex.captures_iter(&(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 source = format!("https://downloads.wordpress.org/plugin/{}.zip", self.name);
|
|
let zip = format!("/tmp/{}.zip", self.name);
|
|
try!(self.command_runner.run_successfully("curl", &[source.as_ref() as &str, "-o", zip.as_ref()]));
|
|
try!(self.command_runner.run_successfully("rm", &["-rf", &self.get_path().to_string_lossy()]));
|
|
self.command_runner.run_successfully("unzip", &[zip.as_ref(), "-d".as_ref(), Path::new(&*self.base).join("wp-content/plugins").as_os_str()])
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|