ProjectInfo.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // ProjectInfo.m
  3. // UUTUtils
  4. //
  5. // Created by zhuge on 2017/12/22.
  6. // Copyright © 2017年 zhuge. All rights reserved.
  7. //
  8. #import "ProjectInfo.h"
  9. #import "DataUtils.h"
  10. #import "ZGFileUtils.h"
  11. #import "CountryInfoManager.h"
  12. #import "ZGApkToolUtils.h"
  13. #include "ZGJsonUtils.h"
  14. #import "HttpUtils.h"
  15. #import "ProjectInfo_Server_AdId.h"
  16. #define key_projectType @"project_type"
  17. #define key_projectName @"project_name"
  18. #define key_package @"package"
  19. #define key_local @"local"
  20. #define key_upload @"upload"
  21. @implementation ProjectInfo
  22. - (instancetype)init
  23. {
  24. self = [super init];
  25. if (self) {
  26. self.projectType = @"Android";
  27. self.projectName = @"未命名";
  28. self.package = @"这是包名";
  29. }
  30. return self;
  31. }
  32. + (ProjectInfo *)createWithDir:(NSString *)dir {
  33. ProjectInfo *bean = [[ProjectInfo alloc] init];
  34. [bean init:dir];
  35. return bean;
  36. }
  37. -(void)init:(NSString *)dir {
  38. NSString *plist = [NSString stringWithFormat:@"%@/config.plist", dir];
  39. NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:plist];
  40. self.projectName = dic[key_projectName];
  41. self.projectType = dic[key_projectType];
  42. self.package = dic[key_package];
  43. // path
  44. self.path = [ProjectInfo_Path create:self.package];
  45. // local
  46. self.local = [ProjectInfo_Local create:dic[key_local]];
  47. self.local.translate.path = self.path;
  48. // upload
  49. self.upload = [[ProjectInfo_Upload alloc] initWithDic:dic[key_upload] path:self.path];
  50. // apk
  51. self.apk = [[ZGApk alloc] initWithPath:self.path.apkCode];
  52. // fix config
  53. self.fixConfigs = [[ZGApkFixConfigs alloc] init];
  54. }
  55. - (void)loadServerData {
  56. self.server = [ProjectInfo_Server create:self.package];
  57. }
  58. - (void)createKeystoreFile {
  59. self.path = [ProjectInfo_Path create:self.package];
  60. NSString *keystore = self.path.config.keystore;
  61. if (![ZGFileUtils fileExist:keystore]) {
  62. [ZGFileUtils createDirectoryIfNotExist:self.path.config.root];
  63. NSString *keystoreName = [ZGApkToolUtils genRandomKeystoreFile:[self.path.config.root stringByAppendingString:@"/"]];
  64. NSLog(@"创建签名文件[%@]成功!", keystoreName);
  65. }
  66. }
  67. - (BOOL)save {
  68. @try {
  69. NSString *rootPath = self.path.root;
  70. [ZGFileUtils createDirectoryIfNotExist:rootPath];
  71. NSString *plist = self.path.configPlist;
  72. NSMutableDictionary *md = [NSMutableDictionary dictionaryWithCapacity:0];
  73. md[key_projectType] = self.projectType;
  74. md[key_projectName] = self.projectName;
  75. md[key_package] = self.package;
  76. if (self.local) {
  77. md[key_local] = [self.local toDic];
  78. }
  79. if (self.upload) {
  80. md[key_upload] = [self.upload toDic];
  81. }
  82. [md writeToFile:plist atomically:YES];
  83. } @catch (NSException *exception) {
  84. NSLog(@"%@", exception.callStackSymbols);
  85. return false;
  86. }
  87. return true;
  88. }
  89. - (BOOL)sendToServerAtWindow:(NSWindow *)window {
  90. @try {
  91. NSDictionary *dic = [self.server jsonDic];
  92. if(dic == nil){
  93. return false;
  94. }
  95. NSString *serverStr = [ZGJsonUtils dictionaryToString:dic];
  96. // NSLog(@"%@", serverStr);
  97. [[[HttpUtils alloc] init] saveAdConfigs:serverStr forPackage:self.package atWindow:window];
  98. } @catch (NSException *exception) {
  99. NSLog(@"%@", exception.callStackSymbols);
  100. return false;
  101. }
  102. return true;
  103. }
  104. #pragma mark out
  105. - (BOOL)isTranslateFinished {
  106. NSString *path = self.path.config.translate;
  107. // 翻译目录下的文件数量与配置里的相同,说明翻译是成功的
  108. if ([ZGFileUtils fileExist:path]) {
  109. NSArray *arrTxts = [ZGFileUtils arrayOfFileAtDir:path extensionFilterArray:@[@"txt"]];
  110. CountryInfoManager *cim = [[CountryInfoManager alloc] init];
  111. return [arrTxts count] == [[cim countryInfos] count];
  112. }
  113. return NO;
  114. }
  115. - (BOOL)isNotTranslateFinished {
  116. return ![self isTranslateFinished];
  117. }
  118. - (NSString *)outputApkFilePath {
  119. return [NSString stringWithFormat:@"%@/%@_%@.apk", self.path.output, self.projectName, self.apk.yml.versionName];
  120. }
  121. - (NSString *)description {
  122. return [NSString stringWithFormat:@"【%@】【%@】【%@】", self.projectType, self.projectName, self.package];
  123. }
  124. #pragma mark - 按类型过滤广告id
  125. - (NSArray<ProjectInfo_Server_AdId *> *)allAdmobAdIds {
  126. NSMutableArray<ProjectInfo_Server_AdId*> *ma = [[NSMutableArray alloc] initWithCapacity:0];
  127. for (ProjectInfo_Server_AdId *adid in self.server.xibAdIdArray) {
  128. if ([adid isAdmob]) {
  129. [ma addObject:adid];
  130. }
  131. }
  132. return ma;
  133. }
  134. - (NSArray<ProjectInfo_Server_AdId *> *)allFacebookAdIds {
  135. NSMutableArray<ProjectInfo_Server_AdId*> *ma = [[NSMutableArray alloc] initWithCapacity:0];
  136. for (ProjectInfo_Server_AdId *adid in self.server.xibAdIdArray) {
  137. if ([adid isFacebook]) {
  138. [ma addObject:adid];
  139. }
  140. }
  141. return ma;
  142. }
  143. @end