Laravel Makefiles Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back guys in this course we're going to learn how to build make files a common problem you will face when working on any project is how to build and work with your project by using make files you can forever change how you build your projects stop wasting your time running your commands to build your projects manually it's not efficient today you might remember all the commands that you need to build your projects but few months down the road you will forget lots of them for this course we will be using Docker if you're not using Docker you can still follow along the important part is you know how to create make files for your laravel projects if you're completely new to Docker I highly recommend you watch my other videos relating to building Docker for laravel before we get started don't forget to subscribe to my channel to stay up to date with my latest training videos the end result of this course is to build a make file template that you guys can use as a basis and a reference point for all your future laravel projects I'm just going to give you guys a quick demo I am inside one of my level projects let's go ahead and write make and here you can see all my make commands I have a make help section make PS Fresh Start stop restart and the list goes on let's go ahead and take a look at make fresh so what make fresh does it destroys all our Docker instances and it recreates all our containers now without a make file to do this to stop your container you need to write Docker compose down that's one command you also need to delete your containers and depending on how many containers you have implemented you need to write Docker RM and you need to remove each individual container you also need to delete your volume and once that's done you need to build your instance and you need to recreate it so there's a lot of commands that you can run manually or here I can just write make fresh now that's done running you can see here we executed a lot of commands Docker commands we were deleting our images here we we built our containers and we started them here just using this command will save you a lot of time instead of manually calling them now let's go ahead and write make and see what other commands we have let's take a look at this command the cache clear and the cache if your laravel projects involve caching there's probably at least four or five different types of caches that you can cash in laravel you can sit down and call each cash command manually or you could just use the make file here so you guys can see that the make file will make your life much easier and it will also help other people to run your project before you attempt to install make let's go ahead and make sure that we don't already have it installed on our server I am using WSL for Docker on Windows I highly recommend that you guys use WSL if you're not using WSL I highly recommend you watch my last video on this to check if make is installed go ahead and try make and here I'm getting this error command make not found and here it gives you the instructions on how to install it let's go ahead and copy this so sudo apt install make once that's done go ahead and run make and you should be able to see this error here no target specified and no make file found all right guys so for this course we're going to use one of my GitHub projects that we used in our last tutorial let's go ahead and go to Google and here you want to search GitHub emad zamut and go ahead and click on the first link you should be inside my GitHub account go to repositories and select the laravel 9 complete course blog implementation copy the URL here if you're using Windows you want to go ahead and open your wsl2 terminal and here we're going to create a folder for our project let's start off by doing ls-la and I'm going to create my project inside my workspace folder and here let's go ahead and create a new directory I'm going to call it laravel blog let's go ahead and go inside our new directory and you want to write get init then you want to do get pull and paste the URL here once that's done go ahead and open vs code so to do that in the terminal you write code and a DOT and in here you should see all our project files let's go ahead and open a new terminal in vs code and let's make sure that we are inside our WSL tool instance and you can see we are inside our Linux distribution in order for us to start using make we will need to create a new make file inside our main project directory so in here I'm going to create a new file let's call it make file now inside our make file each command that you want to make it's pretty much called a Target let's go ahead and create few lines here and let's create a new Target so we're going to create a command let's call it help and in here let's go ahead and add an echo and let's just say help now let's go ahead under it and create another Target call it temp just for now and let's go ahead and Echo temp now let's go ahead and go back to our terminal and let's run make now if you take a look here we ran our help command make files by default will always run your first Target if you don't specify any so here we run make if you say make help for example or make temp it will run the specific command in here if you don't mention a command it will run your first Target a very common error that you guys will encounter when using make files is if you use spaces instead of indentation and here we indented this line by a tab let's just see what happens when you use spaces instead now I'm gonna run make and you should see this error that says missing separator so it's very important that you indent your Lines by tabs not spaces Also let's go ahead and run make again we can see the command that we ran so this one is running Echo help if you don't want to display the commands that are running inside your Target in here you just add an add sign now let's try to run make and you can see we no longer see the command that was executed now let's go ahead and erase what we have in here and I want to just explain one thing quickly by default when you have a make file and let's just say you write make help you will not see your commands in here it will execute the first Target so we need to generate our commands when someone types make we want to see what commands are available to do that we're going to use awk the awk command is a Linux tool and programming language that allows users to process and manipulate data and produce formatted reports for our make files we will use it to read our file and Echo some output text based on a specific pattern inside our make file let's go ahead and go back to our browser and you want to go to Google and here search for emad zamut z-a-a-m-o-u-t GitHub this is my GitHub account and if you go to repositories you want to search for this repository make file Auto generate help I have also added the inside our description and here you will see this make file and this is a starter make file that I added let's go ahead and copy this help section in here and let's paste it inside our code so here you can see I am using the awk Linux command and we're searching based on this pattern here you have your Target colon and two number signs and then you will add your description text so if we write make now you will see this output in here this here print help is coming right from the text description here also another problem that you will encounter when using make files is when you have a Target name that's similar to a file name in here for example you don't have to do this I'm just demonstrating a point I'm going to create a new file I'm gonna call it help so here we have a file in our project called Help inside our make file we have a command called Help let's go ahead and write make help let's go ahead and also write make you will see this output health is up to date so the whole purpose of make files is to build file files your targets are pretty much going to be physical files on your project if you want me not to interpret your command Target as a file name on the top you will write dot p-h-o-n-y colon and you want to specify your make Targets in here so I'm going to say help so what this does is say to make files this target is not a physical file it will just execute this command let's try it out let's try make help and now you can see it works let's try running make and you can see that it still works now just to demonstrate again you don't have to do this I'm gonna write another command I'm gonna call it temp I'm going to add a comment here and say temp in here I'm going to Echo Temp and I'm going to rename this help file to Temp and let's run make Temp and we see the error again so to fix that on the top we're gonna also add temp let's write make temp again and you can see that it works so the point is in here you want to add all your targets it's good practice to prevent it from checking uh physical files now I'm going to add few more helper commands for our make file when you're using docker lots of time you want to write Docker compost BS to see the status of your containers so let's make a command for that let's write PS and let's go ahead and execute Docker compose PS and here let's add a description text and let's say show containers let's give it a try and here I'm going to write make and let's write make PS and you should see that we're executing this command here it's up to you if you guys want to remove this add sign and display these commands I just find that sometimes it clutters the screen now before we dive further let's go ahead and go to our Docker compose file and let's just see what containers we have in here so we have a PHP container we have a database container and we have a node container and here we have given each container a name this one node this one's called database this one is called PHP and we also have a volume in here that's called DB Dash Vault so let's go to our make file and let's create variables for our container names so I'm going to create a new variable I'm gonna call it container underscore PHP and let's set this equal to to our PHP container name let's do the same thing for node and let's do the same thing for our database now for our volume and the volume is for our database so let's create a new variable call it volume underscore database let's set this equal to you want to make sure that you set it equal to your folder name so mine is called laravel Dash blog underscore DB Dash fall so this part here is coming from the volume name here and all the way this part here the larval Dash blog this is your project folder name so mine is called level Dash blog underscore to separate the project folder name and your volume name here and actually I'm just going to delete the temp file that I made now let's go ahead and scroll down I'm going to create another command to build our containers so it's going to be called build let's add a description here and let's just say we want to build all containers and here let's run Docker build no cache and add a DOT here so this will build all your containers let's go ahead and give this a try I'm gonna write make build and you can see that it works now let's go ahead and add two more quick commands one is going to be called start and this will be used to start all our containers so I'm gonna say here start all containers and we're going to run Docker compose up Force recreate you don't need the force your great flag but I find it useful and I'm going to add D for detached mode let's add stop let's add a comma here and say stop all containers and we're just going to run Docker compose down now let's go ahead and give this a quick try I'm gonna write make start and it looks like it worked let's just try make PS and make sure all our containers are running you can see our PHP container is running on Port 8000. let's give this a quick try in our browser so let's go to 127.0.0.1 at Port 8000 and you should be able to see your web page loading here we created this in our previous tutorial if you guys are curious you can log in in here the username is C4 at ahdcloud.com the password is password and here you can create edit and delete blogs and you can do the same thing for users let's go ahead and go back to our code editor now let's go ahead and create more commands I'm going to create one to restart our containers so let's give it a comment restart all containers and for this one I'm just going to call my other two Targets the Stop and the start and make files if you want to call other targets you have to do it on the same line where you create your target right after the column you can do it on the other lines here I'm gonna call stop first then I'm gonna call start now let's go ahead and create another command I'm gonna call it destroy and what this will do is it will delete all our containers and volumes but before we do that I'm gonna call my stop command first and let's add the comment in here and let's just say destroy all containers and to do that it's easy to delete Docker images we're going to write Docker RM Dash VF and you want to add your container name so we created those variables if you guys remember I'm going to add my PHP let's copy this line and paste it in two times and let's change the reference so One's Gonna Be for the PHP one for our database one for our node and to delete our volumes we're going to run Docker volume RM to remove it and the volume name here so let's reference our variable now let's just give this a quick try I'm gonna write make destroy now you can see here our Command works except for our containers here we're getting all these errors they're not found so the reason why none of these containers are deleted is because it's already deleted inside our stop command let's actually change the stop from down to stop what down does it deletes all your images and networks but the stop command will only stop your containers this way we will leave the part where we delete them to our destroy command now instead of doing all these manually you could just run Docker compose down and I'm just going to add this here as well the add sign let's give this a try I'm gonna write make destroy and now you see this error no such volume exists the reason why this volume doesn't exist is because we just run this command previously but it's a good idea to fix this by adding an if statement to prevent deleting the volume if it doesn't exist in here we're going to write if and this is Bash syntax so I want to say if we want to check if our container exists so we're gonna write dollar sign and let's add this in Brackets shell Docker volume LS and we want to filter by name and that's going to be our volume database and we want to make sure that Docker only Returns the name we don't want all the other fields so if this value exists then we want to run this command in here now let's go ahead and close our if statement if you guys see here I have these backslashes let me just remove them and show you what will happen without them so I'm gonna write make destroy all right now looks like we're getting this error unexpected in the file on line 28 and right here we just needed to add a semicolon that's right make destroy again and we still get that same error so in here you need to add a backslash and here you need to add another backslash if you try this it will work the reason why you need all these backslashes is that if you have a command that takes more than one line you need to add the backslash to the next line except for the last line let's write make so far we have all these commands one to build start stop restart and destroy our containers I'm going to write another one let's do it right under Start and here I'm gonna call it fresh so what this will do is we want to stop our containers we want to destroy our containers we want to build our containers and then we want to start our containers the fresh command is going to give us a fresh start we're destroying everything install starting from scratch let's add a comment in here and say destroy and recreate all containers let's give this command a try make fresh alright looks like it's working let's try make PS and you should see all our containers have a status of running now let's scroll down and let's create few more commands we want to write a command to help us cache all our laravel project let's call it cache and let's give it a comment cache project now since we're using Docker we have to run for example PHP Artisan cache inside our PHP container so we're going to write Docker execute we're going to reference our container PHP and we want to write PHP Artisan cash let's go ahead and copy paste this few times do view cache config cache events cache by the way the project is not really using events but the reason why I'm adding all these caches is just to provide you guys a reference point for this file and before we try this out let's add another command let's call it cache clear and this will clear our cache and let's just go ahead and copy all this let's paste it in here the first one is going to be cash clear the second one view clear config clear event clear route clear let's try this out so I'm gonna write make cash actually looks like I made a typo we don't need this first line right here let's remove it let's try this out make cash and you can see the command is working let's run make cash clear again guys I'm emphasizing the point is not the cache is here it is how to write commands um so you want to go ahead and enter all your values in here that you're using now let's create another command I think this one is going to be helpful so lots of times you want to write PHP Artisan migrate and another one for migrate fresh so let's create a new command let's call it migrate and let's add summer run migration files and here we want to execute in Docker inside our PHP container PHP Artisan migrate let's copy paste this again and let's do another one for fresh so I'm going to say clear database and run all migrations and in here we want to run PHP Artisan migrate fresh now for my project to build the front-end assets you need to run npm run Dev and npm run prod one of those so I'm gonna create commands for this so I'm going to say npm Dash Dev and this one let's give it a comment compile front assets for Dev and we want to write Docker execute inside our node container npm run Dev let's copy paste this and do the same thing for prod so instead of calling it npm Dev I'm going to call it npm-broad and here we're gonna write npm run prod actually let's make it production and let's just try one of those out I'm gonna right here make npm Dev and looks like here I'm having some errors well let's just scroll to the top and looks like here I forgot to uh adjust the name here so the migrate the second command let's add Dash fresh so it's gonna be uh one migrate and one migrate Dash fresh and here I'm getting this error because I did not run npm install it's a good idea to add also a command for that might as well so let's do npm Dash install let's give it a comment install front end assets and here we're gonna write Docker execute inside our node container npm install let's try this out so I'm gonna write make npm-install and it looks like it worked let's try to run make npm- Dev and you can see now it's working alright guys just few more commands when you're using Docker a lot of times you want to look up your Docker logs now to do that you can do for example Docker logs and your container name so let's go ahead and do that in here I'm gonna write logs and this one is going to print all Docker logs and it's pretty simple we're going to write Docker compose logs Dash if let's do another one logs for our PHP container and I'm going to say print all PHP container logs and we're gonna run here Docker logs and we're gonna reference our PHP container let's copy paste this two times one for node and one for our database so this one logs Dash node and we want to reference our node variable and here let's reference our database variable and let's change the name to database all right let's give this a try I'm going to write make logs Dash database and you can see our database logs in here now the last set of commands that I want to show you guys that might be useful is a lot of times you want to SSH into your Docker containers it would be nice to have made commands for those so let's create a command let's call it SSH Dash PHP and for this one let's add a comment and say SSH inside PHP container and let's write Dot worker execute minus it and our container PHP name sh let's give this a try let's do make SSH PHP and you can see we are inside our PHP container you guys gotta admit man this is much better than running these commands manually let's copy paste this few times one two one four node here let's change this to node let's update this to node and one for our database let's update this to a database and let's change the variable to database and that should be pretty much it I hope you guys found this tutorial useful don't forget to subscribe to my channel and if you found this tutorial useful I would greatly appreciate it if you guys hit that like button if you guys have any questions feel free to post it on the comment down below and we'll see you guys on the next video
Info
Channel: Emad Zaamout
Views: 1,100
Rating: undefined out of 5
Keywords:
Id: D4m0fTPw5Vg
Channel Id: undefined
Length: 21min 22sec (1282 seconds)
Published: Mon Oct 24 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.