Sibande_

Auto-compile Twitter Bootstrap (C++)

I'm moving http://zabalaza.org from Blueprint CSS to Twitter Bootstrap. I initially tried using less.js but it was too slow for my dev machine (I'm using an old low-spec laptop). I ended up installing node.js to get the less.css compiler, but manually recompiling the files is frustrating.

So, I have written a small auto compiler in C++ :) It polls for changes in the directory with the .less files and runs the lessc command.

// Auto reloads/compiles Twitter Bootstrap's .less files.
#include <iostream>
#include <unistd.h>
#include <sys/stat.h>
#include <ctime>
#include <cstdlib>

const std::string BOOTSTRAP_DIRECTORY = "zabalaza/static/less/",
    BOOTSTRAP_MAIN = "zabalaza/static/less/bootstrap.less",
    BOOTSTRAP_CSS = "zabalaza/static/css/bootstrap.css";

int main()
{
    using namespace std;

    struct tm* modified;
    struct stat file_stats;
    int modified_time, current_time = 0;
    // $ lessc input.less > output.css
    string cmd = "lessc " + BOOTSTRAP_MAIN + " > " + BOOTSTRAP_CSS;

    system(cmd.c_str());

    while (true) {
        stat(BOOTSTRAP_DIRECTORY.c_str(), &file_stats);
        modified = gmtime(&(file_stats.st_mtime));
        modified_time = mktime(modified);

        if (current_time == 0) {
            current_time = modified_time;
        }
        if (modified_time > current_time) {
            current_time = modified_time;
            cout << "Recompiling .less files..." << endl;
            system(cmd.c_str());
            cout << "Done." << endl;
        }

        cout << "Polling directory..." << endl;
        sleep(3);
    }

    return 0;
}
08 August, 2012 at 21:17PM

© Copyright 2012 by Jose Sibande.
Login