Developing through packages in Laravel is a great way to achieve a structure layout and basis for better re-using your code. In this article I will show how you can use rsync to keep your development of packages in one place but keep synchronising to your main Laravel application for rapid testing.
I assume you are already familiar how to create packages in Laravel, if not it would be a good idea to get a understanding of that first.
While rsync comes out of the box in MacOS and Linux such as Ubuntu, you will have to install it if you are using Windows. This makes it slightly harder to use on Windows, but not impossible. I haven't tested myself, but you should be fine with cwRsync: rsync.samba.org/download.html
Create an environment for you package development. As I have all my websites located at
~/Websites I like to keep the packages in
~/Websites/Packages/<package> – the choice is yours.
In your Laravel application, in the
vendor folder, create a folder with the namespace you have given in your
name property of your file.
Now create a shell script in ie.
~/Websites, let's call it
auto_sync, again you can rename it to what you like. Make it executable by running this command in the terminal
chmod +x auto_sync (MacOS and Linux only).
We want to make the files synchronise without having to do manual work, so the approach here will be a loop that repeats itself every 2nd second, terminate the script to stop the synch process. The script, in your
auto_sync should look like this:
- This example is made for Mac, paths differ on Windows and Linux *
- Double check that this is your path to rsync:
while true do /usr/bin/rsync --archive --verbose --delete --stats /Users/<name>/Websites/Packages/<package name>/ /Users/<name>/Websites/<Laravel project>/vendor/<namespace>/<package name> sleep 2 done
package name should be in lower case.
All set. You can now execute it by typing this in your terminal:
./auto_sync. Cancel it again by closing the terminal or hitting cancel (
ctrl + c most often).
Be careful if you have a huge folder, as everything synchronises. It would also be smarter to watch for changes and only synchronise these. This is just a basic and free implementation of a folder sync.