(index<- ) ./librustdoc/plugins.rs
git branch: * master 5200215 auto merge of #14035 : alexcrichton/rust/experimental, r=huonw
modified: Sat Apr 19 11:22:39 2014
1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use clean;
12
13 use dl = std::unstable::dynamic_lib;
14 use serialize::json;
15 use std::strbuf::StrBuf;
16
17 pub type PluginJson = Option<(~str, json::Json)>;
18 pub type PluginResult = (clean::Crate, PluginJson);
19 pub type PluginCallback = fn (clean::Crate) -> PluginResult;
20
21 /// Manages loading and running of plugins
22 pub struct PluginManager {
23 dylibs: Vec<dl::DynamicLibrary> ,
24 callbacks: Vec<PluginCallback> ,
25 /// The directory plugins will be loaded from
26 pub prefix: Path,
27 }
28
29 impl PluginManager {
30 /// Create a new plugin manager
31 pub fn new(prefix: Path) -> PluginManager {
32 PluginManager {
33 dylibs: Vec::new(),
34 callbacks: Vec::new(),
35 prefix: prefix,
36 }
37 }
38
39 /// Load a plugin with the given name.
40 ///
41 /// Turns `name` into the proper dynamic library filename for the given
42 /// platform. On windows, it turns into name.dll, on OS X, name.dylib, and
43 /// elsewhere, libname.so.
44 pub fn load_plugin(&mut self, name: ~str) {
45 let x = self.prefix.join(libname(name));
46 let lib_result = dl::DynamicLibrary::open(Some(&x));
47 let lib = lib_result.unwrap();
48 let plugin = unsafe { lib.symbol("rustdoc_plugin_entrypoint") }.unwrap();
49 self.dylibs.push(lib);
50 self.callbacks.push(plugin);
51 }
52
53 /// Load a normal Rust function as a plugin.
54 ///
55 /// This is to run passes over the cleaned crate. Plugins run this way
56 /// correspond to the A-aux tag on Github.
57 pub fn add_plugin(&mut self, plugin: PluginCallback) {
58 self.callbacks.push(plugin);
59 }
60 /// Run all the loaded plugins over the crate, returning their results
61 pub fn run_plugins(&self, krate: clean::Crate) -> (clean::Crate, Vec<PluginJson> ) {
62 let mut out_json = Vec::new();
63 let mut krate = krate;
64 for &callback in self.callbacks.iter() {
65 let (c, res) = callback(krate);
66 krate = c;
67 out_json.push(res);
68 }
69 (krate, out_json)
70 }
71 }
72
73 #[cfg(target_os="win32")]
74 fn libname(n: ~str) -> ~str {
75 let mut n = StrBuf::from_owned_str(n);
76 n.push_str(".dll");
77 n.into_owned()
78 }
79
80 #[cfg(target_os="macos")]
81 fn libname(n: ~str) -> ~str {
82 let mut n = StrBuf::from_owned_str(n);
83 n.push_str(".dylib");
84 n.into_owned()
85 }
86
87 #[cfg(not(target_os="win32"), not(target_os="macos"))]
88 fn libname(n: ~str) -> ~str {
89 let mut i = StrBuf::from_str("lib");
90 i.push_str(n);
91 i.push_str(".so");
92 i.into_owned()
93 }
librustdoc/plugins.rs:21:43-21:43 -struct- definition:
/// Manages loading and running of plugins
pub struct PluginManager {
dylibs: Vec<dl::DynamicLibrary> ,
references:- 330: /// Create a new plugin manager
31: pub fn new(prefix: Path) -> PluginManager {
32: PluginManager {
33: dylibs: Vec::new(),
librustdoc/plugins.rs:18:52-18:52 -NK_AS_STR_TODO- definition:
pub type PluginResult = (clean::Crate, PluginJson);
pub type PluginCallback = fn (clean::Crate) -> PluginResult;
/// Manages loading and running of plugins
references:- 223: dylibs: Vec<dl::DynamicLibrary> ,
24: callbacks: Vec<PluginCallback> ,
25: /// The directory plugins will be loaded from
--
56: /// correspond to the A-aux tag on Github.
57: pub fn add_plugin(&mut self, plugin: PluginCallback) {
58: self.callbacks.push(plugin);
librustdoc/plugins.rs:17:50-17:50 -NK_AS_STR_TODO- definition:
pub type PluginJson = Option<(~str, json::Json)>;
pub type PluginResult = (clean::Crate, PluginJson);
pub type PluginCallback = fn (clean::Crate) -> PluginResult;
references:- 618: pub type PluginResult = (clean::Crate, PluginJson);
19: pub type PluginCallback = fn (clean::Crate) -> PluginResult;
librustdoc/lib.rs:
60: type Pass = (&'static str, // name
61: fn(clean::Crate) -> plugins::PluginResult, // fn
62: &'static str); // description
librustdoc/passes.rs:
242: pub fn collapse_docs(krate: clean::Crate) -> plugins::PluginResult {
243: struct Collapser;
librustdoc/plugins.rs:16:1-16:1 -NK_AS_STR_TODO- definition:
pub type PluginJson = Option<(~str, json::Json)>;
pub type PluginResult = (clean::Crate, PluginJson);
pub type PluginCallback = fn (clean::Crate) -> PluginResult;
references:- 417: pub type PluginJson = Option<(~str, json::Json)>;
18: pub type PluginResult = (clean::Crate, PluginJson);
19: pub type PluginCallback = fn (clean::Crate) -> PluginResult;
--
60: /// Run all the loaded plugins over the crate, returning their results
61: pub fn run_plugins(&self, krate: clean::Crate) -> (clean::Crate, Vec<PluginJson> ) {
62: let mut out_json = Vec::new();
librustdoc/lib.rs:
85: type Output = (clean::Crate, Vec<plugins::PluginJson> );
--
373: /// destination.
374: fn json_output(krate: clean::Crate, res: Vec<plugins::PluginJson> ,
375: dst: Path) -> io::IoResult<()> {