Create global .gitignore for user settings

ignored

When it comes to ignoring files in a git repository I do something I think many others have done.  I’ve added user level settings files to my .gitignore because I don’t want them included in my git repository.  You know, the files created by an IDE, operating system, or other applications.  Such as ‘.project’ created by Zend Studio or ‘.idea’ created by PHPStorm, and there are many others.

While it may be acceptable to add them to the .gitignore file in a private repo, should they be ignored in a publicly shared repository?  To answer this let me explain that I believe in “freedom”, and think everyone is entitled to do what they want, as long as it doesn’t hinder someone else’s freedom.  This is important when it comes to code.  Therefore, these files should NOT be ignored in the .gitignore of a public repo because someone creating a clone of the repo may desire to do something different.  So marking these files to be ignored, or not, should totally be a personal decision made by the user.

However, there is a way to have our cake, and eat it too.  We can inform our local instance of git to have a system global .gitignore file.  Therefore while the individual repository has a clean .gitignore file, with only references specific to the project, we can still have our user level ignores in place.  Here is how to do it.

From command line issue the following command: (command is non-Windows because of the file location, I’m not sure what it would be on a Windows machine, so perhaps someone can comment with a Windows friendly command)

$ git config --global core.excludesfile ~/.gitignore_global

This command creates a global setting in our systems git configuration informing git of an excludes file containing additional files to ignore globally.  The file name will be called ‘.gitignore_global’ and will be located in the users home directory.

Adding this –global config setting to git does not create the file for us, so we will still need to create this file in the location we specified. (The home directory in this case.)  Here is what mine looks like:

.idea
.buildpath
.project
.settings
.vagrant
.DS_Store
nbproject
.thumbs

Meanwhile my project level .gitignore might look like:

/vendor
local.config.php

For more on this topic, and perhaps a better explanation, please see https://help.github.com/articles/ignoring-files.

Developer pool sustainability

Developer hunting

Over the past couple years I’ve noticed a rise of good companies no longer outsource offshore to save money, instead they outsource because they can’t find developers here.  Notice, I said “good companies” because there are still poorly managed companies who believe the hype of offshore developers saving them money.  Since the poorly managed companies will eventually join the ranks of failures with their next social “thing”, we’ll stay focused on good companies here.

I’m sad to see the dwindling number of developers available to fill a growing number of jobs.  Local colleges and universities don’t seem to be helping, with outdated course material and terrible intern programs that aren’t helping prepare grads for “real” jobs after graduation.  Couple this with most companies and recruiters simply draining from the pool without giving back, and governments sinking more and more of our hard earned taxes into already flooded non-tech related fields.  The end result is higher unemployment, folks with a degree who can’t find work, and the vicious cycle continues on and on.

We have a pretty large tech hub here in South Florida, but we are losing developers without re-filling tomorrow’s developer pool.  Where are developers going?

  • Good students are picked up from companies outside of the area.
  • Other students tend to work in unrelated fields because local companies are unwilling to hire entry level and train. (myth of getting to market faster)
  • Experienced developers move to other areas with better culture and/or higher salaries.
  • Those developers who stick around often burn out after a few years because companies didn’t treat them well.

If we continually cut down trees in the forest we eventually find there are no more to cut down, leading to an environment where companies and recruiters head hunt among themselves to stay afloat.  Hint: This doesn’t help developers, it doesn’t help the community, and it doesn’t help companies (higher acquisition, training, and retention costs).

Companies removing developers from the market, but not contributing so other developers can be created is a fairly large problem.  It is not sustainable, and we need to make companies aware of this, to prevent a very dismal tomorrow.  The folks over at Tech.Nottingham wrote a great blog post about this, so go check it out for more details on how you and your company can start contributing for a better tomorrow. http://www.technottingham.com/news/2014/7/29/tech-companies-are-you-contributing-or-just-consuming

For companies or techs who doubt this, look no farther than the nearest event happening in your own back yard.  Look down…at the companies sponsoring these events.  Are they local companies?  Are they companies that are interested in growing the local community?  Is it your company?  I’ll close with that thought.

Fun with Travis CI and PHP projects

I know I should have done this a long time ago, but I finally got my hands dirty with Travis CI.  I wanted to set up a php project on github to use Travis CI to monitor the status, in case I forgot to run the tests prior to pushing.  Unfortunately it was not as easy as it’s made out to be.  But now that I’ve done it once, it’ll be easier next time.  So, here is how I tackled it.

First, creating an account and getting started was easy.  I simply clicked the “sign in” link on the Travis CI site and entered my github credentials which authorized Travis CI to connect to my account. (the site informs you exactly what Travis CI will have access to)  Once that’s done Travis gets all my repos, so I can then activate them for Travis CI.  If that doesn’t happen automatically there is a handy “sync now” button to coax Travis CI.

NOTE: To connect public repos you would use https://travis-ci.org, while for private repos you would use https://travis-ci.com.  While pubic repos are free private ones cost money, though you do get 100 pushes free to get you started.

Second, it’s now time to click the + and add a new repo to be tracked by Travis CI.  After clicking you will be presented with a list of your repos to choose from.  It is simply a matter of turning the repo ON by clicking the switch.  I also clicked the wrench to select the option to “Built only if .travis.yml is present”.

Travis CI add repo

The structure of the app I added looks like this: (including all files needed for travis and unit testing) https://github.com/adamculp/api-consumer

Application structure

Third, I needed to create the .travis.yml file with the directives needed to make it all work.  Here is what my file looked like.

travis.yml file

Pretty simple.  Here is what it all means: I specified what language to use (php), and what versions of the language to test with (5.3, 5.4, and 5.5), I also instructed to have Composer install prior to any other scripts run (needed to ensure there was an autoloader, created by Composer), and finally I add the phpunit command and tell it where to find the phpunit.xml file (in this case it was in the tests directory).

Fourth, ensure that PHPUnit runs as expected locally.  Yes, you will need unit tests on your code.  That’s like one of the main reasons to do this in the first place.  Here is what my phpunit.xml and bootstrap.php look like:

phpunit.xml file

The phpunit.xml file is fairly simple.  It informs where PHPUnit can find the bootstrap.php file (same directory as a the phpunit.xml), and sets a whitelist of directory where code can be found (some use a blacklist and specify not to use the /vendor directory), and what directory to find tests in (the current directory).

phpunit bootstrap.php file

The bootstrap.php file specified by the phpunit.xml file is even simpler, as it only specifies where to find the autoload.php file created by the Composer install.

Fifth, take a quick peek at the settings for the repo on github and you will notice that Travis CI should be already set up and ready for something to be pushed.

github settings

Final, that’s it!  That is everything needed for Travis CI.  Of course this example is very simplistic, since the repo and tests are only on a single wrapper class I created.  But it’s good enough for a start, and if you need more the documentation is pretty good.  With these files created, all that remains is for me to commit changes to git, then do a push to origin at github.  Once the push to origin happens a Travis CI trigger at github fires and informs Travis CI to create a new build on a VM (virtual machine) then run the tests on the newest code.  After Travis CI finishes it lets you know with an email, and through the site. (green = good)

Travis CI feedback

One last thing you may want to do is add the Travis CI image to your README.md at github.  This will allow you and others to see whether the current master branch had a successful build, or if it failed.

[![Build Status](https://travis-ci.org/adamculp/api-consumer.svg?branch=master)](https://travis-ci.org/adamculp/api-consumer)

Travis CI build status indicator

Enjoy!

Zend Framework 2 XML Sitemap

While tweaking the SunshinePHP site to be a bit more SEO friendly I realized we had neglected to create a sitemap for search engines to find.  I was pleasantly surprised to see the Navigation component of Zend Framework 2 includes a bunch of view helpers, including a Sitemap helper.  So now I have an xml sitemap created by Zend Framework 2 that works hand in hand with the site navigation.  However, the documentation was not complete as of this writing and caused me to do a bit of trial and error debugging to get it working.  Below I will post how I got it working, in hopes it will help others. (If the ZF2 folks like this post I will go in an update the documentation later.)  As with most things in Zend Framework 2 there may be more than one way to do things, but this is how I did it. (Until someone informs me of a better way.)

Module Config

In the Application module.config.php I created a factories node in the service_manager container where I pulled in the DefaultNavigationFactory.

factory

Then I also added a navigation container where I specified the sitemap for the site.

NOTE: To add navigation specific for each module you would simply create this container in the specific module.config.php.

navigation

Next I added a route for the future sitemap to be viewed.  Notice how I simply added a sitemapAction to the Application IndexController.  You can add it wherever you desire if you want to create a separate controller or whatever, I just left it there.

route

Layout

Because I just want the xml produced by the helper, I created a blank layout xml.phtml that does nothing more than output the content of the view.

layout

View

The sitemap.phtml view is also pretty simple and outputs the xml sitemap created by the helper.

view

Controller

In my controller I specified the layout to use, nothing more was needed.

controller

Verify

By navigation to the URL specified in the route we should now be able to view the XML output.

sitemap

Future

In this example someone would need to navigate to /sitemap to view the sitemap, but some automated tools would try to go to /sitemap.xml which would fail with this setup.  I will come back at some point in the future and enable the file extension to be ignored (after I figure out how).

Conclusion

The entire process is really pretty simple once the pieces are all in place, and the output was accepted by the various search engine webmaster tools…SCORE!