CodeSamplez.com

Programming, Web development, Cloud Technologies

  • Facebook
  • Google+
  • RSS
  • Twitter
  • Home
  • Featured
    • C# Tutorials
      • LinQ Tutorials
      • Facebook C# API Tutorials
    • PHP Tutorials
      • CodeIgniter Tutorials
    • Amazon AWS Tutorials
  • Categories
    • Programming
    • Development
    • Database
    • Web Server
    • Source Control
    • Management
    • Project
  • About
  • Write
  • Contact
Home Management Build PHP Web Application In One Step Using Makefile

Build PHP Web Application In One Step Using Makefile

Rana Ahsan August 17, 2014 Leave a Comment


 Build PHP Web Application In One Step Using Makefile    

If you are deploying your PHP web application manually by updating revision, setting proper permissions, running tests etc from command line, may be its time for you to better automate these things to save your time. In case you already tried and may be you are a little distracted to learn such build system like phing, well there is a good news for you that, you can avoid those third-party software and rather use the gnu-make software to do all the staffs to build php web application that you used to do by executing several terminal commands instead.

How To Use GNU Make?

As this usually comes with most of the Linux operating systems, there is no more headache to get it installed manually anywhere at all. All we have to do is create a file named ‘makefile’ and put our commands in there and execute them! It’s that easy! really! Well, you might be already excited to start some experiments, I am going to show you to do it in the right way with examples in next few moments.

What Are The Commands Usually Needed?

Well there could be a couple of tasks such as:

  • Update to latest version from source control repository.
  • Update PHP Dependencies to latest versions using composer.
  • Run PHPUnit Tests.
  • Update front end dependencies(like using bower)
  • Set permission on certain directories
  • Generate proxy classes for doctrine ORM using CLI command.

Note that, the order in which the commands are required to executed, is also important as well. So, we should better organize the commands into different groups and then execute them in specific order. Makefile facilitate this completely to create separate execution group and execute those in specific order. This may could be a reason, why we may prefer using makefile over a shell script as both seems similar in other cases.

Create Command Groups:

Lets categorize our necessary commands into meaningful steps. I will be using my codeigniterplus project’s build steps here as example so that you can use this one as exercise in case you don’t have any of your own project in mind at this moment.

  1. Update Source From Repository: Assuming the repo on a git source repository, the following commands will do the necessary updates most of the time without any issue:
    git reset --hard
    git pull origin master
    

    Just to warn you that, ‘reset’ command will undo all changes done in the repository directory which aren’t committed. So, any changes that might have been done by your application will be gone as well! so make sure to exclude such paths(cache/logs/file upload paths etc) from repository by editing .gitignore file.

  2. Installing The dependencies: Using composer to update your dependencies is as simple as the following command:

    #assuming you commit the lock file with latest stable dependencies
    # otherwise you may try 'update' command, but not recommended as that will have chance to fail
    /usr/local/bin/composer install
    
  3. Running PHPUnit Tests: Assuming that you are already using phpunit tests in your application, after all codes are updated, its time to run those tests to verify nothing is broken yet. If you are using XML configuration file, then running phpunit is as simple as:
    ./vendor/bin/phpunit
    
  4. Installing Front End Dependencies: If you are using front end package manager such as bower to install CSS/js libraries/frameworks, you will need to run that command as well:
    bower install
    
  5. File permissions: This step may not be needed all the times usually, but it’s still good to have in case you need to switch servers in cases, so that this step doesn’t get forgotten and keep your project broken. The following commands will do such task:
    chmod 765 application/logs
    chmod 765 application/cache
    chmod 765 application/models/proxies
    
  6. Generate ORM Proxies: In case you are using doctrine, you may want to have all proxy classes created at the beginning instead of on the fly. Then you are probably using this command:
    vendor/bin/doctrine orm:generate:proxies
    

It’s Now Time To Build PHP Web Application:

Now, if we bring all together in a single ‘makefile’, it should look like something as below:

.PHONY: all update-repo dependency-install unit-tests file-permission

all: update-repo dependency-install unit-tests file-permission

update-repo:
	git reset --hard
	git pull origin master

dependency-install:
	/usr/local/bin/composer update

unit-tests:
	vendor/bin/phpunit

file-permission:
	chmod 765 application/logs
	chmod 765 application/cache
	chmod 765 application/models/proxies

Now, we can run the command ‘make’ or ‘make all’ to execute all the commands in specific orders defined in ‘all: …’ section. You can also run seperate command group using ‘make unit-tests’ or ‘make dependency-install’ as well. We are using ‘.PHONY’ section just to avoid confusion with any other existing file named similar to the command group names.

Now, lets run our command ‘make all'(or only ‘make’) and you should see some output as below:

git reset --hard
HEAD is now at 9dbd0c4 * minor text changes.
git pull origin master
From github.com:ranacseruet/codeigniterplus
 * branch            master     -> FETCH_HEAD
Already up-to-date.
/usr/local/bin/composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Removing ocramius/instantiator (1.0.0)
  - Installing ocramius/instantiator (1.1.2)
    Downloading: 100%

  - Installing doctrine/instantiator (dev-master bbb57e0)
    Cloning bbb57e04abb15f4a4eb3778158f01280bbb982cd

  - Updating doctrine/dbal dev-master (6d0b048 => 83f92c3)
    Checking out 83f92c3f28e927917ddcb7cf214ce99c0b72004b

Writing lock file
Generating autoload files
vendor/bin/phpunit
PHPUnit 4.4-gaef6cd6 by Sebastian Bergmann.

Configuration read from /Users/Rana/Sites/codeigniterplus/phpunit.xml

.

Time: 38 ms, Memory: 2.50Mb

OK (1 test, 1 assertion)
chmod 765 application/logs
chmod 765 application/cache
chmod 765 application/models/proxies

And finally you can see that our project is completely ready to explore, which was done in a single command instead of several!

Final Words:

You need to remember that, this makefile must need to be an ignored file so that it doesn’t get updated/modified during source update while executing itself. Also the user executing the make command, should have proper write permission on the file system as well. If you are practicing with my example project above, make sure to change the database settings and commit them locally, before running(otherwise doctrine ORM proxy generation step will fail).

Hope this will help you save time by implementing this single step build PHP web application. Happy coding 🙂

Related

Filed Under: Management Tagged With: make, php

About Rana Ahsan

Rana is a passionate software engineer/Technology Enthusiast.
Github: ranacseruet

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Email Subscription

Never miss any programming tutorial again.

Popular Tutorials

  • PHP HTML5 Video Streaming Tutorial
  • How To Work With JSON In Node.js / JavaScript
  • Generate HTTP Requests using c#
  • How To Work With C# Serial Port Communication
  • Facebook C# API Tutorials
  • LinQ To SQL Database Update Operations In C#
  • How To Work With CodeIgniter Pagination
  • How To Work With Multithreaded Programming In C#.NET Application
  • Get Facebook C# Api Access Token
  • How To Work With Codeigniter Caching In PHP

Recent Tutorials

  • Building Auth With JWT – Part 1
  • Document Your REST API Like A Pro
  • Understanding Golang Error Handling
  • Web Application Case Studies You Must Read
  • Getting Started With Golang Unit Testing
  • Getting Started With Big Data Analytics Pipeline
  • NodeJS Tips And Tricks For Beginners
  • Apple Push Notification Backend In NodeJS
  • Web Based Universal Language Translator, Voice/Text Messaging App
  • How To Dockerize A Multi-Container App From Scratch

Recent Comments

  • intolap on PHP HTML5 Video Streaming Tutorial
  • manishpanchal on PHP HTML5 Video Streaming Tutorial
  • Rana Ghosh on PHP HTML5 Video Streaming Tutorial
  • ld13 on Pipe Email To PHP And Parse Content
  • Daniel on PHP HTML5 Video Streaming Tutorial

Archives

Resources

  • CodeSamplez.com Demo

Tags

.net apache api audio aws c# cache cloud server codeigniter deployment doctrine facebook git github golang htaccess html5 http image java javascript linq mysql nodejs oop performance php phpmyadmin plugin process python regular expression scalability server smarty ssh tfs thread tips ubuntu unit-test utility web application wordpress wpf

Copyright © 2010 - 2021 · CodeSamplez.com ·

Copyright © 2021 · Streamline Pro Theme on Genesis Framework · WordPress · Log in