<commit_before>#!/usr/bin/php
<?php

$pathToLib = dirname(dirname(__DIR__)) . '/min/lib';

// needed because of dumb require statements in class files :(
set_include_path($pathToLib . PATH_SEPARATOR . get_include_path());

// barebones autoloader
spl_autoload_register(function ($class) use ($pathToLib) {
    $file = $pathToLib . '/' . str_replace(array('_', '\\'), DIRECTORY_SEPARATOR, $class) . '.php';
    if (is_file($file)) {
        require $file;
        return true;
    }
    return false;
});

$cli = new MrClay\Cli;

$cli->addRequiredArg('d')->assertDir()->setDescription('Path of your webserver\'s DOCUMENT_ROOT. Relative paths will be rewritten relative to this path.');

$cli->addOptionalArg('o')->useAsOutfile()->setDescription('Outfile. If given, output will be placed in this file.');

$cli->addOptionalArg('v')->setDescription('Verbose: show rewriting algorithm. This is ignored if you don\'t use an outfile.');

if (! $cli->validate()) {
    echo "USAGE: ./rewrite-uris.php -d DOC_ROOT [-o OUTFILE [-v]] file ...\n";
    if ($cli->isHelpRequest) {
        echo $cli->getArgumentsListing();
    }
    echo "EXAMPLE: ./rewrite-uris.php -v -d../.. ../../min_unit_tests/_test_files/css/paths_rewrite.css ../../min_unit_tests/_test_files/css/comments.css
    \n";
    exit(0);
}

$outfile = $cli->values['o'];
$verbose = $cli->values['v'];
$docRoot = $cli->values['d'];

$pathRewriter = function($css, $options) {
    return Minify_CSS_UriRewriter::rewrite($css, $options['currentDir'], $options['docRoot']);
};

$fp = $cli->openOutput();

$paths = $cli->getPathArgs();
$sources = array();
foreach ($paths as $path) {
    $sources[] = new Minify_Source(array(
        'filepath' => $path,
        'minifier' => $pathRewriter,
        'minifyOptions' => array('docRoot' => $docRoot),
    ));
}
fwrite($fp, Minify::combine($sources) . "\n");

if ($outfile && $verbose) {
    echo Minify_CSS_UriRewriter::$debugText . "\n";
}

$cli->closeOutput();<commit_msg>Added test-run option in place of verbose<commit_after>#!/usr/bin/php
<?php

$pathToLib = dirname(dirname(__DIR__)) . '/min/lib';

// needed because of dumb require statements in class files :(
set_include_path($pathToLib . PATH_SEPARATOR . get_include_path());

// barebones autoloader
spl_autoload_register(function ($class) use ($pathToLib) {
    $file = $pathToLib . '/' . str_replace(array('_', '\\'), DIRECTORY_SEPARATOR, $class) . '.php';
    return is_file($file) ? ((require $file) || true) : false;
});

$cli = new MrClay\Cli;

$cli->addRequiredArg('d')->assertDir()->setDescription('Your webserver\'s DOCUMENT_ROOT: Relative paths will be rewritten relative to this path.');

$cli->addOptionalArg('o')->useAsOutfile()->setDescription('Outfile: If given, output will be placed in this file.');

$cli->addOptionalArg('t')->setDescription('Test run: Return output followed by rewriting algorithm.');

if (! $cli->validate()) {
    echo "USAGE: ./rewrite-uris.php [-t] -d DOC_ROOT [-o OUTFILE] file ...\n";
    if ($cli->isHelpRequest) {
        echo $cli->getArgumentsListing();
    }
    echo "EXAMPLE: ./rewrite-uris.php -t -d../.. ../../min_unit_tests/_test_files/css/paths_rewrite.css ../../min_unit_tests/_test_files/css/comments.css
    \n";
    exit(0);
}

$outfile = $cli->values['o'];
$testRun = $cli->values['t'];
$docRoot = $cli->values['d'];

$pathRewriter = function($css, $options) {
    return Minify_CSS_UriRewriter::rewrite($css, $options['currentDir'], $options['docRoot']);
};

$paths = $cli->getPathArgs();
var_export($paths);

$sources = array();
foreach ($paths as $path) {
    if (is_file($path)) {
        $sources[] = new Minify_Source(array(
            'filepath' => $path,
            'minifier' => $pathRewriter,
            'minifyOptions' => array('docRoot' => $docRoot),
        ));
    } else {
        $sources[] = new Minify_Source(array(
            'id' => $path,
            'content' => "/* $path not found */\n",
            'minifier' => '',
        ));
    }
}
$combined = Minify::combine($sources) . "\n";

if ($testRun) {
    echo $combined;
    echo Minify_CSS_UriRewriter::$debugText . "\n";
} else  {
    $fp = $cli->openOutput();
    fwrite($fp, $combined);
    $cli->closeOutput();
}

{'fraction_non_alphanumeric': 0.19586894586894588, 'fraction_numerical': 0.0004748338081671415, 'mean_word_length': 3.650110375275938, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '498116', 'n_tokens_mistral': 1453, 'n_tokens_neox': 1425, 'n_words': 305}