]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/plugins.rs
2e7c4224ee186ef9c9f5077df3db64c1b83e53b6
[rust.git] / src / librustdoc / plugins.rs
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 serialize::json;
14 use dl = std::unstable::dynamic_lib;
15
16 pub type PluginJson = Option<(~str, json::Json)>;
17 pub type PluginResult = (clean::Crate, PluginJson);
18 pub type PluginCallback = fn (clean::Crate) -> PluginResult;
19
20 /// Manages loading and running of plugins
21 pub struct PluginManager {
22     priv dylibs: ~[dl::DynamicLibrary],
23     priv callbacks: ~[PluginCallback],
24     /// The directory plugins will be loaded from
25     prefix: Path,
26 }
27
28 impl PluginManager {
29     /// Create a new plugin manager
30     pub fn new(prefix: Path) -> PluginManager {
31         PluginManager {
32             dylibs: ~[],
33             callbacks: ~[],
34             prefix: prefix,
35         }
36     }
37
38     /// Load a plugin with the given name.
39     ///
40     /// Turns `name` into the proper dynamic library filename for the given
41     /// platform. On windows, it turns into name.dll, on OS X, name.dylib, and
42     /// elsewhere, libname.so.
43     pub fn load_plugin(&mut self, name: ~str) {
44         let x = self.prefix.join(libname(name));
45         let lib_result = dl::DynamicLibrary::open(Some(&x));
46         let lib = lib_result.unwrap();
47         let plugin = unsafe { lib.symbol("rustdoc_plugin_entrypoint") }.unwrap();
48         self.dylibs.push(lib);
49         self.callbacks.push(plugin);
50     }
51
52     /// Load a normal Rust function as a plugin.
53     ///
54     /// This is to run passes over the cleaned crate. Plugins run this way
55     /// correspond to the A-aux tag on Github.
56     pub fn add_plugin(&mut self, plugin: PluginCallback) {
57         self.callbacks.push(plugin);
58     }
59     /// Run all the loaded plugins over the crate, returning their results
60     pub fn run_plugins(&self, krate: clean::Crate) -> (clean::Crate, ~[PluginJson]) {
61         let mut out_json = ~[];
62         let mut krate = krate;
63         for &callback in self.callbacks.iter() {
64             let (c, res) = callback(krate);
65             krate = c;
66             out_json.push(res);
67         }
68         (krate, out_json)
69     }
70 }
71
72 #[cfg(target_os="win32")]
73 fn libname(mut n: ~str) -> ~str {
74     n.push_str(".dll");
75     n
76 }
77
78 #[cfg(target_os="macos")]
79 fn libname(mut n: ~str) -> ~str {
80     n.push_str(".dylib");
81     n
82 }
83
84 #[cfg(not(target_os="win32"), not(target_os="macos"))]
85 fn libname(n: ~str) -> ~str {
86     let mut i = ~"lib";
87     i.push_str(n);
88     i.push_str(".so");
89     i
90 }