loreanvictor / tmplr

Automate Code Scaffolding
MIT License
26 stars 0 forks source link

`update` and `copy` commands don't work on Windows #29

Closed fwextensions closed 9 months ago

fwextensions commented 9 months ago

I really want to use this utility, but it doesn't seem to work reliably on Windows. I've got a super simple test .tmplr.yml file that looks like this:

steps:
  - read: pluginName
    prompt: What is the name of your plugin?
    default: Bloop Blorp

  - update: derp.txt

  - write: '{{ pluginName | kebab-case }}'
    to: ffs.txt

derp.txt:

{{ tmplr.pluginName | kebab-case }}

When I run npx tmplr preview in the Windows terminal, it prompts for the pluginName var and correctly writes it out to ffs.txt in the preview folder. But derp.txt doesn't get updated at all. I also had a copy command that I was trying that similarly had no effect.

If I run the same command inside a WSL shell, the derp.txt file is updated as expected. So this seems like a Windows vs. Linux issue. Has it been tested on Windows?

loreanvictor commented 9 months ago

Has it been tested on Windows?

Not specifically, the APIs should generally behave in a cross-platform manner, though there is always possibility of some little mistake 😅 Can you try the following recipe?

steps:
  - read: pluginName
    prompt: What is the name of your plugin?
    default: Bloop Blorp

  - read: derp
    from file: derp.txt

  - write: '{{ derp }}'
    to: derp.txt
fwextensions commented 9 months ago

Thanks, that does work. Isn't that what update should be doing?

Here's the copy command that also doesn't work:

  - copy: package-template.json
    to: out.json

The template file has several tokens like "name": "{{ tmplr.pluginName | kebab-case }}". The out.json file doesn't get generated at all when run in the Windows terminal, but it does when run in WSL. I noticed that tokens in the file that don't have the tmplr. prefix actually don't get replaced . I thought that prefix was optional, but maybe need to read the docs more.

loreanvictor commented 9 months ago

Thanks, that does work. Isn't that what update should be doing?

Yes, but they also support handling multiple files using glob patterns, which (I just learned) don't work smoothly in windows due to different path schema (\ is a separator in windows while an escaping character in minimatch which I was using for matching glob patterns).

I've now updated the fs lib to normalise paths and always use forward slash regardless of the platform, and the core lib to assume a POSIX format for file paths. I've also updated CI for these libraries (and the main repo) to run tests on windows as well, and have added an e2e test specifically reflecting this case.

@fwextensions Updating to tmplr@0.3.0 should fix this issue, please check and close this issue if it does.

I thought that prefix was optional, but maybe need to read the docs more.

Or perhaps I need to update the docs for further clarity 😅

fwextensions commented 9 months ago

Excellent, that worked! Thanks for the quick fix!

Funny seeing my ridiculous temp filenames enshrined in a test.

they also support handling multiple files using glob patterns, which (I just learned) don't work smoothly in windows due to different path schema

Yeah, that accident of history has caused a lot of heartache.

fwextensions commented 9 months ago

Re: needing the tmplr. prefix, here's the part that tripped me up:

For using variables in expressions, you don't need the tmplr. prefix.

Maybe emphasizing that that only applies within the YAML file, and not within other files that are processed.

loreanvictor commented 9 months ago

@fwextensions thanks for the tip, I tried to further clarify that section.