FallenRayne Dev One developer's random thoughts are better than nothing.

28Dec/100

Phing – An example build script menu system

Got a little delayed on this post due to trying to find a decent code formatter for WordPress.  So here is my example menu script that I built with Phing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?xml version="1.0" encoding="UTF-8"?>

<project name="test" default="main" description="A test build script to see how things work.">    
    <property name="choice" value=""/>

    <target name="main">
        <!-- Main Menu -->
        <echo>Welcome to the Phing Build System</echo>
        <echo>---------------------------------</echo>
        <echo>1) Build Scripts</echo>
        <echo>2) Misc. Scripts</echo>
        <echo>x) Exit</echo>
        <echo>---------------------------------</echo>
       
        <input propertyName="choice" validargs="1,2,x"/>
       
        <if>
            <equals arg1="${choice}" arg2="1" />
            <then>
                <phingcall target="buildscripts"/>
            </then>
           
            <elseif>
                <equals arg1="${choice}" arg2="2" />
                <then>
                    <phingcall target="miscscripts"/>
                </then>
            </elseif>
           
            <elseif>
                <equals arg1="${choice}" arg2="x" />
                <then>
                    <echo>Thanks for using the Phing Build System!</echo>
                    <php expression="exit();"/>
                </then>
            </elseif>
        </if>
    </target>
   
    <target name="buildscripts">
        <!-- Build Script Menu -->
        <echo>Build Scripts</echo>
        <echo>---------------------------------</echo>
        <echo>1) Normal Release</echo>
        <echo>2) Bug Release</echo>
        <echo>x) Back</echo>
        <echo>---------------------------------</echo>
       
        <input propertyName="choice" validargs="1,2,x"/>
       
        <if>
            <equals arg1="${choice}" arg2="1" />
            <then>
                <phing phingfile="normal_release_build_script.xml" inheritRefs="true" target="main" />
                <phingcall target="main"/>
            </then>
           
            <elseif>
                <equals arg1="${choice}" arg2="2" />
                <then>
                    <echo>This should call the bug release build script.</echo>    
                    <echo>That script should house what is needed to execute a build.</echo>
                    <phingcall target="main"/>
                </then>
            </elseif>
           
            <elseif>
                <equals arg1="${choice}" arg2="x" />
                <then>
                    <phingcall target="main"/>
                </then>
            </elseif>
        </if>
    </target>
   
    <target name="miscscripts">
        <!-- Misc Script Menu -->
        <echo>Misc Scripts</echo>
        <echo>---------------------------------</echo>
        <echo>1) Do something</echo>
        <echo>2) Do something else</echo>
        <echo>x) Back</echo>
        <echo>---------------------------------</echo>
       
        <input propertyName="choice" validargs="1,2,x"/>
       
        <if>
            <equals arg1="${choice}" arg2="1" />
            <then>
                <echo>This should do something really cool.</echo>    
                <phingcall target="main"/>
            </then>
           
            <elseif>
                <equals arg1="${choice}" arg2="2" />
                <then>
                    <echo>This should do something cooler than the last thing.</echo>    
                    <phingcall target="main"/>
                </then>
            </elseif>
           
            <elseif>
                <equals arg1="${choice}" arg2="x" />
                <then>
                    <phingcall target="main"/>
                </then>
            </elseif>
        </if>
    </target>
</project>

So, here is a quick explanation of what is going on here. First off, the Project tag sets up the project and defines with the default target will be. A target is basically a function call. I then instantiate a property called "choice". This will hold the menu option that is selected by the user. Target main, the default target, is the main menu. You can see the echo tasks that display the menu. The input task asks the user for a value and that value has to be 1,2, or x. The if statement looks at the selected value and each value leads to either a new menu, a task, or exits. For example, if 1 is chosen then the task phingcall will call for the buildscripts target to execute. This will bring up the menu for buildscripts. If 1 is chosen in buildscripts then first it will call for a different build script file, execute that, and then it will return back to the main target with a phingcall task.

The overall goal of this script is to keep the menu up all day long and just have all of the common tasks built into this. I find this very handy on my Ubuntu box just because I always forget where all of the files I need are located and what commands I need to run for certain tasks. If I use a menu system in Phing then I don't have to remember any of that and I can automate some of my more tedious tasks (like database backups).

If there is a better way to set this up or if you have any questions about this script please drop me a line or leave a comment. I am always looking for ways to improve my processes and products.

Hope this helps someone out! Next week I will probably be just talking about my new phone (I get a brand new EVO 4g on Saturday!) and the apps/customizations that will be done. I especially want to play around with Tasker, that app looks amazing!

Everyone have a happy New Year!

22Dec/100

Phing – A quick intro

Phing is a build system that is entirely built using PHP and is based off of Apache Ant. I have been checking Phing out a bit lately due to needing to automate a bunch of tasks at my job. I kept seeing Phing mentioned on various websites and decided that it could be a pretty good choice after seeing what it was capable of. The tasks we want to automate are:

  • Transferring data from one database to another regularly
  • Doing weekly releases, sending e-mail upon completion to our users and the help desk
  • Running PHPUnit tests on a regular basis
  • Building PHPDoc for every release
  • Bringing on new customers (database changes, file changes, account creation, etc...)
  • Hiring new developers (account creation, directory creation, svn checkouts, etc...)

As you can see, we have a decent little laundry list of things that we could automate and with some research I figured out how to do all of these with Phing. The big selling point for us was the fact that Phing can be extended using Tasks. Tasks are basically PHP scripts that are run in Phing through XML tags. This makes anything that is possible for PHP to do for Phing to automate. Phing can send e-mail, manipulate the file system, access APIs, make database changes, and even build a menu system so that all of your common tasks can be accessed from one script even if the scripts are separated into multiple files.

Next week I am going to show an example build script that I worked on that makes a basic menu system that you can leave up all day to accomplish all of your common command line tasks. I used to have one written in Bash but I seemed to have misplaced it and it only ran on Linux anyways. With Phing I can run the build script on Windows, Linux, or Mac.  Better yet, I can even run it on my Ubuntu machine and have it SSH into my Windows machine to accomplish additional tasks if need be.