// // ZGApk_yml.m // UUTUtils // // Created by zhuge on 2017/12/28. // Copyright © 2017年 zhuge. All rights reserved. // #import "ZGApk_yml.h" #import "ZGApk.h" #import "WordUtils.h" @implementation ZGApk_yml @synthesize versionCode = _versionCode, versionName = _versionName; - (id)initWithApk:(ZGApk *)apk { self.apk = apk; self.path = [NSString stringWithFormat:@"%@/apktool.yml", self.apk.path]; return self; } #pragma mark setter - (void)setVersionCode:(NSString *)versionCode { NSString *old = [NSString stringWithFormat:@"versionCode: '%@'", self.versionCode]; NSString *new = [NSString stringWithFormat:@"versionCode: '%@'", versionCode]; _fileContent = [_fileContent stringByReplacingOccurrencesOfString:old withString:new]; } - (void)setVersionName:(NSString *)versionName { NSString *old = [NSString stringWithFormat:@"versionName: '%@'", self.versionName]; NSString *new = [NSString stringWithFormat:@"versionName: '%@'", versionName]; _fileContent = [_fileContent stringByReplacingOccurrencesOfString:old withString:new]; old = [NSString stringWithFormat:@"versionName: %@", self.versionName]; new = [NSString stringWithFormat:@"versionName: %@", versionName]; _fileContent = [_fileContent stringByReplacingOccurrencesOfString:old withString:new]; _versionName = nil; _versionCode = nil; } #pragma mark getter - (NSString *)versionName { if (_versionName) { return _versionName; } else { NSArray *toolStringLines = [self.fileContent componentsSeparatedByString:@"\n"]; for (NSString *line in toolStringLines) { if ([line rangeOfString:@"versionName"].location != NSNotFound) { _versionName = [WordUtils removeAllSuffix:@" " ofString:line]; _versionName = [WordUtils removeAllPrefix:@" " ofString:_versionName]; _versionName = [_versionName stringByReplacingOccurrencesOfString:@"versionName: " withString:@""]; _versionName = [_versionName stringByReplacingOccurrencesOfString:@"'" withString:@""]; return _versionName; } } } return @""; } - (NSString *)versionCode { if (_versionCode) { return _versionCode; } else { NSArray *toolStringLines = [self.fileContent componentsSeparatedByString:@"\n"]; for (NSString *line in toolStringLines) { if ([line rangeOfString:@"versionCode"].location != NSNotFound) { _versionCode = [line stringByReplacingOccurrencesOfString:@"versionCode: " withString:@""]; _versionCode = [_versionCode stringByReplacingOccurrencesOfString:@"'" withString:@""]; _versionCode = [WordUtils removeAllSuffix:@" " ofString:_versionCode]; _versionCode = [WordUtils removeAllPrefix:@" " ofString:_versionCode]; return _versionCode; } } } return @""; } - (NSString *)fileContent { if (_fileContent) { return _fileContent; } else { _fileContent = [NSString stringWithContentsOfFile:self.path encoding:NSUTF8StringEncoding error:nil]; return _fileContent; } } - (NSString *)apkFileName { if (_apkFileName) { return _apkFileName; } else { NSArray *toolStringLines = [self.fileContent componentsSeparatedByString:@"\n"]; for (NSString *line in toolStringLines) { if ([line rangeOfString:@"apkFileName"].location != NSNotFound) { _apkFileName = [line stringByReplacingOccurrencesOfString:@"apkFileName: " withString:@""]; _apkFileName = [_apkFileName stringByDeletingPathExtension]; return _apkFileName; } } } return @""; } - (void)save { [self.fileContent writeToFile:self.path atomically:YES encoding:NSUTF8StringEncoding error:nil]; } @end