build.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. // imag - the personal information management suite for the commandline
  3. // Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
  4. //
  5. // This library is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU Lesser General Public
  7. // License as published by the Free Software Foundation; version
  8. // 2.1 of the License.
  9. //
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. // Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public
  16. // License along with this library; if not, write to the Free Software
  17. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. //
  19. extern crate clap;
  20. extern crate libimagrt;
  21. extern crate libimagentrytag;
  22. extern crate libimagutil;
  23. #[macro_use] extern crate version;
  24. use clap::Shell;
  25. use libimagrt::runtime::Runtime;
  26. /// This macro generates mods with the given '$modulename',
  27. /// whose content is the file given with `$path`.
  28. /// In this case, It is used specifically to include the
  29. /// `ui.rs` files of the imag binaries.
  30. /// The imag project (accidentally?) followed the convention
  31. /// to write a `ui.rs` containing the function
  32. /// `fn build_ui(app : App) -> App`.
  33. /// This macro allows us to use the same named functions by
  34. /// putting them each into their own module.
  35. macro_rules! gen_mods_buildui {
  36. ($(($path:expr, $modulename:ident)$(,)*)*) => (
  37. $(
  38. mod $modulename {
  39. include!($path);
  40. }
  41. )*
  42. )
  43. }
  44. /// This macro reduces boilerplate code.
  45. ///
  46. /// For example: `build_subcommand!("counter", imagcounter)`
  47. /// will result in the following code:
  48. /// ```ignore
  49. /// imagcounter::build_ui(Runtime::get_default_cli_builder(
  50. /// "counter",
  51. /// &version!()[..],
  52. /// "counter"))
  53. /// ```
  54. /// As for the `&version!()[..]` part, it does not matter
  55. /// which version the subcommand is getting here, as the
  56. /// output of this script is a completion script, which
  57. /// does not contain information about the version at all.
  58. macro_rules! build_subcommand {
  59. ($name:expr, $module:ident) => (
  60. $module::build_ui(Runtime::get_default_cli_builder(
  61. $name,
  62. &version!()[..],
  63. $name))
  64. )
  65. }
  66. // Actually generates the module.
  67. gen_mods_buildui!(
  68. ("../imag-link/src/ui.rs", imaglink),
  69. ("../imag-notes/src/ui.rs", imagnotes),
  70. ("../imag-ref/src/ui.rs", imagref),
  71. ("../imag-store/src/ui.rs", imagstore),
  72. ("../imag-tag/src/ui.rs", imagtag),
  73. ("../imag-view/src/ui.rs", imagview)
  74. );
  75. fn main() {
  76. // Make the `imag`-App...
  77. let mut app = Runtime::get_default_cli_builder(
  78. "imag",
  79. &version!()[..],
  80. "imag")
  81. // and add all the subapps as subcommands.
  82. .subcommand(build_subcommand!("link", imaglink))
  83. .subcommand(build_subcommand!("notes", imagnotes))
  84. .subcommand(build_subcommand!("ref", imagref))
  85. .subcommand(build_subcommand!("store", imagstore))
  86. .subcommand(build_subcommand!("tag", imagtag))
  87. .subcommand(build_subcommand!("view", imagview));
  88. let outdir = std::env::var("OUT_DIR").unwrap();
  89. // Actually generates the completion files
  90. app.gen_completions("imag", Shell::Bash, outdir.clone());
  91. app.gen_completions("imag", Shell::Fish, outdir.clone());
  92. app.gen_completions("imag", Shell::Zsh, outdir);
  93. }