scrawl

#!/usr/bin/env php
<?php

if (Phar::running()==''){

// if global phptest is called & phptest is also installed to the current package via composer
// then only run the package-level install
$vendor_install = getcwd().'/vendor/bin/scrawl';
//vendor_install_2 accounts for the composer update that stopped symlinking & started include-ing the target bin script
$vendor_install2 = getcwd().'/vendor/taeluf/code-scrawl/bin/scrawl';
if (file_exists($vendor_install)
    &&realpath(__FILE__)!=realpath($vendor_install)
    &&realpath(__FILE__)!=realpath($vendor_install2)
){
    $args = array_slice($argv,1);
    $args = array_filter($args, function($v){
        return '"'.addslashes($v).'"';
    });
    $cmd = "$vendor_install ". implode(' ',$args);
    // because composer started using an include() instead of symlink, the #!/shebang line was occuring before `namespace Composer` & causing errors
    // so now we use `passthru` here to execute the vendor script rather than include it
    passthru("$vendor_install ". implode(' ',$args));
    return;
}

// require() the current package's autoloader
$cwd_autoload = getcwd().'/vendor/autoload.php';
if (!is_file($cwd_autoload)){
    echo "\n\nAutoloader file '$cwd_autoload' not found. ";
} else {
    require($cwd_autoload);
}

//require own global autoloader
$own_global_autoload = dirname(__DIR__).'/vendor/autoload.php';
if (is_file($own_global_autoload)&&realpath($cwd_autoload)!=realpath($own_global_autoload)){
    require($own_global_autoload);
}

} else {
    require(__DIR__.'/../vendor/autoload.php');
}

$dir = getcwd();


$cli = new \Tlf\Cli();
$cli->load_inputs(json_decode(file_get_contents(dirname(__DIR__).'/src/defaults.json'),true));

if (is_file($config=$dir.'/.config/scrawl.json')
    ||is_file($config=$dir.'/scrawl.json')
){
    $cli->load_inputs(json_decode(file_get_contents($config),true));
} else if (is_file($config=$dir.'/.docsrc/config.json')){
    $cli->load_inputs(json_decode(file_get_contents($config),true));
} else if (is_file($config=$dir.'/config/scrawl.json')){
    $cli->load_inputs(json_decode(file_get_contents($config),true));
}

$cli->load_stdin();

if (!isset($cli->args['dir.root']))$cli->args['dir.root'] = getcwd();



/////////
// backward compatability config changes
/////////
# old_config_name becomes new_config_name
# dir.code becomes dir.scan
if (isset($cli->args['dir.code']) && !isset($cli->args['dir.scan'])){
    $cli->args['dir.scan'] = $cli->args['dir.code'];
}
unset($cli->args['dir.code']);
# dir.template becomes dir.src
if (isset($cli->args['dir.template']) && !isset($cli->args['dir.src'])){
    $cli->args['dir.src'] = $cli->args['dir.template'];
}
unset($cli->args['dir.template']);
# dir.template_files becomes template.dirs 
if (isset($cli->args['dir.template_files']) && !isset($cli->args['template.dirs'])){
    $cli->args['template.dirs'] = $cli->args['dir.template_files'];
}
unset($cli->args['dir.template_files']);

# convert template.dirs from string to array
if (isset($cli->args['template.dirs'])&&is_string($cli->args['template.dirs'])){
    $cli->args['template.dirs'] = [$cli->args['template.dirs']];
}

/////////
// prepend `dir.root` to these path configs
/////////
$root_prefix = ['dir.src', 'dir.docs', 'template.dirs', 'file.bootstrap'];
foreach ($root_prefix as $key){
    $arg = $cli->args[$key] ?? null;
    if ($arg==null)continue;
    if (is_array($arg)){
        $cli->args[$key] = array_map(function($path) use ($cli){
            return $cli->args['dir.root'].'/'.$path;
        },$cli->args[$key]);
    } else {
        $cli->args[$key] = $cli->args['dir.root'].'/'.$arg;
    }
}


////////
// init scrawl & run it
////////
$scrawl = new \Tlf\Scrawl($cli->args);


$cli->load_command('main', [$scrawl, 'run'], "Generate Documentation");

/**
 * Get absolute path to the generated documentation file of a code file.
 *
 * @usage `scrawl get_doc_path "/absolute/path.php"
 * @arg absolute path to document
 */
$cli->load_command('get_doc_path', [$scrawl, 'get_doc_path'], "Get absolute path to the generated documentation file of a code file.");
$cli->load_command('get_doc_src_path', [$scrawl, 'get_doc_source_path'], "Get absolute path to the Editable .src.md file of a .md file.");
// $cli->load_command('init', [$scrawl, 'run_init']);

// $runner->backward_compatability();
return $cli->execute();