Skip to content

Commit a86c5eb

Browse files
committed
Merge Flow-Launcher#3573 with some changes
1 parent b04b9d9 commit a86c5eb

24 files changed

Lines changed: 709 additions & 128 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net10.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<RootNamespace>Flow.Launcher.Command</RootNamespace>
9+
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
10+
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
11+
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
12+
13+
<PublishDir>..\Output\Publish\Command\</PublishDir>
14+
<PublishAot>true</PublishAot>
15+
</PropertyGroup>
16+
17+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
18+
<PlatformTarget>AnyCPU</PlatformTarget>
19+
<DebugSymbols>true</DebugSymbols>
20+
<DebugType>portable</DebugType>
21+
<Optimize>false</Optimize>
22+
<OutputPath>..\Output\Debug\Command\</OutputPath>
23+
<DefineConstants>DEBUG;TRACE</DefineConstants>
24+
<ErrorReport>prompt</ErrorReport>
25+
<WarningLevel>4</WarningLevel>
26+
<UseVSHostingProcess>true</UseVSHostingProcess>
27+
<Prefer32Bit>false</Prefer32Bit>
28+
</PropertyGroup>
29+
30+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
31+
<PlatformTarget>AnyCPU</PlatformTarget>
32+
<DebugType>pdbonly</DebugType>
33+
<Optimize>true</Optimize>
34+
<OutputPath>..\Output\Release\Command\</OutputPath>
35+
<DefineConstants>TRACE;RELEASE</DefineConstants>
36+
<ErrorReport>prompt</ErrorReport>
37+
<WarningLevel>4</WarningLevel>
38+
<Prefer32Bit>false</Prefer32Bit>
39+
</PropertyGroup>
40+
41+
</Project>

Flow.Launcher.Command/Program.cs

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
using System.Diagnostics;
2+
3+
namespace Flow.Launcher.Command;
4+
5+
internal static class Program
6+
{
7+
[STAThread]
8+
private static int Main(string[] args)
9+
{
10+
if (args.Length == 0) return -1;
11+
12+
// Start process with arguments
13+
// Usage: Flow.Launcher.Command -StartProcess -FileName <file> -WorkingDirectory <directory> -Arguments <args> -UseShellExecute <true|false> -Verb <verb> -CreateNoWindow <true|false>
14+
if (args[0] == @"-StartProcess")
15+
{
16+
var fileName = string.Empty;
17+
var workingDirectory = Environment.CurrentDirectory;
18+
var argumentList = new List<string>();
19+
var useShellExecute = true;
20+
var verb = string.Empty;
21+
var createNoWindow = false;
22+
var isArguments = false;
23+
24+
for (int i = 1; i < args.Length; i++)
25+
{
26+
switch (args[i])
27+
{
28+
case "-FileName":
29+
if (i + 1 < args.Length)
30+
fileName = args[++i];
31+
isArguments = false;
32+
break;
33+
34+
case "-WorkingDirectory":
35+
if (i + 1 < args.Length)
36+
workingDirectory = args[++i];
37+
isArguments = false;
38+
break;
39+
40+
case "-Arguments":
41+
if (i + 1 < args.Length)
42+
argumentList.Add(args[++i]);
43+
isArguments = true;
44+
break;
45+
46+
case "-UseShellExecute":
47+
if (i + 1 < args.Length && bool.TryParse(args[++i], out bool useShell))
48+
useShellExecute = useShell;
49+
isArguments = false;
50+
break;
51+
52+
case "-Verb":
53+
if (i + 1 < args.Length)
54+
verb = args[++i];
55+
isArguments = false;
56+
break;
57+
58+
case "-CreateNoWindow":
59+
if (i + 1 < args.Length && bool.TryParse(args[++i], out bool createNoWin))
60+
createNoWindow = createNoWin;
61+
break;
62+
63+
default:
64+
if (isArguments)
65+
argumentList.Add(args[i]);
66+
else
67+
Console.WriteLine($"Unknown parameter: {args[i]}");
68+
break;
69+
}
70+
}
71+
72+
if (string.IsNullOrEmpty(fileName))
73+
{
74+
Console.WriteLine("Error: -FileName is required.");
75+
return -2;
76+
}
77+
78+
try
79+
{
80+
ProcessStartInfo info;
81+
if (argumentList.Count == 0)
82+
{
83+
info = new ProcessStartInfo
84+
{
85+
FileName = fileName,
86+
WorkingDirectory = workingDirectory,
87+
UseShellExecute = useShellExecute,
88+
Verb = verb,
89+
CreateNoWindow = createNoWindow
90+
};
91+
}
92+
else if (argumentList.Count == 1)
93+
{
94+
info = new ProcessStartInfo
95+
{
96+
FileName = fileName,
97+
WorkingDirectory = workingDirectory,
98+
Arguments = argumentList[0],
99+
UseShellExecute = useShellExecute,
100+
Verb = verb,
101+
CreateNoWindow = createNoWindow
102+
};
103+
}
104+
else
105+
{
106+
info = new ProcessStartInfo
107+
{
108+
FileName = fileName,
109+
WorkingDirectory = workingDirectory,
110+
UseShellExecute = useShellExecute,
111+
Verb = verb,
112+
CreateNoWindow = createNoWindow
113+
};
114+
foreach (var arg in argumentList)
115+
{
116+
info.ArgumentList.Add(arg);
117+
}
118+
}
119+
Process.Start(info)?.Dispose();
120+
Console.WriteLine("Success.");
121+
return 0;
122+
}
123+
catch (Exception ex)
124+
{
125+
Console.WriteLine($"Error: {ex.Message}");
126+
return -3;
127+
}
128+
}
129+
130+
return -4;
131+
}
132+
}

Flow.Launcher.Infrastructure/Constant.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public static class Constant
1313
private static readonly Assembly Assembly = Assembly.GetExecutingAssembly();
1414
public static readonly string ProgramDirectory = Directory.GetParent(Assembly.Location)?.ToString() ?? throw new NullReferenceException("Failed to get program directory");
1515
public static readonly string ExecutablePath = Path.Combine(ProgramDirectory, FlowLauncher + ".exe");
16+
public static readonly string CommandExecutablePath = Path.Combine(ProgramDirectory, "Command", "Flow.Launcher.Command.exe");
1617
public static readonly string ApplicationDirectory = Directory.GetParent(ProgramDirectory)?.ToString() ?? throw new NullReferenceException("Failed to get app directory");
1718

1819
public const string IssuesUrl = "https://github.com/TBM13/Flow.Launcher/issues";

Flow.Launcher.Infrastructure/Helpers/FilesFolders.cs

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -158,35 +158,6 @@ public static bool FileOrLocationExists(this string path)
158158
return LocationExists(path) || FileExists(path);
159159
}
160160

161-
/// <summary>
162-
/// Open a directory window (using the OS's default handler, usually explorer)
163-
/// </summary>
164-
/// <param name="fileOrFolderPath"></param>
165-
/// <param name="messageBoxExShow"></param>
166-
public static void OpenPath(string fileOrFolderPath, Func<string, MessageBoxResult>? messageBoxExShow = null)
167-
{
168-
var psi = new ProcessStartInfo
169-
{
170-
FileName = FileExplorerProgramName,
171-
UseShellExecute = true,
172-
Arguments = '"' + fileOrFolderPath + '"'
173-
};
174-
try
175-
{
176-
if (LocationExists(fileOrFolderPath) || FileExists(fileOrFolderPath))
177-
Process.Start(psi);
178-
}
179-
catch (Exception)
180-
{
181-
#if DEBUG
182-
throw;
183-
#else
184-
messageBoxExShow ??= MessageBox.Show;
185-
messageBoxExShow(string.Format("Unable to open the path {0}, please check if it exists", fileOrFolderPath));
186-
#endif
187-
}
188-
}
189-
190161
/// <summary>
191162
/// Open a file with associated application
192163
/// </summary>
@@ -206,6 +177,7 @@ public static void OpenFile(string filePath, string workingDir = "", bool asAdmi
206177
try
207178
{
208179
if (FileExists(filePath))
180+
// TODO: We should probably de-elevate here
209181
Process.Start(psi);
210182
}
211183
catch (Exception)

Flow.Launcher.Infrastructure/Helpers/SearchWeb.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,15 @@ public static void OpenInBrowserWindow(this string url, string? browserPath = nu
6060

6161
try
6262
{
63+
// TODO: We should probably de-elevate here
6364
Process.Start(psi)?.Dispose();
6465
}
6566
// This error may be thrown if browser path is incorrect
6667
catch (Win32Exception)
6768
{
6869
try
6970
{
71+
// TODO: We should probably de-elevate here
7072
Process.Start(new ProcessStartInfo
7173
{
7274
FileName = url,
@@ -103,13 +105,15 @@ public static void OpenInBrowserTab(this string url, string? browserPath = null,
103105
psi.FileName = url;
104106
}
105107

108+
// TODO: We should probably de-elevate here
106109
Process.Start(psi)?.Dispose();
107110
}
108111
// This error may be thrown if browser path is incorrect
109112
catch (Win32Exception)
110113
{
111114
try
112115
{
116+
// TODO: We should probably de-elevate here
113117
Process.Start(new ProcessStartInfo
114118
{
115119
FileName = url,

0 commit comments

Comments
 (0)