iOS 常用Shell

[TOC]

修改类的前缀

echo 'sh file.sh -o 旧的前缀 -n 新的前缀 需要替换的目录'

while getopts "o:n:" opt; do
  case $opt in
    o)
      oldPrefix=$OPTARG 
      ;;
    n)
      newPrefix=$OPTARG 
      ;;
    \?)
      echo "Invalid option: -$OPTARG" 
      ;;
  esac
done

src=${@: -1}

echo $oldPrefix
echo $newPrefix
echo $src

for file in `find $src -name "${oldPrefix}*"`; do
    
    # 文件名,包含后缀
    fileName=${file##*/}
    
    if [[ ! -d $file ]]; then

        # 文件所在目录
        dir=${file%/*}
        # 新的文件名,包含后缀
        nFilePath=${newPrefix}${fileName#${oldPrefix}}
        # 新文件的完整地址
        nPath=${dir}/${nFilePath}

        # 原文件名,不含后缀
        oFileName=${fileName%.*}

        # 新文件名,不含后缀
        nFileName=${newPrefix}${oFileName#${oldPrefix}}

        # 文本替换
        LC_ALL=C sed -i "" "s/$oFileName/$nFileName/g" `grep $oFileName -rl $src`

        # 重命名文件
        mv -v $file $nPath
    fi
done

查找指定内容出现的次数

# 部分统计埋点有问题,主要因为新人大段复制老代码导致的。
#
# StatisticsDefine.h 内容 (主要是 注释 + 常量定义)
#
# // 应用启动
# static NSString * const StatisticsTypeAppStartup = @"10000";
# // 打开推送
# static NSString * const StatisticsTypeAppPushOpen = @"10001"
#

# 过滤,只保留含有const的行
rm ~/Desktop/Define.h
rm ~/Desktop/imp.txt
rm ~/Desktop/result.txt

grep const ~/Desktop/StatisticsDefine.h >> ~/Desktop/Define.h

echo "Define finish"

# # 找到项目中所有的m,只有在m中才会调用常量定义
find ~/Workspace/projectName -name "*.m" >> ~/Desktop/imp.txt
# 最好对Define.h和imp.txt进行优化,内容越少脚本执行时间越短
echo "imp finish"

for i in `awk '{print $5}' ~/Desktop/Define.h`; do

    count=0
    for j in `awk '{print $1}' ~/Desktop/imp.txt`; do
        array=$(grep -c "$i" "$j")
        count=`expr ${count} + ${array}`
    done

    # 一个常量应该最多只有一个地方用到,如果有多个地方用到,可能有问题,人工复查。
    if [[ ${count} > 1 ]]; then
        echo $i+${count}
        echo $i ${count} >> ~/Desktop/result.txt
    fi     
done

上面的方法就是无脑的查找,可以依据实际情况进行调整

# 过滤,只保留含有const的行
rm ~/Desktop/Define.h
rm ~/Desktop/call.txt
rm ~/Desktop/repeat.txt

grep const ~/Desktop/StatisticsManager/StatisticsDefine.h >> ~/Desktop/Define.h

echo "Define finish"

# 找到项目中所有的调用语句
grep -R 'StatisticsManager shared' ~/Workspace/projectName  >> ~/Desktop/call.txt

echo "call finish"

for i in `awk '{print $5}' ~/Desktop/Define.h`; do

    count=$(grep -c -w "$i" ~/Desktop/call.txt)
    
    # 一个常量应该最多只有一个地方用到,如果有多个地方用到,可能有问题,人工复查。
    if [[ ${count} > 1 ]]; then
        echo $i  ${count} >> ~/Desktop/repeat.txt
        echo "--------------------------------------" >> ~/Desktop/repeat.txt
        grep "$i" ~/Desktop/call.txt | awk -F ':' '{print $1}' >> ~/Desktop/repeat.txt
        echo "\n" >> ~/Desktop/repeat.txt
    fi     
done

#! /bin/sh

for eachfile in $(ls -B $1)
do
 
 # j截取
 filename=${eachfile%.png}

 mv $1/${eachfile} $1/${filename}@3x.png

done

为目录下的文件生成markdown图片链接

path=`pwd`

for file in `ls`
do
    echo "![$file](${path}/${file})" >> tmp.txt
done

生成模板文件

#!bin/sh
# sh template.sh model fileType tableType
#
# fileType
#   vc 一般的控制器,继承自XFViewController
#   model model
#   view view
#   cell model对应的Cell
#   table table类型的控制器,继承自XFRefreshTableViewController
#
# tableType
#   refresh 下拉刷新,没有加载更多
#   refresh & load  more 下拉刷新上拉加载更多
#   simple 没有下拉刷新和加载更多
#
#

authorInfoFunc() {
    mdate=`date +%Y/%m/%d`
    year=${mdate%%/*}
    info="//\n//    $1\n//  XNOnline\n//\n//  Created by ${USER} on ${mdate}.\n//  Copyright © ${year} xiaoniu88. All rights reserved.\n//\n\n

"
    echo $info
}

createModelFiles() {
entity=$1
authorInfo=`authorInfoFunc ${entity}.h`

# 创建model.h
echo "${authorInfo}
#import 

@interface $entity : NSObject

@end

" >> $entity".h"

# 创建Model.m
authorInfo=`authorInfoFunc ${entity}.m`

echo "${authorInfo}
#import \"${entity}.h\"
//#import 

@implementation ${entity}

//+ (nullable NSDictionary *)modelCustomPropertyMapper {
//    return @{};
//}


@end

" >> ${entity}.m
}

createVCFiles() {
    # 创建.h
    viewController=$1VC
    authorInfo=`authorInfoFunc ${viewController}.h`

    echo "${authorInfo}
#import 

@interface $viewController : XFViewController


@end

" >> $viewController.h

    authorInfo=`authorInfoFunc ${viewController}.m`
    # 创建.m
    echo "${authorInfo}
#import \"$viewController.h\"
#import \"$1.h\"

@interface ${viewController}()

@end

@implementation ${viewController}

#pragma mark - Initialize Methods


#pragma mark - Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];

    [self sendDefaultRequest];
}

#pragma mark - Super Methods


#pragma mark - Private Methods

- (NSString *)defaultPath {
    return @\"\";
}

- (void)sendDefaultRequest {
    XFRequest *request = [[XFRequest alloc] initWithPath:[self defaultPath] finish:^(XFRequest *request, id result) {
        
//      RequestResult *res = [RequestResult yy_modelWithJSON:result];
//        
//        if (res.code == 200) {
//          
//        } else {
//          Toast(res.msg);
//        }
    }];
    
    [self.mainQueue push:request];
}


#pragma mark - Property

@end

" >> ${viewController}.m
}

createCellXib() {
    file=$1

cat >> ${file}.xib <

    
        
        
        
    
    
        
        
        
            
            
            
                
                
            
            
        
    


EOF

}

createCellFiles() {

    cell=$1Cell
    authorInfo=`authorInfoFunc ${cell}.h`

    echo "${authorInfo}
#import 

@interface $cell : XFTableViewCell


@end

" >> ${cell}.h

    authorInfo=`authorInfoFunc ${cell}.h`
    
    echo "${authorInfo}
#import \"${cell}.h\"
#import \"$1.h\"

@interface ${cell}()

@end

@implementation ${cell}

#pragma mark - Initialize Methods

#pragma mark - Public Methods

- (void)configCellWithData:(id)item {
    [super configCellWithData:item];

    $1 *data = item;


}

@end

" >> ${cell}.m

createCellXib $cell
}

createTable() {
echo "createTable"
echo $1
echo $2

    list=$1ListVC
    authorInfo=`authorInfoFunc ${list}.h`

    echo "${authorInfo}
#import 

@interface $list : XFRefreshTableViewController

@end
" >> ${list}.h

# tableType
#   refresh 下拉刷新,没有加载更多
#   refresh & load  more 下拉刷新上拉加载更多
#   simple 没有下拉刷新和加载更多

refresh=""
if [[ $2 == "simple" ]]; then
    refresh="
    self.canUpdateData = NO;
    self.canAppendData = NO;
"   
elif [[ $2 == 'refresh' ]]; then
    refresh="self.canAppendData = NO;"
else
    refresh=""  
fi

    authorInfo=`authorInfoFunc ${list}.m`
    echo "${authorInfo}
#import \"${list}.h\"

#import \"$1.h\"
#import \"$1Cell.h\"
#import \"RequestResult.h\"
#import 
#import 

//typedef NS_ENUM(NSUInteger,$1CellType) {
//    $1CellTypeOne,
//    $1CellCount
//};

@interface ${list} ()

@end

@implementation ${list}

#pragma mark - Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];

    ${refresh}
    [self sendDefaultRequest];
}

#pragma mark - Request

- (NSString *)defaultPath {
    return @\"\";
}

- (void)sendDefaultRequest {
    XFRequest *request = [[XFRequest alloc] initWithPath:[self defaultPath] finish:^(XFRequest *request, id result) {
        if (self.page == [self startPage]) {
            [self.refreshHeader endRefreshing];
        } else {
            [self.refreshFooter endRefreshing];
        }
        
        RequestResult *res = [RequestResult yy_modelWithJSON:result];
        
        if (res.code == 200) {
            NSArray *array = [NSArray yy_modelArrayWithClass:[self modelClass] json:res.data];
            
            if (self.page == [self startPage]) {
                self.dataSource = [array mutableCopy];
            } else {
                [self.dataSource addObjectsFromArray:array];
            }
        } else {
            Toast(res.msg);
        }
    }];
    
    [self.mainQueue push:request];
}

#pragma mark - 

- (Class)modelClass {
    return [$1 class];
}

- (Class)cellClass {
    return [$1Cell class];
}

//- (Class)cellClassForIndexPath:(NSIndexPath *)indexPath {
//      return [$1Cell class];
//}


#pragma mark - 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    id obj = self.dataSource[indexPath.row];
//    
//    UIViewController *vc = nil;
//    [self.navigationController pushViewController:vc animated:YES];
}

@end

" >> ${list}.m

}


if [[ -n $1 ]]; then
echo $1
#创建一个目录

mkdir $1
cd $1

    if [[ -n $2 ]]; then
    
        if [[ $2 = "vc" ]]; then
            createVCFiles $1
        elif [[ $2 = "cell" ]]; then
            createCellFiles $1
        elif [[ $2 = "model" ]]; then
            createModelFiles $1
        elif [[ $2 = "table" ]]; then
            createTable $1 $3
        fi

    else    
        createModelFiles $1
        createVCFiles $1
        createCellFiles $1
        createTable $1 $3
    fi

path=`pwd`

osascript <

你可能感兴趣的:(iOS 常用Shell)