in src/Cli/func/Actions/LocalActions/CreateFunctionAction.cs [102:260]
public override async Task RunAsync()
{
// Check if the command only ran for help.
if (!string.IsNullOrEmpty(TriggerNameForHelp))
{
await ProcessHelpRequest(TriggerNameForHelp, true);
return;
}
if (!ValidateInputs())
{
return;
}
await UpdateLanguageAndRuntime();
if (WorkerRuntimeLanguageHelper.IsDotnet(_workerRuntime) && !Csx)
{
if (string.IsNullOrWhiteSpace(TemplateName))
{
SelectionMenuHelper.DisplaySelectionWizardPrompt("template");
TemplateName ??= SelectionMenuHelper.DisplaySelectionWizard(DotnetHelpers.GetTemplates(_workerRuntime));
}
else
{
ColoredConsole.WriteLine($"Template: {TemplateName}");
}
ColoredConsole.Write("Function name: ");
FunctionName = FunctionName ?? Console.ReadLine();
ColoredConsole.WriteLine(FunctionName);
var namespaceStr = Path.GetFileName(Environment.CurrentDirectory);
await DotnetHelpers.DeployDotnetFunction(TemplateName.Replace(" ", string.Empty), Utilities.SanitizeClassName(FunctionName), Utilities.SanitizeNameSpace(namespaceStr), Language.Replace("-isolated", string.Empty), _workerRuntime, AuthorizationLevel);
}
else if (IsNewPythonProgrammingModel())
{
if (string.IsNullOrEmpty(TemplateName))
{
SelectionMenuHelper.DisplaySelectionWizardPrompt("template");
TemplateName = TemplateName ?? SelectionMenuHelper.DisplaySelectionWizard(GetTriggerNamesFromNewTemplates(Language));
}
// Defaulting the filename to "function_app.py" if the file name is not provided.
if (string.IsNullOrWhiteSpace(FileName))
{
FileName = "function_app.py";
}
var userPrompt = _userPrompts.Value.First(x => string.Equals(x.Id, "app-selectedFileName", StringComparison.OrdinalIgnoreCase));
while (!_userInputHandler.ValidateResponse(userPrompt, FileName))
{
_userInputHandler.PrintInputLabel(userPrompt, PySteinFunctionAppPy);
FileName = Console.ReadLine();
if (string.IsNullOrEmpty(FileName))
{
FileName = PySteinFunctionAppPy;
}
}
var providedInputs = new Dictionary<string, string>()
{
{ GetFunctionNameParamId, FunctionName },
{ HttpTriggerAuthLevelParamId, AuthorizationLevel?.ToString().ToUpperInvariant() }
};
var jobName = "appendToFile";
if (FileName != PySteinFunctionAppPy)
{
var filePath = Path.Combine(Environment.CurrentDirectory, FileName);
if (FileUtility.FileExists(filePath))
{
jobName = "AppendToBlueprint";
providedInputs[GetBluePrintExistingFileNameParamId] = FileName;
}
else
{
jobName = "CreateNewBlueprint";
providedInputs[GetBluePrintFileNameParamId] = FileName;
}
}
else
{
providedInputs[GetFileNameParamId] = FileName;
}
var template = _newTemplates.Value.FirstOrDefault(t => string.Equals(t.Name, TemplateName, StringComparison.CurrentCultureIgnoreCase) && string.Equals(t.Language, Language, StringComparison.CurrentCultureIgnoreCase));
var templateJob = template.Jobs.Single(x => x.Type.Equals(jobName, StringComparison.OrdinalIgnoreCase));
var variables = new Dictionary<string, string>();
_userInputHandler.RunUserInputActions(providedInputs, templateJob.Inputs, variables);
if (string.IsNullOrEmpty(FunctionName))
{
FunctionName = providedInputs[GetFunctionNameParamId];
}
await _templatesManager.Deploy(templateJob, template, variables);
}
else
{
SelectionMenuHelper.DisplaySelectionWizardPrompt("template");
string templateLanguage;
try
{
templateLanguage = WorkerRuntimeLanguageHelper.NormalizeLanguage(Language);
}
catch (Exception)
{
// Ideally this should never happen.
templateLanguage = WorkerRuntimeLanguageHelper.GetDefaultTemplateLanguageFromWorker(_workerRuntime);
}
TelemetryHelpers.AddCommandEventToDictionary(TelemetryCommandEvents, "language", templateLanguage);
TemplateName = TemplateName ?? SelectionMenuHelper.DisplaySelectionWizard(GetTriggerNames(templateLanguage));
ColoredConsole.WriteLine(TitleColor(TemplateName));
Template template = GetLanguageTemplates(templateLanguage).FirstOrDefault(t => Utilities.EqualsIgnoreCaseAndSpace(t.Metadata.Name, TemplateName));
if (template == null)
{
TelemetryHelpers.AddCommandEventToDictionary(TelemetryCommandEvents, "template", "N/A");
throw new CliException($"Can't find template \"{TemplateName}\" in \"{Language}\"");
}
else
{
TelemetryHelpers.AddCommandEventToDictionary(TelemetryCommandEvents, "template", TemplateName);
var extensionBundleManager = ExtensionBundleHelper.GetExtensionBundleManager();
if (template.Metadata.Extensions != null && !extensionBundleManager.IsExtensionBundleConfigured() && !CommandChecker.CommandExists("dotnet"))
{
throw new CliException($"The {template.Metadata.Name} template has extensions. {Constants.Errors.ExtensionsNeedDotnet}");
}
if (!IsNewNodeJsProgrammingModel(_workerRuntime) && AuthorizationLevel.HasValue)
{
ConfigureAuthorizationLevel(template);
}
ColoredConsole.Write($"Function name: [{template.Metadata.DefaultFunctionName}] ");
FunctionName = FunctionName ?? Console.ReadLine();
FunctionName = string.IsNullOrEmpty(FunctionName) ? template.Metadata.DefaultFunctionName : FunctionName;
await _templatesManager.Deploy(FunctionName, FileName, template);
PerformPostDeployTasks(FunctionName, Language);
}
}
ColoredConsole.WriteLine($"The function \"{FunctionName}\" was created successfully from the \"{TemplateName}\" template.");
if (string.Equals(Language, Languages.Python, StringComparison.CurrentCultureIgnoreCase) && !IsNewPythonProgrammingModel())
{
PythonHelpers.PrintPySteinAwarenessMessage();
}
var isNewNodeJsModel = IsNewNodeJsProgrammingModel(_workerRuntime);
if (_workerRuntime == WorkerRuntime.Node && !isNewNodeJsModel)
{
NodeJSHelpers.PrintV4AwarenessMessage();
}
}