Skip to content

Commit

Permalink
fix(designer): More Custom Code improvements (#4720)
Browse files Browse the repository at this point in the history
even more!
  • Loading branch information
Eric-B-Wu committed Apr 29, 2024
1 parent 13eacfc commit 101e420
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 60 deletions.
4 changes: 2 additions & 2 deletions libs/designer-ui/src/lib/code/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ function formatForPowershell(property: string, actionName?: string, source?: str

function formatForCSharp(property: string, actionName?: string, source?: string): string {
const result = actionName
? `(await context.GetActionResult("${actionName}"))${source ? `.${source}` : ''}${property}`
: `(await context.GetTriggerResults())${source ? `.${source}` : ''}${property}`;
? `(await context.GetActionResult("${actionName}").ConfigureAwait(false))${source ? `.${source}` : ''}${property}`
: `(await context.GetTriggerResults().ConfigureAwait(false))${source ? `.${source}` : ''}${property}`;
return result;
}

Expand Down
1 change: 0 additions & 1 deletion libs/designer/src/lib/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ export default {
},
},
DEFAULT_CUSTOM_CODE_INPUT: 'CodeFile',
INLINECODE: 'connectionProviders/inlineCode',
EVENT_AUTH_COMPLETED: 'MSLA_AUTH_COMPLETED',
ERROR_MESSAGES: {
FAILED_TO_FETCH: 'Failed to fetch',
Expand Down
8 changes: 5 additions & 3 deletions libs/designer/src/lib/core/actions/bjsworkflow/add.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isCustomCode } from '@microsoft/designer-ui';
import Constants from '../../../common/constants';
import type { WorkflowNode } from '../../parsers/models/workflowNode';
import { getConnectionsForConnector, getConnectorWithSwagger } from '../../queries/connections';
Expand All @@ -21,7 +22,7 @@ import { addNode, setFocusNode } from '../../state/workflow/workflowSlice';
import type { AppDispatch, RootState } from '../../store';
import { getBrandColorFromManifest, getIconUriFromManifest } from '../../utils/card';
import { getTriggerNodeId, isRootNodeInGraph } from '../../utils/graph';
import { updateDynamicDataInNode } from '../../utils/parameters/helper';
import { getParameterFromName, updateDynamicDataInNode } from '../../utils/parameters/helper';
import { getInputParametersFromSwagger, getOutputParametersFromSwagger } from '../../utils/swagger/operation';
import { convertOutputsToTokens, getBuiltInTokens, getTokenNodeIds } from '../../utils/tokens';
import { getAllVariables, getVariableDeclarations, setVariableMetadata } from '../../utils/variables';
Expand Down Expand Up @@ -128,8 +129,9 @@ export const initializeOperationDetails = async (
const iconUri = getIconUriFromManifest(manifest);
const brandColor = getBrandColorFromManifest(manifest);
const { inputs: nodeInputs, dependencies: inputDependencies } = getInputParametersFromManifest(nodeId, manifest, presetParameterValues);
if (equals(operationInfo.connectorId, Constants.INLINECODE) && !equals(operationInfo.operationId, 'javascriptcode')) {
initializeCustomCodeDataInInputs(nodeInputs, nodeId, dispatch);
const customCodeParameter = getParameterFromName(nodeInputs, Constants.DEFAULT_CUSTOM_CODE_INPUT);
if (customCodeParameter && isCustomCode(customCodeParameter?.editor, customCodeParameter?.editorOptions?.language)) {
initializeCustomCodeDataInInputs(customCodeParameter, nodeId, dispatch);
}
const { outputs: nodeOutputs, dependencies: outputDependencies } = getOutputParametersFromManifest(
manifest,
Expand Down
19 changes: 6 additions & 13 deletions libs/designer/src/lib/core/actions/bjsworkflow/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { getSplitOnOptions, getUpdatedManifestForSchemaDependency, getUpdatedMan
import {
addRecurrenceParametersInGroup,
getAllInputParameters,
getCustomCodeFileName,
getCustomCodeFileNameFromParameter,
getDependentParameters,
getInputsValueFromDefinitionForManifest,
getParameterFromName,
Expand Down Expand Up @@ -451,8 +451,7 @@ export const updateCallbackUrlInInputs = async (
return;
};

export const initializeCustomCodeDataInInputs = (inputs: NodeInputs, nodeId: string, dispatch: Dispatch) => {
const parameter = getParameterFromName(inputs, Constants.DEFAULT_CUSTOM_CODE_INPUT);
export const initializeCustomCodeDataInInputs = (parameter: ParameterInfo, nodeId: string, dispatch: Dispatch) => {
const language: EditorLanguage = parameter?.editorOptions?.language;
if (parameter) {
const fileData = generateDefaultCustomCodeValue(language);
Expand All @@ -473,24 +472,18 @@ export const initializeCustomCodeDataInInputs = (inputs: NodeInputs, nodeId: str
}
};

export const updateCustomCodeInInputs = async (
nodeId: string,
fileExtension: string,
nodeInputs: NodeInputs,
customCode: CustomCodeFileNameMapping
) => {
export const updateCustomCodeInInputs = async (parameter: ParameterInfo, customCode: CustomCodeFileNameMapping) => {
if (isNullOrEmpty(customCode)) {
return;
}
// getCustomCodeFileName does not return the file extension because the editor view model is not populated yet
const fileName = getCustomCodeFileName(nodeId, nodeInputs) + fileExtension;
const language: EditorLanguage = parameter?.editorOptions?.language;
const fileName = getCustomCodeFileNameFromParameter(parameter);
try {
const customCodeValue = getRecordEntry(customCode, fileName)?.fileData;
const parameter = getParameterFromName(nodeInputs, Constants.DEFAULT_CUSTOM_CODE_INPUT);

if (parameter && customCodeValue) {
parameter.editorViewModel = {
customCodeData: { fileData: customCodeValue, fileExtension, fileName },
customCodeData: { fileData: customCodeValue, fileExtension: getFileExtensionName(language), fileName },
};
}
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable no-param-reassign */
import { isCustomCode } from '@microsoft/designer-ui';
import type { CustomCodeFileNameMapping } from '../../..';
import Constants from '../../../common/constants';
import type { ConnectionReference, ConnectionReferences, WorkflowParameter } from '../../../common/models/workflow';
Expand Down Expand Up @@ -34,6 +35,7 @@ import type { RepetitionContext } from '../../utils/parameters/helper';
import {
flattenAndUpdateViewModel,
getAllInputParameters,
getParameterFromName,
shouldIncludeSelfForRepetitionReference,
updateDynamicDataInNode,
updateScopePasteTokenMetadata,
Expand Down Expand Up @@ -68,7 +70,6 @@ import {
aggregate,
equals,
getRecordEntry,
getFileExtensionNameFromOperationId,
parseErrorMessage,
} from '@microsoft/logic-apps-shared';
import type { InputParameter, OutputParameter, LogicAppsV2, OperationManifest } from '@microsoft/logic-apps-shared';
Expand Down Expand Up @@ -256,9 +257,10 @@ export const initializeOperationDetailsForManifest = async (
await updateCallbackUrlInInputs(nodeId, nodeOperationInfo, nodeInputs);
}

const customCodeParameter = getParameterFromName(nodeInputs, Constants.DEFAULT_CUSTOM_CODE_INPUT);
// Populate Customcode with values gotten from file system
if (equals(operationInfo.connectorId, Constants.INLINECODE) && !equals(operationInfo.operationId, 'javascriptcode')) {
updateCustomCodeInInputs(nodeId, getFileExtensionNameFromOperationId(operationInfo.operationId), nodeInputs, customCode);
if (customCodeParameter && isCustomCode(customCodeParameter?.editor, customCodeParameter?.editorOptions?.language)) {
updateCustomCodeInInputs(customCodeParameter, customCode);
}

const { outputs: nodeOutputs, dependencies: outputDependencies } = getOutputParametersFromManifest(
Expand Down
4 changes: 4 additions & 0 deletions libs/designer/src/lib/core/utils/parameters/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2636,6 +2636,10 @@ export function getGroupAndParameterFromParameterKey(
return undefined;
}

export const getCustomCodeFileNameFromParameter = (parameter: ParameterInfo): string => {
return parameter.value?.[0].value ?? '';
};

export const getCustomCodeFileName = (nodeId: string, nodeInputs?: NodeInputs, idReplacements?: Record<string, string>): string => {
const updatedNodeId = idReplacements?.[nodeId] || nodeId;
let fileName = replaceWhiteSpaceWithUnderscore(updatedNodeId);
Expand Down
84 changes: 46 additions & 38 deletions libs/logic-apps-shared/src/utils/src/lib/helpers/customcode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,6 @@ export const getFileExtensionName = (language: EditorLanguage): string => {
}
};

export const getFileExtensionNameFromOperationId = (operationId: string): string => {
switch (operationId) {
case 'csharpscriptcode':
return '.csx';
case 'powershellcode':
return '.ps1';
default:
return '.txt';
}
};

export const getAppFileForFileExtension = (fileExtension: string): string => {
if (fileExtension === '.ps1') {
return "# This file enables modules to be automatically managed by the Functions service.\r\n# See https://aka.ms/functionsmanageddependency for additional information.\r\n#\r\n@{\r\n # For latest supported version, go to 'https://www.powershellgallery.com/packages/Az'. Uncomment the next line and replace the MAJOR_VERSION, e.g., 'Az' = '5.*'\r\n 'Az' = '10.*'\r\n}";
Expand All @@ -69,42 +58,61 @@ $result = Start-AzLogicApp -ResourceGroupName $resourceGroupName -Name $logicApp
Push-ActionOutputs -body $result`;
case EditorLanguage.csharp:
return `// Add the required libraries
const Newtonsoft = require("Newtonsoft.Json");
const AzureScripting = require("Microsoft.Azure.Workflows.Scripting");
// Define the function to run
async function run(context) {
// Get the outputs from the 'compose' action
const outputs = (await context.GetActionResults("compose")).Outputs;
#r "Newtonsoft.Json"
#r "Microsoft.Azure.Workflows.Scripting"
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Microsoft.Azure.Workflows.Scripting;g;
/// <summary>
/// Executes the inline csharp code.
/// </summary>
/// <param name="context">The workflow context.</param>
public static async Task<Weather> Run(WorkflowContext context)
{
var outputs = (await context.GetActionResults("compose").ConfigureAwait(false)).Outputs;
// Generate random temperature within a range based on the temperature scale
const temperatureScale = outputs["temperatureScale"].toString();
const currentTemp = temperatureScale === "Celsius" ? Math.floor(Math.random() * (30 - 1 + 1)) + 1 : Math.floor(Math.random() * (90 - 40 + 1)) + 40;
const lowTemp = currentTemp - 10;
const highTemp = currentTemp + 10;
Random rnd = new Random();
var temperatureScale = outputs["temperatureScale"].ToString();
var currentTemp = temperatureScale == "Celsius" ? rnd.Next(1, 30) : rnd.Next(40, 90);
var lowTemp = currentTemp - 10;
var highTemp = currentTemp + 10;
// Create a Weather object with the temperature information
const weather = {
ZipCode: parseInt(outputs["zipCode"]),
CurrentWeather: \`The current weather is \${currentTemp} \${temperatureScale}\`,
DayLow: \`The low for the day is \${lowTemp} \${temperatureScale}\`,
DayHigh: \`The high for the day is \${highTemp} \${temperatureScale}\`
var weather = new Weather()
{
ZipCode = (int) outputs["zipCode"],
CurrentWeather = $"The current weather is {currentTemp} {temperatureScale}",
DayLow = $"The low for the day is {lowTemp} {temperatureScale}",
DayHigh = $"The high for the day is {highTemp} {temperatureScale}"
};
return weather;
}
// Define the Weather class
class Weather {
constructor() {
this.ZipCode = 0;
this.CurrentWeather = "";
this.DayLow = "";
this.DayHigh = "";
}
}
module.exports = run;`;
/// <summary>
/// Represents the weather information.
/// </summary>
public class Weather
{
/// <summary>
/// Gets or sets the zip code.
/// </summary>
public int ZipCode { get; set; }
/// <summary>
/// Gets or sets the current weather.
/// </summary>
public string CurrentWeather { get; set; }
/// <summary>
/// Gets or sets the low temperature for the day.
/// </summary>
public string DayLow { get; set; }
/// <summary>
/// Gets or sets the high temperature for the day.
/// </summary>
public string DayHigh { get; set; }
}`;
default:
return '';
}
Expand Down

0 comments on commit 101e420

Please sign in to comment.