Generator template locations

3 views
Skip to first unread message

devboy

unread,
Aug 17, 2011, 5:57:59 AM8/17/11
to gator-g...@googlegroups.com
While I am working on the generalization of generators to remove code duplication (https://github.com/devboy/gator-as3/commit/fcc1c57a73b7edebc6a776e51f57a7c0a16fd6fd).
I also started thinking about where to store our generator templates.
Right now a Generator only looks in the directory its Ruby file is in because we set the source root to that directory:
def self.source_root
  File.dirname __FILE__
end
But thor offers a nice way to lookup different paths in a specific order as you can see here: http://rubydoc.info/gems/thor/0.14.6/frames
As I am sure people want to have their own templates to suit their liking we have to offer something here.
A generator gem obviously needs to ship with templates to work out of the box, thats one location.
A user might want to change these template on a global scope and maybe also on a project scope.

Now we have to deal with 3 different locations where looking for a single filename for a template isn't gonna work as it is now:
template "klass.as.tt", File.join(src, "#{class_name}.as")

What about we change the lookup to something like this to prevent naming clashes:
"as3/klass.as.tt" & "as3/test/asunit4/klass.as.tt"

Then we can add multiple directories to the template-lookup and based on the order it would choose the first directory containing the template we are looking for.

So we could define a template directory on a project base (in the gator.rb file) which would be searched first.
Second place would be the global template directory (~/.gator/templates/generators/...).
And if the file couldn't be found there we will look for the template in the gems directory.

Is that a plan?

Simon Bailey

unread,
Aug 17, 2011, 6:18:43 AM8/17/11
to gator-g...@googlegroups.com
Sounds good man, v0.7 of sprouts had similar logic from what I recall and made project specific templates really easy by defining a scripts dir. What you suggested would make it even more flexible as you can name it anything you want in any location so long as the path is stated in the gator.rb.

Thumbs up from me.
--
Cheers,

Simon

[ http://newtriks.com ]


Dominic Graefen

unread,
Aug 17, 2011, 5:45:52 PM8/17/11
to gator-g...@googlegroups.com
This has been implemented while I overhauled the generators.

You can now add template directories in the gator.rb file with:

project.template_roots << "path/to/a/template/directory" 


-- 
Dominic Graefen
Freelance: Interactive Developer / Creative Technologist

Reply all
Reply to author
Forward
0 new messages