-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support single-file templates #35
Comments
#10 is the oldest and now contains enough info for implementation. |
Could someone explain how this can be accomplished? I have looked through the linked issues/PRs and do not see a reference on how to support single file templates. It appears this is no longer an issue, but see no good examples. /cc @michaeljoseph |
Hey, this issue has the duplicate but isn't a duplicate anymore. I would remove the tag but I'm not allowed (only maintainers). |
I've added a pull request for a first cut of this feature. |
Oops, accidentally self-hooray-emoji'd my own messages while trying to work out what the emoji meant "Hmm, what does that pizza emoji mean?". Please ignore... |
@talwrii Can you please reference the PR. |
Found it: #885 |
@talwrii I tried to rebase this branch on master: https://github.com/machinekoder/cookiecutter/tree/talwrii--freestanding-files--issue-35--2017-01-06 |
Cool cool. Thanks for the heads up. Hmm... I'm not sure what I think about this at the moment. I'm a little unhopeful about it getting it merged. I made the pull request more than a year ago for an outstanding issues with some discussion and tried quite hard to jump through the checklists and be a responsible citizen. Also I'm rather disinclined to push too hard myself because of behaviour like this from the project maintainers: "@vaab, this is your first and last warning about Code of Conduct violations against other contributors to Cookiecutter. The item of the Code of Conduct you are specifically violating is: [...] Any future violations will result in an immediate banning from this project. While we encourage debate, we refuse to condone this kind of ongoing abusive behavior. We wouldn't allow this kind of treatment as paid employees, and we certainly won't take it as volunteers for an unpaid project." (though perhaps this is a rather extreme example). On the other hand I use this feature semi-regularly (though only for a couple of purposes) and find it useful. So will likely maintain this feature as a fork or implement it in some other means. |
Hello All. So I'm not sure if this is spam or not but I've been working on a small project (cookie) born out of the desire for this feature, so I thought everyone else might benefit from it. The project's real focus has been allowing the quick initialization of scripts, but it works well with all filetypes. The template engine (if you can even call it that) is extremely minimal but it suffices for most of my purposes. Whereas cookiecutter requires a more extensive setup and is more full-featured, I've been trying to focus on keeping the interface as quick and minimal as possible. |
@bbugyi200 An interesting idea. I think this might be a good approach to getting this sort of feature into some sort of "maintained" repository. Although running my own patched, out-of-date cookiecutter with this feature seems to be working fine for me :). Some thoughts (I'm being a little opinionated here so would encourage you to take this all with a heap load of salt).
Update: So I've just had a look at your tool to understand what is going on:
So your tool does feel a lot more "unixy" and a lot less monolithic. I can see why you wrote it. I do find being able to list my cookies useful. Also I'm suspicious that this all falls into "convenience" category... I guess loose coupling would be good. Does anyone have opinions. I do realise I've introduced a whole load of discussion on another tool onto the cookiecutter list... oh well. |
@talwrii Thanks for such detailed feedback! I've tried to address each point as best as I can. Let me know if you have any other questions or advice!
Hmmm. I've never really thought about using multiple files (outside of a full-fledged cookiecutter project). I think the real value-added of the project how it makes writing system scripts easier. Some kind of multiple file feature would definitely be possible though if there was a lot of desire for it.
I would definitely be concerned about "reinventing the wheel" if I decide to add any more jinja-like templating features, but I don't think that single files will require such complexity. As far as
I'm not sure what you mean when you say that "listing your templates becomes difficult". In fact, after reading your feedback, I thought that listing templates would be useful so I added a new command-line option: running |
In your documentaiton, you seemed to use aliases for running templates. I was thinking that listing just these aliases might be difficult. |
@talwrii Ahhh. I understand now. Yeah that might be a bit harder. If they were just aliases you could use |
I realise this is a bit of a hack, but to get similar functionality as described, i.e. that of generating a single file, I just let cookiecutter create the directory like normal and then copy the file back a directory. In my instance I just wanted a docker-compose file, so by post_gen_project.py looks like so: import os
import shutil
PROJECT_DIR = os.path.realpath(os.path.curdir)
PARENT_DIR = os.path.dirname(PROJECT_DIR)
os.rename(os.path.join(PROJECT_DIR, 'docker-compose.yaml'),
os.path.join(PARENT_DIR, 'docker-compose.yaml'))
shutil.rmtree(PROJECT_DIR) The trick is not to create a directory that is going to be present already, but the user can override that at generation time. |
Too bad this is not supported yet. It would be very nice to have native support. I solved the issue with with a post gen hook shell script but i'ts a hack. cp -rf . ..
rm -rf -- "$(pwd -P)" Furthermore, it requires that the directory name does not already exist. |
I had forgotten that a hook could just be a shell script! Much cleaner solution. |
For any wayward souls who find their way here. You can use this little YMMV/alpha/works-on-my-machine In your template, put the following in the file # hooks\post_gen_project.py
import os
import pathlib
os.chdir("..")
p = pathlib.Path("{{cookiecutter.file_name}}")
f = p.joinpath("{{cookiecutter.file_name}}.py")
f.rename("./{{cookiecutter.file_name}}.py")
p.rmdir() |
@jensens is there any recommended solution for the single file challenge that ended up in the docs. I guess not. The last entry was NOT 10 years old, but from 2022. It would be a good idea to wrap this up this up in a final comment or a sticky one at the top with a hint at the end and maybe point to an external discussion or duplicate ticket. Unfortunately I am not at the point of enlightment to do this (new to cookiecutter). Maybe later. Unless this may happen, any hints to tackle single file stuff with the current cookiecutter version? My personal use case is to create a subtemplate to inject additional yaml subconfigs (using the python extension for pyyamlinclude) in an existing subdir and then add them as an include to a main yaml config one directory higher. But this should live only deeper in the tree and be generated by the parent cookiecutter but not included (isolated). Ince we also need to create xml and bat files outside of the generated project config, the approach to generate locally and then move it around with post creation hooks may work (or not) Any hints welcome... |
I'm trying to do this with GitHub gists: |
docs: updating readme
This may be out of scope for you, but personally I like the ability to instantiate single files—such as scripts—without any containing directories. As far as I understand, this is currently not possible with Cookiecutter. Could be a magic option in
cookiecutter.json
, like"__single_file__": true
.The text was updated successfully, but these errors were encountered: