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