一般咱們在使用Git管理代碼的時候都會建立不一樣的分支進行管理,而不一樣分支通常又對應了不一樣的環境,如master(預發佈或生產),develop(開發測試),staging(預發佈),feature等等。對iOS來講,每次提測不一樣環境的安裝包的時候,總須要手動進行切換環境(相信對大部分人來講是這樣的??),如何解決這個痛點(我就想偷個懶....)前端
目標:實現相似前端打包同樣的配置,與git分支作匹配,自動化部署環境,同時提供接口支持應用內切換環境git
最終實現:發佈測試/預發佈/生產,只需將代碼合併到master,staging等分支,無需手動切換環境,而配合jenkins將能實現不一樣環境的自動化構建發佈github
示例代碼AutoDemobash
注意配置好以後,須要Clean一遍項目,否則會報錯GitCommitBranch不存在測試
#當前的分支
git_branch=$(git symbolic-ref --short -q HEAD)
#獲取App安裝包下的info.plist文件路徑
info_plist="${BUILT_PRODUCTS_DIR}/${EXECUTABLE_FOLDER_PATH}/Info.plist"
#利用PlistBuddy改變info.plist的值
/usr/libexec/PlistBuddy -c "Set :'GitCommitBranch' '${git_branch}'" "${info_plist}"
複製代碼
建立AutoConfig單例,實現自動化部署邏輯,提供一切受環境影響的接口供外部調用,今後只需關注功能開發,打包發佈不再用去切換環境! AutoConfig.h:ui
#import <Foundation/Foundation.h>
typedef NS_ENUM(NSInteger, AutoEnvironmentType) {
AutoEnvironmentTypeDevelop = 1, //開發/測試環境
AutoEnvironmentTypeStaging, //預發佈環境
AutoEnvironmentTypeProduction, //生產環境
};
NS_ASSUME_NONNULL_BEGIN
@interface AutoConfig : NSObject
+ (NSString *)baseURL;
/**
自定義環境,一步實現應用內環境切換 及 方便開發調試
@param env 要切換到的環境
*/
+ (void)setEnviroment:(AutoEnvironmentType)env;
@end
複製代碼
AutoConfig.m實現atom
#import "AutoConfig.h"
const static NSString *kDevelopRegx = @"^(develop|feature)_.*$";
const static NSString *kStagingRegx = @"^master";
const static NSString *kProductionRegx = @"^production";
@interface AutoConfig()
@property (nonatomic, copy) NSString *baseURL;
@property (nonatomic, assign) AutoEnvironmentType env;
@property (nonatomic, strong) NSPredicate *developPredicate;
@property (nonatomic, strong) NSPredicate *stagingPredicate;
@property (nonatomic, strong) NSPredicate *productionPredicate;
@end
@implementation AutoConfig
#pragma mark -Public Methods
+ (NSString *)baseURL {
return [AutoConfig shared].baseURL;
}
+ (void)setEnviroment:(AutoEnvironmentType)env {
[AutoConfig shared].env = env;
[[AutoConfig shared] updateConfig];
}
#pragma mark -life cycle
+ (AutoConfig *)shared {
static AutoConfig *_config = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_config = [[AutoConfig alloc] init];
[_config commonInit];
});
return _config;
}
- (void)commonInit {
_developPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", kDevelopRegx];
_stagingPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",kStagingRegx];
_productionPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",kProductionRegx];
[self envForCurrentBranch];
[self updateConfig];
}
/**
根據分支名稱,進行正則匹配,指定環境
*/
- (void)envForCurrentBranch {
NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
NSString * branch = infoDict[@"GitCommitBranch"];
if ([self.developPredicate evaluateWithObject:branch]) {
//測試
self.env = AutoEnvironmentTypeDevelop;
}
else if ([self.stagingPredicate evaluateWithObject:branch]) {
//預發佈
self.env = AutoEnvironmentTypeStaging;
}
else {
//生產環境
self.env = AutoEnvironmentTypeProduction;
}
}
- (void)updateConfig {
/**
* 對根據環境變化的參數進行配置
*/
switch (self.env) {
case AutoEnvironmentTypeDevelop:
self.baseURL = @"http://test.com.cn";
break;
case AutoEnvironmentTypeStaging:
self.baseURL = @"https://staging.com.cn";
break;
case AutoEnvironmentTypeProduction:
self.baseURL = @"https://production.com.cn";
break;
default:
self.baseURL = @"http://test.com.cn";
break;
}
}
@end
複製代碼
iOS獲取Git信息lua