ZGApk_yml.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // ZGApk_yml.m
  3. // UUTUtils
  4. //
  5. // Created by zhuge on 2017/12/28.
  6. // Copyright © 2017年 zhuge. All rights reserved.
  7. //
  8. #import "ZGApk_yml.h"
  9. #import "ZGApk.h"
  10. #import "WordUtils.h"
  11. @implementation ZGApk_yml
  12. @synthesize versionCode = _versionCode, versionName = _versionName;
  13. - (id)initWithApk:(ZGApk *)apk {
  14. self.apk = apk;
  15. self.path = [NSString stringWithFormat:@"%@/apktool.yml", self.apk.path];
  16. return self;
  17. }
  18. #pragma mark setter
  19. - (void)setVersionCode:(NSString *)versionCode {
  20. NSString *old = [NSString stringWithFormat:@"versionCode: '%@'", self.versionCode];
  21. NSString *new = [NSString stringWithFormat:@"versionCode: '%@'", versionCode];
  22. _fileContent = [_fileContent stringByReplacingOccurrencesOfString:old withString:new];
  23. }
  24. - (void)setVersionName:(NSString *)versionName {
  25. NSString *old = [NSString stringWithFormat:@"versionName: '%@'", self.versionName];
  26. NSString *new = [NSString stringWithFormat:@"versionName: '%@'", versionName];
  27. _fileContent = [_fileContent stringByReplacingOccurrencesOfString:old withString:new];
  28. old = [NSString stringWithFormat:@"versionName: %@", self.versionName];
  29. new = [NSString stringWithFormat:@"versionName: %@", versionName];
  30. _fileContent = [_fileContent stringByReplacingOccurrencesOfString:old withString:new];
  31. _versionName = nil;
  32. _versionCode = nil;
  33. }
  34. #pragma mark getter
  35. - (NSString *)versionName {
  36. if (_versionName) {
  37. return _versionName;
  38. } else {
  39. NSArray *toolStringLines = [self.fileContent componentsSeparatedByString:@"\n"];
  40. for (NSString *line in toolStringLines) {
  41. if ([line rangeOfString:@"versionName"].location != NSNotFound) {
  42. _versionName = [WordUtils removeAllSuffix:@" " ofString:line];
  43. _versionName = [WordUtils removeAllPrefix:@" " ofString:_versionName];
  44. _versionName = [_versionName stringByReplacingOccurrencesOfString:@"versionName: " withString:@""];
  45. _versionName = [_versionName stringByReplacingOccurrencesOfString:@"'" withString:@""];
  46. return _versionName;
  47. }
  48. }
  49. }
  50. return @"";
  51. }
  52. - (NSString *)versionCode {
  53. if (_versionCode) {
  54. return _versionCode;
  55. } else {
  56. NSArray *toolStringLines = [self.fileContent componentsSeparatedByString:@"\n"];
  57. for (NSString *line in toolStringLines) {
  58. if ([line rangeOfString:@"versionCode"].location != NSNotFound) {
  59. _versionCode = [line stringByReplacingOccurrencesOfString:@"versionCode: " withString:@""];
  60. _versionCode = [_versionCode stringByReplacingOccurrencesOfString:@"'" withString:@""];
  61. _versionCode = [WordUtils removeAllSuffix:@" " ofString:_versionCode];
  62. _versionCode = [WordUtils removeAllPrefix:@" " ofString:_versionCode];
  63. return _versionCode;
  64. }
  65. }
  66. }
  67. return @"";
  68. }
  69. - (NSString *)fileContent {
  70. if (_fileContent) {
  71. return _fileContent;
  72. } else {
  73. _fileContent = [NSString stringWithContentsOfFile:self.path encoding:NSUTF8StringEncoding error:nil];
  74. return _fileContent;
  75. }
  76. }
  77. - (NSString *)apkFileName {
  78. if (_apkFileName) {
  79. return _apkFileName;
  80. } else {
  81. NSArray *toolStringLines = [self.fileContent componentsSeparatedByString:@"\n"];
  82. for (NSString *line in toolStringLines) {
  83. if ([line rangeOfString:@"apkFileName"].location != NSNotFound) {
  84. _apkFileName = [line stringByReplacingOccurrencesOfString:@"apkFileName: " withString:@""];
  85. _apkFileName = [_apkFileName stringByDeletingPathExtension];
  86. return _apkFileName;
  87. }
  88. }
  89. }
  90. return @"";
  91. }
  92. - (void)save {
  93. [self.fileContent writeToFile:self.path atomically:YES encoding:NSUTF8StringEncoding error:nil];
  94. }
  95. @end