derRobert derRobert - 3 months ago 13
Bash Question

composer script section os.dependent

Is it possible to check for the current operating system?
The following cmd fails on windows, because chmod does not exist

{ ...
"scripts": {
"post-update-cmd": "chmod -R 777 ../log"
}
}


Is there a workaround to solve this ?
Kind regards, Robert

Answer

Thanks to @alcohol for pointing me tothe right direction. Simple solution:

"scripts": {
  "post-update-cmd": "php post-update-cmd.php"
}

post-update-cmd.php:

#!/bin/php
<?php

ini_set('register_argc_argv', 0);
if (!isset($argc) || is_null($argc)) die('CLI only');

$log_dir = dirname(dirname(__FILE__)). DIRECTORY_SEPARATOR . 'log';

if( strpos(strtolower(php_uname('s')), 'win') === false ) {
    exec("mkdir -p $log_dir && chmod -R 777 $log_dir");
} else { // Windows !
    if( !file_exists($log_dir) ) {
        exec("mkdir $log_dir");
    }
    echo "WinOS - no chmod available\n";
}

echo "Finished\n";