Skip to content

Commit

Permalink
fix: adjust Windows global path, remove all paths containing php.exe
Browse files Browse the repository at this point in the history
  • Loading branch information
MircoBabin committed Apr 5, 2024
1 parent 3adecdd commit 8fd9f52
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 62 deletions.
80 changes: 19 additions & 61 deletions src/GitNoob.Utils/BatFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,78 +225,36 @@ private string WriteBatFile()
if (NeedsPhp())
{
string phpPath = FileUtils.GetExactPathName(_projectWorkingDirectory.Php.Path.ToString());

contents.AppendLine("set PHPRC=" + _phpIni.IniPath); /* Directory containing php.ini */
contents.AppendLine("path %path%;" + phpPath);

var environmentPathParts = FileUtils.RemoveExeFromEnvironmentPath("php.exe");
environmentPathParts.Add(phpPath);

//Global Composer bin directory, e.g. for php-cs-fixer
// %appdata%\Composer\\vendor\bin
string composerGlobalBin = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Composer\\vendor\\bin");
if (Directory.Exists(composerGlobalBin))
{
contents.AppendLine("path %path%;" + composerGlobalBin);
environmentPathParts.Add(composerGlobalBin);
}

string phpExe = FileUtils.GetExactPathName(Path.Combine(phpPath, "php.exe"));
string tmpFile;
string tmpExpectFile;
{

var now = DateTime.UtcNow;
var randomBytes = new Byte[30];
Random random = new Random();
random.NextBytes(randomBytes);

tmpFile = Path.Combine(path, "where-php-exe." +
now.Year.ToString().PadLeft(4, '0') + "-" + now.Month.ToString().PadLeft(2, '0') + "-" + now.Day.ToString().PadLeft(2, '0') +
"_" +
now.Hour.ToString().PadLeft(2, '0') + "_" + now.Minute.ToString().PadLeft(2, '0') + "_" + now.Second.ToString().PadLeft(2, '0') + "_" + now.Millisecond.ToString().PadLeft(3, '0') +
"_" +
BitConverter.ToString(randomBytes).Replace("-", string.Empty)
);
tmpExpectFile = tmpFile + ".expect";
}
File.WriteAllText(tmpExpectFile, phpExe + "\r\n", new UTF8Encoding(false));

contents.AppendLine("where php.exe > \"" + tmpFile + "\" 2>nul");
contents.AppendLine("fc /b \"" + tmpFile + "\" \"" + tmpExpectFile + "\" >nul 2>&1");
contents.AppendLine("if errorlevel 1 goto php_find_error");
contents.AppendLine("del /q \"" + tmpFile + "\" >nul 2>&1");
contents.AppendLine("del /q \"" + tmpExpectFile + "\" >nul 2>&1");
contents.AppendLine("goto php_find_done");
contents.AppendLine(":php_find_error");
contents.AppendLine("echo.");
contents.AppendLine("echo.");
contents.AppendLine("echo.");
contents.AppendLine("echo PHP.EXE is not found or found in the wrong path.");
contents.AppendLine("echo Maybe another tool like XAMPP / WAMP or the like is globally installed in the Windows PATH?");
contents.AppendLine("echo This interferes with GitNoob.");
contents.AppendLine("echo.");
contents.AppendLine("echo ----------------------------------------------------------------------");
contents.AppendLine("echo Expected:");
contents.AppendLine("echo " + phpExe);
contents.AppendLine("echo ----------------------------------------------------------------------");
contents.AppendLine("echo But found:");
contents.AppendLine("type \"" + tmpFile + "\"");
contents.AppendLine("echo ----------------------------------------------------------------------");
contents.AppendLine("echo.");
contents.AppendLine("echo.");
contents.AppendLine("echo.");

switch (_window)
//Rebuild path
bool first = true;
foreach(var part in environmentPathParts)
{
case WindowType.showWindow:
case WindowType.showWindowWithPauseAtEnd:
contents.AppendLine();
contents.AppendLine("pause");
break;
if (first)
{
contents.AppendLine("set GitNoob_TmpPath=" + part);
first = false;
}
else
{
contents.AppendLine("set GitNoob_TmpPath=%GitNoob_TmpPath%;" + part);
}
}

contents.AppendLine("del /q \"" + tmpFile + "\" >nul 2>&1");
contents.AppendLine("del /q \"" + tmpExpectFile + "\" >nul 2>&1");
contents.AppendLine("exit 1");


contents.AppendLine(":php_find_done");
contents.AppendLine("path %GitNoob_TmpPath%");
contents.AppendLine("set GitNoob_TmpPath=");
}

if (!string.IsNullOrEmpty(_projectWorkingDirectory.Ngrok.AuthToken))
Expand Down
26 changes: 25 additions & 1 deletion src/GitNoob.Utils/FileUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,30 @@ public static string FindExeInProgramFiles(string subpathAndExe)
throw new FileNotFoundException(new FileNotFoundException().Message, subpathAndExe);
}

public static List<string> RemoveExeFromEnvironmentPath(string exe, string EnvironmentPath = null)
{
List<string> parts = new List<string>();

if (EnvironmentPath == null)
EnvironmentPath = Environment.GetEnvironmentVariable("PATH");

string[] paths = EnvironmentPath.Split(new char[1] { Path.PathSeparator });
foreach (string directory in paths)
{
string dir = directory.Trim();
if (dir.Length > 2 && dir.StartsWith("\""))
dir = dir.Substring(1, dir.Length - 2);

string tryexe = Path.Combine(dir, exe);
if (!String.IsNullOrEmpty(dir) && !File.Exists(tryexe))
{
parts.Add(directory.Trim());
}
}

return parts;
}

public static string FindExePath(string exe)
{
exe = Environment.ExpandEnvironmentVariables(exe);
Expand All @@ -91,7 +115,7 @@ public static string FindExePath(string exe)
dir = dir.Substring(1, dir.Length - 2);

string tryexe = Path.Combine(dir, exe);
if (!String.IsNullOrEmpty(path) && File.Exists(tryexe))
if (!String.IsNullOrEmpty(dir) && File.Exists(tryexe))
return Path.GetFullPath(tryexe);
}

Expand Down

0 comments on commit 8fd9f52

Please sign in to comment.