Add pakefile as replacement for makefile
[plcapi.git] / pakefile.php
1 <?php
2 /**
3  * Makefile for phpxmlrpc library.
4  * To be used with the Pake tool: https://github.com/indeyets/pake/wiki
5  *
6  * @todo allow user to specify location for zip command
7  * @todo allow user to specify release number and tag/branch to use
8  */
9
10 namespace PhpXmlRpc {
11
12 class Builder
13 {
14     protected static $buildDir = 'build/';
15     protected static $libVersion;
16     protected static $sourceBranch = 'master';
17
18     public static function libVersion()
19     {
20         return self::$libVersion;
21     }
22
23     public static function buildDir()
24     {
25         return self::$buildDir;
26     }
27
28     public static function workspaceDir()
29     {
30         return self::buildDir().'workspace';
31     }
32
33     /// most likely things will break if this one is moved outside of BuildDir
34     public static function distDir()
35     {
36         return self::buildDir().'xmlrpc-'.self::libVersion();
37     }
38
39     /// these will be generated in BuildDir
40     public static function distFiles()
41     {
42         return array(
43             'xmlrpc-'.self::libVersion().'.tar.gz',
44             'xmlrpc-'.self::libVersion().'.zip',
45         );
46     }
47
48     public static function sourceRepo()
49     {
50         return 'https://github.com/gggeek/phpxmlrpc';
51     }
52
53     /// @todo move git branch to be a named option?
54     public static function getOpts($args=array(), $cliOpts=array())
55     {
56         if (count($args) < 1)
57             throw new \Exception('Missing library version argument');
58         self::$libVersion = $args[0];
59         if (count($args) > 1)
60             self::$sourceBranch = $args[1];
61
62         pake_echo('---'.self::$libVersion.'---');
63     }
64 }
65
66 }
67
68 namespace {
69
70 use PhpXmlRpc\Builder;
71
72 function run_default($task=null, $args=array(), $cliOpts=array())
73 {
74     echo "Syntax: pake {\$pake-options} \$task \$lib-version [\$git-tag]\n";
75     echo "\n";
76     echo "  Run 'pake help' to list all pake options\n";
77     echo "  Run 'pake -T' to list all available tasks\n";
78 }
79
80 function run_getopts($task=null, $args=array(), $cliOpts=array())
81 {
82     Builder::getOpts($args, $cliOpts);
83 }
84
85 /**
86  * Downloads source code in the build workspace directory, optionally checking out the given branch/tag
87  */
88 function run_init($task=null, $args=array(), $cliOpts=array())
89 {
90     // download the current version into the workspace
91     $targetDir = Builder::workspaceDir();
92     $targetBranch = 'php53';
93
94     // check if workspace exists and is not already set to the correct repo
95     if (is_dir($targetDir) && pakeGit::isRepository($targetDir)) {
96         $repo = new pakeGit($targetDir);
97         $remotes = $repo->remotes();
98         if (trim($remotes['origin']['fetch']) != Builder::sourceRepo()) {
99             throw new Exception("Directory '$targetDir' exists and is not linked to correct git repo");
100         }
101
102         /// @todo should we not just fetch instead?
103         $repo->pull();
104     } else {
105         pake_mkdirs(dirname($targetDir));
106         $repo = pakeGit::clone_repository(Builder::sourceRepo(), Builder::workspaceDir());
107     }
108
109     $repo->checkout($targetBranch);
110 }
111
112 /**
113  * Runs all the build steps.
114  *
115  * (does nothing by itself, as all the steps are managed via task dependencies)
116  */
117 function run_build($task=null, $args=array(), $cliOpts=array())
118 {
119 }
120
121 /**
122  * Generates documentation in all formats
123  */
124 function run_doc($task=null, $args=array(), $cliOpts=array())
125 {
126     pake_echo('TBD...');
127 }
128
129 function run_clean_dist()
130 {
131     pake_remove_dir(Builder::distDir());
132     $finder = pakeFinder::type('file')->name(Builder::distFiles());
133     pake_remove($finder, Builder::buildDir());
134 }
135
136 /**
137  * Creates the tarballs for a release
138  */
139 function run_dist($task=null, $args=array(), $cliOpts=array())
140 {
141     // copy workspace dir into dist dir, without git
142     pake_mkdirs(Builder::distDir());
143     $finder = pakeFinder::type('any')->ignore_version_control();
144     pake_mirror($finder, realpath(Builder::workspaceDir()), realpath(Builder::distDir()));
145
146     // remove unwanted files from dist dir
147
148     // also: do we still need to run dos2unix?
149
150     // create tarballs
151     chdir(dirname(Builder::distDir()));
152     foreach(Builder::distFiles() as $distFile) {
153         // php can not really create good zip files via phar: they are not compressed!
154         if (substr($distFile, -4) == '.zip') {
155             $cmd = 'zip';
156             $extra = '-9 -r';
157             pake_sh("$cmd $distFile $extra ".basename(Builder::distDir()));
158         }
159         else {
160             $finder = pakeFinder::type('any')->pattern(basename(Builder::distDir()).'/**');
161             // see https://bugs.php.net/bug.php?id=58852
162             $pharFile = str_replace(Builder::libVersion(), '_LIBVERSION_', $distFile);
163             pakeArchive::createArchive($finder, '.', $pharFile);
164             rename($pharFile, $distFile);
165         }
166     }
167 }
168
169 /**
170  * Cleans up the build directory
171  * @todo 'make clean' usually just removes the results of the build, distclean removes all but sources
172  */
173 function run_clean($task=null, $args=array(), $cliOpts=array())
174 {
175     pake_remove_dir(Builder::buildDir());
176 }
177
178 // helper task: display help text
179 pake_task( 'default' );
180 // internal task: parse cli options
181 pake_task('getopts');
182 pake_task('init', 'getopts');
183 pake_task('doc', 'getopts', 'init');
184 pake_task('build', 'getopts', 'init', 'doc');
185 pake_task('dist', 'getopts', 'init', 'build', 'clean-dist');
186 pake_task('clean-dist', 'getopts');
187 pake_task('clean', 'getopts');
188
189 }