Many of us, there are situations when the project requires preparation for the conclusion of the production, and often this preparation consists of many actions that have to be performed many times (for example when assembling a release version). Often, tasks are reduced to executing simple console commands, clearing the cache, minifying JS and CSS, or assembling them into one file, but this whole routine can take quite a long time, and since it is performed, most often, at the end of the working day, it’s pretty easy to get sealed send a few days \ hours \ minutes of work on the project into oblivion. In such a situation, programmers are trying, how to automate the assembly and preparation of the project to the working state, in which various assembly systems help them, one of which will be discussed in the article.
1. Introduction
So, meet -
Phing .
A kind of PHP Ant Ant remix, but easier to learn, and at the same time powerful enough. Phing allows you to solve trivial tasks for PHP projects, has many built-in tools and a good
API , in case the built-in tools are still not enough. We can manage the assembly with the help of XML files, in which we describe the goals and tasks performed during the project assembly process.
2. Installation
Phing - the easiest way to install from the PEAR repository.
$ pear channel-discover pear.phing.info $ pear install phing/phing
Those who want to use the latest versions can install beta.
$ pear install phing/phing-beta
And for those who want to help the project, or just to see how it works - there is SVN (dev version of the product - may contain bugs!).
$ svn checkout http://svn.phing.info/trunk phing
You can also download more complete packages from the
download page on the official website.
3. Hello World!
Or rather, Hello Build!
Example (build.xml):
<project name="make_project" basedir=".." default="build"> <property name="src_dir" value="protected/" override="false"/> <property name="res_dir" value="public/" override="false"/> <target name="clear_cache_dir"> <echo> </echo> <delete> <fileset dir="${res_dir}/assets/"> <include name="**"/> </fileset> <fileset dir="${src_dir}/runtime/"> <include name="**"/> </fileset> </delete> </target> <target name="build" depends="clear_cache_dir"> </target> </project>
')
Let's try to parse the file by commands.
The above code declares a project with the name
make_project and the base directory, one level higher than the build file, and the original purpose of the
build . At the same time, the
build target has a dependency (
depends property) on the
clear_cache_dir target.
Even higher, we see a description of the
clear_cache_dir target. This goal has 2 tasks:
1.
echo - output the line “Clearing cache directories”
2.
delete - recursive cleaning of the / assets / and / runtime / directories
For interest, let's try to complicate the task.
Suppose now we need to compile .less files in the project.
We describe the task:
<target name="less_exec"> <exec command="lessc ${project.basedir}/${src_dir}/${current_file} > ${project.basedir}/${src_dir}/${current_file}.css" /> </target> <target name="compile_less"> <echo> .less </echo> <foreach param="current_file" target="less_exec"> <fileset dir="${src_dir}/"> <include name="**/assets/css/*.less" /> </fileset> </foreach> </target>
For
starters, let's consider a
fileset — a task that creates a list of files according to a certain criterion. Our
fileset has only one property
include (include files) with the pattern
** / assets / css / *. Less , where ** is a directory of arbitrary nesting.
That is, the
fileset has the following condition: search for files with the
.less extension
located in the
/ assets / css / directories, which in turn are in arbitrary directories nested in $ {
src_dir } (
see code above ).
Then we go through the list of found files using the
foreach task, sending each found file in the $ {
current_file }
variable (
param is the variable in which the current list value will be transferred to the target task ) to the
less_exec task (
target is the target task processing each list value ).
less_exec - just inserts the path to the file into a call to the
lessc command in the terminal, and specifies the path to the
.less file +
.css as the output file. as a result, all
.less files in the project will be compiled into
% filename% .less.css .
If you wish, you can write a task that renames these files into a standard .css or minifitsiruyuschim them, but we leave this as a home task to readers.
Just isn't it? And considering that Phing has quite extensive built-in tools (JsMin, JsLint, tar, and even tools for interacting with various VCSs), we can pretty quickly automate building, sending to the repository, or project backup.
4. Profit?
And in this case it is obvious - in half an hour we can automate the assembly of the project and save ourselves from routine operations. If standard Phing capabilities are not enough for you, you can create your own types of tasks, everything is done very simply and for a developer who understands PHP and XML is not difficult.
I was interested in Phing when I needed to compile
.less files into a regular .css before uploading it to a combat server. But I am sure that every developer has tasks for which Phing will be useful.