实用脚本分享

实用脚本分享

最近工作需要,写了几个脚本,介绍给需要的同学,顺便说下实现的原理
地址:ShellSnippet

检测iOS主题资源包图片大小的脚本

使用步骤

该脚本用于检测iOS的资源包大小是否符合iOS的资源大小规范,防止因为图标大小的不规范导致在不同屏幕的机型上体验不一致。
使用步骤如下:
1、先把资源包解压,资源文件会放在同一个目录下,演示资源包存放的路径是:
/Users/aron/GitRepos/iosdemos/YTThemeManagerDemo/Doocuments/IOS
2、然后执行checkImage.sh脚本,如果提示没有权限,使用命令chmod u+x ./checkImages.sh 给脚本添加运行权限,会提示输入目录,复制粘贴第一步的目录即可
执行checkImage.sh
 3、执行了脚本之后会输出一个分析结果的文件outLogFile.txt,该文件中保存了可能存在问题的资源文件列表
outLogFile.txt
 比如nav_btn_share_black这个文件2x和3x的大小是一样的,在3x的机器上显示就会存在问题:图标变小或者图标的显示效果变模糊,需要让UI提供合适的图标。
Compare1
Compare2

实现分析

实现步骤如下,主要使用了sips命令获取图片的信息,进行循环比较:

  • ls ${imagesFolder}命令获取文件夹下的所有资源文件
  • sips -g pixelWidth ${imagePath}命令获取图片资源的信息
  • 记录上个图片资源的信息,然后循环比较判断图片尺寸是否合适

对应的代码如下,关键的地方有做了注释了:

#/bin/sh

imagesFolder="/Users/aron/Desktop/CheckImages/IOS"
outpuLogFile="$(pwd)/outLogFile.txt"
echo "" > ${outpuLogFile}

# 输入文件夹
echo -n "请输入图片目录: "
read imagesFolder
if [[ -d $imagesFolder ]]; then
	echo "图片目录正确"
else
	echo -n "输入的目录无效,请重新执行脚本"
fi

# 获取下面的所有文件
lastHandleFile=""
lastHandleImageWidthHeight=""
for imageFullName in $(ls ${imagesFolder}); do
	echo "imageFullName = ${imageFullName} lastHandleFile = ${lastHandleFile}"
	// 使用sed的替换命令s,去掉图片名称中的 @2x.png  @3x.png 结尾,方便比较
	pureImageName=`echo ${imageFullName} | sed 's/@.*.png//g' | sed 's/\.png//g'`
	if [[ ${pureImageName} == ${lastHandleFile} ]]; then
		# 判断大小
		imagePath="${imagesFolder}/${imageFullName}"
		// sips -g pixelWidth ${imagePath} 获取到的结果如下:
		//  pixelHeight: 1334
		// 使用awk命令使用:分隔符分割获取第二部分也就是图片的宽度
		ImageWidth=`sips -g pixelWidth ${imagePath} | awk -F: '{print $2}'`
		ImageHeight=`sips -g pixelHeight ${imagePath} | awk -F: '{print $2}'`
		height=`echo $ImageHeight`
		width=`echo $ImageWidth`
		currentImageWidthHeight="${width}_${height}"
		if [[ ${currentImageWidthHeight} == ${lastHandleImageWidthHeight} ]]; then
			// 使用>>追加写入可能不符合要求的文件名
			echo ${lastHandleFile} >> ${outpuLogFile}
		fi
	else
		lastHandleFile=${pureImageName}

		imagePath="${imagesFolder}/${imageFullName}"
		ImageWidth=`sips -g pixelWidth ${imagePath} | awk -F: '{print $2}'`
		ImageHeight=`sips -g pixelHeight ${imagePath} | awk -F: '{print $2}'`
		height=`echo $ImageHeight`
		width=`echo $ImageWidth`
		lastHandleImageWidthHeight="${width}_${height}"

		echo "pureImageName = ${pureImageName} lastHandleImageWidthHeight=${lastHandleImageWidthHeight}"
	fi
done

批量文件转换编码的脚本

把源文件的编码转换为UTF-8,默认转换的源代码是php和html结尾的源码,需要转换其它格式的自定义即可,修改代码中的如下行

if [[ $(expr "$item" : '.*\.php') -gt 0 ]] || [[ $(expr "$item" : '.*\.html') -gt 0 ]]; then

比如需要转换xxx.m xxx.h xxx.mm xxx.cpp xxx.hpp文件,这一行的写法如下

if [[ $(expr "$item" : '.*\.') -gt 0 ]] || [[ $(expr "$item" : '.*\.h') -gt 0 ]] || [[ $(expr "$item" : '.*\.cpp') -gt 0 ]] || [[ $(expr "$item" : '.*\.hpp') -gt 0 ]]; then

使用enca命令转换,如果没安装,脚本会自动使用brew安装enca命令,如果没安装brew,emmm。。。请自行百度安装吧。

使用步骤 1、执行命令,提示输入源代码的目录,脚本会遍历该目录下的源文件,进行编码转换,结束。 

实现分下

该脚本实现很简单,主要是read_implement_file_recursively方法中处理递归遍历,判断文件的扩展名是否是php/html文件,符合要求的话使用enca命令转换格式即可,使用的一些辅助方法包括

  • 检测命令是否安装的方法test_command_installed,如果未安装,调用brew install enca进行安装enca
  • 检测文件加是否存在并提示输入的方法checkDirCorecheckInputDestDirRecursive
#!/bin/sh

# 检测某个命令是否安装
# @return
# 0 未安装
# 1 安装
function test_command_installed {
	echo "test_command_installed=="
	which_command=`which $1`
	echo $which_command
	if [[ -n  $which_command ]]; then
		echo "$1 command installed"
		return 1
	else
		echo "$1 command not installed"
		return 0
	fi
}

# 循环检测输入的文件夹
checkInputDestDirRecursive() {
	echo -n "请输入目录: "
	read path
	if [[ -d $path ]]; then
		CheckInputDestDirRecursiveReturnValue=$path
	else
		echo -n "输入的目录无效,"
		checkInputDestDirRecursive
	fi
}

# 检测文件夹存在的方法,结果保存在全局变量`CheckInputDestDirRecursiveReturnValue`中
# 参数一:检测的文件夹路径
# 参数二:提示消息字符串
# 使用方式如下,去掉注释
# # 导入工具脚本
# . ./FileUtil.sh
# # 检测class_search_dir
# checkDirCore $class_search_dir "指定类的查找目录不存在"
# class_search_dir=${CheckInputDestDirRecursiveReturnValue}
class_search_dir=""
checkDirCore() {
	to_process_dir=$1
	message=$2
	# 需处理源码目录检查
	if [[ -d $to_process_dir ]]; then
		echo "目录存在 $to_process_dir"
		CheckInputDestDirRecursiveReturnValue=$to_process_dir
		return 1
	else
		echo "${message} ${to_process_dir}"
		checkInputDestDirRecursive ${to_process_dir}
	fi
}


# 递归函数读取目录下的所有.m文件
function read_implement_file_recursively {
	echo "read_implement_file_recursively"
	if [[ -d $1 ]]; then
		for item in $(ls $1); do
			itemPath="$1/${item}"
			if [[ -d $itemPath ]]; then
				# 目录
				echo "处理目录 ${itemPath}"
				read_implement_file_recursively $itemPath
				echo "处理目录结束====="
			else 
				# 文件
				echo "处理文件 ${itemPath}"
				// 判断是否是.php/.html扩展名的文件
				if [[ $(expr "$item" : '.*\.php') -gt 0 ]] || [[ $(expr "$item" : '.*\.html') -gt 0 ]]; then
					echo ">>>>>>>>>>>>mmmmmmm"
					# 使用enca转换格式
					enca -L zh_CN -x UTF-8 ${itemPath}
				fi
				echo ""
			fi
		done
	else
		echo "err:不是一个目录"
	fi
}

test_command_installed enca
result=$?
if [[ $result = 0 ]]; then
	echo "enca not installed now install"
	brew install enca || exit 1
fi
checkDirCore $class_search_dir "指定目录不存在"
class_search_dir=${CheckInputDestDirRecursiveReturnValue} 
read_implement_file_recursively ${class_search_dir}

转载于:https://my.oschina.net/FEEDFACF/blog/3045369

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值