Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Renames preset get to config get. Closes #973 #985

Merged
merged 2 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace DevProxy.CommandHandlers;

class ProxyPresetInfo
class ProxyConfigInfo
{
public IList<string> ConfigFiles { get; set; } = [];
public IList<string> MockFiles { get; set; } = [];
Expand All @@ -26,9 +26,9 @@ class GitHubTreeItem
public string Type { get; set; } = string.Empty;
}

public static class PresetGetCommandHandler
public static class ConfigGetCommandHandler
{
public static async Task DownloadPresetAsync(string presetId, ILogger logger)
public static async Task DownloadConfigAsync(string configId, ILogger logger)
{
try
{
Expand All @@ -39,60 +39,65 @@ public static async Task DownloadPresetAsync(string presetId, ILogger logger)
return;
}

var presetsFolderPath = Path.Combine(appFolder, "presets");
logger.LogDebug("Checking if presets folder {presetsFolderPath} exists...", presetsFolderPath);
if (!Directory.Exists(presetsFolderPath))
var configFolderPath = Path.Combine(appFolder, "config");
logger.LogDebug("Checking if config folder {configFolderPath} exists...", configFolderPath);
if (!Directory.Exists(configFolderPath))
{
logger.LogDebug("Presets folder not found, creating it...");
Directory.CreateDirectory(presetsFolderPath);
logger.LogDebug("Presets folder created");
logger.LogDebug("Config folder not found, creating it...");
Directory.CreateDirectory(configFolderPath);
logger.LogDebug("Config folder created");
}

logger.LogDebug("Getting target folder path for preset {presetId}...", presetId);
var targetFolderPath = GetTargetFolderPath(appFolder, presetId);
logger.LogDebug("Getting target folder path for config {configId}...", configId);
var targetFolderPath = GetTargetFolderPath(appFolder, configId);
logger.LogDebug("Creating target folder {targetFolderPath}...", targetFolderPath);
Directory.CreateDirectory(targetFolderPath);

logger.LogInformation("Downloading preset {presetId}...", presetId);
logger.LogInformation("Downloading config {configId}...", configId);

var sampleFiles = await GetFilesToDownloadAsync(presetId, logger);
var sampleFiles = await GetFilesToDownloadAsync(configId, logger);
if (sampleFiles.Length == 0)
{
logger.LogError("Config {configId} not found in the samples repo", configId);
return;
}
foreach (var sampleFile in sampleFiles)
{
await DownloadFileAsync(sampleFile, targetFolderPath, presetId, logger);
await DownloadFileAsync(sampleFile, targetFolderPath, configId, logger);
}

logger.LogInformation("Preset saved in {targetFolderPath}\r\n", targetFolderPath);
var presetInfo = GetPresetInfo(targetFolderPath, logger);
if (!presetInfo.ConfigFiles.Any() && !presetInfo.MockFiles.Any())
logger.LogInformation("Config saved in {targetFolderPath}\r\n", targetFolderPath);
var configInfo = GetConfigInfo(targetFolderPath, logger);
if (!configInfo.ConfigFiles.Any() && !configInfo.MockFiles.Any())
{
return;
}

if (presetInfo.ConfigFiles.Any())
if (configInfo.ConfigFiles.Any())
{
logger.LogInformation("To start Dev Proxy with the preset, run:");
foreach (var configFile in presetInfo.ConfigFiles)
logger.LogInformation("To start Dev Proxy with the config, run:");
foreach (var configFile in configInfo.ConfigFiles)
{
logger.LogInformation(" devproxy --config-file \"{configFile}\"", configFile.Replace(appFolder, "~appFolder"));
}
}
else
{
logger.LogInformation("To start Dev Proxy with the mock file, enable the MockResponsePlugin or GraphMockResponsePlugin and run:");
foreach (var mockFile in presetInfo.MockFiles)
foreach (var mockFile in configInfo.MockFiles)
{
logger.LogInformation(" devproxy --mock-file \"{mockFile}\"", mockFile.Replace(appFolder, "~appFolder"));
}
}
}
catch (Exception ex)
{
logger.LogError(ex, "Error downloading presets");
logger.LogError(ex, "Error downloading config");
}
}

/// <summary>
/// Returns the list of files that can be used as entry points for the preset
/// Returns the list of files that can be used as entry points for the config
/// </summary>
/// <remarks>
/// A sample in the gallery can have multiple entry points. It can
Expand All @@ -104,18 +109,18 @@ public static async Task DownloadPresetAsync(string presetId, ILogger logger)
/// an array of all the mock files. If there are no mocks, it'll return
/// an empty array indicating that there's no entry point.
/// </remarks>
/// <param name="presetFolder">Full path to the folder with preset files</param>
/// <param name="configFolder">Full path to the folder with config files</param>
/// <returns>Array of files that can be used to start proxy with</returns>
private static ProxyPresetInfo GetPresetInfo(string presetFolder, ILogger logger)
private static ProxyConfigInfo GetConfigInfo(string configFolder, ILogger logger)
{
var presetInfo = new ProxyPresetInfo();
var configInfo = new ProxyConfigInfo();

logger.LogDebug("Getting list of JSON files in {presetFolder}...", presetFolder);
var jsonFiles = Directory.GetFiles(presetFolder, "*.json");
logger.LogDebug("Getting list of JSON files in {configFolder}...", configFolder);
var jsonFiles = Directory.GetFiles(configFolder, "*.json");
if (!jsonFiles.Any())
{
logger.LogDebug("No JSON files found");
return presetInfo;
return configInfo;
}

foreach (var jsonFile in jsonFiles)
Expand All @@ -126,32 +131,32 @@ private static ProxyPresetInfo GetPresetInfo(string presetFolder, ILogger logger
if (fileContents.Contains("\"plugins\":"))
{
logger.LogDebug("File {jsonFile} contains proxy config", jsonFile);
presetInfo.ConfigFiles.Add(jsonFile);
configInfo.ConfigFiles.Add(jsonFile);
continue;
}

if (fileContents.Contains("\"responses\":"))
{
logger.LogDebug("File {jsonFile} contains mock data", jsonFile);
presetInfo.MockFiles.Add(jsonFile);
configInfo.MockFiles.Add(jsonFile);
continue;
}

logger.LogDebug("File {jsonFile} is not a proxy config or mock data", jsonFile);
}

if (presetInfo.ConfigFiles.Any())
if (configInfo.ConfigFiles.Any())
{
logger.LogDebug("Found {configFilesCount} proxy config files. Clearing mocks...", presetInfo.ConfigFiles.Count);
presetInfo.MockFiles.Clear();
logger.LogDebug("Found {configFilesCount} proxy config files. Clearing mocks...", configInfo.ConfigFiles.Count);
configInfo.MockFiles.Clear();
}

return presetInfo;
return configInfo;
}

private static string GetTargetFolderPath(string appFolder, string presetId)
private static string GetTargetFolderPath(string appFolder, string configId)
{
var baseFolder = Path.Combine(appFolder, "presets", presetId);
var baseFolder = Path.Combine(appFolder, "config", configId);
var newFolder = baseFolder;
var i = 1;
while (Directory.Exists(newFolder))
Expand Down Expand Up @@ -199,7 +204,7 @@ private static async Task<string[]> GetFilesToDownloadAsync(string sampleFolderN
}
}

private static async Task DownloadFileAsync(string filePath, string targetFolderPath, string presetId, ILogger logger)
private static async Task DownloadFileAsync(string filePath, string targetFolderPath, string configId, ILogger logger)
{
var url = $"https://raw.githubusercontent.com/pnp/proxy-samples/main/{filePath.Replace("#", "%23")}";
logger.LogDebug("Downloading file {filePath}...", filePath);
Expand All @@ -210,7 +215,7 @@ private static async Task DownloadFileAsync(string filePath, string targetFolder
if (response.IsSuccessStatusCode)
{
var contentStream = await response.Content.ReadAsStreamAsync();
var filePathInsideSample = Path.GetRelativePath($"samples/{presetId}", filePath);
var filePathInsideSample = Path.GetRelativePath($"samples/{configId}", filePath);
var directoryNameInsideSample = Path.GetDirectoryName(filePathInsideSample);
if (directoryNameInsideSample is not null)
{
Expand Down
20 changes: 10 additions & 10 deletions dev-proxy/ProxyHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,25 +323,25 @@ public RootCommand GetRootCommand(ILogger logger)
};
command.Add(msGraphDbCommand);

var presetCommand = new Command("preset", "Manage Dev Proxy presets");
var configCommand = new Command("config", "Manage Dev Proxy configs");

var presetGetCommand = new Command("get", "Download the specified preset from the Sample Solution Gallery");
var presetIdArgument = new Argument<string>("preset-id", "The ID of the preset to download");
presetGetCommand.AddArgument(presetIdArgument);
presetGetCommand.SetHandler(async presetId => await PresetGetCommandHandler.DownloadPresetAsync(presetId, logger), presetIdArgument);
presetCommand.Add(presetGetCommand);
var configGetCommand = new Command("get", "Download the specified config from the Sample Solution Gallery");
var configIdArgument = new Argument<string>("config-id", "The ID of the config to download");
configGetCommand.AddArgument(configIdArgument);
configGetCommand.SetHandler(async configId => await ConfigGetCommandHandler.DownloadConfigAsync(configId, logger), configIdArgument);
configCommand.Add(configGetCommand);

command.Add(presetCommand);

var configCommand = new Command("config", "Open devproxyrc.json");
configCommand.SetHandler(() =>
var configOpenCommand = new Command("open", "Open devproxyrc.json");
configOpenCommand.SetHandler(() =>
{
var cfgPsi = new ProcessStartInfo(ConfigFile)
{
UseShellExecute = true
};
Process.Start(cfgPsi);
});
configCommand.Add(configOpenCommand);

command.Add(configCommand);

var outdatedCommand = new Command("outdated", "Check for new version");
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes
10 changes: 5 additions & 5 deletions dev-proxy/dev-proxy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,19 @@
<None Update="trust-cert.sh">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="presets\m365.json">
<None Update="config\m365.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="presets\m365-mocks.json">
<None Update="config\m365-mocks.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="presets\microsoft-graph.json">
<None Update="config\microsoft-graph.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="presets\microsoft-graph-rate-limiting.json">
<None Update="config\microsoft-graph-rate-limiting.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="presets\picture.jpg">
<None Update="config\picture.jpg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
Expand Down