Shell Module Guide
| diversen | |
| 08-Mar-2011 09:58:53 |
0 Comments
What we will make
This is very simple. We want to make a shell module that can clone a site with wget and make a static site which we then can publish on github.com. For an example you can see my personal github site which is made from the following module. Se http://diversen.github.com This is a static site made from a dynamic site.
If you just want to look at the source you can do a:
./coscli.sh git --mod-in git://github.com/diversen/gh-static.git
Install File
First step is to make a install file
<?php // first we set a version for our module Note it has to be a float! $_INSTALL['VERSION'] = 1.41; // we specify that it is a shell module. // this means that the source files will be loaded when the base shell // command is getting loaded. $_INSTALL['IS_SHELL'] = "1"; // we add a public clone url for easy distribution $_INSTALL['PUBLIC_CLONE_URL'] = 'git://github.com/diversen/gh-static.git'; // and also a private url if we want to commit directly from base // shell command $_INSTALL['PRIVATE_CLONE_URL'] = 'git@github.com:diversen/gh-static.git';
Ini File
We also make a ini file where we can add some settings which can be changed with ease. It looks like this:
gh_static_push_url = "git@github.com:diversen/diversen.github.com.git" gh_static_site_dir = "/home/dennis/www/diversen.github.com" gh_static_index_url = "cos.org"
The first setting of the ini file is the push url. This is the git repo url we can push to. The next setting is the dir where our site will be placed local. Third setting is the url to index: mygithub (a more normal url to spider would be e.g. www.example.com).
The module file
Then we can make our module:
<?php /** * This is function which genrates the static site. */ function gh_static_generate (){ // ini settings will be found in the static var mainClin::$ini $static_dir = config::getModuleIni('gh_static_site_dir'); // The url to index (mygithub) $url = config::getModuleIni('gh_static_index_url'); // create dir where files will be put if (!file_exists($static_dir)) { $command = "mkdir $static_dir"; cos_exec($command); } // wget command $command = "wget -m -k -K -E $url"; // execute cos_exec($command); // copy the content of the url to the static dir // (/home/dennis/www/diversen.github.com) //$command = "mkdir $static_dir"; $command = "cp -rf ./$url/* $static_dir"; // execute system($command); } /** * next command is the push command. */ function gh_static_push (){ // the static dir where we will push from $static_dir = config::getModuleIni('gh_static_site_dir'); // cd into dir add - commit - push $command = "cd $static_dir && git add . && git commit -m \"auto-commit\" && git push"; // execute cos_exec($command); } // enable commands in shell - first we set a base command mainCli::setCommand('gh-static', array( 'description' => 'Command to generate a static site from a dynamic', )); // then we set a option mainCli::setOption('gh_static_generate', array( 'long_name' => '--generate', 'description' => 'Will generate the static site into location specified in .ini file', 'action' => 'StoreTrue' )); // and another option mainCli::setOption('gh_static_push', array( 'long_name' => '--push', 'description' => 'Will push the static site to git url specified in .ini file', 'action' => 'StoreTrue' ));
Usage
./coscli.sh gh-static -h
Will give you the help options.
./coscli.sh gh-static --generate
Will generate the static site with wget
./coscli.sh gh-static --push
Will push the site to remote repo.
Simple as that!
Share a link to this on email, Google+, Twitter, Facebook.
