diff --git a/schema/v1/ScriptRunnerSchema.json b/schema/v1/ScriptRunnerSchema.json index 5c3a517..14d3196 100644 --- a/schema/v1/ScriptRunnerSchema.json +++ b/schema/v1/ScriptRunnerSchema.json @@ -80,7 +80,8 @@ "directoryPicker", "datePicker", "numeric", - "timePicker" + "timePicker", + "fileContent" ] } }, @@ -208,6 +209,22 @@ }, "required": ["prompt"] }, + { + "properties": { + "prompt": { + "const": "fileContent" + }, + "promptSettings": { + "type": "object", + "properties": { + "extension": { + "type": "string" + } + } + } + }, + "required": ["prompt"] + }, { "properties": { "prompt": { diff --git a/src/ScriptRunner/ScriptRunner.GUI/ParamsPanelFactory.cs b/src/ScriptRunner/ScriptRunner.GUI/ParamsPanelFactory.cs index 8cf82ca..a210de0 100644 --- a/src/ScriptRunner/ScriptRunner.GUI/ParamsPanelFactory.cs +++ b/src/ScriptRunner/ScriptRunner.GUI/ParamsPanelFactory.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using System.IO; +using System.Text; using Avalonia.Collections; using Avalonia.Controls; using Avalonia.Media; @@ -146,6 +148,17 @@ private static IControlRecord CreateControlRecord(ScriptParam p, string? value) Text = value } }; + case PromptType.FileContent: + return new FileContent(p.GetPromptSettings("extension", out var extension)?extension:"dat") + { + Control = new TextBox + { + TextWrapping = TextWrapping.Wrap, + AcceptsReturn = true, + Height = 100, + Text = File.Exists(value)? File.ReadAllText(value): string.Empty + } + }; case PromptType.FilePicker: return new FilePickerControl { @@ -270,6 +283,26 @@ public string GetFormattedValue() public string Name { get; set; } public bool MaskingRequired { get; set; } } +public class FileContent : IControlRecord +{ + public IControl Control { get; set; } + public string FileName { get; set; } + + public FileContent(string extension) + { + FileName = Path.GetTempFileName() + "." + extension; + } + + public string GetFormattedValue() + { + var fileContent = ((TextBox)Control).Text; + File.WriteAllText(FileName, fileContent, Encoding.UTF8); + return FileName; + } + + public string Name { get; set; } + public bool MaskingRequired { get; set; } +} public class MultiSelectControl : IControlRecord { diff --git a/src/ScriptRunner/ScriptRunner.GUI/ScriptConfigs/PromptType.cs b/src/ScriptRunner/ScriptRunner.GUI/ScriptConfigs/PromptType.cs index 14a57c8..0b98a6a 100644 --- a/src/ScriptRunner/ScriptRunner.GUI/ScriptConfigs/PromptType.cs +++ b/src/ScriptRunner/ScriptRunner.GUI/ScriptConfigs/PromptType.cs @@ -12,5 +12,6 @@ public enum PromptType FilePicker, DirectoryPicker, Numeric, - TimePicker + TimePicker, + FileContent } \ No newline at end of file diff --git a/src/ScriptRunner/ScriptRunner.GUI/ScriptReader/PromptTypeJsonConverter.cs b/src/ScriptRunner/ScriptRunner.GUI/ScriptReader/PromptTypeJsonConverter.cs index 52c00d1..bfa09b8 100644 --- a/src/ScriptRunner/ScriptRunner.GUI/ScriptReader/PromptTypeJsonConverter.cs +++ b/src/ScriptRunner/ScriptRunner.GUI/ScriptReader/PromptTypeJsonConverter.cs @@ -22,6 +22,7 @@ public override PromptType Read(ref Utf8JsonReader reader, Type typeToConvert, J "directorypicker" => PromptType.DirectoryPicker, "numeric" => PromptType.Numeric, "timepicker" => PromptType.TimePicker, + "filecontent" => PromptType.FileContent, _ => PromptType.Text }; } diff --git a/src/ScriptRunner/ScriptRunner.GUI/ScriptReader/ScriptConfigReader.cs b/src/ScriptRunner/ScriptRunner.GUI/ScriptReader/ScriptConfigReader.cs index 533eba8..f8d9127 100644 --- a/src/ScriptRunner/ScriptRunner.GUI/ScriptReader/ScriptConfigReader.cs +++ b/src/ScriptRunner/ScriptRunner.GUI/ScriptReader/ScriptConfigReader.cs @@ -50,6 +50,7 @@ private static IEnumerable LoadFileSource(string fileName) var scriptConfig = JsonSerializer.Deserialize(jsonString, new JsonSerializerOptions { PropertyNameCaseInsensitive = true, + AllowTrailingCommas = true, Converters = {new PromptTypeJsonConverter(), new ParamTypeJsonConverter()} })!; @@ -57,10 +58,22 @@ private static IEnumerable LoadFileSource(string fileName) foreach (var action in scriptConfig.Actions) { action.Source = fileName; + + if (string.IsNullOrWhiteSpace(action.WorkingDirectory)) + { + action.WorkingDirectory = Path.GetDirectoryName(fileName); + } + + if (string.IsNullOrWhiteSpace(action.InstallCommandWorkingDirectory)) + { + action.InstallCommandWorkingDirectory = Path.GetDirectoryName(fileName); + } + var defaultSet = new ArgumentSet() { Description = "" }; + foreach (var param in action.Params) { defaultSet.Arguments[param.Name] = param.Default; @@ -78,14 +91,22 @@ private static IEnumerable LoadFileSource(string fileName) } action.PredefinedArgumentSets.Insert(0, defaultSet); - if (string.IsNullOrWhiteSpace(action.WorkingDirectory)) - { - action.WorkingDirectory = Path.GetDirectoryName(fileName); - } - if (string.IsNullOrWhiteSpace(action.InstallCommandWorkingDirectory)) + foreach (var param in action.Params.Where(x=>x.Prompt == PromptType.FileContent)) { - action.InstallCommandWorkingDirectory = Path.GetDirectoryName(fileName); + foreach (var set in action.PredefinedArgumentSets) + { + if (set.Arguments.TryGetValue(param.Name, out var defaultValue)) + { + if (string.IsNullOrWhiteSpace(defaultValue) == false) + { + if (Path.IsPathRooted(defaultValue) == false) + { + set.Arguments[param.Name] = Path.Combine(Path.GetDirectoryName(fileName)!, defaultValue); + } + } + } + } } }