Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Build Automation with Phive and Phing

Build Automation with Phive and Phing

PHPUG Cologne 2018-06-01

Thomas Weinert

June 01, 2018
Tweet

More Decks by Thomas Weinert

Other Decks in Programming

Transcript

  1. TOOLS FOR A PROJECT TOOLS FOR A PROJECT Which tool?

    Which version? How to call? What to do next?
  2. INSTALL PHIVE INSTALL PHIVE > wget https://phar.io/releases/phive.phar > wget https://phar.io/releases/phive.phar.asc

    > gpg --keyserver hkps.pool.sks-keyservers.net --recv-keys 0x9 > gpg --verify phive.phar.asc phive.phar > chmod +x phive.phar > sudo mv phive.phar /usr/bin/phive Shell
  3. PHING PHING based on Apache Ant Phar, installable using Phive

    IDE integration (PHPStorm, Netbeans, Eclipse) XML build file Extendable via PHP
  4. THE FIRST CONTACT II THE FIRST CONTACT II > git

    checkout [email protected]:awesome-project.git > cd awesome-project > phive install > tools/phing Shell
  5. COMPOSER PROJECT EXAMPLE COMPOSER PROJECT EXAMPLE > composer create-project someone/awesome-project

    new-project > cd new-project > phive install > tools/phing > git init && git add * && git commit -m"new project" > tools/phing run Shell
  6. A PROPERTY A PROPERTY <project name="A Project" default="build"> <property name="database.user"

    value="username"/> <target name="build"> <echo message="Database User: ${database.user}"/> </target> </project> XML
  7. PROPERTIES FILE PROPERTIES FILE <project name="A Project" default="build"> <property file="build.properties"

    prefix="configuration"/> <property name="database.user" value="${configuration.database.user}"/> <target name="build"> <echo message="Database User: ${database.user}"/> </target> </project> XML
  8. DISTRIBUTED PROPERTIES FILE DISTRIBUTED PROPERTIES FILE <project name="A Project" default="build">

    <property file="dist.build.properties" prefix="configuration"/> <property file="build.properties" prefix="configuration" override="true"/> <property name="database.user" value="${configuration.database.user}"/> <!--...--> </project> XML
  9. [property] Loading /path/dist.build.properties [property] Loading /path/build.properties [property] Unable to find

    property file: /path/build.properties... skipped A Project > build: [echo] Database User: default-user BUILD FINISHED Shell
  10. FILESET FILESET <fileset id="dependencies" dir="${directory.src}"> <include name="composer.json"/> <include name="composer.lock"/> </fileset>

    XML <fileset id="public" dir="${directory.document-root}"> <include name="**"/> <exclude name="**/.git/**"/> </fileset> XML
  11. COPY COPY <target name="build"> <copy todir="${directory.export}/source/htdocs"> <fileset refid="public"/> </copy> <copy

    todir="${directory.export}/source"> <fileset refid="dependencies"/> </copy> </target> XML
  12. CREATE DIRECTORY CREATE DIRECTORY <property name="mode.writeable" value="0777"/> <target name="prepare"> <mkdir

    dir="${directory.src}/data/cache" mode="${mode.writeable}"/> <mkdir dir="${directory.src}/data/media/files" mode="${mode.writeable}"/> <mkdir dir="${directory.src}/data/media/thumbs" mode="${mode.writeable}"/> </target> XML
  13. EXPAND PROPERTIES EXPAND PROPERTIES <property name="config.template" value="${project.basedir}/template.configuration.php" /> <property name="config.file"

    value="${project.basedir}/configuration.php"/> <property name="database.uri" value="sqlite3://project.sqlite" /> <copy file="${config.template}" tofile="${config.file}"> <filterchain> <expandproperties/> </filterchain> </copy> XML
  14. CONDITION CONDITION <if> <isfalse value="${hasConfiguration}"/> <then> <copy file="${config.template}" tofile="${config.file}"> <filterchain>

    <expandproperties/> </filterchain> </copy> </then> <else> <warn message="configuration.php already exists. Skipped."/> </else> </if> XML
  15. RUN 1 RUN 1 A Project > build: [copy] Copying

    1 file to /project/path/ BUILD FINISHED Shell
  16. RUN 2 RUN 2 A Project > build: [warn] configuration.php

    already exists. Skipped. BUILD FINISHED Shell
  17. GIT TAG GIT TAG <property name="revision" value="dev"/> <exec executable="git" dir="${project.basedir}"

    returnProperty="git.return" outputProperty="git.output"> <arg line="describe --tags"/> </exec> <if> <equals arg1="${git.return}" arg2="0"/> <then> <property name="revision" value="${git.output}" override="true"/> </then> </if> XML
  18. GIT DESCRIBE GIT DESCRIBE A Project > project-revision: [echo] Project

    revision: 4.0.1-14-g7597c11 BUILD FINISHED Shell
  19. PHP BUILTIN WEBSERVER PHP BUILTIN WEBSERVER <exec executable="php" passthru="true"> <arg

    line="-S localhost:8080 -t ./htdocs server.php"/> </exec> XML
  20. DEFINE ADHOC TASK DEFINE ADHOC TASK <adhoc-task name="composer-version"> <![CDATA[ class

    AVersionTask extends Task { function main() { $json = json_decode(file_get_contents("composer.lock")); foreach ($json->packages as $package) { if ($package->name === 'carica/io') { $this->project->setProperty( 'revision', $package->version.' '.substr($package->source->reference, 0, 8) ); } } } } XML
  21. USE ADHOC TASK USE ADHOC TASK <property name="revision" value=""/> <trycatch

    property="error"> <try> <adhoc-task name="composer-version">...</adhoc-task> <composer-version/> <echo message="Revision: ${revision}"/> </try> <catch> <echo level="warning">There was an error...</echo> <echo level="debug">${error}</echo> <property name="revision" value="unknown" override="true"/> </catch> </trycatch> XML
  22. A Project > package-revision: [PHP Error] file_get_contents(composer.lock): failed to open

    s [PHP Error] Invalid argument supplied for foreach() [line 6 of [echo] Revision: BUILD FINISHED Shell