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

convert only if the matcha file has been modified #27

Merged
merged 2 commits into from
Dec 4, 2023

Conversation

perrygeo
Copy link
Contributor

@perrygeo perrygeo commented Dec 2, 2023

The current behavior is to re-render the templates every time. It's not a problem for resource usage (matcha is crazy fast!) but I use gleam fix and gleam format on my codebase. So that means I need to run fix/format on the codebase after every matcha call. That's fine but fix/format apply to the whole src tree so I end up "fixing" files that are opened for editing in other windows.

I suppose getting perfectly formatted output would be one way to solve it! But that might be a bigger lift.

The solution I'm using is to check the dates of the .gleam and .matcha files and only run convert if the matcha template was modified more recently.

This could also be a flag you want to keep the default behavior.

Hopefully you find this useful, thanks for you work on matcha!

@michaeljones
Copy link
Owner

Thanks for the work and the explanation. I can see the need. The fact that matcha produces unformatted output is not super ideal. I think timestamps are a very reasonable way to go and we might have to use that strategy. I don't love timestamps as they feel a bit unreliable in some situations and then we might have to have a --force flag to make matcha optionally ignore the timestamps. That might all be fine too.

A few different options:

  • I definitely prefer having content hashes to check if we need to redo the work but I can see that the formatting causes issues there. I guess that as Gleam doesn't care about whitespace we could tokenize the output and hash the list of tokens though we'd still need to parse strings properly as whitespace is meaningful inside those. Verdict: a bit too tricky probably.
  • We could also look at having a sidecar file or cache of some kind which stores hash of the input against the hash of the output in some fashion and then we wouldn't re-write if the output hash was the same as whatever is currently stored. Certainly not ideal and not something I'm aware of being used in similar solutions. Verdict: a bit ugly.
  • We could also look at supporting formatted output but deferring to gleam format if the gleam executable can be found in the environment. We could have a --formatted flag for matcha which would cause an error if gleam wasn't found but would otherwise generated formatted output so that further formatting wasn't needed and hash content checks would also work in that scenario. Verdict: non-standard but probably the cleanest of these three options.

I'd be happy to hear your thoughts if you have any. I realise it is also reasonable to just say "most build systems rely on timestamps, let's just do that" :)

@perrygeo
Copy link
Contributor Author

perrygeo commented Dec 2, 2023

reasonable to just say "most build systems rely on timestamps, let's just do that" :)

Yep, if it's good enough for GNU Make, it's good enough for me! :-)

I think formatting immediately after creating the file with the --formatted flag is also a good option too and stays backwards compatible. Most users are going to be running gleam format anyways. I'll leave it up to you. I've got my fork for now so I'm not in a hurry.

@michaeljones
Copy link
Owner

Yeah, happy to have slept on it but I suspect timestamps are the way forward here.

I think we can try to avoid the .unwrap calls. I'll look at adjusting it and merging over the next few days. Glad you've got something that is working for you.

And to use a direct comparison instead of the duration_since method
which I assume is ok.

Otherwise, we just and_then the results and exit early if we're not
getting access to the 'modified' times that we need to make the
decision. We conservatively require an update if we fail to access the
modified times.
@michaeljones michaeljones merged commit 84c7682 into michaeljones:main Dec 4, 2023
4 checks passed
@michaeljones
Copy link
Owner

Merged. I'll do a release shortly if all goes well. Thanks again for the thought and the work!

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

Successfully merging this pull request may close these issues.

2 participants