sagiegurari / shell2batch

Coverts simple basic shell scripts to windows batch scripts.
Apache License 2.0
57 stars 7 forks source link

cp of a single file hangs on a prompt #5

Closed roblabla closed 5 years ago

roblabla commented 5 years ago

Describe the bug Currently, a copy of a single file, e.g. cp linker-scripts/bootstrap.ld link.T will generate the command xcopy linker-scripts\bootstrap.ld link.T. This command will prompt the user, staying stuck there forever:

Does link.T specify a file name
or directory name on the target
(F = file, D = directory)?

A simple way to avoid this would be to use copy for cp without -r. cp without -r can only have files in source (directory sources are ignored), which is exactly what the copy command on windows is for. This way:

cp -r a b
xcopy a b

cp a b
copy a b

With this solution, cp -r file1 file2 will stay broken, but I think that's an okay trade-off to make. Alternatively, cp a b could be turned into echo f | xcopy a b.

Here's a stackoverflow link about the whole xcopy on single files issue: https://stackoverflow.com/questions/33752732/xcopy-still-asking-f-file-d-directory-confirmation

To Reproduce

From cargo-make: Make a folder with a file1 file, then run the following Makefile.toml:

[tasks.default]
workspace = false
script_runner = "@shell"
script = ["cp file1 file2"]

It will hang until the user press F or D.

sagiegurari commented 5 years ago

i liked your echo f | solution better then have the copy directories broken. this way we support both file copy and recursive dir copy. right?

roblabla commented 5 years ago

I think so. I'm not super good at command-line windows stuff though, so don't take my word for it ^^'.

There are four scenarios off the top of my head that we need to support:

sagiegurari commented 5 years ago

ya, i'll check it out unless you want to check it out yourself and publish a PR? up to you

roblabla commented 5 years ago

I already have a fix coded up using the first solution, since I needed a quick fix to make progress on my project. I'll investigate the above cases when I'm around a windows machine sometimes tonight.

roblabla commented 5 years ago

Turns out, microsoft provides windows VM so I managed to test it right now. And the result is really sad:

C:\Users\IEUser\Desktop>xcopy /E desktop.ini test.ini
Cannot perform a cyclic copy
0 File(s) copied

So cp -r file file would not work under the echo f | xcopy solution.

roblabla commented 5 years ago

More fun stuff:

C:\Users\IEUser\Desktop>mkdir test

C:\Users\IEUser\Desktop>xcopy desktop.ini test
0 File(s) copied

it seems xcopy file directory does not work.

roblabla commented 5 years ago

copy has its own share of problem. It won't allow copying multiple files:

C:\Users\IEUser\Desktop>copy eula.lnk test.txt test
The syntax of the command is incorrect.

A stackoverflow solution:

for %I in (eula.lnk test.txt) do copy %I test

sagiegurari commented 5 years ago

so.... if there is no good solution, maybe completely different approach. shell2batch tries to convert commands, but sometimes you might want to help it out. so what if we had something like the following

some command
another command
problematic command //shell2batch: dos command

meaning, if a line ends with some comment and a prefix, we take the rest of the line as the replacement command. so users could define a specific shell command line replacement but only for specific problematic lines.

mkdir -p somedir
cd somedir
cp a b //shell2batch: copy a b
echo whatever
sagiegurari commented 5 years ago

also i wonder if /i solves the xcopy issues?

roblabla commented 5 years ago

/i doesn't solve the xcopy issue:

C:\Users\IEUser\Desktop>xcopy /i eula.lnk test2.txt
Does test2.txt specify a file name
or directory name on the target
(F = file, D = directory)?

Current discoveries, I'll edit this comment if I find any new ones:

cmd echo f xcopy/copy for + xcopy/copy
cp file file
cp -r directory directory
cp -r file file 🔴 🔴 🔴
cp file directory 🔴
cp file1 file2 directory 🔴 🔴
cp -r dir1 dir2 directory 🔴 🔴

if a line ends with some comment and a prefix, we take the rest of the line as the replacement command.

Independently of this issue, this might be a good idea. It would help big scripts that need to differenciate only for a couple commands. So that's a big :+1: for me.

However, I still think we should try to find something that works for cp. It's such a simple command, it'd be a shame if we couldn't find a good translation for it.

I'm currently thinking this: If it's a cp -r, treat it as an xcopy. If it's a non-recursive cp, use copy. And in both case, apply the for %I in (files) do copy %I dest loop if there's more than one non-flag argument. I think this would make all the common cases of cp work correctly.

(I'm half-tempted to attempt creating a shell2powershell to see if it's any simpler...)

sagiegurari commented 5 years ago

i'm for doing the cp changes and in addition doing the hint (as separate issue). as for powershell.... i'm not sure since if i remember correctly, you can't run ps files as is. you need to first enable powershell running files via some policy command.