Skip to content

Commit 0fac623

Browse files
Renames preset get to config get. Closes #973 (#985)
Renames config to config open. Closes #974 Renames presets folder to config Extends config get with checking if the sample exists
1 parent 3052cc1 commit 0fac623

File tree

9 files changed

+59
-54
lines changed

9 files changed

+59
-54
lines changed

dev-proxy/CommandHandlers/PresetGetCommandHandler.cs renamed to dev-proxy/CommandHandlers/ConfigGetCommandHandler.cs

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace DevProxy.CommandHandlers;
1010

11-
class ProxyPresetInfo
11+
class ProxyConfigInfo
1212
{
1313
public IList<string> ConfigFiles { get; set; } = [];
1414
public IList<string> MockFiles { get; set; } = [];
@@ -26,9 +26,9 @@ class GitHubTreeItem
2626
public string Type { get; set; } = string.Empty;
2727
}
2828

29-
public static class PresetGetCommandHandler
29+
public static class ConfigGetCommandHandler
3030
{
31-
public static async Task DownloadPresetAsync(string presetId, ILogger logger)
31+
public static async Task DownloadConfigAsync(string configId, ILogger logger)
3232
{
3333
try
3434
{
@@ -39,60 +39,65 @@ public static async Task DownloadPresetAsync(string presetId, ILogger logger)
3939
return;
4040
}
4141

42-
var presetsFolderPath = Path.Combine(appFolder, "presets");
43-
logger.LogDebug("Checking if presets folder {presetsFolderPath} exists...", presetsFolderPath);
44-
if (!Directory.Exists(presetsFolderPath))
42+
var configFolderPath = Path.Combine(appFolder, "config");
43+
logger.LogDebug("Checking if config folder {configFolderPath} exists...", configFolderPath);
44+
if (!Directory.Exists(configFolderPath))
4545
{
46-
logger.LogDebug("Presets folder not found, creating it...");
47-
Directory.CreateDirectory(presetsFolderPath);
48-
logger.LogDebug("Presets folder created");
46+
logger.LogDebug("Config folder not found, creating it...");
47+
Directory.CreateDirectory(configFolderPath);
48+
logger.LogDebug("Config folder created");
4949
}
5050

51-
logger.LogDebug("Getting target folder path for preset {presetId}...", presetId);
52-
var targetFolderPath = GetTargetFolderPath(appFolder, presetId);
51+
logger.LogDebug("Getting target folder path for config {configId}...", configId);
52+
var targetFolderPath = GetTargetFolderPath(appFolder, configId);
5353
logger.LogDebug("Creating target folder {targetFolderPath}...", targetFolderPath);
5454
Directory.CreateDirectory(targetFolderPath);
5555

56-
logger.LogInformation("Downloading preset {presetId}...", presetId);
56+
logger.LogInformation("Downloading config {configId}...", configId);
5757

58-
var sampleFiles = await GetFilesToDownloadAsync(presetId, logger);
58+
var sampleFiles = await GetFilesToDownloadAsync(configId, logger);
59+
if (sampleFiles.Length == 0)
60+
{
61+
logger.LogError("Config {configId} not found in the samples repo", configId);
62+
return;
63+
}
5964
foreach (var sampleFile in sampleFiles)
6065
{
61-
await DownloadFileAsync(sampleFile, targetFolderPath, presetId, logger);
66+
await DownloadFileAsync(sampleFile, targetFolderPath, configId, logger);
6267
}
6368

64-
logger.LogInformation("Preset saved in {targetFolderPath}\r\n", targetFolderPath);
65-
var presetInfo = GetPresetInfo(targetFolderPath, logger);
66-
if (!presetInfo.ConfigFiles.Any() && !presetInfo.MockFiles.Any())
69+
logger.LogInformation("Config saved in {targetFolderPath}\r\n", targetFolderPath);
70+
var configInfo = GetConfigInfo(targetFolderPath, logger);
71+
if (!configInfo.ConfigFiles.Any() && !configInfo.MockFiles.Any())
6772
{
6873
return;
6974
}
7075

71-
if (presetInfo.ConfigFiles.Any())
76+
if (configInfo.ConfigFiles.Any())
7277
{
73-
logger.LogInformation("To start Dev Proxy with the preset, run:");
74-
foreach (var configFile in presetInfo.ConfigFiles)
78+
logger.LogInformation("To start Dev Proxy with the config, run:");
79+
foreach (var configFile in configInfo.ConfigFiles)
7580
{
7681
logger.LogInformation(" devproxy --config-file \"{configFile}\"", configFile.Replace(appFolder, "~appFolder"));
7782
}
7883
}
7984
else
8085
{
8186
logger.LogInformation("To start Dev Proxy with the mock file, enable the MockResponsePlugin or GraphMockResponsePlugin and run:");
82-
foreach (var mockFile in presetInfo.MockFiles)
87+
foreach (var mockFile in configInfo.MockFiles)
8388
{
8489
logger.LogInformation(" devproxy --mock-file \"{mockFile}\"", mockFile.Replace(appFolder, "~appFolder"));
8590
}
8691
}
8792
}
8893
catch (Exception ex)
8994
{
90-
logger.LogError(ex, "Error downloading presets");
95+
logger.LogError(ex, "Error downloading config");
9196
}
9297
}
9398

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

113-
logger.LogDebug("Getting list of JSON files in {presetFolder}...", presetFolder);
114-
var jsonFiles = Directory.GetFiles(presetFolder, "*.json");
118+
logger.LogDebug("Getting list of JSON files in {configFolder}...", configFolder);
119+
var jsonFiles = Directory.GetFiles(configFolder, "*.json");
115120
if (!jsonFiles.Any())
116121
{
117122
logger.LogDebug("No JSON files found");
118-
return presetInfo;
123+
return configInfo;
119124
}
120125

121126
foreach (var jsonFile in jsonFiles)
@@ -126,32 +131,32 @@ private static ProxyPresetInfo GetPresetInfo(string presetFolder, ILogger logger
126131
if (fileContents.Contains("\"plugins\":"))
127132
{
128133
logger.LogDebug("File {jsonFile} contains proxy config", jsonFile);
129-
presetInfo.ConfigFiles.Add(jsonFile);
134+
configInfo.ConfigFiles.Add(jsonFile);
130135
continue;
131136
}
132137

133138
if (fileContents.Contains("\"responses\":"))
134139
{
135140
logger.LogDebug("File {jsonFile} contains mock data", jsonFile);
136-
presetInfo.MockFiles.Add(jsonFile);
141+
configInfo.MockFiles.Add(jsonFile);
137142
continue;
138143
}
139144

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

143-
if (presetInfo.ConfigFiles.Any())
148+
if (configInfo.ConfigFiles.Any())
144149
{
145-
logger.LogDebug("Found {configFilesCount} proxy config files. Clearing mocks...", presetInfo.ConfigFiles.Count);
146-
presetInfo.MockFiles.Clear();
150+
logger.LogDebug("Found {configFilesCount} proxy config files. Clearing mocks...", configInfo.ConfigFiles.Count);
151+
configInfo.MockFiles.Clear();
147152
}
148153

149-
return presetInfo;
154+
return configInfo;
150155
}
151156

152-
private static string GetTargetFolderPath(string appFolder, string presetId)
157+
private static string GetTargetFolderPath(string appFolder, string configId)
153158
{
154-
var baseFolder = Path.Combine(appFolder, "presets", presetId);
159+
var baseFolder = Path.Combine(appFolder, "config", configId);
155160
var newFolder = baseFolder;
156161
var i = 1;
157162
while (Directory.Exists(newFolder))
@@ -199,7 +204,7 @@ private static async Task<string[]> GetFilesToDownloadAsync(string sampleFolderN
199204
}
200205
}
201206

202-
private static async Task DownloadFileAsync(string filePath, string targetFolderPath, string presetId, ILogger logger)
207+
private static async Task DownloadFileAsync(string filePath, string targetFolderPath, string configId, ILogger logger)
203208
{
204209
var url = $"https://raw.githubusercontent.com/pnp/proxy-samples/main/{filePath.Replace("#", "%23")}";
205210
logger.LogDebug("Downloading file {filePath}...", filePath);
@@ -210,7 +215,7 @@ private static async Task DownloadFileAsync(string filePath, string targetFolder
210215
if (response.IsSuccessStatusCode)
211216
{
212217
var contentStream = await response.Content.ReadAsStreamAsync();
213-
var filePathInsideSample = Path.GetRelativePath($"samples/{presetId}", filePath);
218+
var filePathInsideSample = Path.GetRelativePath($"samples/{configId}", filePath);
214219
var directoryNameInsideSample = Path.GetDirectoryName(filePathInsideSample);
215220
if (directoryNameInsideSample is not null)
216221
{

dev-proxy/ProxyHost.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -323,25 +323,25 @@ public RootCommand GetRootCommand(ILogger logger)
323323
};
324324
command.Add(msGraphDbCommand);
325325

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

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

334-
command.Add(presetCommand);
335-
336-
var configCommand = new Command("config", "Open devproxyrc.json");
337-
configCommand.SetHandler(() =>
334+
var configOpenCommand = new Command("open", "Open devproxyrc.json");
335+
configOpenCommand.SetHandler(() =>
338336
{
339337
var cfgPsi = new ProcessStartInfo(ConfigFile)
340338
{
341339
UseShellExecute = true
342340
};
343341
Process.Start(cfgPsi);
344342
});
343+
configCommand.Add(configOpenCommand);
344+
345345
command.Add(configCommand);
346346

347347
var outdatedCommand = new Command("outdated", "Check for new version");
File renamed without changes.

dev-proxy/dev-proxy.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,19 @@
6666
<None Update="trust-cert.sh">
6767
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
6868
</None>
69-
<None Update="presets\m365.json">
69+
<None Update="config\m365.json">
7070
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
7171
</None>
72-
<None Update="presets\m365-mocks.json">
72+
<None Update="config\m365-mocks.json">
7373
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
7474
</None>
75-
<None Update="presets\microsoft-graph.json">
75+
<None Update="config\microsoft-graph.json">
7676
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
7777
</None>
78-
<None Update="presets\microsoft-graph-rate-limiting.json">
78+
<None Update="config\microsoft-graph-rate-limiting.json">
7979
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
8080
</None>
81-
<None Update="presets\picture.jpg">
81+
<None Update="config\picture.jpg">
8282
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
8383
</None>
8484
</ItemGroup>

0 commit comments

Comments
 (0)