CreatedExtensionForm.cs
1 // Copyright (c) Microsoft Corporation 2 // The Microsoft Corporation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System.Text.Json; 6 using System.Text.Json.Nodes; 7 using Microsoft.CommandPalette.Extensions; 8 using Microsoft.CommandPalette.Extensions.Toolkit; 9 10 namespace Microsoft.CmdPal.UI.ViewModels.BuiltinCommands; 11 12 internal sealed partial class CreatedExtensionForm : NewExtensionFormBase 13 { 14 public CreatedExtensionForm(string name, string displayName, string path) 15 { 16 var serializeString = (string? s) => JsonSerializer.Serialize(s, JsonSerializationContext.Default.String); 17 TemplateJson = CardTemplate; 18 DataJson = $$""" 19 { 20 "name": {{serializeString(name)}}, 21 "directory": {{serializeString(path)}}, 22 "displayName": {{serializeString(displayName)}} 23 } 24 """; 25 _name = name; 26 _displayName = displayName; 27 _path = path; 28 } 29 30 public override ICommandResult SubmitForm(string inputs, string data) 31 { 32 var dataInput = JsonNode.Parse(data)?.AsObject(); 33 if (dataInput is null) 34 { 35 return CommandResult.KeepOpen(); 36 } 37 38 var verb = dataInput["x"]?.AsValue()?.ToString() ?? string.Empty; 39 return verb switch 40 { 41 "sln" => OpenSolution(), 42 "dir" => OpenDirectory(), 43 "new" => CreateNew(), 44 _ => CommandResult.KeepOpen(), 45 }; 46 } 47 48 private ICommandResult OpenSolution() 49 { 50 string[] parts = [_path, _name, $"{_name}.sln"]; 51 var pathToSolution = Path.Combine(parts); 52 ShellHelpers.OpenInShell(pathToSolution); 53 return CommandResult.Hide(); 54 } 55 56 private ICommandResult OpenDirectory() 57 { 58 string[] parts = [_path, _name]; 59 var pathToDir = Path.Combine(parts); 60 ShellHelpers.OpenInShell(pathToDir); 61 return CommandResult.Hide(); 62 } 63 64 private ICommandResult CreateNew() 65 { 66 RaiseFormSubmit(null); 67 return CommandResult.KeepOpen(); 68 } 69 70 private static readonly string CardTemplate = $$""" 71 { 72 "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", 73 "type": "AdaptiveCard", 74 "version": "1.6", 75 "body": [ 76 { 77 "type": "TextBlock", 78 "text": "{{Properties.Resources.builtin_create_extension_success}}", 79 "size": "large", 80 "weight": "bolder", 81 "style": "heading", 82 "wrap": true 83 }, 84 { 85 "type": "TextBlock", 86 "text": "{{Properties.Resources.builtin_created_in_text}}", 87 "wrap": true 88 }, 89 { 90 "type": "TextBlock", 91 "text": "${directory}", 92 "fontType": "monospace" 93 }, 94 { 95 "type": "TextBlock", 96 "text": "{{Properties.Resources.builtin_created_next_steps_title}}", 97 "style": "heading", 98 "wrap": true 99 }, 100 { 101 "type": "TextBlock", 102 "text": "{{Properties.Resources.builtin_created_next_steps}}", 103 "wrap": true 104 }, 105 { 106 "type": "TextBlock", 107 "text": "{{Properties.Resources.builtin_created_next_steps_p2}}", 108 "wrap": true 109 }, 110 { 111 "type": "TextBlock", 112 "text": "{{Properties.Resources.builtin_created_next_steps_p3}}", 113 "wrap": true 114 } 115 ], 116 "actions": [ 117 { 118 "type": "Action.Submit", 119 "title": "{{Properties.Resources.builtin_create_extension_open_solution}}", 120 "data": { 121 "x": "sln" 122 } 123 }, 124 { 125 "type": "Action.Submit", 126 "title": "{{Properties.Resources.builtin_create_extension_open_directory}}", 127 "data": { 128 "x": "dir" 129 } 130 }, 131 { 132 "type": "Action.Submit", 133 "title": "{{Properties.Resources.builtin_create_extension_create_another}}", 134 "data": { 135 "x": "new" 136 } 137 } 138 ] 139 } 140 """; 141 142 private readonly string _name; 143 private readonly string _displayName; 144 private readonly string _path; 145 }