Skip to content

Instantly share code, notes, and snippets.

@kent013
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kent013/bdaefd9c372ada6fd356 to your computer and use it in GitHub Desktop.
Save kent013/bdaefd9c372ada6fd356 to your computer and use it in GitHub Desktop.
replace codesign
<?php
$action = _getArgOrDie("action", "action must be specified[apply, revert, set-stampcheck]");
if(!in_array($action, ["apply", "revert", "set-stampcheck"])){
_error("action must be specified and one of apply, revert");
}
$pbxproj_filename = getFilename("pbxproj");
$pbxproj_backupfilename = $pbxproj_filename . ".back";
if($action == "apply"){
$plist_filename = getFilename("plist");
$plist_backupfilename = $plist_filename . ".back";
$teamid = _getArgOrDie("teamid", "teamid must be specified.");
$codeid_pattern = _getArgOrDie("codeid-pattern", "codeid-pattern must be specified");
$codeid = _getArgOrDie("codeid", "codeid must be specified");
$pbxfile = file_get_contents($pbxproj_filename);
$pbxfile = preg_replace('/DevelopmentTeam = [^;]+;/', "DevelopmentTeam = $teamid;", $pbxfile);
$pbxfile = preg_replace("/CODE_SIGN_IDENTITY = \"[^\"]*{$codeid_pattern}[^\"]*\";/",
"CODE_SIGN_IDENTITY = \"$codeid\";", $pbxfile);
$pbxfile = preg_replace("/\"CODE_SIGN_IDENTITY\[sdk=iphoneos\*\]\" = \"[^\"]*{$codeid_pattern}[^\"]*\";/",
"\"CODE_SIGN_IDENTITY[sdk=iphoneos*]\" = \"$codeid\";", $pbxfile);
$bundleid = _getArgOrDie("bundleid", "bundleid must be specified");
$bundleid_pattern = _getArgOrDie("bundleid-pattern", "bundleid-pattern must be specified");
$plistfile = file_get_contents($plist_filename);
$plistfile = preg_replace("/<string>[^>]*{$bundleid_pattern}[^>]*<\/string>/",
"<string>{$bundleid}</string>", $plistfile);
copy($pbxproj_filename, $pbxproj_backupfilename);
copy($plist_filename, $plist_backupfilename);
file_put_contents($pbxproj_filename, $pbxfile);
file_put_contents($plist_filename, $plistfile);
}else if($action == "revert"){
if(!file_exists($backupfilename)){
_error("backup file is not present");
}
$plist_filename = getFilename("plist");
$plist_backupfilename = $plist_filename . ".back";
copy($pbxproj_backupfilename, $pbxproj_filename);
copy($plist_backupfilename, $plist_filename);
unlink($pbxproj_backupfilename);
unlink($plist_backupfilename);
}else if($action == "set-stampcheck"){
$value = _getArgOrDie("value", "value[01] must be specified.");
if($value != "0" && $value != "1"){
_error("value must be 0 or 1");
}
$pbxfile = file_get_contents($pbxproj_filename);
$pbxfile = preg_replace('/SKIP_STAMPCHECK=[01]/', "SKIP_STAMPCHECK=$value", $pbxfile);
copy($pbxproj_filename, $pbxproj_backupfilename);
file_put_contents($pbxproj_filename, $pbxfile);
}
function getFilename($name){
$filename = _getArgOrDie($name, "$name must be specified.");
if(!preg_match("/.{$name}$/", $filename)){
_error("target file must be $name.");
}
if(!file_exists($filename)){
_error("target file not exists");
}
return $filename;
}
//get arg or die
function _getArgOrDie($name, $message){
$value = _getArg($name);
if(is_null($value)){
_error($message);
}
return $value;
}
//find command line argument
function _findArgFlag($name){
$argv = $_SERVER['argv'];
foreach($argv as $v){
if(preg_match('/(-+)' . $name . '/', $v)){
return true;
}
}
return false;
}
//get command line argument
function _getArg($name, $default = null){
$argv = $_SERVER['argv'];
foreach($argv as $k => $v){
if(preg_match('/(-+)' . $name . '$/', $v) &&
isset($argv[$k + 1])){
return $argv[$k + 1];
}
}
return $default;
}
//print message
function _msg($msg, $indent = 0){
$sp = '';
for ($i = 0; $i < $indent; $i++){
$sp .= ' ';
}
echo $sp . $msg . "\n";
}
//print info message
function _info($msg, $indent = 0){
$date = date('Y/m/d H:i:s');
$msg = '[INFO] ' . $date . " : " . $msg;
_msg($msg, $indent);
}
//print warn message
function _warn($msg, $indent = 0){
$date = date('Y/m/d H:i:s');
$msg = '[WARN] ' . $date . " : " . $msg;
_msg($msg, $indent);
}
//print error message
function _error($msg, $indent = 0){
$date = date('Y/m/d H:i:s');
$msg = '[FATAL] ' . $date . " : " . $msg;
_exit($msg, $indent);
}
//print exit message
function _exit($msg, $indent = 0){
_msg($msg, $indent);
exit;
}
//create dir
function _createDirectory($dir){
if(file_exists($dir)){
return;
}
mkdir($dir);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment