Skip to content

Instantly share code, notes, and snippets.

@Leask
Last active December 19, 2015 23:59
Show Gist options
  • Save Leask/6037948 to your computer and use it in GitHub Desktop.
Save Leask/6037948 to your computer and use it in GitHub Desktop.
Build script for php projects
#!/usr/bin/env php
<?php
// by @leaskh
// config
error_reporting(E_ALL ^ E_NOTICE);
$binFolder = 'bin';
$sources = [
// PUT YOUR SOURCE HERE!
// SAMPLE:
'A', # is a DIR
'B.php', # is a FILE
];
// define
function delDir($path) {
$files = array_diff(scandir($path), ['.', '..']);
foreach ($files as $file) {
$curPath = "{$path}/{$file}";
is_dir($curPath) ? delDir($curPath) : unlink($curPath);
}
return rmdir($path);
}
function copyDir($src, $dst) {
@mkdir($dst);
$files = array_diff(scandir($src), ['.', '..']);
foreach ($files as $file) {
copyAny("{$src}/{$file}", "{$dst}/{$file}");
}
}
function copyAny($src, $dst) {
is_dir($src) ? copyDir($src, $dst) : copy($src, $dst);
}
function lintFile($path) {
if (preg_match('/^.*\.php$/i', $path)) {
$result = shell_exec("php -l {$path}");
if (preg_match('/No syntax errors detected/', $result)) {
return 0;
}
$arrResult = explode("\n", $result);
foreach ($arrResult as $item) {
if ($item) {
echo "❌ $item\n";
}
}
return 1;
}
return -1;
}
function lintDir($path) {
$files = array_diff(scandir($path), ['.', '..']);
foreach ($files as $file) {
if (lintAny("{$path}/{$file}") === 1) {
return 1;
}
}
return 0;
}
function lintAny($path) {
return is_dir($path) ? lintDir($path) : lintFile($path);
}
// start
echo "Build started:\n";
$curDir = dirname(__FILE__);
$binDir = "{$curDir}/{$binFolder}";
// flush bin
echo "* Flush bin folder\n";
if (file_exists($binDir) && is_dir($binDir)) {
if (!delDir($binDir)) {
echo "❌ Failed on making bin folder\n";
exit(1);
}
}
@mkdir($binDir);
// lint
echo "* Lint codes\n";
foreach ($sources as $item) {
if (lintAny("{$curDir}/{$item}") === 1) {
exit(1);
}
}
// make
echo "* Make codes\n";
foreach ($sources as $item) {
if (copyAny("{$curDir}/{$item}", "{$binDir}/{$item}")) {
echo "❌ Failed making codes\n";
exit(1);
}
}
// finished
echo "⭕ Built successfully\n";
return 0;
#!/bin/bash
# init
TARGET="$1"
TAG="$2"
PROTECTED=(eimgs static config.php)
# check input
if [ "${TARGET}" = "" ]; then
echo Target cannot be empty
exit -1
fi
# clear old codes
for file in ${TARGET}/*
do
prot=0
for iPt in ${PROTECTED[*]}
do
if [ "${file}" = "${TARGET}/${iPt}" ]; then
prot=1
fi
done
if [[ ${prot} -eq 0 && "${file}" != "" ]]; then
rm -rf "$file"
fi
done
# deploy new codes
cp -r ./bin/* ${TARGET}
cd ${TARGET}
git add .
git commit -am "${TAG}" || echo '';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment