-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Input Handling and App Creation code (#613)
Co-authored-by: Andrew Petrochuk <[email protected]>
- Loading branch information
Showing
8 changed files
with
195 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.PowerPlatform.PowerApps.Persistence.Extensions; | ||
using Microsoft.PowerPlatform.PowerApps.Persistence.MsApp; | ||
|
||
namespace MSAppGenerator; | ||
|
||
public class AppCreator | ||
{ | ||
/// <summary> | ||
/// Configures default services for generating the MSApp representation | ||
/// </summary> | ||
private static IServiceProvider ConfigureServiceProvider() | ||
{ | ||
var serviceCollection = new ServiceCollection(); | ||
serviceCollection.AddPowerAppsPersistence(true); | ||
serviceCollection.AddSingleton<IAppGeneratorFactory, AppGeneratorFactory>(); | ||
var serviceProvider = serviceCollection.BuildServiceProvider(); | ||
return serviceProvider; | ||
} | ||
|
||
IServiceProvider _serviceProvider { get; set; } | ||
|
||
public AppCreator(IServiceProvider serviceProvider) | ||
{ | ||
_serviceProvider = serviceProvider; | ||
} | ||
|
||
public AppCreator() | ||
{ | ||
_serviceProvider = ConfigureServiceProvider(); | ||
} | ||
|
||
/// <summary> | ||
/// Attempt to do specified app creation | ||
/// </summary> | ||
public void CreateMSApp(bool interactive, string fullPathToMsApp, int numScreens, IList<string>? controlsinfo) | ||
{ | ||
// Create a new empty MSApp | ||
using var msapp = _serviceProvider.GetRequiredService<IMsappArchiveFactory>().Create(fullPathToMsApp); | ||
|
||
// Select Generator based off specified mode | ||
var generator = _serviceProvider.GetRequiredService<IAppGeneratorFactory>().Create(interactive); | ||
|
||
// Generate the app | ||
msapp.App = generator.GenerateApp(Path.GetFileNameWithoutExtension(fullPathToMsApp), | ||
numScreens, controlsinfo); | ||
|
||
// Output the MSApp to the path provided | ||
msapp.Save(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System.CommandLine; | ||
using MSAppGenerator; | ||
|
||
namespace Test.AppWriter; | ||
|
||
internal class InputProcessor | ||
{ | ||
/// <summary> | ||
/// Validate that the provided filepath is accessible and not an existing file | ||
/// </summary> | ||
private static bool ValidateFilePath(string filePath, out string error) | ||
{ | ||
error = string.Empty; | ||
if (!File.Exists(filePath)) | ||
return true; | ||
|
||
Console.WriteLine($"Warning: File '{filePath}' already exists"); | ||
Console.Write("Overwrite? ([y]/n) - enter for yes: "); | ||
|
||
var input = Console.ReadKey(); | ||
Console.WriteLine(); | ||
if (input.Key == ConsoleKey.Enter || input.Key == ConsoleKey.Y) | ||
{ | ||
try | ||
{ | ||
File.Delete(filePath); | ||
} | ||
catch (IOException ex) | ||
{ | ||
error = $"Error: {ex.Message}"; | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
error = $"Unable to overwrite file"; | ||
return false; | ||
} | ||
|
||
/// <summary> | ||
/// Function to bind to the Create command and call App Creation code | ||
/// </summary> | ||
private static void CreateFunction(bool interactive, string fullPathToMsApp, int numScreens, IList<string>? controlsinfo) | ||
{ | ||
var creator = new AppCreator(); | ||
|
||
try | ||
{ | ||
creator.CreateMSApp(interactive, fullPathToMsApp, numScreens, controlsinfo); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine(ex.Message); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Configures and returns the root command to process commandline arguments | ||
/// </summary> | ||
public static RootCommand GetRootCommand() | ||
{ | ||
var interactiveOption = new Option<bool>( | ||
name: "--interactive", | ||
description: "Enables interactive mode for MSApp creation", | ||
getDefaultValue: () => false // If --interactive is not specified, default to argument based creation | ||
); | ||
var filePathOption = new Option<FileInfo?>( | ||
name: "--filepath", | ||
description: "(string) The path where the msapp file should be generated, including filename and extension", | ||
parseArgument: result => | ||
{ | ||
var filePath = result.Tokens.Single().Value; | ||
if (ValidateFilePath(filePath, out var error)) | ||
return new FileInfo(filePath); | ||
result.ErrorMessage = error; | ||
return null; | ||
} | ||
) | ||
{ IsRequired = true }; | ||
var numScreensOption = new Option<int>( | ||
name: "--numscreens", | ||
description: "(integer) The number of screens to generate in the App", | ||
getDefaultValue: () => 1 | ||
); | ||
numScreensOption.AddValidator(result => | ||
{ | ||
if (result.GetValueForOption(numScreensOption) < 0) | ||
{ | ||
result.ErrorMessage = "Number of screens must be greater than 0"; | ||
} | ||
}); | ||
var controlsOptions = new Option<IList<string>>( | ||
name: "--controls", | ||
description: "(list of string) A list of control templates (i.e. Button Label [Template]...)") | ||
{ AllowMultipleArgumentsPerToken = true }; | ||
|
||
var rootCommand = new RootCommand("Test Writer for MSApp files."); | ||
var createCommand = new Command("create", "Create a new MSApp at the specified path.") | ||
{ | ||
interactiveOption, | ||
filePathOption, | ||
numScreensOption, | ||
controlsOptions | ||
}; | ||
|
||
createCommand.SetHandler((interactive, filepath, numscreens, controls) => | ||
{ | ||
CreateFunction(interactive, filepath!.FullName, numscreens, controls); | ||
}, interactiveOption, filePathOption, numScreensOption, controlsOptions); | ||
|
||
rootCommand.AddCommand(createCommand); | ||
|
||
return rootCommand; | ||
} | ||
} |
Oops, something went wrong.