Skip to content

Commit

Permalink
Add FileContent prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarypiatek committed Jul 18, 2022
1 parent 9bdd559 commit 3f56db8
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 8 deletions.
19 changes: 18 additions & 1 deletion schema/v1/ScriptRunnerSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@
"directoryPicker",
"datePicker",
"numeric",
"timePicker"
"timePicker",
"fileContent"
]
}
},
Expand Down Expand Up @@ -208,6 +209,22 @@
},
"required": ["prompt"]
},
{
"properties": {
"prompt": {
"const": "fileContent"
},
"promptSettings": {
"type": "object",
"properties": {
"extension": {
"type": "string"
}
}
}
},
"required": ["prompt"]
},
{
"properties": {
"prompt": {
Expand Down
33 changes: 33 additions & 0 deletions src/ScriptRunner/ScriptRunner.GUI/ParamsPanelFactory.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public enum PromptType
FilePicker,
DirectoryPicker,
Numeric,
TimePicker
TimePicker,
FileContent
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,30 @@ private static IEnumerable<ScriptConfig> LoadFileSource(string fileName)
var scriptConfig = JsonSerializer.Deserialize<ActionsConfig>(jsonString, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
AllowTrailingCommas = true,
Converters = {new PromptTypeJsonConverter(), new ParamTypeJsonConverter()}
})!;


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 = "<default>"
};

foreach (var param in action.Params)
{
defaultSet.Arguments[param.Name] = param.Default;
Expand All @@ -78,14 +91,22 @@ private static IEnumerable<ScriptConfig> 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);
}
}
}
}
}
}

Expand Down

0 comments on commit 3f56db8

Please sign in to comment.