| | 1 | | /* |
| | 2 | | * Copyright (c) 2024 Willy Alberto Kuster |
| | 3 | | * |
| | 4 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| | 5 | | * of this software and associated documentation files (the "Software"), to deal |
| | 6 | | * in the Software without restriction, including without limitation the rights |
| | 7 | | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| | 8 | | * copies of the Software, and to permit persons to whom the Software is |
| | 9 | | * furnished to do so, subject to the following conditions: |
| | 10 | | * |
| | 11 | | * The above copyright notice and this permission notice shall be included in |
| | 12 | | * all copies or substantial portions of the Software. |
| | 13 | | * |
| | 14 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| | 15 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| | 16 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| | 17 | | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| | 18 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| | 19 | | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| | 20 | | * THE SOFTWARE. |
| | 21 | | */ |
| | 22 | | using JetBrains.Annotations; |
| | 23 | | using System; |
| | 24 | | using System.IO; |
| | 25 | | using System.Linq; |
| | 26 | | using System.Threading; |
| | 27 | | using System.Threading.Tasks; |
| | 28 | | using UnityEngine; |
| | 29 | | using UnityObject = UnityEngine.Object; |
| | 30 | |
|
| | 31 | | namespace Willykc.Templ.Editor.Scaffold |
| | 32 | | { |
| | 33 | | using Abstraction; |
| | 34 | | using static TemplSettings; |
| | 35 | |
|
| | 36 | | internal sealed class TemplScaffoldFacade : ITemplScaffoldFacade |
| | 37 | | { |
| | 38 | | private const int MaxOverwriteTries = 20; |
| | 39 | |
|
| | 40 | | private readonly ILogger log; |
| | 41 | | private readonly ISettingsProvider settingsProvider; |
| | 42 | | private readonly IAssetDatabase assetDatabase; |
| | 43 | | private readonly IEditorUtility editorUtility; |
| | 44 | | private readonly ITemplScaffoldWindowManager windowManager; |
| | 45 | | private readonly ITemplScaffoldCore scaffoldCore; |
| 42 | 46 | | private readonly object lockHandle = new object(); |
| | 47 | |
|
| | 48 | | private bool isGenerating; |
| | 49 | |
|
| 2 | 50 | | bool ITemplScaffoldFacade.IsGenerating => isGenerating; |
| | 51 | |
|
| 42 | 52 | | internal TemplScaffoldFacade( |
| | 53 | | ILogger log, |
| | 54 | | ISettingsProvider settingsProvider, |
| | 55 | | IAssetDatabase assetDatabase, |
| | 56 | | IEditorUtility editorUtility, |
| | 57 | | ITemplScaffoldWindowManager windowManager, |
| | 58 | | ITemplScaffoldCore scaffoldCore) |
| 42 | 59 | | { |
| 42 | 60 | | this.log = log; |
| 42 | 61 | | this.settingsProvider = settingsProvider; |
| 42 | 62 | | this.assetDatabase = assetDatabase; |
| 42 | 63 | | this.editorUtility = editorUtility; |
| 42 | 64 | | this.windowManager = windowManager; |
| 42 | 65 | | this.scaffoldCore = scaffoldCore; |
| 42 | 66 | | } |
| | 67 | |
|
| | 68 | | [ItemCanBeNull] |
| | 69 | | async Task<string[]> ITemplScaffoldFacade.GenerateScaffoldAsync( |
| | 70 | | TemplScaffold scaffold, |
| | 71 | | string targetPath, |
| | 72 | | object input, |
| | 73 | | UnityObject selection, |
| | 74 | | OverwriteOptions overwriteOption, |
| | 75 | | CancellationToken cancellationToken) |
| 29 | 76 | | { |
| 29 | 77 | | if (cancellationToken.IsCancellationRequested) |
| 1 | 78 | | { |
| 1 | 79 | | return null; |
| | 80 | | } |
| | 81 | |
|
| 28 | 82 | | scaffold = scaffold ? scaffold : throw new ArgumentNullException(nameof(scaffold)); |
| 27 | 83 | | targetPath = !string.IsNullOrWhiteSpace(targetPath) |
| | 84 | | ? targetPath |
| | 85 | | : throw new ArgumentException($"{nameof(targetPath)} must not be null or empty", |
| | 86 | | nameof(targetPath)); |
| | 87 | |
|
| 25 | 88 | | if (!scaffold.IsValid) |
| 1 | 89 | | { |
| 1 | 90 | | throw new ArgumentException($"{nameof(scaffold)} must be valid", |
| | 91 | | nameof(scaffold)); |
| | 92 | | } |
| | 93 | |
|
| 24 | 94 | | targetPath = targetPath.SanitizePath(); |
| | 95 | |
|
| 24 | 96 | | if (!assetDatabase.IsValidFolder(targetPath)) |
| 1 | 97 | | { |
| 1 | 98 | | throw new DirectoryNotFoundException($"Directory does not exist in the asset database: '{targetPath}'"); |
| | 99 | | } |
| | 100 | |
|
| 23 | 101 | | lock (lockHandle) |
| 23 | 102 | | { |
| 23 | 103 | | if (isGenerating) |
| 1 | 104 | | { |
| 1 | 105 | | log.Error("Only one scaffold can be generated at a time"); |
| 1 | 106 | | return null; |
| | 107 | | } |
| | 108 | |
|
| 22 | 109 | | isGenerating = true; |
| 22 | 110 | | } |
| | 111 | |
|
| 22 | 112 | | string[] generatedPaths = null; |
| | 113 | |
|
| | 114 | | try |
| 22 | 115 | | { |
| 34 | 116 | | generatedPaths = scaffold.DefaultInput && input == null |
| | 117 | | ? await ShowScaffoldInputFormAsync( |
| | 118 | | scaffold, |
| | 119 | | targetPath, |
| | 120 | | selection, |
| | 121 | | overwriteOption, |
| | 122 | | cancellationToken) |
| | 123 | | : await TryGenerateScaffoldAsync( |
| | 124 | | scaffold, |
| | 125 | | targetPath, |
| | 126 | | input, |
| | 127 | | selection, |
| | 128 | | overwriteOption, |
| | 129 | | cancellationToken); |
| 22 | 130 | | } |
| | 131 | | finally |
| 22 | 132 | | { |
| 22 | 133 | | isGenerating = false; |
| 22 | 134 | | } |
| | 135 | |
|
| 22 | 136 | | return generatedPaths; |
| 24 | 137 | | } |
| | 138 | |
|
| | 139 | | void ITemplScaffoldFacade.EnableScaffoldForSelection(TemplScaffold scaffold) |
| 7 | 140 | | { |
| 7 | 141 | | if (!settingsProvider.SettingsExist()) |
| 1 | 142 | | { |
| 1 | 143 | | throw new InvalidOperationException($"{nameof(TemplSettings)} not found"); |
| | 144 | | } |
| | 145 | |
|
| 6 | 146 | | scaffold = scaffold ? scaffold : throw new ArgumentNullException(nameof(scaffold)); |
| | 147 | |
|
| 5 | 148 | | if (!scaffold.IsValid) |
| 1 | 149 | | { |
| 1 | 150 | | throw new ArgumentException("Can not enable for selection an invalid scaffold", |
| | 151 | | nameof(scaffold)); |
| | 152 | | } |
| | 153 | |
|
| 4 | 154 | | var settings = settingsProvider.GetSettings(); |
| | 155 | |
|
| 4 | 156 | | lock (lockHandle) |
| 4 | 157 | | { |
| 4 | 158 | | if (settings.Scaffolds.Contains(scaffold)) |
| 1 | 159 | | { |
| 1 | 160 | | return; |
| | 161 | | } |
| | 162 | |
|
| 3 | 163 | | settings.Scaffolds.Add(scaffold); |
| | 164 | |
|
| 3 | 165 | | editorUtility.SetDirty(settings); |
| 3 | 166 | | assetDatabase.SaveAssets(); |
| 3 | 167 | | } |
| 4 | 168 | | } |
| | 169 | |
|
| | 170 | | void ITemplScaffoldFacade.DisableScaffoldForSelection(TemplScaffold scaffold) |
| 7 | 171 | | { |
| 7 | 172 | | if (!settingsProvider.SettingsExist()) |
| 1 | 173 | | { |
| 1 | 174 | | throw new InvalidOperationException($"{nameof(TemplSettings)} not found"); |
| | 175 | | } |
| | 176 | |
|
| 6 | 177 | | scaffold = scaffold ? scaffold : throw new ArgumentNullException(nameof(scaffold)); |
| | 178 | |
|
| 5 | 179 | | var settings = settingsProvider.GetSettings(); |
| | 180 | |
|
| 5 | 181 | | lock (lockHandle) |
| 5 | 182 | | { |
| 5 | 183 | | if (!settings.Scaffolds.Contains(scaffold)) |
| 1 | 184 | | { |
| 1 | 185 | | return; |
| | 186 | | } |
| | 187 | |
|
| 4 | 188 | | settings.Scaffolds.Remove(scaffold); |
| | 189 | |
|
| 4 | 190 | | editorUtility.SetDirty(settings); |
| 4 | 191 | | assetDatabase.SaveAssets(); |
| 4 | 192 | | } |
| 5 | 193 | | } |
| | 194 | |
|
| | 195 | | private async Task<string[]> ShowScaffoldInputFormAsync( |
| | 196 | | TemplScaffold scaffold, |
| | 197 | | string targetPath, |
| | 198 | | UnityObject selection, |
| | 199 | | OverwriteOptions overwriteOption, |
| | 200 | | CancellationToken cancellationToken) |
| 13 | 201 | | { |
| 13 | 202 | | var completionSource = new TaskCompletionSource<string[]>(); |
| 13 | 203 | | var tokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); |
| | 204 | |
|
| 5 | 205 | | void OnInputFormClosed() => tokenSource.Cancel(); |
| | 206 | |
|
| 13 | 207 | | var tries = 0; |
| | 208 | |
|
| 40 | 209 | | while (!tokenSource.IsCancellationRequested && |
| | 210 | | tries++ < MaxOverwriteTries && |
| | 211 | | await windowManager.ShowInputFormAsync( |
| | 212 | | scaffold, |
| | 213 | | targetPath, |
| | 214 | | selection, |
| | 215 | | OnInputFormClosed, |
| | 216 | | cancellationToken) is { } input) |
| 27 | 217 | | { |
| 35 | 218 | | string[] generatedPaths = await TryGenerateScaffoldAsync( |
| | 219 | | scaffold, |
| | 220 | | targetPath, |
| | 221 | | input, |
| | 222 | | selection, |
| | 223 | | overwriteOption, |
| | 224 | | tokenSource.Token); |
| | 225 | |
|
| 27 | 226 | | if (generatedPaths == null && !tokenSource.IsCancellationRequested) |
| 21 | 227 | | { |
| 21 | 228 | | continue; |
| | 229 | | } |
| | 230 | |
|
| 6 | 231 | | windowManager.CloseInputForm(); |
| 6 | 232 | | return generatedPaths; |
| | 233 | | } |
| | 234 | |
|
| 7 | 235 | | windowManager.CloseInputForm(); |
| 7 | 236 | | return null; |
| 13 | 237 | | } |
| | 238 | |
|
| | 239 | | private async Task<string[]> TryGenerateScaffoldAsync( |
| | 240 | | TemplScaffold scaffold, |
| | 241 | | string targetPath, |
| | 242 | | object input, |
| | 243 | | UnityObject selection, |
| | 244 | | OverwriteOptions overwriteOption, |
| | 245 | | CancellationToken cancellationToken) |
| 36 | 246 | | { |
| 36 | 247 | | var errors = |
| | 248 | | scaffoldCore.ValidateScaffoldGeneration(scaffold, targetPath, input, selection); |
| | 249 | |
|
| 36 | 250 | | var existingFilePaths = errors |
| 32 | 251 | | .Where(e => e.Type == TemplScaffoldErrorType.Overwrite) |
| 31 | 252 | | .Select(e => e.Message) |
| | 253 | | .ToArray(); |
| | 254 | |
|
| 68 | 255 | | if (errors.Any(e => e.Type != TemplScaffoldErrorType.Overwrite)) |
| 1 | 256 | | { |
| 1 | 257 | | log.Error($"Aborted generation of {scaffold.name} scaffold due to errors"); |
| 1 | 258 | | return null; |
| | 259 | | } |
| | 260 | |
|
| 43 | 261 | | var skipPaths = overwriteOption switch |
| | 262 | | { |
| | 263 | | OverwriteOptions.OverwriteAll => EmptyStringArray, |
| | 264 | | OverwriteOptions.SkipAll => existingFilePaths, |
| | 265 | | _ => await GetSkipPaths(scaffold, targetPath, existingFilePaths, cancellationToken) |
| | 266 | | }; |
| | 267 | |
|
| 35 | 268 | | if (existingFilePaths.Length > 0 && skipPaths == null) |
| 23 | 269 | | { |
| 23 | 270 | | return null; |
| | 271 | | } |
| | 272 | |
|
| 12 | 273 | | return scaffoldCore |
| | 274 | | .GenerateScaffold(scaffold, targetPath, input, selection, skipPaths); |
| 36 | 275 | | } |
| | 276 | |
|
| | 277 | | private async Task<string[]> GetSkipPaths( |
| | 278 | | TemplScaffold scaffold, |
| | 279 | | string targetPath, |
| | 280 | | string[] existingFilePaths, |
| 39 | 281 | | CancellationToken cancellationToken) => existingFilePaths.Length > 0 |
| | 282 | | ? await windowManager.ShowOverwriteDialogAsync( |
| | 283 | | scaffold, |
| | 284 | | targetPath, |
| | 285 | | existingFilePaths, |
| | 286 | | cancellationToken) |
| | 287 | | : null; |
| | 288 | | } |
| | 289 | | } |