Skip to content

Commit 201dc76

Browse files
Fix PAC CLI detection for VS Code extension installations (#76)
* Initial plan * Implement PAC CLI detection for VS Code extension Co-authored-by: Power-Maverick <36135520+Power-Maverick@users.noreply.github.com> * Add session caching for detected PAC path Co-authored-by: Power-Maverick <36135520+Power-Maverick@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Power-Maverick <36135520+Power-Maverick@users.noreply.github.com>
1 parent b715256 commit 201dc76

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed

Maverick.PCF.Builder.Helper/Commands.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,16 @@ public static string Check()
154154
return $"pac";
155155
}
156156

157+
/// <summary>
158+
/// Check PAC using custom path
159+
/// </summary>
160+
/// <param name="pacPath">Full path to pac.exe</param>
161+
/// <returns></returns>
162+
public static string Check(string pacPath)
163+
{
164+
return $"\"{pacPath}\"";
165+
}
166+
157167
/// <summary>
158168
/// pac auth create --url <https://xyz.crm.dynamics.com>
159169
/// </summary>

Maverick.PCF.Builder/PCFBuilder.cs

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public partial class PCFBuilder : PluginControlBase, IGitHubPlugin, IHelpPlugin,
3434
{
3535
#region XrmToolBox settings
3636
private Settings pluginSettings;
37+
private string detectedPacPath; // Cache the detected PAC path for session reuse
3738

3839
public string RepositoryName => "PCF-CustomControlBuilder";
3940
public string UserName => "Power-Maverick";
@@ -810,8 +811,30 @@ public void CheckPacVersion(object worker, DoWorkEventArgs args)
810811
{
811812
var start = DateTime.Now;
812813

813-
string[] commands = new string[] { Commands.Pac.Check() };
814-
var output = CommandLineHelper.RunCommand(commands);
814+
// Try to find PAC in multiple locations
815+
string pacPath = FindPacPath();
816+
string[] commands;
817+
string output;
818+
819+
if (!string.IsNullOrEmpty(pacPath))
820+
{
821+
// Use the found PAC path
822+
if (pacPath.Equals("pac", StringComparison.InvariantCultureIgnoreCase))
823+
{
824+
commands = new string[] { Commands.Pac.Check() };
825+
}
826+
else
827+
{
828+
commands = new string[] { Commands.Pac.Check(pacPath) };
829+
}
830+
output = CommandLineHelper.RunCommand(commands);
831+
}
832+
else
833+
{
834+
// Fallback to standard check for backward compatibility
835+
commands = new string[] { Commands.Pac.Check() };
836+
output = CommandLineHelper.RunCommand(commands);
837+
}
815838

816839
StringHelper stringer = new StringHelper();
817840
PacVersionParsedDetails outputParsedPacDetails = stringer.ParsePacVersionOutput(output);
@@ -944,6 +967,56 @@ private string FindMsBuildPath()
944967
return msBuildPath;
945968
}
946969

970+
private string FindPacPath()
971+
{
972+
// Return cached path if available
973+
if (!string.IsNullOrEmpty(detectedPacPath))
974+
{
975+
return detectedPacPath;
976+
}
977+
978+
// First try the standard PAC command (global installation)
979+
string[] standardCommands = new string[] { Commands.Pac.Check() };
980+
var standardOutput = CommandLineHelper.RunCommand(standardCommands);
981+
982+
StringHelper stringer = new StringHelper();
983+
PacVersionParsedDetails standardPacDetails = stringer.ParsePacVersionOutput(standardOutput);
984+
985+
// If PAC is found via standard command, use it
986+
if (!standardPacDetails.CLINotFound)
987+
{
988+
detectedPacPath = "pac"; // Cache the result
989+
return detectedPacPath;
990+
}
991+
992+
// Try VS Code extension location
993+
try
994+
{
995+
string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
996+
string vscodeExtensionPacPath = Path.Combine(appDataPath, "Code", "User", "globalStorage", "microsoft-isvexptools.powerplatform-vscode", "pac", "tools", "pac.exe");
997+
998+
if (File.Exists(vscodeExtensionPacPath))
999+
{
1000+
// Test if this PAC installation works by running it
1001+
string[] vscodeCommands = new string[] { Commands.Pac.Check(vscodeExtensionPacPath) };
1002+
var vscodeOutput = CommandLineHelper.RunCommand(vscodeCommands);
1003+
1004+
PacVersionParsedDetails vscodePacDetails = stringer.ParsePacVersionOutput(vscodeOutput);
1005+
if (!vscodePacDetails.CLINotFound)
1006+
{
1007+
detectedPacPath = vscodeExtensionPacPath; // Cache the result
1008+
return detectedPacPath;
1009+
}
1010+
}
1011+
}
1012+
catch (Exception)
1013+
{
1014+
// If there's any error accessing the VS Code extension path, continue with standard behavior
1015+
}
1016+
1017+
return string.Empty; // PAC not found in any location
1018+
}
1019+
9471020
private void IncrementComponentVersion()
9481021
{
9491022
var start = DateTime.Now;

0 commit comments

Comments
 (0)