一般咱们在使用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