Skip to content

Commit

Permalink
Use ArgumentList if available (netstandard2.1 and up) (#12)
Browse files Browse the repository at this point in the history
* Use ArgumentList if available (netstandard2.1 and up)

* Ensure new example is not packable
  • Loading branch information
Mpdreamz authored Jan 8, 2024
1 parent 57b271f commit 027a0c1
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 4 deletions.
8 changes: 8 additions & 0 deletions examples/ScratchPad.Fs.ArgumentPrinter/Program.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// For more information see https://aka.ms/fsharp-console-apps

open System

let args = Environment.GetCommandLineArgs()
printfn "%i" args.Length
for a in args do
printfn "%s" a
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<Compile Include="Program.fs"/>
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions examples/ScratchPad.Fs/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ exec { run "dotnet" args }
let _ = shell { exec "dotnet" args }
let statusCode = exec { exit_code_of "dotnet" "--help"}

exec { run "dotnet" "run" "--project" "examples/ScratchPad.Fs.ArgumentPrinter" "--" "With Space" }

printfn "That's all folks!"
7 changes: 7 additions & 0 deletions proc.sln
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Proc.Fs", "src\Proc.Fs\Proc
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "ScratchPad.Fs", "examples\ScratchPad.Fs\ScratchPad.Fs.fsproj", "{C33E3F7C-0C2A-4DD2-91E4-328866195997}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "ScratchPad.Fs.ArgumentPrinter", "examples\ScratchPad.Fs.ArgumentPrinter\ScratchPad.Fs.ArgumentPrinter.fsproj", "{50A40BEB-1C22-41CF-908F-F24FB34B1699}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -79,6 +81,10 @@ Global
{C33E3F7C-0C2A-4DD2-91E4-328866195997}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C33E3F7C-0C2A-4DD2-91E4-328866195997}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C33E3F7C-0C2A-4DD2-91E4-328866195997}.Release|Any CPU.Build.0 = Release|Any CPU
{50A40BEB-1C22-41CF-908F-F24FB34B1699}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{50A40BEB-1C22-41CF-908F-F24FB34B1699}.Debug|Any CPU.Build.0 = Debug|Any CPU
{50A40BEB-1C22-41CF-908F-F24FB34B1699}.Release|Any CPU.ActiveCfg = Release|Any CPU
{50A40BEB-1C22-41CF-908F-F24FB34B1699}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -92,5 +98,6 @@ Global
{D6997ADC-E933-418E-831C-DE1A78897493} = {E89606EC-111B-4151-997C-8006627F1926}
{5EA4E26F-F623-473D-9CD7-E590A3E54239} = {9C336E9A-3FC8-4F77-A5B4-1D07E4E54924}
{C33E3F7C-0C2A-4DD2-91E4-328866195997} = {E4B3DD3A-E36C-46D6-B35E-D824EB9B3C06}
{50A40BEB-1C22-41CF-908F-F24FB34B1699} = {E4B3DD3A-E36C-46D6-B35E-D824EB9B3C06}
EndGlobalSection
EndGlobal
25 changes: 25 additions & 0 deletions src/Proc/Extensions/ArgumentExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProcNet.Extensions;

internal static class ArgumentExtensions
{
public static string NaivelyQuoteArguments(this IEnumerable<string> arguments)
{
if (arguments == null) return string.Empty;
var args = arguments.ToList();
if (args.Count == 0) return string.Empty;
var quotedArgs = args
.Select(a =>
{
if (!a.Contains(" ")) return a;
return $"\"{a}\"";
})
.ToList();
return string.Join(" ", quotedArgs);
}

}
9 changes: 8 additions & 1 deletion src/Proc/ObservableProcessBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using ProcNet.Extensions;
using ProcNet.Std;

namespace ProcNet
Expand Down Expand Up @@ -140,13 +141,19 @@ private Process CreateProcess()
var processStartInfo = new ProcessStartInfo
{
FileName = s.Binary,
Arguments = args != null ? string.Join(" ", args) : string.Empty,
#if !NETSTANDARD2_1
Arguments = args.NaivelyQuoteArguments(),
#endif
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true
};
#if NETSTANDARD2_1
foreach (var arg in s.Args)
processStartInfo.ArgumentList.Add(arg);
#endif
if (s.Environment != null)
{
foreach (var kv in s.Environment)
Expand Down
12 changes: 10 additions & 2 deletions src/Proc/Proc.Exec.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using ProcNet.Extensions;

namespace ProcNet
{
Expand Down Expand Up @@ -46,11 +47,18 @@ public static partial class Proc
/// <returns>The exit code of the binary being run</returns>
public static int? Exec(ExecArguments arguments, TimeSpan timeout)
{
var args = string.Join(" ", arguments.Args ?? new string[]{});
var info = new ProcessStartInfo(arguments.Binary, args)
var args = arguments.Args.NaivelyQuoteArguments();
var info = new ProcessStartInfo(arguments.Binary)
{
UseShellExecute = false
#if !NETSTANDARD2_1
, Arguments = args
#endif
};
#if NETSTANDARD2_1
foreach (var arg in arguments.Args)
info.ArgumentList.Add(arg);
#endif

var pwd = arguments.WorkingDirectory;
if (!string.IsNullOrWhiteSpace(pwd)) info.WorkingDirectory = pwd;
Expand Down
2 changes: 1 addition & 1 deletion src/Proc/Proc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AssemblyName>proc</AssemblyName>
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1;net461</TargetFrameworks>
<RootNamespace>ProcNet</RootNamespace>


Expand Down

0 comments on commit 027a0c1

Please sign in to comment.