How do I generate a mesh in the editor?

I have a script that generates a mesh when attached to a game object. But the mesh is only generated when the game runs.

How do I get the mesh to be generated in the editor as well?

Something like this.