Sessions is temporarily moving to YouTube, check out all our new videos here.

Diving into Rake and its future in Rails

Brandon Burton speaking at North West Ruby User Group in August, 2017
JustUploaded
 
Great talks, fired to your inbox 👌
No junk, no spam, just great talks. Unsubscribe any time.

About this talk

We'll dive into the tools available inside the Rake library (hint: it's more than just rake db:migrate), and discuss how changes in Rails 5 might affect how we use it going forward.


Transcript


Diving into Rake and its future in Rails, yeah that's what we're going to discuss today, from the point of view of someone who doesn't really know Rake or is new to it or is new to Ruby on Rails and is sort of used Rake but doesn't know why they have to use it when they're setting up their environment or running Rake DB migrates, or something like that. So what is it? It's a task management tool that we use in Rails and in Ruby. It helps automate builds and it comes with a few main sort of task management I suppose tools, being FileTask and Task. They do two slightly different things. Task is what we mainly use to I suppose provide something to build up our environments, so our sort of Dev environment if you want to populate it with data, we'll probably create a task and then give it something to do in the block, and then, viola, out pops our information. Whereas a FileTask usually helps convert something, let's say for instance, HTML into an ePub file. If you're, you might be I don't know releasing a book on Amazon or something like that, that's what that might be used for. And again, yeah, it's really handy for setting up our environments, we use it a lot to set up our Dev environment, we're constantly dropping it, recreating it, adding new data, yeah. So, I'm going to kinda start from the somewhat basic approach I suppose, to creating a Task in Rake. And the way you can create it in a Rake file if you want, in the main Rake file right in the root directory in your app. The neater way to do it I think is to add it into your lib tasks path. And then, namespace it, give it something nice and descriptive. So in this case, we're gonna do, we're gonna rake some leaves as a chore. So we've got a chore file, which is namespace chore, and then running this task would be as simple as saying rake, chore, rake leaves. And then our output sort of appears. And then using this namespace and that sort of description command can be helpful later on, I mean it is not so necessary right now but it's useful for some of the commands that Rake provides you. So if you were actually working in a large Rails application you might loads of tasks, and loads of different files that you're running through, some that you just get from the start and some you add later, so it might come in handy, we'll discuss that later. Yeah, before I move to that slide, so this being very very basic, it's probably not very descriptive of actually what's involved for instance in raking leaves, a few things that you need to rake leaves you need to rake. You might need a paper bag as well to store the leaves in there's usually some element of travel as well, to get those things, to buy them, so one of the cool things that Rake allows is dependencies, so if we're actually building this task to kind of maybe more, mimic real life, raking leaves is actually gonna be dependent on a couple of things, one that we have a rake, and two that we've got those paper bags, that we can store those leaves in, and then to shuffle them off to the garbage man as they come around. So this is what it sort of looks like here. We've got the rake leaves task, which is gonna dependent on buying the rake, and buying the bags, and then both of those are dependent on going to the store, as we have to go to the store to then buy these things. Now what rake is also very good at is not running tasks multiple times, so if we run our task again, in this case here, so rake chore rake_leaves, you'll see the output from here starting from the go_to_store task, we've printed out once heading to the store, and then buy_paper_bags and buy_rake, we know that that task is now run, it's that dependency is satisfied, and then they can both run and thus you can complete it, rake leaves and you're happy. That's sort of the basic of that namespace, so moving on from creating the task and creating something kind of basic like this, we can jump into some neat tools that are provided. One of them being Rake Notes. So as part of your application, you're always especially in a larger one, you're always gonna encounter something that you, you might find that this needs reworking, this isn't right. But it's not within the scope of the ticket that I'm looking on right now. Well just, whatever item you're dealing with, it might be a really big rework, something that you've not really noticed, you might inherited the app, whatever it might be. Really great tool is adding a to do in a comment and then just, just listing something simple to bring your attention back to, and what you can then do is run rake notes in your terminal and it will search for anything that has todo, I believe fix me, and optimise as standard as default, and print those out in the console for you, so you can look and manage anything that might need rework, so for a later day, if you're saving things down, it's a good tool to use for that. And then as well, the other note is if you want something a bit different than todo, fix me, or optimise, you can add your own custom notes and annotations, so you provide the note and the annotation, it will go and find those notes to run your application. So Rake -D, so before we were adding the description to a chore rake_leaves task or just above it, this is what will actually pull it out. So you can output all of them, just by running that command by default, and it will print everything out that has a description in all of your tasks. So then you kind of search through there if you're looking for something specific or you might be able to if you kind of know the task you're looking for, then you can actually provide the task name, which holds that description which is under that description and it will find that for you. So if you I gave it specifically the rake_leaves task and then it just returned that one task for me. The other one is Rake -P, so it will actually list every task, if you sort of go, I think this one won't accept a pattern but it will print out everything, all of your tasks and all of their dependencies, and this, as I'll kind of show later on, is actually really helpful, or can be helpful in debugging your application or debugging a task that might not be working for whatever reason. So yeah, so that kind of shows you our little rake task rebuilt earlier. And sort of the late addition that I kind of came across is Rake Stats, which I kind of found really just, it's not immensely helpful but it's kind of good to know. You'll see here, in the application that I just sort of threw up, code to test ratio isn't great but it's good to know. Yeah I think a lot of people usually go out of their way to find a tool as well to kind of check that, that ratio, that metric, and so it's handy that it's, it comes default with Rake, you can actually just have a quick look. And sort of see where you will test it, where you're building those for your test as well. I know there's that testing pyramid that people like to try and follow so you can actually kind of see just by running this, if you are kind of following that type of metric, where most of your model, tasks, and features and so on, or integration. Yeah so before when I was describing that Rake P thing, I will sort of bring it in to how this helps with tracing in Rake. So the main way to trace in Rake, is by running the command dash trace or --trace to your tasks. So let's say we move that dependency in our previous task, which is just go_to_store, so there's a couple of things, a couple of items obviously that rely on it. So it's gonna, something's gonna break. When we run it initially, all the feedback we get is rake aborted, doesn't have know how to build the task go_to_store but when you're running rake chore, rake_leaves, you've got no idea that just from that one input that there might be a dependency go_to_store unless you've built that task yourself or built that namespace yourself. So you might, if you've got a sort of relatively small app, it might be easy, you might have a good tool for grep-ing and you can jump in, the other option is to run trace. So now this is really small and probably not great, probably should of not included this crap down there, but what this ends up doing is providing a little bit more information as to what Rake was trying to do when it broke, when it aborted basically. So you've got, invoke chore rake_leaves for the first time, and then invoke chore buy_rakes, so it's running down that sort of, that file in order, and it's looking at buy_rake and then stopping. So if we do want to kind of find out what's going on from here, what it's dependencies are, we can now run rake -p and have a quick look and see what does buy_rake depend on, oh it depends on go_to_store, and then we can, I think it was, if you run rake -w, and if you give a task, it will give you the file path and line number for it, so it's another little kind of jump ahead, so if you're still unsure as to where it is, you can't find it, you can actually run that and then we can go and then obviously fix issue, include go_to_store and everyone's happy. Yeah so I guess that's just a few that kind of maybe are standard tools that are really helpful with debugging in Rake. And then jumping on, so Rake is obviously, I'm not sure if everyone's aware, it's slightly changing how we use Rake, so we're still gonna use it going forward but it's actually gonna be encompassed in Rails 5 in the rails command. So previously, especially when you're new to Ruby on Rails, you'll be setup running rails dbmigrate I'm sorry and then rake dbmigrate and then you're using Rails to generate your models controls whatever it might be, your application in general, it's a bit of swapping around, and I knew for myself in being a beginner in Ruby on Rails, I didn't really know why, what the difference was, why run rake or why we run rails, so Rails 5 uses the rake proxy to sort of act as a wrapper around rake, and what it will do is when you run rails dbmigrate now for instance, it will check if that command is native to rails, and if it isn't, then it will hand it off to that proxy, and so then it will say, ah this is a rake command, so then it runs the command through there. So you see, things change from rake dbmigrate to rails dbmigrate, and then there's a few things as well to note, so I think now in Rails 5, you can actually also run rails roots rather than rake roots, you can provide it a path and because rails actually accepts arguments as well, you'll be using things like rails console and then provide it development. So you don't have to, it's a sort of, it's more intelligent way of I suppose providing or running commands, it can do quite a few things. It's a bit of an element of magic I suppose because now we're hiding yet another thing inside the application, which I know quite a few people don't really like about Rails I guess you could say, but it's really neat and I think it's, I think it's a neat tool for the beginners coming in and to make development a little bit easier and yeah swapping through you're just always running the same thing, just keep things consistent I suppose. And I suppose the other thing to note is there are a few rake tasks as well that actually use the rails namespace, so the rails rake proxy is done now, has actually renamed those, so they actually are, let's say rake rails update I think was one, or rake rails template, those are now, will be rails app template or rails app update, it's changing the names, just to I suppose just to be a bit more explicit I suppose in what they're doing. But yeah that was a little bit about my talk about Rake. So we just kind of went through building a really simple rake task, namespacing it, some of the neat tools you kind of get with Rake, as standard and how we can use that in some hopefully really helpful ways, and what then what it looks like in Rails 5 forward. Yeah that's me, thank you for listening.