build.rs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. extern crate clap;
  2. extern crate libimagrt;
  3. extern crate libimagentrytag;
  4. extern crate libimagutil;
  5. #[macro_use] extern crate version;
  6. use clap::Shell;
  7. use libimagrt::runtime::Runtime;
  8. /// This macro generates mods with the given '$modulename',
  9. /// whose content is the file given with `$path`.
  10. /// In this case, It is used specifically to include the
  11. /// `ui.rs` files of the imag binaries.
  12. /// The imag project (accidentally?) followed the convention
  13. /// to write a `ui.rs` containing the function
  14. /// `fn build_ui(app : App) -> App`.
  15. /// This macro allows us to use the same named functions by
  16. /// putting them each into their own module.
  17. macro_rules! gen_mods_buildui {
  18. ($(($path:expr, $modulename:ident)$(,)*)*) => (
  19. $(
  20. mod $modulename {
  21. include!($path);
  22. }
  23. )*
  24. )
  25. }
  26. /// This macro reduces boilerplate code.
  27. ///
  28. /// For example: `build_subcommand!("counter", imagcounter)`
  29. /// will result in the following code:
  30. /// ```ignore
  31. /// imagcounter::build_ui(Runtime::get_default_cli_builder(
  32. /// "counter",
  33. /// &version!()[..],
  34. /// "counter"))
  35. /// ```
  36. /// As for the `&version!()[..]` part, it does not matter
  37. /// which version the subcommand is getting here, as the
  38. /// output of this script is a completion script, which
  39. /// does not contain information about the version at all.
  40. macro_rules! build_subcommand {
  41. ($name:expr, $module:ident) => (
  42. $module::build_ui(Runtime::get_default_cli_builder(
  43. $name,
  44. &version!()[..],
  45. $name))
  46. )
  47. }
  48. // Actually generates the module.
  49. gen_mods_buildui!(
  50. ("../imag-link/src/ui.rs", imaglink),
  51. ("../imag-notes/src/ui.rs", imagnotes),
  52. ("../imag-ref/src/ui.rs", imagref),
  53. ("../imag-store/src/ui.rs", imagstore),
  54. ("../imag-tag/src/ui.rs", imagtag),
  55. ("../imag-view/src/ui.rs", imagview)
  56. );
  57. fn main() {
  58. // Make the `imag`-App...
  59. let mut app = Runtime::get_default_cli_builder(
  60. "imag",
  61. &version!()[..],
  62. "imag")
  63. // and add all the subapps as subcommands.
  64. .subcommand(build_subcommand!("link", imaglink))
  65. .subcommand(build_subcommand!("notes", imagnotes))
  66. .subcommand(build_subcommand!("ref", imagref))
  67. .subcommand(build_subcommand!("store", imagstore))
  68. .subcommand(build_subcommand!("tag", imagtag))
  69. .subcommand(build_subcommand!("view", imagview));
  70. let outdir = std::env::var("OUT_DIR").unwrap();
  71. // Actually generates the completion files
  72. app.gen_completions("imag", Shell::Bash, outdir.clone());
  73. app.gen_completions("imag", Shell::Fish, outdir.clone());
  74. app.gen_completions("imag", Shell::Zsh, outdir);
  75. }