CaptnCodr / Fli

Execute CLI commands from your F# code in F# style!
MIT License
155 stars 5 forks source link

BASH issue #52

Closed ScottHutchinson closed 10 months ago

ScottHutchinson commented 11 months ago

I'm converting a bash script to an F# script to fix a problem with regex in the bash script. But after the regex runs, I need to run more code.

I'm trying to run code like shown below, but it's not doing anything, and it doesn't output anything. I have confirmed that pwd returns the expected current directory. When I run the same command in a bash script, it works as expected.

// 3. Fetch all the new/modified files into the delivery folder.
list_of_files
|> Seq.iter (fun filePath -> 
    cli {
        Shell BASH
        Command $"git show HEAD:{filePath} | install -D /dev/stdin {delivery_folder}/{repo_name}/{branch_name}/{filePath}"
    }
    |> Command.execute
    |> ignore

    Console.WriteLine $"Downloaded {filePath}"
)

The original bash script looked like this:

IFS="
"
for filename in $list_of_files; do
    trimmed_filename=$(echo ${filename} | xargs)
    git show HEAD:${trimmed_filename} | install -D /dev/stdin ${delivery_folder}/${repo_name}/${branch_name}/${trimmed_filename}
    echo \'${trimmed_filename}\'
done
ScottHutchinson commented 11 months ago

Actually, I just discovered that the script is doing something. It is creating files in the wrong path (due to mismatch between Windows and Linux/bash syntax), but the files are all empty.

CaptnCodr commented 10 months ago

Hi @ScottHutchinson, can you try to quote (\") the command again like shown below?

Command $"\"git show HEAD:{filePath} | install -D /dev/stdin {delivery_folder}/{repo_name}/{branch_name}/{filePath}\""

If this works for you, then I'll fix this really quick.

ScottHutchinson commented 10 months ago

Thank you for the prompt response. I will try your suggestion on Monday, since I am out of the office today. However, I think the only problem might be that the filePath and delivery_folder paths have the wrong slashes, and the delivery_folder path starts with c:... Instead of ~. Once I get it working, I'll post the solution, so you can judge if any improvements could be made.

Also it might be easier for me to run the git commands in PowerShell instead of bash, if I could use Windows paths throughout the whole script. Not sure though.

CaptnCodr commented 10 months ago

Not only that, I encountered a bug that just nothing happens when commands are executed in the BASH context. The workaround is just adding quotes at the beginning and the end. That will be fixed definately!

ScottHutchinson commented 10 months ago

can you try to quote (\") the command again like shown below?

Command $"\"git show HEAD:{filePath}

That did not seem to make any difference.

I corrected the path to match the format that worked in my bash script, but the ~ character resolved to "C:\Users\hutchinsons\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\LocalState\rootfs\home\scott\Deliveries" instead of "C:\Users\hutchinsons".

So, unless you have a better fix, I am going to replace ~ with '/c/Users/hutchinsons' or something like that.

ScottHutchinson commented 10 months ago

Replacing ~ with '/mnt/c/Users/hutchinsons' caused the files to be created in the correct destination. However, the content of all the files is 'usage: git [--version] [--help] [-C ] [-c =]...'. Adding the quote (\") as you suggested, causes it to create empty files instead.

CaptnCodr commented 10 months ago

Ok, then it's an issue of the base location as you already found out.

Depending on the location of your F# script file you can also use __SOURCE_DIRECTORY__ to locate your actual directory instead of using e.g. "C:\Users\hutchinsons".

I don't have another solution for you, it seems that's a little issue in locating directories between Windows and WSL Systems.

ScottHutchinson commented 10 months ago

Well, the path issue seems to be more or less solved, but the file content issue remains.

ScottHutchinson commented 10 months ago

It appears that the shell recognizes that I'm trying to run a git command, but it's not recognizing a valid git show command for some reason.

ScottHutchinson commented 10 months ago

I guess I will try using PowerShell instead of a bash shell, since I'm feeling like the BASH support in Fli might not be fully baked. There are no examples in the readme file, and there doesn't seem to be support for Arguments.

CaptnCodr commented 10 months ago

You could also try that one: Command $"\"git show HEAD:{filePath}\" | \"install -D /dev/stdin {delivery_folder}/{repo_name}/{branch_name}/{filePath}\""

If this problem still persists then just try it with PowerShell.

Bash might have some quirks I didn't really explore or even know. I will spend some time with bash the next few weeks to have a better integration into Fli.

CaptnCodr commented 10 months ago

But thank you for using Fli and don't hesitate to open further issues when you have any.

ScottHutchinson commented 10 months ago

You could also try that one: Command $"\"git show HEAD:{filePath}\" | \"install -D /dev/stdin {delivery_folder}/{repo_name}/{branch_name}/{filePath}\""

I tried that, but it did not create any files. I'll see if I can get Powershell to work. Thanks for your help.

ScottHutchinson commented 10 months ago

I think using the CMD shell is going to work, and it seems to be quite fast.

// 3. Download all the new/modified files into the delivery folder.
let branch_directory = $"{delivery_folder_windows}/{repo_name}/{branch_name}"
list_of_files
|> Seq.iter (fun filePath -> 
    let dest = $"{branch_directory}/{filePath}"
    let dest_directory = Path.GetDirectoryName dest
    Directory.CreateDirectory(dest_directory) |> ignore
    cli {
        Shell CMD
        Command $"git show HEAD:{filePath} > \"{dest}\""
    }
    |> Command.execute
    |> ignore

    Console.WriteLine $"Downloaded {filePath}"
)
CaptnCodr commented 10 months ago

I'm happy that you found a solution. Sometimes BASH is just weird with its pipe and you can't pipe everything into eachother.

I hope you have fun with Fli though. 🙂

ScottHutchinson commented 10 months ago

FYI. Here's a Gist for my finished script, which uses the CMD shell instead of BASH. I think being able to use BASH would have made the script more compact (fewer lines) and would have saved me some time developing it, but the finished script seems to run at least as fast and do everything that the original bash script did.

dart_git_delivery.fsx

CaptnCodr commented 10 months ago

That looks pretty nice. Thanks for sharing.