main.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #include "util/tc_option.h"
  17. #include "util/tc_file.h"
  18. #include "tars2android.h"
  19. void usage()
  20. {
  21. cout << "Usage : tars2android [OPTION] tarsfile" << endl;
  22. cout << " tars2android support type: bool byte short int long float double vector map" << endl;
  23. cout << "supported [OPTION]:" << endl;
  24. cout << " --help help,print this" << endl;
  25. cout << " --dir=DIRECTORY generate java file to DIRECTORY(default to current)" << endl;
  26. cout << " --base-package=NAME package prefix, default(com.qq.)" << endl;
  27. cout << " --not-force-array default changed byte vector to byte[], use this for list" << endl;
  28. cout << " --check-default=<true,false> optional field not package(default package)" << endl;
  29. cout << " --extends-package=NAME set the extends package name"<< endl;
  30. cout << " --with-charset set charset, default UTF8" << endl;
  31. cout << " --with-JavaBeanRule support javabeab, default not support" << endl;
  32. cout << " --include=dir1;dir2;dir3 set search path of tars proto files" << endl;
  33. cout << endl;
  34. exit(0);
  35. }
  36. void check(vector<string> &vTars)
  37. {
  38. for(size_t i = 0; i < vTars.size(); i++)
  39. {
  40. string ext = tars::TC_File::extractFileExt(vTars[i]);
  41. if(ext == "tars")
  42. {
  43. if(!tars::TC_File::isFileExist(vTars[i]))
  44. {
  45. cerr << "file '" << vTars[i] << "' not exists" << endl;
  46. usage();
  47. exit(0);
  48. }
  49. }
  50. else
  51. {
  52. cerr << "only support tars file." << endl;
  53. exit(0);
  54. }
  55. }
  56. }
  57. int main(int argc, char* argv[])
  58. {
  59. if(argc < 2)
  60. {
  61. usage();
  62. }
  63. tars::TC_Option option;
  64. option.decode(argc, argv);
  65. vector<string> vTars = option.getSingle();
  66. check(vTars);
  67. if(option.hasParam("help"))
  68. {
  69. usage();
  70. }
  71. Tars2Java t2a;
  72. g_parse->setTars(option.hasParam("with-tars"));
  73. if(option.getValue("dir") != "")
  74. {
  75. t2a.setBaseDir(option.getValue("dir"));
  76. }
  77. else
  78. {
  79. t2a.setBaseDir(".");
  80. }
  81. if(option.hasParam("base-package"))
  82. {
  83. t2a.setBasePackage(option.getValue("base-package"));
  84. }
  85. else
  86. {
  87. t2a.setBasePackage("prx.");
  88. }
  89. if (option.hasParam("not-force-array"))
  90. {
  91. t2a.setForceArray(false);
  92. }
  93. else
  94. {
  95. t2a.setForceArray(true);
  96. }
  97. if (option.hasParam("extends-package"))
  98. {
  99. t2a.setTarsPacket(option.getValue("extends-package"));
  100. }
  101. t2a.setCheckDefault(tars::TC_Common::lower(option.getValue("check-default")) == "true"?true:false);
  102. if (option.hasParam("with-charset"))
  103. {
  104. t2a.setCharset(option.getValue("with-charset"));
  105. }
  106. else
  107. {
  108. t2a.setCharset("UTF8");
  109. }
  110. if(option.hasParam("with-JavaBeanRule"))
  111. {
  112. t2a.setWithJbr(true);
  113. }
  114. else
  115. {
  116. t2a.setWithJbr(false);
  117. }
  118. t2a.setWithCompact(false);
  119. t2a.setEnumCompact(true);
  120. t2a.setWithGenerateInterfaceDependencies(true);
  121. t2a.setWithFilterRomTars(true);
  122. try
  123. {
  124. //增加include搜索路径
  125. g_parse->addIncludePath(option.getValue("include"));
  126. for(size_t i = 0; i < vTars.size(); i++)
  127. {
  128. g_parse->parse(vTars[i]);
  129. t2a.createFile(vTars[i]);
  130. }
  131. }catch(exception& e)
  132. {
  133. cerr<<e.what()<<endl;
  134. }
  135. return 0;
  136. }