Skip to content
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

feat: creating a file conditionally #751

Closed
RandalSchwartz opened this issue Feb 13, 2023 · 10 comments
Closed

feat: creating a file conditionally #751

RandalSchwartz opened this issue Feb 13, 2023 · 10 comments
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@RandalSchwartz
Copy link

Is there a way to conditionally generate a file based on a bool var? I don't mean just generating an empty file, but not even having the file. It's not clear to me how mustache fully works with filenames either, because I see some weird examples in items like bricks/plugin that start a mustache in the directory name and end it at the filename. Is that official?

@felangel
Copy link
Owner

Yup, you can check out the plugin brick as an example. The same conditional syntax within a file also works in file/directory paths.

@felangel felangel self-assigned this Feb 13, 2023
@felangel felangel added the question Further information is requested label Feb 13, 2023
@RandalSchwartz
Copy link
Author

I guess I need a bit more formal description of how that works. It looks to me like the mustache is started in the directory above, and continues to the file below. How is that even parsed? Is it like the recursive listing of the brick folder is treated as one flat file, and any newlines embedded within a resulting mustacche are equivalent to newlines in a mustache in a flat file, and only the output newlines delimit actual files to be built? That's about the only theory I can come up with, but did I get it? Also, what is {{.}}... I found it in the source code for mason, but I don't quite understand its undocumented usage.

@RandalSchwartz
Copy link
Author

Aha... I stared long and hard at the plugin example, and seem to have figured it out through trial and error. The / for a subdirectory ends up literally in the middle of a mustache as a forward slash, which needs to be the beginning of an end-tag. Weird, but I worked it out and got my code working.

@RandalSchwartz
Copy link
Author

I did it! First brick! https://brickhub.dev/bricks/codepad/0.4.0

@felangel
Copy link
Owner

That's awesome congrats! I'll make sure to work on doc improvements to address the areas you called out 👍

@RandalSchwartz
Copy link
Author

Yeah, if you could write a comprehensive guide, or at least a cookbook, on how to use mustache in filenames to (a) provide part of the name, (b) provide all of the basename, (c) provide the name of a directory, and then (d) how to conditionally generate a file, that'd be great. The first three are covered pretty well. The last one was the crazy part. Along with whatever {{.}} must be.

@felangel felangel added documentation Improvements or additions to documentation and removed question Further information is requested labels Feb 16, 2023
@vkammerer
Copy link

@RandalSchwartz I was also confused with the syntax for conditional files. But I just found out that you can use a backslash instead of a slash! If you're on MacOS, that keeps it a single file (not sure about Windows). It is also displayed as a single file when browsing your repo on Github.

@RandalSchwartz
Copy link
Author

Whoa?! Really!? ok... I gotta go test that out. A backslash is weird to have in a pathname because it'll freak out some tools, but well-written tools should be ok.

@RandalSchwartz
Copy link
Author

Wow... works great. New brick published!

@felangel
Copy link
Owner

felangel commented Mar 4, 2023

https://docs.brickhub.dev/brick-syntax has been updated to address the above topics 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants