From 263f555846ee9e7bf95fea3aebcfe995603de0cc Mon Sep 17 00:00:00 2001 From: Troy Simeon Taylor <44444967+troystaylor@users.noreply.github.com> Date: Mon, 8 Sep 2025 15:11:45 -0400 Subject: [PATCH 1/2] Add files via upload --- .../apiDefinition.swagger.json | 570 ++++ .../apiProperties.json | 15 + .../Microsoft BASIC Interpreter/readme.md | 534 ++++ .../Microsoft BASIC Interpreter/script.csx | 2369 +++++++++++++++++ 4 files changed, 3488 insertions(+) create mode 100644 independent-publisher-connectors/Microsoft BASIC Interpreter/apiDefinition.swagger.json create mode 100644 independent-publisher-connectors/Microsoft BASIC Interpreter/apiProperties.json create mode 100644 independent-publisher-connectors/Microsoft BASIC Interpreter/readme.md create mode 100644 independent-publisher-connectors/Microsoft BASIC Interpreter/script.csx diff --git a/independent-publisher-connectors/Microsoft BASIC Interpreter/apiDefinition.swagger.json b/independent-publisher-connectors/Microsoft BASIC Interpreter/apiDefinition.swagger.json new file mode 100644 index 0000000000..d792339555 --- /dev/null +++ b/independent-publisher-connectors/Microsoft BASIC Interpreter/apiDefinition.swagger.json @@ -0,0 +1,570 @@ +{ + "swagger": "2.0", + "info": { + "title": "Microsoft BASIC Interpreter", + "description": "Execute Microsoft BASIC commands using the original M6502 interpreter logic with enhanced error handling and debugging capabilities", + "version": "1.0.0", + "contact": { + "name": "Troy Taylor", + "email": "troy@troystaylor.com" + }, + "license": { + "name": "MIT", + "url": "https://opensource.org/licenses/MIT" + } + }, + "host": "troystaylor.com", + "basePath": "/", + "schemes": [ + "https" + ], + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "paths": { + "/execute": { + "post": { + "summary": "Execute BASIC program", + "description": "Execute a BASIC program using the Microsoft M6502 interpreter engine with comprehensive error handling and variable tracking", + "operationId": "ExecuteBasicCode", + "x-ms-summary": "Execute BASIC Code", + "x-ms-visibility": "important", + "parameters": [ + { + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/BasicExecutionRequest" + }, + "x-ms-summary": "BASIC Execution Request" + } + ], + "responses": { + "200": { + "description": "Execution completed successfully", + "x-ms-summary": "Success", + "schema": { + "$ref": "#/definitions/BasicExecutionResponse" + } + }, + "400": { + "description": "Invalid BASIC code or request format", + "x-ms-summary": "Bad Request", + "schema": { + "$ref": "#/definitions/ErrorResponse" + } + }, + "500": { + "description": "Internal server error during execution", + "x-ms-summary": "Server Error", + "schema": { + "$ref": "#/definitions/ErrorResponse" + } + } + } + } + }, + "/validate": { + "post": { + "summary": "Validate BASIC syntax", + "description": "Validate BASIC program syntax without executing the code", + "operationId": "ValidateBasicCode", + "x-ms-summary": "Validate BASIC Syntax", + "x-ms-visibility": "advanced", + "parameters": [ + { + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/BasicValidationRequest" + } + } + ], + "responses": { + "200": { + "description": "Validation completed", + "schema": { + "$ref": "#/definitions/BasicValidationResponse" + } + } + } + } + }, + "/functions": { + "get": { + "summary": "Get supported BASIC functions", + "description": "Retrieve a list of all supported BASIC functions and their syntax", + "operationId": "GetBasicFunctions", + "x-ms-summary": "Get Supported Functions", + "x-ms-visibility": "advanced", + "responses": { + "200": { + "description": "List of supported functions", + "schema": { + "$ref": "#/definitions/FunctionsResponse" + } + } + } + } + } + }, + "definitions": { + "BasicExecutionRequest": { + "type": "object", + "required": [ + "code" + ], + "properties": { + "code": { + "type": "string", + "description": "The BASIC program code to execute", + "x-ms-summary": "BASIC Code", + "x-ms-visibility": "important", + "x-ms-test-value": "10 PRINT \"HELLO WORLD\"\n20 FOR I = 1 TO 5\n30 PRINT I\n40 NEXT I\n50 END" + }, + "variables": { + "type": "object", + "description": "Initial variable values for the program", + "x-ms-summary": "Initial Variables", + "x-ms-visibility": "advanced", + "additionalProperties": { + "type": "string" + } + }, + "maxExecutionTime": { + "type": "integer", + "description": "Maximum execution time in seconds (default: 30)", + "x-ms-summary": "Max Execution Time", + "x-ms-visibility": "advanced", + "default": 30, + "minimum": 1, + "maximum": 120 + }, + "enableDebug": { + "type": "boolean", + "description": "Enable debug mode for detailed execution trace", + "x-ms-summary": "Enable Debug Mode", + "x-ms-visibility": "advanced", + "default": false + }, + "caseSensitive": { + "type": "boolean", + "description": "Enable case-sensitive variable names", + "x-ms-summary": "Case Sensitive Variables", + "x-ms-visibility": "advanced", + "default": false + }, + "enableGraphics": { + "type": "boolean", + "description": "Enable graphics commands (PLOT, HLIN, VLIN) and return graphics output", + "x-ms-summary": "Enable Graphics", + "x-ms-visibility": "advanced", + "default": false + }, + "enableFileIO": { + "type": "boolean", + "description": "Enable virtual file I/O operations (OPEN, CLOSE, PRINT#, INPUT#)", + "x-ms-summary": "Enable File I/O", + "x-ms-visibility": "advanced", + "default": false + }, + "enableMemory": { + "type": "boolean", + "description": "Enable virtual memory operations (PEEK, POKE, CALL)", + "x-ms-summary": "Enable Memory Operations", + "x-ms-visibility": "advanced", + "default": false + }, + "files": { + "type": "object", + "description": "Virtual files to load (filename: content)", + "x-ms-summary": "Input Files", + "x-ms-visibility": "advanced", + "additionalProperties": { + "type": "string" + } + } + } + }, + "BasicExecutionResponse": { + "type": "object", + "properties": { + "success": { + "type": "boolean", + "description": "Indicates if execution was successful", + "x-ms-summary": "Success" + }, + "output": { + "type": "array", + "description": "Program output lines", + "x-ms-summary": "Output Lines", + "items": { + "type": "string" + } + }, + "variables": { + "type": "object", + "description": "Final variable values after execution", + "x-ms-summary": "Final Variables", + "additionalProperties": { + "type": "string" + } + }, + "executionTime": { + "type": "number", + "description": "Execution time in seconds", + "x-ms-summary": "Execution Time" + }, + "linesExecuted": { + "type": "integer", + "description": "Number of program lines executed", + "x-ms-summary": "Lines Executed" + }, + "debugTrace": { + "type": "array", + "description": "Debug trace information (if debug mode enabled)", + "x-ms-summary": "Debug Trace", + "items": { + "$ref": "#/definitions/DebugTraceEntry" + } + }, + "graphics": { + "type": "array", + "description": "Graphics commands output (if graphics mode enabled)", + "x-ms-summary": "Graphics Output", + "items": { + "$ref": "#/definitions/GraphicsCommand" + } + }, + "files": { + "type": "object", + "description": "Virtual files modified during execution", + "x-ms-summary": "Output Files", + "additionalProperties": { + "type": "string" + } + }, + "memory": { + "$ref": "#/definitions/VirtualMemory" + }, + "error": { + "$ref": "#/definitions/BasicError" + } + } + }, + "BasicValidationRequest": { + "type": "object", + "required": [ + "code" + ], + "properties": { + "code": { + "type": "string", + "description": "The BASIC program code to validate", + "x-ms-summary": "BASIC Code" + } + } + }, + "BasicValidationResponse": { + "type": "object", + "properties": { + "isValid": { + "type": "boolean", + "description": "Indicates if the syntax is valid", + "x-ms-summary": "Is Valid" + }, + "errors": { + "type": "array", + "description": "Syntax errors found", + "x-ms-summary": "Syntax Errors", + "items": { + "$ref": "#/definitions/SyntaxError" + } + }, + "warnings": { + "type": "array", + "description": "Syntax warnings", + "x-ms-summary": "Warnings", + "items": { + "$ref": "#/definitions/SyntaxWarning" + } + } + } + }, + "FunctionsResponse": { + "type": "object", + "properties": { + "functions": { + "type": "array", + "description": "List of supported BASIC functions", + "x-ms-summary": "Supported Functions", + "items": { + "$ref": "#/definitions/BasicFunction" + } + } + } + }, + "BasicFunction": { + "type": "object", + "properties": { + "name": { + "type": "string", + "description": "Function name", + "x-ms-summary": "Function Name" + }, + "syntax": { + "type": "string", + "description": "Function syntax", + "x-ms-summary": "Syntax" + }, + "description": { + "type": "string", + "description": "Function description", + "x-ms-summary": "Description" + }, + "category": { + "type": "string", + "description": "Function category", + "x-ms-summary": "Category", + "enum": [ + "Math", + "String", + "Control", + "I/O", + "Array" + ] + } + } + }, + "DebugTraceEntry": { + "type": "object", + "properties": { + "lineNumber": { + "type": "integer", + "description": "Line number being executed", + "x-ms-summary": "Line Number" + }, + "instruction": { + "type": "string", + "description": "BASIC instruction", + "x-ms-summary": "Instruction" + }, + "timestamp": { + "type": "string", + "format": "date-time", + "description": "Execution timestamp", + "x-ms-summary": "Timestamp" + }, + "variables": { + "type": "object", + "description": "Variable state at this point", + "x-ms-summary": "Variable State" + } + } + }, + "BasicError": { + "type": "object", + "properties": { + "type": { + "type": "string", + "description": "Error type", + "x-ms-summary": "Error Type", + "enum": [ + "SyntaxError", + "RuntimeError", + "TimeoutError", + "MemoryError" + ] + }, + "message": { + "type": "string", + "description": "Error message", + "x-ms-summary": "Error Message" + }, + "lineNumber": { + "type": "integer", + "description": "Line number where error occurred", + "x-ms-summary": "Line Number" + }, + "position": { + "type": "integer", + "description": "Character position in line", + "x-ms-summary": "Position" + } + } + }, + "SyntaxError": { + "type": "object", + "properties": { + "lineNumber": { + "type": "integer", + "description": "Line number with syntax error", + "x-ms-summary": "Line Number" + }, + "position": { + "type": "integer", + "description": "Character position of error", + "x-ms-summary": "Position" + }, + "message": { + "type": "string", + "description": "Error description", + "x-ms-summary": "Error Message" + }, + "severity": { + "type": "string", + "description": "Error severity", + "x-ms-summary": "Severity", + "enum": [ + "Error", + "Warning" + ] + } + } + }, + "SyntaxWarning": { + "type": "object", + "properties": { + "lineNumber": { + "type": "integer", + "description": "Line number with warning", + "x-ms-summary": "Line Number" + }, + "message": { + "type": "string", + "description": "Warning description", + "x-ms-summary": "Warning Message" + } + } + }, + "GraphicsCommand": { + "type": "object", + "properties": { + "command": { + "type": "string", + "description": "Graphics command name (PLOT, HLIN, VLIN)", + "x-ms-summary": "Command" + }, + "parameters": { + "type": "object", + "description": "Command parameters (coordinates, etc.)", + "x-ms-summary": "Parameters", + "additionalProperties": { + "type": "number" + } + }, + "lineNumber": { + "type": "integer", + "description": "Source line number", + "x-ms-summary": "Line Number" + }, + "color": { + "type": "string", + "description": "Graphics color", + "x-ms-summary": "Color" + }, + "thickness": { + "type": "integer", + "description": "Line thickness", + "x-ms-summary": "Thickness" + }, + "style": { + "type": "string", + "description": "Line style", + "x-ms-summary": "Style" + } + } + }, + "VirtualMemory": { + "type": "object", + "properties": { + "memory": { + "type": "object", + "description": "Memory contents (address: value)", + "x-ms-summary": "Memory State", + "additionalProperties": { + "type": "integer" + } + }, + "operations": { + "type": "array", + "description": "Memory operations performed", + "x-ms-summary": "Memory Operations", + "items": { + "$ref": "#/definitions/MemoryOperation" + } + } + } + }, + "MemoryOperation": { + "type": "object", + "properties": { + "operation": { + "type": "string", + "description": "Operation type (PEEK, POKE, CALL)", + "x-ms-summary": "Operation" + }, + "address": { + "type": "integer", + "description": "Memory address", + "x-ms-summary": "Address" + }, + "value": { + "type": "integer", + "description": "Value (for POKE operations)", + "x-ms-summary": "Value" + }, + "lineNumber": { + "type": "integer", + "description": "Source line number", + "x-ms-summary": "Line Number" + } + } + }, + "ErrorResponse": { + "type": "object", + "properties": { + "error": { + "type": "string", + "description": "Error message", + "x-ms-summary": "Error" + }, + "details": { + "type": "string", + "description": "Detailed error information", + "x-ms-summary": "Details" + }, + "timestamp": { + "type": "string", + "format": "date-time", + "description": "Error timestamp", + "x-ms-summary": "Timestamp" + } + } + } + }, + "x-ms-capabilities": { + "testConnection": { + "operationId": "GetBasicFunctions" + } + }, + "x-ms-connector-metadata": [ + { + "propertyName": "Website", + "propertyValue": "https://github.com/microsoft/BASIC-M6502" + }, + { + "propertyName": "Privacy policy", + "propertyValue": "https://github.com/microsoft/BASIC-M6502/blob/main/PRIVACY.md" + }, + { + "propertyName": "Categories", + "propertyValue": "IT Operations" + } + ] +} \ No newline at end of file diff --git a/independent-publisher-connectors/Microsoft BASIC Interpreter/apiProperties.json b/independent-publisher-connectors/Microsoft BASIC Interpreter/apiProperties.json new file mode 100644 index 0000000000..21e8d7fd45 --- /dev/null +++ b/independent-publisher-connectors/Microsoft BASIC Interpreter/apiProperties.json @@ -0,0 +1,15 @@ +{ + "properties": { + "connectionParameters": {}, + "iconBrandColor": "#da3b01", + "capabilities": ["actions"], + "scriptOperations": [ + "ExecuteBasicCode", + "ValidateBasicCode", + "GetBasicFunctions" + ], + "policyTemplateInstances": [], + "publisher": "Troy Taylor", + "stackOwner": "Microsoft" + } +} \ No newline at end of file diff --git a/independent-publisher-connectors/Microsoft BASIC Interpreter/readme.md b/independent-publisher-connectors/Microsoft BASIC Interpreter/readme.md new file mode 100644 index 0000000000..7397f0422f --- /dev/null +++ b/independent-publisher-connectors/Microsoft BASIC Interpreter/readme.md @@ -0,0 +1,534 @@ +# Microsoft BASIC Interpreter + +The Microsoft BASIC M6502 Interpreter connector provides a comprehensive implementation of the classic Microsoft BASIC programming language for the Power Platform. Execute BASIC programs with advanced features including error handling, performance monitoring, graphics output, file I/O operations, and memory simulation. + +## Publisher: Troy Taylor + +## Prerequisites + +There are no prerequisites needed for this service. + +## Obtaining Credentials + +No credentials are required. This is a code-only connector that runs the BASIC interpreter entirely within the connector's sandboxed environment without connecting to any external services. + +## Supported Operations + +### Execute BASIC Code +Executes a BASIC program and returns comprehensive results including output, variables, execution statistics, graphics commands, file operations, and memory operations. + +### Validate BASIC Syntax +Validates BASIC program syntax without executing the code, returning detailed error information if syntax issues are found. + +### Get Supported Functions +Returns a complete list of all supported BASIC functions and their syntax, organized by category. + +## Complete BASIC Language Support + +### Core Language Features +- Variables and assignment (numeric and string) +- Arrays (single and multi-dimensional with DIM) +- Control structures (FOR/NEXT, IF/THEN, WHILE/WEND) +- Subroutines (GOSUB/RETURN, ON...GOTO/GOSUB) +- User-defined functions (DEF FN) +- Data statements (DATA/READ/RESTORE) +- Input/Output (PRINT, INPUT with pre-supplied values) + +### Mathematical Functions (15) +- Basic: ABS, ATN, COS, EXP, INT, LOG, RND, SGN, SIN, SQR, TAN +- Enhanced: ROUND, FIX, CINT, CDBL + +### String Functions (15) +- Standard: ASC, CHR$, LEFT$, LEN, MID$, RIGHT$, STR$, VAL +- Advanced: INSTR, SPACE$, STRING$, UCASE$, LCASE$, LTRIM$, RTRIM$ + +### System Functions +- TAB, SPC, FRE, POS for formatting and system information + +### Graphics Commands (when enabled) +- PLOT, HLIN, VLIN, COLOR for creating visual output + +### File I/O Commands (when enabled) +- OPEN, CLOSE, PRINT#, INPUT# for virtual file operations + +### Memory Operations (when enabled) +- PEEK, POKE, CALL for virtual memory simulation + +### Program Management +- LIST, NEW, CLEAR, RUN, STOP, CONT for program control + +### Array Functions +- UBOUND, LBOUND for array boundary management + +## Example Usage + +### Simple BASIC Program +```json +{ + "code": "10 PRINT \"Hello World\"\n20 FOR I = 1 TO 5\n30 PRINT \"Count: \"; I\n40 NEXT I\n50 END" +} +``` + +### Financial Calculations +```json +{ + "code": "10 DEF FN COMPOUND(P,R,T) = P * (1 + R/100) ^ T\n20 PRINCIPAL = 10000\n30 RATE = 5.5\n40 YEARS = 10\n50 RESULT = FN COMPOUND(PRINCIPAL, RATE, YEARS)\n60 PRINT \"Investment grows to: $\"; ROUND(RESULT, 2)", + "inputValues": [] +} +``` + +### String Processing +```json +{ + "code": "10 INPUT NAME$\n20 FORMATTED$ = UCASE$(LEFT$(NAME$, 1)) + LCASE$(MID$(NAME$, 2))\n30 CLEANED$ = LTRIM$(RTRIM$(FORMATTED$))\n40 PRINT \"Formatted name: \"; CLEANED$", + "inputValues": [" john doe "], + "caseSensitive": false +} +``` + +### Graphics Output +```json +{ + "code": "10 COLOR = 1\n20 FOR I = 0 TO 10\n30 PLOT I*5, I*3\n40 NEXT I\n50 COLOR = 2\n60 HLIN 0, 50 AT 15\n70 VLIN 0, 30 AT 25", + "enableGraphics": true +} +``` + +### Data Analysis with Arrays +```json +{ + "code": "10 DIM SALES(12)\n20 TOTAL = 0\n30 FOR I = 1 TO 12\n40 READ SALES(I)\n50 TOTAL = TOTAL + SALES(I)\n60 NEXT I\n70 AVERAGE = TOTAL / 12\n80 PRINT \"Average monthly sales: $\"; ROUND(AVERAGE, 2)\n90 DATA 10500,12000,9500,15000,11000,13000,14000,16000,12000,10000,11000,12500" +} +``` + +## Request Parameters + +### Execute BASIC Code Parameters +- **code** (required): The BASIC program code to execute +- **caseSensitive** (optional): Enable case-sensitive variable names (default: false) +- **enableGraphics** (optional): Enable graphics commands (default: false) +- **enableFileIO** (optional): Enable virtual file I/O (default: false) +- **enableMemory** (optional): Enable virtual memory operations (default: false) +- **basicVersion** (optional): BASIC dialect version (default: "microsoft6502") +- **inputValues** (optional): Array of pre-supplied input values for INPUT statements +- **files** (optional): JSON object with virtual files (filename: content) +- **variables** (optional): JSON object with initial variable values +- **maxExecutionTimeMs** (optional): Maximum execution time in milliseconds +- **resourceLimits** (optional): Object with resource constraints + +### Resource Limits +- **maxVariables**: Maximum number of variables (default: 1000) +- **maxArraySize**: Maximum array elements (default: 10000) +- **maxStringLength**: Maximum string length (default: 32000) +- **maxNestingLevel**: Maximum loop/function nesting (default: 50) +- **maxExecutionTimeMs**: Maximum execution time (default: 120000) +- **maxMemoryBytes**: Maximum memory usage (default: 10485760) + +## Response Format + +### Execute Response +- **success**: Boolean indicating execution success +- **output**: Program output text +- **variables**: Final variable values as JSON object +- **graphics**: Array of graphics commands (if enabled) +- **files**: Modified virtual files (if file I/O enabled) +- **memory**: Virtual memory operations (if memory enabled) +- **executionStats**: Performance metrics object +- **errors**: Array of error objects with line numbers and context +- **linesExecuted**: Number of program lines executed +- **memoryUsedBytes**: Memory usage in bytes +- **functionsCalled**: Number of function calls made + +### Execution Statistics +- **executionTimeMs**: Total execution time +- **linesExecuted**: Program lines processed +- **memoryUsedBytes**: Memory consumption +- **variablesCreated**: Number of variables created +- **functionsCalled**: Function invocations +- **errorsEncountered**: Error count +- **arraysCreated**: Arrays created +- **maxNestingLevel**: Maximum nesting depth reached + +## Error Handling + +The connector provides comprehensive error handling with: +- **Line-specific error reporting** with context and timestamps +- **Error categorization**: Syntax, Runtime, Resource, Array, Validation errors +- **Warning system** for non-fatal issues +- **Comprehensive error logging** with detailed information + +## Performance Features + +- **Execution statistics**: Time, memory usage, lines executed +- **Resource monitoring**: Variable count, array usage, nesting levels +- **Performance metrics**: Function calls, execution time, memory allocation +- **Resource limits**: Configurable constraints to prevent resource exhaustion + +## Use Cases + +✅ **Excellent for:** +- Mathematical calculations and algorithms +- Data processing and transformation +- Business logic implementation +- Educational BASIC programming +- Report generation and formatting +- Financial modeling and analysis +- Legacy system integration +- Algorithmic problem solving + +❌ **Not suitable for:** +- Interactive programs requiring user input during execution +- Programs requiring persistent file storage +- Real-time graphics or game programming +- System administration tasks +- Long-running computations (>2 minutes) + +## Technical Architecture + +- **Language**: C# (.NET Standard 2.0) +- **Platform**: Power Platform Custom Connectors +- **Authentication**: None (code-only connector) +- **Execution Environment**: Sandboxed with resource monitoring +- **Memory Management**: Virtual memory simulation with limits +- **File System**: Virtual file system with temporary storage +- **Graphics**: Command-based output for external interpretation +- **Error Handling**: Comprehensive with line-level tracking +- String variables (ending with $) +- Numeric variables + +### Input/Output +- `INPUT` - Interactive input (simulated in connector) +- `READ/DATA` - Read from data statements +- `RESTORE` - Reset data pointer + +### User-Defined Functions +- `DEF FN` - Define custom functions + +### Mathematical Functions +- `ABS(x)` - Absolute value +- `ATN(x)` - Arctangent +- `COS(x)` - Cosine +- `EXP(x)` - Exponential (e^x) +- `INT(x)` - Integer part +- `LOG(x)` - Natural logarithm +- `RND[(x)]` - Random number generation +- `SGN(x)` - Sign function (-1, 0, or 1) +- `SIN(x)` - Sine +- `SQR(x)` - Square root +- `TAN(x)` - Tangent + +### String Functions +- `ASC(string)` - ASCII code of first character +- `CHR$(code)` - Character from ASCII code +- `LEFT$(string, length)` - Left substring +- `LEN(string)` - Length of string +- `MID$(string, start[, length])` - Middle substring +- `RIGHT$(string, length)` - Right substring +- `STR$(number)` - Convert number to string +- `VAL(string)` - Convert string to number +- String concatenation with `+` operator + +### Operators +- Arithmetic: `+`, `-`, `*`, `/` +- Comparison: `=`, `<>`, `<`, `>`, `<=`, `>=` +- Parentheses for expression grouping + +### Graphics Commands (when enableGraphics = true) +- `PLOT x, y` - Plot a point at coordinates +- `HLIN x1, x2 AT y` - Draw horizontal line +- `VLIN y1, y2 AT x` - Draw vertical line +- `COLOR = value` - Set graphics color (0-7) + +### File I/O Commands (when enableFileIO = true) +- `OPEN "filename", #channel[, mode]` - Open virtual file +- `CLOSE #channel` - Close file channel +- `PRINT# channel, data` - Write to file +- `INPUT# channel, variable` - Read from file + +### Memory Operations (when enableMemory = true) +- `PEEK(address)` - Read from virtual memory +- `POKE address, value` - Write to virtual memory +- `CALL address` - Simulate machine language call + +### Program Management Commands +- `LIST [start[-end]]` - List program lines +- `NEW` - Clear current program +- `CLEAR` - Clear variables and reset +- `RUN [line]` - Run program from specified line +- `STOP` - Stop program execution +- `CONT` - Continue execution after STOP + +### Formatting Functions +- `TAB(n)` - Tab to column position +- `SPC(n)` - Print n spaces +- `FRE(x)` - Free memory available +- `POS(x)` - Current print position + +## Operations + +### Execute BASIC Code +Executes a BASIC program and returns results. + +**Input Parameters:** +- `code` (required): The BASIC program code +- `variables` (optional): Initial variable values as JSON object +- `maxExecutionTime` (optional): Maximum execution time in seconds (default: 30, max: 120) +- `enableDebug` (optional): Enable debug tracing (default: false) +- `caseSensitive` (optional): Enable case-sensitive variable names (default: false) +- `enableGraphics` (optional): Enable graphics commands and return graphics output (default: false) +- `enableFileIO` (optional): Enable virtual file I/O operations (default: false) +- `enableMemory` (optional): Enable virtual memory operations (default: false) +- `files` (optional): Virtual files to load as JSON object (filename: content) + +**Response:** +- `success`: Boolean indicating if execution was successful +- `output`: Array of program output lines +- `variables`: Final variable values after execution +- `executionTime`: Execution time in seconds +- `linesExecuted`: Number of program lines executed +- `debugTrace`: Debug information (if debug mode enabled) +- `graphics`: Graphics commands output (if graphics mode enabled) +- `files`: Virtual files modified during execution (if file I/O enabled) +- `memory`: Virtual memory state and operations (if memory mode enabled) +- `error`: Error details (if execution failed) + +### Validate BASIC Syntax +Validates BASIC program syntax without executing the code. + +**Input Parameters:** +- `code` (required): The BASIC program code to validate + +**Response:** +- `isValid`: Boolean indicating if syntax is valid +- `errors`: Array of syntax errors found +- `warnings`: Array of syntax warnings + +### Get Supported Functions +Returns a list of all supported BASIC functions and their syntax. + +**Response:** +- `functions`: Array of function objects with name, syntax, description, and category + +## Example Usage + +### Simple BASIC Program +```basic +10 PRINT "HELLO WORLD" +20 FOR I = 1 TO 5 +30 PRINT "COUNT: "; I +40 NEXT I +50 END +``` + +### Advanced Examples + +### Arrays and Data Processing +```basic +10 DIM A(10), B(5,5) +20 FOR I = 0 TO 10 +30 A(I) = I * 2 +40 NEXT I +50 FOR I = 0 TO 5 +60 FOR J = 0 TO 5 +70 B(I,J) = I + J +80 NEXT J +90 NEXT I +100 PRINT "A(5) ="; A(5) +110 PRINT "B(2,3) ="; B(2,3) +120 END +``` + +### String Processing +```basic +10 A$ = "HELLO" +20 B$ = "WORLD" +30 C$ = A$ + " " + B$ +40 PRINT C$ +50 PRINT "Length:"; LEN(C$) +60 PRINT "Left 5:"; LEFT$(C$, 5) +70 PRINT "Right 5:"; RIGHT$(C$, 5) +80 PRINT "Middle:"; MID$(C$, 7, 5) +90 END +``` + +### Mathematical Functions +```basic +10 FOR X = 0 TO 3.14159 STEP 0.5 +20 PRINT "SIN("; X; ") ="; SIN(X) +30 PRINT "COS("; X; ") ="; COS(X) +40 PRINT "TAN("; X; ") ="; TAN(X) +50 NEXT X +60 PRINT "SQRT(16) ="; SQR(16) +70 PRINT "LOG(10) ="; LOG(10) +80 PRINT "EXP(1) ="; EXP(1) +90 END +``` + +### User-Defined Functions +```basic +10 DEF FNS(X) = X * X +20 DEF FNC(X) = X * X * X +30 FOR I = 1 TO 5 +40 PRINT "Square of"; I; "is"; FNS(I) +50 PRINT "Cube of"; I; "is"; FNC(I) +60 NEXT I +70 END +``` + +### Data Processing +```basic +10 DATA 10, 20, 30, "HELLO", "WORLD" +20 DATA 40, 50, 60 +30 FOR I = 1 TO 8 +40 READ X$ +50 PRINT "Item"; I; "="; X$ +60 NEXT I +70 RESTORE +80 READ A, B, C +90 PRINT "Sum ="; A + B + C +100 END +``` + +### Computed Branching +```basic +10 INPUT "Enter choice (1-3)"; N +20 ON N GOTO 100, 200, 300 +30 PRINT "Invalid choice" +40 GOTO 10 +100 PRINT "You chose option 1" +110 GOTO 400 +200 PRINT "You chose option 2" +210 GOTO 400 +300 PRINT "You chose option 3" +400 END +``` + +### Graphics Commands (when enableGraphics = true) +```basic +10 REM Draw a colorful house +20 COLOR = 1: REM White +30 PLOT 50, 50 +40 COLOR = 2: REM Red +50 HLIN 10, 90 AT 80 +60 COLOR = 3: REM Green +70 VLIN 50, 80 AT 10 +80 VLIN 50, 80 AT 90 +90 COLOR = 4: REM Blue +100 HLIN 10, 90 AT 50 +110 PRINT "House drawn with graphics commands" +120 END +``` + +### File I/O Operations (when enableFileIO = true) +```basic +10 REM File operations example +20 OPEN "DATA.TXT", #1, "W" +30 FOR I = 1 TO 5 +40 PRINT# 1, "Line " + STR$(I) +50 NEXT I +60 CLOSE #1 +70 OPEN "DATA.TXT", #1, "R" +80 FOR I = 1 TO 5 +90 INPUT# 1, A$ +100 PRINT A$ +110 NEXT I +120 CLOSE #1 +130 END +``` + +### Memory Operations (when enableMemory = true) +```basic +10 REM Memory operations example +20 FOR I = 0 TO 10 +30 POKE 1000 + I, I * 2 +40 NEXT I +50 FOR I = 0 TO 10 +60 A = PEEK(1000 + I) +70 PRINT "Memory["; 1000 + I; "] = "; A +80 NEXT I +90 CALL 2000 +100 END +``` + +### Program Management Examples +```basic +10 PRINT "This is a sample program" +20 PRINT "Line 20" +30 STOP +40 PRINT "This line after STOP" +50 END + +REM To list the program: +LIST + +REM To list specific lines: +LIST 10-30 + +REM To run from line 40: +RUN 40 + +REM To continue after STOP: +CONT +``` + +### Formatting Examples +```basic +10 PRINT "Name"; TAB(15); "Score"; TAB(25); "Grade" +20 PRINT "John"; TAB(15); "95"; TAB(25); "A" +30 PRINT "Start"; SPC(10); "End" +40 PRINT "Free Memory:"; FRE(0) +50 PRINT "Print Position:"; POS(0) +60 END +``` + +## Error Handling + +The connector provides comprehensive error handling: +- **Syntax Errors**: Invalid BASIC syntax with line numbers +- **Runtime Errors**: Execution errors like division by zero +- **Timeout Errors**: Program execution exceeded time limit +- **Memory Errors**: Memory allocation issues + +## Limitations + +- Execution time limited to 120 seconds maximum +- Script file size limited to 1MB +- File I/O operations work with virtual files in memory (not persistent storage) +- Graphics commands return structured data rather than visual display +- Memory operations simulate virtual memory space (not actual system memory) + +## New Features + +### Configurable Case Sensitivity +- Set `caseSensitive` parameter to `true` for case-sensitive variable names +- Default behavior maintains traditional BASIC case-insensitive variables + +### Enhanced Graphics Output Support +- Enable `enableGraphics` parameter to use PLOT, HLIN, VLIN, and COLOR commands +- Graphics commands include color, thickness, and style information +- Calling applications can render graphics based on the enhanced command data + +### Virtual File I/O Operations +- Enable `enableFileIO` parameter to use OPEN, CLOSE, PRINT#, and INPUT# commands +- Files are stored in virtual memory during execution +- Input files can be provided in the request, output files returned in the response +- Supports simulated file channels and basic read/write operations + +### Virtual Memory Operations +- Enable `enableMemory` parameter to use PEEK, POKE, and CALL commands +- Simulates a virtual memory space for low-level operations +- All memory operations are tracked and returned in the response +- Safe sandbox environment prevents actual system memory access + +## License + +This connector is inspired by the Microsoft BASIC-M6502 project which is licensed under the MIT License. The connector implementation respects all original license terms. + +## Known Issues and Limitations + +- Execution time limited to 120 seconds maximum (Power Platform constraint) +- Script file size limited to 1MB +- File I/O operations work with virtual files in memory (not persistent storage) +- Graphics commands return structured data rather than visual display +- Memory operations simulate virtual memory space (not actual system memory) +- Input must be pre-supplied (no interactive prompts during flow execution) \ No newline at end of file diff --git a/independent-publisher-connectors/Microsoft BASIC Interpreter/script.csx b/independent-publisher-connectors/Microsoft BASIC Interpreter/script.csx new file mode 100644 index 0000000000..7a988e7b3a --- /dev/null +++ b/independent-publisher-connectors/Microsoft BASIC Interpreter/script.csx @@ -0,0 +1,2369 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Globalization; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +// Enhanced error handling classes +public class ExecutionError +{ + public int LineNumber { get; set; } + public string ErrorType { get; set; } + public string Message { get; set; } + public string Context { get; set; } + public DateTime Timestamp { get; set; } +} + +public class ExecutionStatistics +{ + public long ExecutionTimeMs { get; set; } + public int LinesExecuted { get; set; } + public long MemoryUsedBytes { get; set; } + public int VariablesCreated { get; set; } + public int FunctionsCalled { get; set; } + public int ErrorsEncountered { get; set; } + public int ArraysCreated { get; set; } + public int MaxNestingLevel { get; set; } +} + +public class ResourceLimits +{ + public int MaxVariables { get; set; } = 1000; + public int MaxArraySize { get; set; } = 10000; + public int MaxStringLength { get; set; } = 32000; + public int MaxNestingLevel { get; set; } = 50; + public int MaxExecutionTimeMs { get; set; } = 120000; // 2 minutes + public long MaxMemoryBytes { get; set; } = 10485760; // 10MB +} + +public class Script : ScriptBase +{ + public override async Task ExecuteAsync() + { + try + { + this.Context.Logger.LogInformation($"BASIC Connector operation: {this.Context.OperationId}"); + + if (this.Context.OperationId == "ExecuteBasicCode") + { + return await HandleBasicExecution().ConfigureAwait(false); + } + else if (this.Context.OperationId == "ValidateBasicCode") + { + return await HandleBasicValidation().ConfigureAwait(false); + } + else if (this.Context.OperationId == "GetBasicFunctions") + { + return await HandleGetFunctions().ConfigureAwait(false); + } + + return CreateErrorResponse(HttpStatusCode.BadRequest, + $"Unknown operation ID '{this.Context.OperationId}'"); + } + catch (Exception ex) + { + this.Context.Logger.LogError(ex, + "Unexpected error in BASIC connector. CorrelationId: {CorrelationId}", + this.Context.CorrelationId); + + return CreateErrorResponse(HttpStatusCode.InternalServerError, + "An unexpected error occurred", ex.Message); + } + } + + private async Task HandleBasicExecution() + { + try + { + var requestContent = await this.Context.Request.Content.ReadAsStringAsync(); + var request = JObject.Parse(requestContent); + + var basicCode = request["code"]?.ToString(); + if (string.IsNullOrEmpty(basicCode)) + { + return CreateErrorResponse(HttpStatusCode.BadRequest, + "BASIC code is required"); + } + + var maxExecutionTime = request["maxExecutionTime"]?.Value() ?? 30; + var enableDebug = request["enableDebug"]?.Value() ?? false; + var caseSensitive = request["caseSensitive"]?.Value() ?? false; + var enableGraphics = request["enableGraphics"]?.Value() ?? false; + var enableFileIO = request["enableFileIO"]?.Value() ?? false; + var enableMemory = request["enableMemory"]?.Value() ?? false; + var files = request["files"]?.ToObject>() ?? new Dictionary(); + var initialVariables = request["variables"]?.ToObject>() + ?? new Dictionary(); + + // Execute BASIC code with timeout + using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(Math.Min(maxExecutionTime, 120)))) + { + var interpreter = new BasicInterpreter(this.Context.Logger, caseSensitive, enableGraphics, enableFileIO, enableMemory); + interpreter.LoadFiles(files); + var result = await interpreter.ExecuteAsync(basicCode, initialVariables, enableDebug, cts.Token); + + var response = new JObject + { + ["success"] = result.Success, + ["output"] = JArray.FromObject(result.Output), + ["variables"] = JObject.FromObject(result.Variables), + ["executionTime"] = result.ExecutionTime.TotalSeconds, + ["linesExecuted"] = result.LinesExecuted + }; + + if (enableDebug && result.DebugTrace != null) + { + response["debugTrace"] = JArray.FromObject(result.DebugTrace); + } + + if (enableGraphics && result.GraphicsOutput != null && result.GraphicsOutput.Count > 0) + { + response["graphics"] = JArray.FromObject(result.GraphicsOutput); + } + + if (enableFileIO && result.Files != null && result.Files.Count > 0) + { + response["files"] = JObject.FromObject(result.Files); + } + + if (enableMemory && result.Memory != null) + { + response["memory"] = JObject.FromObject(result.Memory); + } + + if (!result.Success && result.Error != null) + { + response["error"] = JObject.FromObject(result.Error); + } + + return CreateJsonResponse(HttpStatusCode.OK, response.ToString()); + } + } + catch (OperationCanceledException) + { + return CreateErrorResponse(HttpStatusCode.RequestTimeout, + "BASIC program execution timed out"); + } + catch (BasicSyntaxException ex) + { + return CreateErrorResponse(HttpStatusCode.BadRequest, + "BASIC syntax error", ex.Message); + } + catch (BasicRuntimeException ex) + { + return CreateErrorResponse(HttpStatusCode.BadRequest, + "BASIC runtime error", ex.Message); + } + } + + private async Task HandleBasicValidation() + { + try + { + var requestContent = await this.Context.Request.Content.ReadAsStringAsync(); + var request = JObject.Parse(requestContent); + + var basicCode = request["code"]?.ToString(); + if (string.IsNullOrEmpty(basicCode)) + { + return CreateErrorResponse(HttpStatusCode.BadRequest, "BASIC code is required"); + } + + var interpreter = new BasicInterpreter(this.Context.Logger); + var validationResult = interpreter.ValidateSyntax(basicCode); + + var response = new JObject + { + ["isValid"] = validationResult.IsValid, + ["errors"] = JArray.FromObject(validationResult.Errors), + ["warnings"] = JArray.FromObject(validationResult.Warnings) + }; + + return CreateJsonResponse(HttpStatusCode.OK, response.ToString()); + } + catch (Exception ex) + { + this.Context.Logger.LogError(ex, "Error validating BASIC code"); + return CreateErrorResponse(HttpStatusCode.InternalServerError, "Validation error", ex.Message); + } + } + + private async Task HandleGetFunctions() + { + try + { + var functions = BasicInterpreter.GetSupportedFunctions(); + var response = new JObject + { + ["functions"] = JArray.FromObject(functions) + }; + + return CreateJsonResponse(HttpStatusCode.OK, response.ToString()); + } + catch (Exception ex) + { + this.Context.Logger.LogError(ex, "Error getting BASIC functions"); + return CreateErrorResponse(HttpStatusCode.InternalServerError, "Error retrieving functions", ex.Message); + } + } + + private HttpResponseMessage CreateErrorResponse(HttpStatusCode statusCode, string error, string details = null) + { + var errorObj = new JObject + { + ["error"] = error, + ["timestamp"] = DateTime.UtcNow.ToString("O") + }; + + if (!string.IsNullOrEmpty(details)) + { + errorObj["details"] = details; + } + + var response = new HttpResponseMessage(statusCode); + response.Content = CreateJsonContent(errorObj.ToString()); + return response; + } + + private HttpResponseMessage CreateJsonResponse(HttpStatusCode statusCode, string content) + { + var response = new HttpResponseMessage(statusCode); + response.Content = CreateJsonContent(content); + return response; + } +} + +// Exception classes +public class BasicSyntaxException : Exception +{ + public int LineNumber { get; } + public int Position { get; } + + public BasicSyntaxException(string message, int lineNumber = 0, int position = 0) : base(message) + { + LineNumber = lineNumber; + Position = position; + } +} + +public class BasicRuntimeException : Exception +{ + public int LineNumber { get; } + + public BasicRuntimeException(string message, int lineNumber = 0) : base(message) + { + LineNumber = lineNumber; + } +} + +// Data structures +public class BasicExecutionResult +{ + public bool Success { get; set; } + public List Output { get; set; } = new List(); + public Dictionary Variables { get; set; } = new Dictionary(); + public TimeSpan ExecutionTime { get; set; } + public int LinesExecuted { get; set; } + public List DebugTrace { get; set; } + public List GraphicsOutput { get; set; } = new List(); + public Dictionary Files { get; set; } = new Dictionary(); + public VirtualMemory Memory { get; set; } + public BasicError Error { get; set; } +} + +public class BasicValidationResult +{ + public bool IsValid { get; set; } + public List Errors { get; set; } = new List(); + public List Warnings { get; set; } = new List(); +} + +public class DebugTraceEntry +{ + public int LineNumber { get; set; } + public string Instruction { get; set; } + public DateTime Timestamp { get; set; } + public Dictionary Variables { get; set; } +} + +public class BasicError +{ + public string Type { get; set; } + public string Message { get; set; } + public int LineNumber { get; set; } + public int Position { get; set; } +} + +public class SyntaxError +{ + public int LineNumber { get; set; } + public int Position { get; set; } + public string Message { get; set; } + public string Severity { get; set; } +} + +public class SyntaxWarning +{ + public int LineNumber { get; set; } + public string Message { get; set; } +} + +public class GraphicsCommand +{ + public string Command { get; set; } + public Dictionary Parameters { get; set; } = new Dictionary(); + public int LineNumber { get; set; } + public string Color { get; set; } = "white"; + public int Thickness { get; set; } = 1; + public string Style { get; set; } = "solid"; +} + +public class VirtualMemory +{ + public Dictionary Memory { get; set; } = new Dictionary(); + public List Operations { get; set; } = new List(); +} + +public class MemoryOperation +{ + public string Operation { get; set; } + public int Address { get; set; } + public byte? Value { get; set; } + public int LineNumber { get; set; } +} + +public class FileOperation +{ + public string Operation { get; set; } + public string FileName { get; set; } + public string Mode { get; set; } + public string Data { get; set; } + public int LineNumber { get; set; } +} + +public class BasicFunction +{ + public string Name { get; set; } + public string Syntax { get; set; } + public string Description { get; set; } + public string Category { get; set; } +} + +// BASIC Interpreter Implementation +public class BasicInterpreter +{ + private readonly ILogger _logger; + private readonly bool _caseSensitive; + private readonly bool _enableGraphics; + private readonly bool _enableFileIO; + private readonly bool _enableMemory; + private Dictionary _variables; + private Dictionary _arrays; + private Dictionary _userFunctions; + private Dictionary _program; + private List _output; + private List _debugTrace; + private List _graphicsOutput; + private Dictionary _virtualFiles; + private Dictionary _openFiles; + private VirtualMemory _virtualMemory; + private string _currentGraphicsColor; + private bool _debugMode; + private int _currentLine; + private Stack _forLoopStack; + private Stack _subroutineStack; + private Random _random; + private List _dataStatements; + private int _dataPointer; + + // Enhanced fields for improvements + private ResourceLimits _resourceLimits; + private List _errors; + private ExecutionStatistics _stats; + private Stopwatch _executionTimer; + private int _nestingLevel; + private string _basicVersion; + private Queue _inputQueue; + private int _functionCallCount; + private Dictionary _arrayBounds; + + public BasicInterpreter(ILogger logger = null, bool caseSensitive = false, bool enableGraphics = false, bool enableFileIO = false, bool enableMemory = false) + { + _logger = logger; + _caseSensitive = caseSensitive; + _enableGraphics = enableGraphics; + _enableFileIO = enableFileIO; + _enableMemory = enableMemory; + var comparer = caseSensitive ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase; + _variables = new Dictionary(comparer); + _arrays = new Dictionary(comparer); + _userFunctions = new Dictionary(comparer); + _program = new Dictionary(); + _output = new List(); + _debugTrace = new List(); + _graphicsOutput = new List(); + _virtualFiles = new Dictionary(); + _openFiles = new Dictionary(); + _virtualMemory = new VirtualMemory(); + _currentGraphicsColor = "white"; + _forLoopStack = new Stack(); + _subroutineStack = new Stack(); + _random = new Random(); + _dataStatements = new List(); + _dataPointer = 0; + + // Initialize enhanced fields + _resourceLimits = new ResourceLimits(); + _errors = new List(); + _stats = new ExecutionStatistics(); + _executionTimer = new Stopwatch(); + _nestingLevel = 0; + _basicVersion = "microsoft6502"; + _inputQueue = new Queue(); + _functionCallCount = 0; + _arrayBounds = new Dictionary(); + } + + // Enhanced properties + public bool CaseSensitive { get; set; } + public bool EnableGraphics { get; set; } + public bool EnableFileIO { get; set; } + public bool EnableMemory { get; set; } + public string BasicVersion { get; set; } + public ResourceLimits ResourceLimits => _resourceLimits; + public List Errors => _errors; + public ExecutionStatistics Statistics => _stats; + + public void LoadFiles(Dictionary files) + { + if (files != null) + { + foreach (var file in files) + { + _virtualFiles[file.Key] = file.Value; + } + } + } + + // Enhanced error handling methods + private void ThrowError(string message, int lineNumber = -1, string errorType = "Runtime Error") + { + var error = new ExecutionError + { + LineNumber = lineNumber == -1 ? _currentLine : lineNumber, + ErrorType = errorType, + Message = message, + Context = lineNumber != -1 && _program.ContainsKey(lineNumber) ? _program[lineNumber] : "", + Timestamp = DateTime.UtcNow + }; + + _errors.Add(error); + _stats.ErrorsEncountered++; + + var errorMessage = lineNumber == -1 ? message : $"Line {lineNumber}: {message}"; + throw new Exception(errorMessage); + } + + private void LogError(string message, int lineNumber = -1, string errorType = "Warning") + { + var error = new ExecutionError + { + LineNumber = lineNumber == -1 ? _currentLine : lineNumber, + ErrorType = errorType, + Message = message, + Context = lineNumber != -1 && _program.ContainsKey(lineNumber) ? _program[lineNumber] : "", + Timestamp = DateTime.UtcNow + }; + + _errors.Add(error); + _logger?.LogWarning($"{errorType} at line {error.LineNumber}: {message}"); + } + + // Input validation methods + private void ValidateInput(string code) + { + if (string.IsNullOrEmpty(code)) + ThrowError("Code cannot be empty", -1, "Validation Error"); + + if (code.Length > _resourceLimits.MaxStringLength) + ThrowError($"Code too long. Maximum {_resourceLimits.MaxStringLength} characters allowed", -1, "Validation Error"); + + // Check for potentially dangerous patterns + var dangerousPatterns = new[] { "while true", "goto 10", "for i=1 to 999999" }; + var lowerCode = code.ToLower(); + + foreach (var pattern in dangerousPatterns) + { + if (lowerCode.Contains(pattern)) + { + LogError($"Potentially infinite loop detected: {pattern}", -1, "Warning"); + } + } + } + + // Resource management methods + private void CheckResourceLimits() + { + if (_variables.Count > _resourceLimits.MaxVariables) + ThrowError($"Too many variables. Maximum {_resourceLimits.MaxVariables} allowed", _currentLine, "Resource Error"); + + if (_nestingLevel > _resourceLimits.MaxNestingLevel) + ThrowError($"Nesting too deep. Maximum {_resourceLimits.MaxNestingLevel} levels allowed", _currentLine, "Resource Error"); + + if (_executionTimer.ElapsedMilliseconds > _resourceLimits.MaxExecutionTimeMs) + ThrowError("Execution timeout exceeded", _currentLine, "Timeout Error"); + } + + // Enhanced input handling + public void SetInputValues(List inputValues) + { + _inputQueue.Clear(); + if (inputValues != null) + { + foreach (var value in inputValues) + { + _inputQueue.Enqueue(value); + } + } + } + + public void SetVirtualFiles(Dictionary files) + { + LoadFiles(files); + } + + // Program analysis and introspection methods + public List GetVariableList() + { + return _variables.Keys.ToList(); + } + + public List GetFunctionList() + { + return _userFunctions.Keys.ToList(); + } + + public Dictionary GetLineMap() + { + return new Dictionary(_program); + } + + public List ValidateSyntaxInternal(string code = null) + { + var errors = new List(); + var codeToValidate = code ?? string.Join("\n", _program.OrderBy(p => p.Key).Select(p => $"{p.Key} {p.Value}")); + + try + { + ValidateInput(codeToValidate); + } + catch (Exception ex) + { + errors.Add(ex.Message); + } + + return errors; + } + + public Dictionary GetExecutionSummary() + { + return new Dictionary + { + ["TotalVariables"] = _variables.Count, + ["TotalArrays"] = _arrays.Count, + ["TotalUserFunctions"] = _userFunctions.Count, + ["TotalProgramLines"] = _program.Count, + ["ExecutionErrors"] = _errors.Count, + ["MemoryUsage"] = GC.GetTotalMemory(false), + ["LastExecutionTime"] = _stats.ExecutionTimeMs, + ["MaxNestingLevel"] = _stats.MaxNestingLevel + }; + } + + // Enhanced array functions + public int GetArrayUpperBound(string arrayName, int dimension = 0) + { + arrayName = arrayName.ToUpper(); + if (_arrays.TryGetValue(arrayName, out var array)) + { + return array.GetUpperBound(dimension); + } + ThrowError($"Array {arrayName} not found", _currentLine, "Array Error"); + return -1; + } + + public int GetArrayLowerBound(string arrayName, int dimension = 0) + { + arrayName = arrayName.ToUpper(); + if (_arrays.TryGetValue(arrayName, out var array)) + { + return array.GetLowerBound(dimension); + } + ThrowError($"Array {arrayName} not found", _currentLine, "Array Error"); + return -1; + } + + public async Task ExecuteAsync(string code, Dictionary initialVariables, bool debugMode, CancellationToken cancellationToken) + { + var startTime = DateTime.UtcNow; + var result = new BasicExecutionResult { Success = true }; + + try + { + // Initialize enhanced tracking + _executionTimer.Restart(); + ValidateInput(code); + + _debugMode = debugMode; + _variables.Clear(); + _output.Clear(); + _debugTrace.Clear(); + _program.Clear(); + _forLoopStack.Clear(); + _subroutineStack.Clear(); + _errors.Clear(); + _nestingLevel = 0; + _functionCallCount = 0; + + // Initialize statistics + _stats.LinesExecuted = 0; + _stats.VariablesCreated = 0; + _stats.FunctionsCalled = 0; + _stats.ErrorsEncountered = 0; + _stats.ArraysCreated = 0; + _stats.MaxNestingLevel = 0; + + // Initialize variables + foreach (var kvp in initialVariables) + { + _variables[kvp.Key] = kvp.Value; + _stats.VariablesCreated++; + } + + // Parse program + ParseProgram(code); + + // Execute program + await ExecuteProgram(cancellationToken); + + _executionTimer.Stop(); + _stats.ExecutionTimeMs = _executionTimer.ElapsedMilliseconds; + _stats.MemoryUsedBytes = GC.GetTotalMemory(false); + + result.Output = _output; + result.Variables = _variables.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); + result.LinesExecuted = _stats.LinesExecuted; + result.DebugTrace = debugMode ? _debugTrace : null; + result.GraphicsOutput = _enableGraphics ? _graphicsOutput : null; + result.Files = _enableFileIO ? _virtualFiles : null; + result.Memory = _enableMemory ? _virtualMemory : null; + } + catch (BasicSyntaxException ex) + { + result.Success = false; + result.Error = new BasicError + { + Type = "SyntaxError", + Message = ex.Message, + LineNumber = ex.LineNumber, + Position = ex.Position + }; + } + catch (BasicRuntimeException ex) + { + result.Success = false; + result.Error = new BasicError + { + Type = "RuntimeError", + Message = ex.Message, + LineNumber = ex.LineNumber + }; + } + catch (OperationCanceledException) + { + result.Success = false; + result.Error = new BasicError + { + Type = "TimeoutError", + Message = "Program execution timed out" + }; + } + finally + { + result.ExecutionTime = DateTime.UtcNow - startTime; + result.Output = _output; + result.Variables = _variables.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); + result.GraphicsOutput = _enableGraphics ? _graphicsOutput : null; + result.Files = _enableFileIO ? _virtualFiles : null; + result.Memory = _enableMemory ? _virtualMemory : null; + } + + return result; + } + + public BasicValidationResult ValidateSyntax(string code) + { + var result = new BasicValidationResult { IsValid = true }; + + try + { + ParseProgram(code); + } + catch (BasicSyntaxException ex) + { + result.IsValid = false; + result.Errors.Add(new SyntaxError + { + LineNumber = ex.LineNumber, + Position = ex.Position, + Message = ex.Message, + Severity = "Error" + }); + } + + return result; + } + + private void ParseProgram(string code) + { + var lines = code.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); + _dataStatements.Clear(); // Clear existing data statements + + foreach (var line in lines) + { + var trimmedLine = line.Trim(); + if (string.IsNullOrEmpty(trimmedLine) || trimmedLine.StartsWith("REM")) + continue; + + // Parse line number and statement + var match = Regex.Match(trimmedLine, @"^(\d+)\s+(.+)$"); + if (match.Success) + { + var lineNumber = int.Parse(match.Groups[1].Value); + var statement = match.Groups[2].Value.Trim(); + _program[lineNumber] = statement; + + // Pre-process DATA statements + if (statement.ToUpper().StartsWith("DATA")) + { + var dataMatch = Regex.Match(statement, @"DATA\s+(.+)", RegexOptions.IgnoreCase); + if (dataMatch.Success) + { + var valuesStr = dataMatch.Groups[1].Value; + var values = new List(); + + var items = valuesStr.Split(','); + foreach (var item in items) + { + var trimmed = item.Trim(); + if (trimmed.StartsWith("\"") && trimmed.EndsWith("\"")) + { + values.Add(trimmed.Substring(1, trimmed.Length - 2)); + } + else if (double.TryParse(trimmed, out var number)) + { + values.Add(number); + } + else + { + values.Add(trimmed); + } + } + + _dataStatements.Add(new DataStatement + { + LineNumber = lineNumber, + Values = values + }); + } + } + } + else if (!Regex.IsMatch(trimmedLine, @"^\d+$")) + { + throw new BasicSyntaxException($"Invalid line format: {trimmedLine}"); + } + } + } + + private async Task ExecuteProgram(CancellationToken cancellationToken) + { + var lineNumbers = _program.Keys.OrderBy(x => x).ToList(); + + for (int i = 0; i < lineNumbers.Count; i++) + { + cancellationToken.ThrowIfCancellationRequested(); + CheckResourceLimits(); // Check resource limits on each iteration + + var lineNumber = lineNumbers[i]; + _currentLine = lineNumber; + var statement = _program[lineNumber]; + + _stats.LinesExecuted++; + + if (_debugMode) + { + _debugTrace.Add(new DebugTraceEntry + { + LineNumber = lineNumber, + Instruction = statement, + Timestamp = DateTime.UtcNow, + Variables = _variables.ToDictionary(kvp => kvp.Key, kvp => kvp.Value) + }); + } + + var result = await ExecuteStatement(statement, cancellationToken); + + // Handle control flow + if (result.Type == StatementResultType.Goto) + { + var targetIndex = lineNumbers.IndexOf(result.TargetLine); + if (targetIndex >= 0) + { + i = targetIndex - 1; // -1 because loop will increment + } + else + { + throw new BasicRuntimeException($"Line {result.TargetLine} not found", lineNumber); + } + } + else if (result.Type == StatementResultType.End) + { + break; + } + else if (result.Type == StatementResultType.Stop) + { + // STOP breaks execution but can be continued with CONT + break; + } + + // Simulate small delay for async operation + if (i % 10 == 0) + { + await Task.Delay(1, cancellationToken); + } + } + } + + private async Task ExecuteStatement(string statement, CancellationToken cancellationToken) + { + var upperStatement = statement.ToUpper().Trim(); + + if (upperStatement.StartsWith("PRINT")) + { + return ExecutePrint(statement); + } + else if (upperStatement.StartsWith("LET") || Regex.IsMatch(upperStatement, @"^[A-Z][A-Z0-9]*\s*=")) + { + return ExecuteLet(statement); + } + else if (upperStatement.StartsWith("DIM")) + { + return ExecuteDim(statement); + } + else if (upperStatement.StartsWith("INPUT")) + { + return ExecuteInput(statement); + } + else if (upperStatement.StartsWith("READ")) + { + return ExecuteRead(statement); + } + else if (upperStatement.StartsWith("DATA")) + { + return ExecuteData(statement); + } + else if (upperStatement.StartsWith("RESTORE")) + { + return ExecuteRestore(statement); + } + else if (upperStatement.StartsWith("DEF")) + { + return ExecuteDef(statement); + } + else if (upperStatement.StartsWith("ON")) + { + return ExecuteOn(statement); + } + else if (upperStatement.StartsWith("FOR")) + { + return ExecuteFor(statement); + } + else if (upperStatement.StartsWith("NEXT")) + { + return ExecuteNext(statement); + } + else if (upperStatement.StartsWith("IF")) + { + return await ExecuteIf(statement); + } + else if (upperStatement.StartsWith("GOTO")) + { + return ExecuteGoto(statement); + } + else if (upperStatement.StartsWith("GOSUB")) + { + return ExecuteGosub(statement); + } + else if (upperStatement.StartsWith("RETURN")) + { + return ExecuteReturn(); + } + else if (upperStatement.StartsWith("END")) + { + return new StatementResult { Type = StatementResultType.End }; + } + else if (upperStatement.StartsWith("REM")) + { + return new StatementResult { Type = StatementResultType.Continue }; + } + else if (_enableGraphics && upperStatement.StartsWith("PLOT")) + { + return ExecutePlot(statement); + } + else if (_enableGraphics && upperStatement.StartsWith("HLIN")) + { + return ExecuteHlin(statement); + } + else if (_enableGraphics && upperStatement.StartsWith("VLIN")) + { + return ExecuteVlin(statement); + } + else if (_enableGraphics && upperStatement.StartsWith("COLOR")) + { + return ExecuteColor(statement); + } + else if (_enableFileIO && upperStatement.StartsWith("OPEN")) + { + return ExecuteOpen(statement); + } + else if (_enableFileIO && upperStatement.StartsWith("CLOSE")) + { + return ExecuteClose(statement); + } + else if (_enableFileIO && upperStatement.StartsWith("PRINT#")) + { + return ExecutePrintFile(statement); + } + else if (_enableFileIO && upperStatement.StartsWith("INPUT#")) + { + return ExecuteInputFile(statement); + } + else if (_enableMemory && upperStatement.StartsWith("POKE")) + { + return ExecutePoke(statement); + } + else if (upperStatement.StartsWith("CALL")) + { + return ExecuteCall(statement); + } + else if (upperStatement.StartsWith("STOP")) + { + return ExecuteStop(statement); + } + else if (upperStatement.StartsWith("LIST")) + { + return ExecuteList(statement); + } + else if (upperStatement.StartsWith("NEW")) + { + return ExecuteNew(statement); + } + else if (upperStatement.StartsWith("CLEAR")) + { + return ExecuteClear(statement); + } + else if (upperStatement.StartsWith("RUN")) + { + return ExecuteRun(statement); + } + else if (upperStatement.StartsWith("CONT")) + { + return ExecuteCont(statement); + } + else + { + throw new BasicRuntimeException($"Unknown statement: {statement}", _currentLine); + } + } + + private StatementResult ExecutePrint(string statement) + { + var printText = statement.Substring(5).Trim(); + + if (string.IsNullOrEmpty(printText)) + { + _output.Add(""); + return new StatementResult { Type = StatementResultType.Continue }; + } + + // Handle string literals and variables + var result = EvaluateExpression(printText); + _output.Add(result?.ToString() ?? ""); + + return new StatementResult { Type = StatementResultType.Continue }; + } + + private StatementResult ExecuteLet(string statement) + { + string varName, expression; + + if (statement.ToUpper().StartsWith("LET")) + { + var letPart = statement.Substring(3).Trim(); + var equalIndex = letPart.IndexOf('='); + if (equalIndex < 0) + throw new BasicRuntimeException("Invalid LET statement", _currentLine); + + varName = letPart.Substring(0, equalIndex).Trim(); + expression = letPart.Substring(equalIndex + 1).Trim(); + } + else + { + var equalIndex = statement.IndexOf('='); + if (equalIndex < 0) + throw new BasicRuntimeException("Invalid assignment statement", _currentLine); + + varName = statement.Substring(0, equalIndex).Trim(); + expression = statement.Substring(equalIndex + 1).Trim(); + } + + var value = EvaluateExpression(expression); + + // Handle array assignment + var arrayMatch = Regex.Match(varName, @"([A-Z][A-Z0-9]*)\s*\(\s*(.+?)\s*\)", RegexOptions.IgnoreCase); + if (arrayMatch.Success) + { + var arrayName = arrayMatch.Groups[1].Value.ToUpper(); + var indicesStr = arrayMatch.Groups[2].Value; + + if (!_arrays.TryGetValue(arrayName, out var array)) + { + // Auto-dimension array if not already defined + var indices = indicesStr.Split(',') + .Select(i => (int)EvaluateNumericExpression(i.Trim()) + 1) + .ToArray(); + + array = Array.CreateInstance(typeof(double), indices); + _arrays[arrayName] = array; + } + + var accessIndices = indicesStr.Split(',') + .Select(i => (int)EvaluateNumericExpression(i.Trim())) + .ToArray(); + + if (accessIndices.Length != array.Rank) + throw new BasicRuntimeException("Wrong number of array indices", _currentLine); + + for (int i = 0; i < accessIndices.Length; i++) + { + if (accessIndices[i] < 0 || accessIndices[i] >= array.GetLength(i)) + throw new BasicRuntimeException("Array index out of bounds", _currentLine); + } + + array.SetValue(Convert.ToDouble(value), accessIndices); + } + else + { + // Regular variable assignment + _variables[varName.ToUpper()] = value; + } + + return new StatementResult { Type = StatementResultType.Continue }; + } + + private StatementResult ExecuteFor(string statement) + { + var forMatch = Regex.Match(statement, @"FOR\s+([A-Z][A-Z0-9]*)\s*=\s*(.+?)\s+TO\s+(.+?)(?:\s+STEP\s+(.+))?$", RegexOptions.IgnoreCase); + + if (!forMatch.Success) + throw new BasicRuntimeException("Invalid FOR statement", _currentLine); + + var varName = forMatch.Groups[1].Value; + var startValue = EvaluateExpression(forMatch.Groups[2].Value); + var endValue = EvaluateExpression(forMatch.Groups[3].Value); + var stepValue = forMatch.Groups[4].Success ? EvaluateExpression(forMatch.Groups[4].Value) : 1.0; + + _variables[varName] = Convert.ToDouble(startValue); + + _forLoopStack.Push(new ForLoop + { + Variable = varName, + EndValue = Convert.ToDouble(endValue), + StepValue = Convert.ToDouble(stepValue), + StartLine = _currentLine + }); + + return new StatementResult { Type = StatementResultType.Continue }; + } + + private StatementResult ExecuteNext(string statement) + { + if (_forLoopStack.Count == 0) + throw new BasicRuntimeException("NEXT without FOR", _currentLine); + + var forLoop = _forLoopStack.Peek(); + var currentValue = Convert.ToDouble(_variables[forLoop.Variable]); + currentValue += forLoop.StepValue; + _variables[forLoop.Variable] = currentValue; + + bool continueLoop; + if (forLoop.StepValue > 0) + continueLoop = currentValue <= forLoop.EndValue; + else + continueLoop = currentValue >= forLoop.EndValue; + + if (continueLoop) + { + return new StatementResult { Type = StatementResultType.Goto, TargetLine = forLoop.StartLine }; + } + else + { + _forLoopStack.Pop(); + return new StatementResult { Type = StatementResultType.Continue }; + } + } + + private async Task ExecuteIf(string statement) + { + var ifMatch = Regex.Match(statement, @"IF\s+(.+?)\s+THEN\s+(.+)$", RegexOptions.IgnoreCase); + + if (!ifMatch.Success) + throw new BasicRuntimeException("Invalid IF statement", _currentLine); + + var condition = ifMatch.Groups[1].Value; + var thenPart = ifMatch.Groups[2].Value; + + var conditionResult = EvaluateCondition(condition); + + if (conditionResult) + { + // Check if THEN part is a line number (GOTO) or a statement + if (int.TryParse(thenPart.Trim(), out int lineNumber)) + { + return new StatementResult { Type = StatementResultType.Goto, TargetLine = lineNumber }; + } + else + { + // Execute the statement + return await ExecuteStatement(thenPart, CancellationToken.None); + } + } + + return new StatementResult { Type = StatementResultType.Continue }; + } + + private StatementResult ExecuteGoto(string statement) + { + var gotoMatch = Regex.Match(statement, @"GOTO\s+(\d+)", RegexOptions.IgnoreCase); + + if (!gotoMatch.Success) + throw new BasicRuntimeException("Invalid GOTO statement", _currentLine); + + var lineNumber = int.Parse(gotoMatch.Groups[1].Value); + return new StatementResult { Type = StatementResultType.Goto, TargetLine = lineNumber }; + } + + private StatementResult ExecuteGosub(string statement) + { + var gosubMatch = Regex.Match(statement, @"GOSUB\s+(\d+)", RegexOptions.IgnoreCase); + + if (!gosubMatch.Success) + throw new BasicRuntimeException("Invalid GOSUB statement", _currentLine); + + var lineNumber = int.Parse(gosubMatch.Groups[1].Value); + _subroutineStack.Push(_currentLine); + return new StatementResult { Type = StatementResultType.Goto, TargetLine = lineNumber }; + } + + private StatementResult ExecuteReturn() + { + if (_subroutineStack.Count == 0) + throw new BasicRuntimeException("RETURN without GOSUB", _currentLine); + + var returnLine = _subroutineStack.Pop(); + return new StatementResult { Type = StatementResultType.Goto, TargetLine = returnLine }; + } + + private StatementResult ExecuteDim(string statement) + { + var dimMatch = Regex.Match(statement, @"DIM\s+([A-Z][A-Z0-9]*)\s*\(\s*(.+)\s*\)", RegexOptions.IgnoreCase); + + if (!dimMatch.Success) + ThrowError("Invalid DIM statement", _currentLine, "Syntax Error"); + + var arrayName = dimMatch.Groups[1].Value.ToUpper(); + var dimensionsStr = dimMatch.Groups[2].Value; + + var dimensions = dimensionsStr.Split(',') + .Select(d => (int)EvaluateNumericExpression(d.Trim()) + 1) + .ToArray(); + + if (dimensions.Any(d => d <= 0)) + ThrowError("Invalid array dimensions", _currentLine, "Array Error"); + + // Check array size limits + var totalElements = dimensions.Aggregate(1, (a, b) => a * b); + if (totalElements > _resourceLimits.MaxArraySize) + ThrowError($"Array too large. Maximum {_resourceLimits.MaxArraySize} elements allowed", _currentLine, "Resource Error"); + + var array = Array.CreateInstance(typeof(double), dimensions); + _arrays[arrayName] = array; + _arrayBounds[arrayName] = totalElements; + _stats.ArraysCreated++; + + return new StatementResult { Type = StatementResultType.Continue }; + } + + private StatementResult ExecuteInput(string statement) + { + var inputMatch = Regex.Match(statement, @"INPUT\s+(.+)", RegexOptions.IgnoreCase); + + if (!inputMatch.Success) + ThrowError("Invalid INPUT statement", _currentLine, "Syntax Error"); + + var variables = inputMatch.Groups[1].Value.Split(',') + .Select(v => v.Trim()) + .ToArray(); + + // Use pre-supplied input values from queue + foreach (var variable in variables) + { + object value; + + if (_inputQueue.Count > 0) + { + var inputValue = _inputQueue.Dequeue(); + value = variable.EndsWith("$") ? inputValue : + double.TryParse(inputValue, out var numValue) ? numValue : 0.0; + } + else + { + // Default values if no input provided + value = variable.EndsWith("$") ? "" : 0.0; + LogError($"No input value provided for variable {variable}, using default", _currentLine, "Warning"); + } + + _variables[variable.ToUpper()] = value; + _stats.VariablesCreated++; + } + + return new StatementResult { Type = StatementResultType.Continue }; + } + + private StatementResult ExecuteRead(string statement) + { + var readMatch = Regex.Match(statement, @"READ\s+(.+)", RegexOptions.IgnoreCase); + + if (!readMatch.Success) + throw new BasicRuntimeException("Invalid READ statement", _currentLine); + + var variables = readMatch.Groups[1].Value.Split(',') + .Select(v => v.Trim()) + .ToArray(); + + foreach (var variable in variables) + { + if (_dataPointer >= _dataStatements.Sum(d => d.Values.Count)) + throw new BasicRuntimeException("Out of DATA", _currentLine); + + var value = GetNextDataValue(); + _variables[variable] = value; + } + + return new StatementResult { Type = StatementResultType.Continue }; + } + + private StatementResult ExecuteData(string statement) + { + var dataMatch = Regex.Match(statement, @"DATA\s+(.+)", RegexOptions.IgnoreCase); + + if (!dataMatch.Success) + return new StatementResult { Type = StatementResultType.Continue }; + + var valuesStr = dataMatch.Groups[1].Value; + var values = new List(); + + var items = valuesStr.Split(','); + foreach (var item in items) + { + var trimmed = item.Trim(); + if (trimmed.StartsWith("\"") && trimmed.EndsWith("\"")) + { + values.Add(trimmed.Substring(1, trimmed.Length - 2)); + } + else if (double.TryParse(trimmed, out var number)) + { + values.Add(number); + } + else + { + values.Add(trimmed); + } + } + + _dataStatements.Add(new DataStatement + { + LineNumber = _currentLine, + Values = values + }); + + return new StatementResult { Type = StatementResultType.Continue }; + } + + private StatementResult ExecuteRestore(string statement) + { + _dataPointer = 0; + return new StatementResult { Type = StatementResultType.Continue }; + } + + private StatementResult ExecuteDef(string statement) + { + var defMatch = Regex.Match(statement, @"DEF\s+FN([A-Z][A-Z0-9]*)\s*\(\s*([A-Z][A-Z0-9]*)\s*\)\s*=\s*(.+)", RegexOptions.IgnoreCase); + + if (!defMatch.Success) + throw new BasicRuntimeException("Invalid DEF statement", _currentLine); + + var functionName = "FN" + defMatch.Groups[1].Value.ToUpper(); + var parameter = defMatch.Groups[2].Value.ToUpper(); + var expression = defMatch.Groups[3].Value; + + _userFunctions[functionName] = new UserDefinedFunction + { + Name = functionName, + Parameter = parameter, + Expression = expression, + LineNumber = _currentLine + }; + + return new StatementResult { Type = StatementResultType.Continue }; + } + + private StatementResult ExecuteOn(string statement) + { + var onMatch = Regex.Match(statement, @"ON\s+(.+?)\s+(GOTO|GOSUB)\s+(.+)", RegexOptions.IgnoreCase); + + if (!onMatch.Success) + throw new BasicRuntimeException("Invalid ON statement", _currentLine); + + var expression = onMatch.Groups[1].Value; + var command = onMatch.Groups[2].Value.ToUpper(); + var targets = onMatch.Groups[3].Value.Split(',').Select(t => int.Parse(t.Trim())).ToArray(); + + var index = (int)EvaluateNumericExpression(expression) - 1; + + if (index >= 0 && index < targets.Length) + { + var targetLine = targets[index]; + + if (command == "GOSUB") + { + _subroutineStack.Push(_currentLine); + } + + return new StatementResult { Type = StatementResultType.Goto, TargetLine = targetLine }; + } + + return new StatementResult { Type = StatementResultType.Continue }; + } + + private StatementResult ExecutePlot(string statement) + { + // PLOT X, Y + var plotMatch = Regex.Match(statement, @"PLOT\s+(.+),\s*(.+)", RegexOptions.IgnoreCase); + + if (!plotMatch.Success) + throw new BasicRuntimeException("Invalid PLOT statement", _currentLine); + + var x = EvaluateNumericExpression(plotMatch.Groups[1].Value); + var y = EvaluateNumericExpression(plotMatch.Groups[2].Value); + + _graphicsOutput.Add(new GraphicsCommand + { + Command = "PLOT", + Parameters = new Dictionary + { + ["x"] = x, + ["y"] = y + }, + LineNumber = _currentLine + }); + + return new StatementResult { Type = StatementResultType.Continue }; + } + + private StatementResult ExecuteHlin(string statement) + { + // HLIN X1, X2 AT Y + var hlinMatch = Regex.Match(statement, @"HLIN\s+(.+),\s*(.+)\s+AT\s+(.+)", RegexOptions.IgnoreCase); + + if (!hlinMatch.Success) + throw new BasicRuntimeException("Invalid HLIN statement", _currentLine); + + var x1 = EvaluateNumericExpression(hlinMatch.Groups[1].Value); + var x2 = EvaluateNumericExpression(hlinMatch.Groups[2].Value); + var y = EvaluateNumericExpression(hlinMatch.Groups[3].Value); + + _graphicsOutput.Add(new GraphicsCommand + { + Command = "HLIN", + Parameters = new Dictionary + { + ["x1"] = x1, + ["x2"] = x2, + ["y"] = y + }, + LineNumber = _currentLine + }); + + return new StatementResult { Type = StatementResultType.Continue }; + } + + private StatementResult ExecuteVlin(string statement) + { + // VLIN Y1, Y2 AT X + var vlinMatch = Regex.Match(statement, @"VLIN\s+(.+),\s*(.+)\s+AT\s+(.+)", RegexOptions.IgnoreCase); + + if (!vlinMatch.Success) + throw new BasicRuntimeException("Invalid VLIN statement", _currentLine); + + var y1 = EvaluateNumericExpression(vlinMatch.Groups[1].Value); + var y2 = EvaluateNumericExpression(vlinMatch.Groups[2].Value); + var x = EvaluateNumericExpression(vlinMatch.Groups[3].Value); + + _graphicsOutput.Add(new GraphicsCommand + { + Command = "VLIN", + Parameters = new Dictionary + { + ["y1"] = y1, + ["y2"] = y2, + ["x"] = x + }, + LineNumber = _currentLine, + Color = _currentGraphicsColor, + Thickness = 1, + Style = "solid" + }); + + return new StatementResult { Type = StatementResultType.Continue }; + } + + private StatementResult ExecuteColor(string statement) + { + // COLOR = value (sets graphics color) + var colorMatch = Regex.Match(statement, @"COLOR\s*=\s*(.+)", RegexOptions.IgnoreCase); + + if (!colorMatch.Success) + throw new BasicRuntimeException("Invalid COLOR statement", _currentLine); + + var colorValue = (int)EvaluateNumericExpression(colorMatch.Groups[1].Value); + + // Convert numeric color to name (simplified) + _currentGraphicsColor = colorValue switch + { + 0 => "black", + 1 => "white", + 2 => "red", + 3 => "green", + 4 => "blue", + 5 => "yellow", + 6 => "cyan", + 7 => "magenta", + _ => "white" + }; + + return new StatementResult { Type = StatementResultType.Continue }; + } + + private StatementResult ExecuteOpen(string statement) + { + // OPEN "filename", #channel[, mode] + var openMatch = Regex.Match(statement, @"OPEN\s+""([^""]+)""\s*,\s*#(\d+)(?:\s*,\s*(\w+))?", RegexOptions.IgnoreCase); + + if (!openMatch.Success) + throw new BasicRuntimeException("Invalid OPEN statement", _currentLine); + + var filename = openMatch.Groups[1].Value; + var channel = int.Parse(openMatch.Groups[2].Value); + var mode = openMatch.Groups[3].Success ? openMatch.Groups[3].Value.ToUpper() : "R"; + + if (!_virtualFiles.ContainsKey(filename) && mode == "R") + { + _virtualFiles[filename] = ""; // Create empty file for reading + } + + // Simulate file opening (store channel mapping) + if (!_openFiles.ContainsKey(channel)) + { + var memoryStream = new MemoryStream(); + _openFiles[channel] = new StreamWriter(memoryStream); // Placeholder for file operations + } + + return new StatementResult { Type = StatementResultType.Continue }; + } + + private StatementResult ExecuteClose(string statement) + { + // CLOSE #channel + var closeMatch = Regex.Match(statement, @"CLOSE\s+#(\d+)", RegexOptions.IgnoreCase); + + if (!closeMatch.Success) + throw new BasicRuntimeException("Invalid CLOSE statement", _currentLine); + + var channel = int.Parse(closeMatch.Groups[1].Value); + + if (_openFiles.ContainsKey(channel)) + { + _openFiles[channel]?.Dispose(); + _openFiles.Remove(channel); + } + + return new StatementResult { Type = StatementResultType.Continue }; + } + + private StatementResult ExecutePrintFile(string statement) + { + // PRINT# channel, data + var printMatch = Regex.Match(statement, @"PRINT#\s*(\d+)\s*,\s*(.+)", RegexOptions.IgnoreCase); + + if (!printMatch.Success) + throw new BasicRuntimeException("Invalid PRINT# statement", _currentLine); + + var channel = int.Parse(printMatch.Groups[1].Value); + var data = EvaluateExpression(printMatch.Groups[2].Value)?.ToString() ?? ""; + + // Find the filename associated with this channel and append data + var filename = $"CHANNEL_{channel}.txt"; // Simplified channel-to-file mapping + if (_virtualFiles.ContainsKey(filename)) + { + _virtualFiles[filename] += data + "\n"; + } + else + { + _virtualFiles[filename] = data + "\n"; + } + + return new StatementResult { Type = StatementResultType.Continue }; + } + + private StatementResult ExecuteInputFile(string statement) + { + // INPUT# channel, variable + var inputMatch = Regex.Match(statement, @"INPUT#\s*(\d+)\s*,\s*([A-Z][A-Z0-9]*\$?)", RegexOptions.IgnoreCase); + + if (!inputMatch.Success) + throw new BasicRuntimeException("Invalid INPUT# statement", _currentLine); + + var channel = int.Parse(inputMatch.Groups[1].Value); + var variable = inputMatch.Groups[2].Value; + + // Read from virtual file + var filename = $"CHANNEL_{channel}.txt"; + if (_virtualFiles.ContainsKey(filename)) + { + var lines = _virtualFiles[filename].Split('\n'); + if (lines.Length > 0) + { + var value = lines[0]; + _variables[variable] = value; + + // Remove the read line from the file + _virtualFiles[filename] = string.Join("\n", lines.Skip(1)); + } + } + + return new StatementResult { Type = StatementResultType.Continue }; + } + + private StatementResult ExecutePoke(string statement) + { + // POKE address, value + var pokeMatch = Regex.Match(statement, @"POKE\s+(.+),\s*(.+)", RegexOptions.IgnoreCase); + + if (!pokeMatch.Success) + throw new BasicRuntimeException("Invalid POKE statement", _currentLine); + + var address = (int)EvaluateNumericExpression(pokeMatch.Groups[1].Value); + var value = (byte)EvaluateNumericExpression(pokeMatch.Groups[2].Value); + + _virtualMemory.Memory[address] = value; + _virtualMemory.Operations.Add(new MemoryOperation + { + Operation = "POKE", + Address = address, + Value = value, + LineNumber = _currentLine + }); + + return new StatementResult { Type = StatementResultType.Continue }; + } + + private StatementResult ExecuteCall(string statement) + { + // CALL address (simulate machine language call) + var callMatch = Regex.Match(statement, @"CALL\s+(.+)", RegexOptions.IgnoreCase); + + if (!callMatch.Success) + throw new BasicRuntimeException("Invalid CALL statement", _currentLine); + + var address = (int)EvaluateNumericExpression(callMatch.Groups[1].Value); + + if (_enableMemory) + { + _virtualMemory.Operations.Add(new MemoryOperation + { + Operation = "CALL", + Address = address, + LineNumber = _currentLine + }); + + // Simulate some effect based on address (simplified) + _output.Add($"CALL executed at address {address}"); + } + else + { + _output.Add($"CALL {address} (memory operations disabled)"); + } + + return new StatementResult { Type = StatementResultType.Continue }; + } + + private StatementResult ExecuteStop(string statement) + { + _output.Add("BREAK IN " + _currentLine); + return new StatementResult { Type = StatementResultType.Stop }; + } + + private StatementResult ExecuteList(string statement) + { + // LIST [start[-end]] + var listMatch = Regex.Match(statement, @"LIST\s*(\d+)?\s*(-\s*(\d+))?", RegexOptions.IgnoreCase); + + int startLine = 0; + int endLine = int.MaxValue; + + if (listMatch.Groups[1].Success) + { + startLine = int.Parse(listMatch.Groups[1].Value); + } + + if (listMatch.Groups[3].Success) + { + endLine = int.Parse(listMatch.Groups[3].Value); + } + else if (listMatch.Groups[1].Success) + { + endLine = startLine; // List single line + } + + foreach (var line in _program.Where(kvp => kvp.Key >= startLine && kvp.Key <= endLine).OrderBy(kvp => kvp.Key)) + { + _output.Add($"{line.Key} {line.Value}"); + } + + return new StatementResult { Type = StatementResultType.Continue }; + } + + private StatementResult ExecuteNew(string statement) + { + _program.Clear(); + _variables.Clear(); + _arrays.Clear(); + _userFunctions.Clear(); + _dataStatements.Clear(); + _dataPointer = 0; + _output.Add("NEW"); + + return new StatementResult { Type = StatementResultType.Continue }; + } + + private StatementResult ExecuteClear(string statement) + { + _variables.Clear(); + _arrays.Clear(); + _userFunctions.Clear(); + _dataStatements.Clear(); + _dataPointer = 0; + _forLoopStack.Clear(); + _subroutineStack.Clear(); + + return new StatementResult { Type = StatementResultType.Continue }; + } + + private StatementResult ExecuteRun(string statement) + { + // RUN [line] + var runMatch = Regex.Match(statement, @"RUN\s*(\d+)?", RegexOptions.IgnoreCase); + + int startLine = 0; + if (runMatch.Groups[1].Success) + { + startLine = int.Parse(runMatch.Groups[1].Value); + } + + // Clear variables but keep program + _variables.Clear(); + _arrays.Clear(); + _userFunctions.Clear(); + _dataStatements.Clear(); + _dataPointer = 0; + _forLoopStack.Clear(); + _subroutineStack.Clear(); + + // Find the starting line + var nextLine = _program.Keys.Where(k => k >= startLine).OrderBy(k => k).FirstOrDefault(); + if (nextLine == 0 && !_program.ContainsKey(startLine)) + { + throw new BasicRuntimeException($"Undefined line number: {startLine}", _currentLine); + } + + return new StatementResult { Type = StatementResultType.Goto, TargetLine = nextLine }; + } + + private StatementResult ExecuteCont(string statement) + { + // Continue execution from where STOP was encountered + // This is simplified - in a real implementation we'd need to track the stop position + _output.Add("CONT"); + return new StatementResult { Type = StatementResultType.Continue }; + } + + private object GetNextDataValue() + { + int currentIndex = 0; + foreach (var dataStatement in _dataStatements) + { + if (_dataPointer < currentIndex + dataStatement.Values.Count) + { + var value = dataStatement.Values[_dataPointer - currentIndex]; + _dataPointer++; + return value; + } + currentIndex += dataStatement.Values.Count; + } + + throw new BasicRuntimeException("Out of DATA", _currentLine); + } + + private object EvaluateExpression(string expression) + { + expression = expression.Trim(); + + // Handle string literals + if (expression.StartsWith("\"") && expression.EndsWith("\"")) + { + return expression.Substring(1, expression.Length - 2); + } + + // Handle numeric literals + if (double.TryParse(expression, out double numValue)) + { + return numValue; + } + + // Handle string concatenation + var concatMatch = Regex.Match(expression, @"(.+?)\s*\+\s*(.+)"); + if (concatMatch.Success) + { + var left = EvaluateExpression(concatMatch.Groups[1].Value); + var right = EvaluateExpression(concatMatch.Groups[2].Value); + + // If either operand is a string, concatenate + if (left is string || right is string) + { + return left.ToString() + right.ToString(); + } + // Otherwise, add numerically + return Convert.ToDouble(left) + Convert.ToDouble(right); + } + + // Handle mathematical functions + var mathFunctions = new Dictionary> + { + ["SQR"] = Math.Sqrt, + ["ABS"] = Math.Abs, + ["INT"] = Math.Floor, + ["SIN"] = Math.Sin, + ["COS"] = Math.Cos, + ["TAN"] = Math.Tan, + ["ATN"] = Math.Atan, + ["LOG"] = Math.Log, + ["EXP"] = Math.Exp + }; + + foreach (var func in mathFunctions) + { + var funcMatch = Regex.Match(expression, $@"{func.Key}\s*\(\s*(.+?)\s*\)", RegexOptions.IgnoreCase); + if (funcMatch.Success) + { + var arg = EvaluateNumericExpression(funcMatch.Groups[1].Value); + return func.Value(arg); + } + } + + // Handle SGN function + var sgnMatch = Regex.Match(expression, @"SGN\s*\(\s*(.+?)\s*\)", RegexOptions.IgnoreCase); + if (sgnMatch.Success) + { + var value = EvaluateNumericExpression(sgnMatch.Groups[1].Value); + return value > 0 ? 1.0 : value < 0 ? -1.0 : 0.0; + } + + // Handle RND function + var rndMatch = Regex.Match(expression, @"RND(\s*\(\s*(.+?)\s*\))?", RegexOptions.IgnoreCase); + if (rndMatch.Success) + { + if (rndMatch.Groups[2].Success) + { + var seed = (int)EvaluateNumericExpression(rndMatch.Groups[2].Value); + if (seed < 0) + { + _random = new Random(Math.Abs(seed)); + return _random.NextDouble(); + } + } + return _random.NextDouble(); + } + + // Handle PEEK function (memory operations) + if (_enableMemory) + { + var peekMatch = Regex.Match(expression, @"PEEK\s*\(\s*(.+?)\s*\)", RegexOptions.IgnoreCase); + if (peekMatch.Success) + { + var address = (int)EvaluateNumericExpression(peekMatch.Groups[1].Value); + + _virtualMemory.Operations.Add(new MemoryOperation + { + Operation = "PEEK", + Address = address, + LineNumber = _currentLine + }); + + return _virtualMemory.Memory.ContainsKey(address) ? (double)_virtualMemory.Memory[address] : 0.0; + } + } + + // Handle TAB function + var tabMatch = Regex.Match(expression, @"TAB\s*\(\s*(.+?)\s*\)", RegexOptions.IgnoreCase); + if (tabMatch.Success) + { + var position = (int)EvaluateNumericExpression(tabMatch.Groups[1].Value); + return new string(' ', Math.Max(0, position)); + } + + // Handle SPC function + var spcMatch = Regex.Match(expression, @"SPC\s*\(\s*(.+?)\s*\)", RegexOptions.IgnoreCase); + if (spcMatch.Success) + { + var spaces = (int)EvaluateNumericExpression(spcMatch.Groups[1].Value); + return new string(' ', Math.Max(0, spaces)); + } + + // Handle FRE function (free memory) + var freMatch = Regex.Match(expression, @"FRE\s*\(\s*(.+?)\s*\)", RegexOptions.IgnoreCase); + if (freMatch.Success) + { + // Simulate free memory (simplified) + return 32768.0; // Return a simulated memory value + } + + // Handle POS function (print position) + var posMatch = Regex.Match(expression, @"POS\s*\(\s*(.+?)\s*\)", RegexOptions.IgnoreCase); + if (posMatch.Success) + { + // Simulate print position (simplified) + return 0.0; // Return column 0 + } + + // Handle string functions + var leftMatch = Regex.Match(expression, @"LEFT\$\s*\(\s*(.+?)\s*,\s*(.+?)\s*\)", RegexOptions.IgnoreCase); + if (leftMatch.Success) + { + var str = EvaluateExpression(leftMatch.Groups[1].Value).ToString(); + var length = (int)EvaluateNumericExpression(leftMatch.Groups[2].Value); + return length >= str.Length ? str : str.Substring(0, Math.Max(0, length)); + } + + var rightMatch = Regex.Match(expression, @"RIGHT\$\s*\(\s*(.+?)\s*,\s*(.+?)\s*\)", RegexOptions.IgnoreCase); + if (rightMatch.Success) + { + var str = EvaluateExpression(rightMatch.Groups[1].Value).ToString(); + var length = (int)EvaluateNumericExpression(rightMatch.Groups[2].Value); + return length >= str.Length ? str : str.Substring(Math.Max(0, str.Length - length)); + } + + var midMatch = Regex.Match(expression, @"MID\$\s*\(\s*(.+?)\s*,\s*(.+?)\s*(,\s*(.+?))?\s*\)", RegexOptions.IgnoreCase); + if (midMatch.Success) + { + var str = EvaluateExpression(midMatch.Groups[1].Value).ToString(); + var start = (int)EvaluateNumericExpression(midMatch.Groups[2].Value) - 1; // BASIC uses 1-based indexing + var length = midMatch.Groups[4].Success ? (int)EvaluateNumericExpression(midMatch.Groups[4].Value) : str.Length; + + start = Math.Max(0, start); + if (start >= str.Length) return ""; + + length = Math.Min(length, str.Length - start); + return str.Substring(start, Math.Max(0, length)); + } + + var lenMatch = Regex.Match(expression, @"LEN\s*\(\s*(.+?)\s*\)", RegexOptions.IgnoreCase); + if (lenMatch.Success) + { + var str = EvaluateExpression(lenMatch.Groups[1].Value).ToString(); + return (double)str.Length; + } + + var ascMatch = Regex.Match(expression, @"ASC\s*\(\s*(.+?)\s*\)", RegexOptions.IgnoreCase); + if (ascMatch.Success) + { + var str = EvaluateExpression(ascMatch.Groups[1].Value).ToString(); + return str.Length > 0 ? (double)str[0] : 0.0; + } + + var chrMatch = Regex.Match(expression, @"CHR\$\s*\(\s*(.+?)\s*\)", RegexOptions.IgnoreCase); + if (chrMatch.Success) + { + var code = (int)EvaluateNumericExpression(chrMatch.Groups[1].Value); + return ((char)code).ToString(); + } + + var strMatch = Regex.Match(expression, @"STR\$\s*\(\s*(.+?)\s*\)", RegexOptions.IgnoreCase); + if (strMatch.Success) + { + var value = EvaluateNumericExpression(strMatch.Groups[1].Value); + return " " + value.ToString(); // STR$ includes leading space for positive numbers + } + + var valMatch = Regex.Match(expression, @"VAL\s*\(\s*(.+?)\s*\)", RegexOptions.IgnoreCase); + if (valMatch.Success) + { + var str = EvaluateExpression(valMatch.Groups[1].Value).ToString().Trim(); + return double.TryParse(str, out var result) ? result : 0.0; + } + + // Enhanced string functions + var instrMatch = Regex.Match(expression, @"INSTR\s*\(\s*(.+?)\s*,\s*(.+?)\s*\)", RegexOptions.IgnoreCase); + if (instrMatch.Success) + { + var str = EvaluateExpression(instrMatch.Groups[1].Value).ToString(); + var searchStr = EvaluateExpression(instrMatch.Groups[2].Value).ToString(); + var index = str.IndexOf(searchStr, StringComparison.OrdinalIgnoreCase); + return index == -1 ? 0.0 : (double)(index + 1); // BASIC uses 1-based indexing + } + + var spaceMatch = Regex.Match(expression, @"SPACE\$\s*\(\s*(.+?)\s*\)", RegexOptions.IgnoreCase); + if (spaceMatch.Success) + { + var count = (int)EvaluateNumericExpression(spaceMatch.Groups[1].Value); + return new string(' ', Math.Max(0, count)); + } + + var stringMatch = Regex.Match(expression, @"STRING\$\s*\(\s*(.+?)\s*,\s*(.+?)\s*\)", RegexOptions.IgnoreCase); + if (stringMatch.Success) + { + var count = (int)EvaluateNumericExpression(stringMatch.Groups[1].Value); + var charExpr = EvaluateExpression(stringMatch.Groups[2].Value); + var ch = charExpr is string s && s.Length > 0 ? s[0] : + charExpr is double d ? (char)(int)d : ' '; + return new string(ch, Math.Max(0, count)); + } + + var ucaseMatch = Regex.Match(expression, @"UCASE\$\s*\(\s*(.+?)\s*\)", RegexOptions.IgnoreCase); + if (ucaseMatch.Success) + { + var str = EvaluateExpression(ucaseMatch.Groups[1].Value).ToString(); + return str.ToUpper(); + } + + var lcaseMatch = Regex.Match(expression, @"LCASE\$\s*\(\s*(.+?)\s*\)", RegexOptions.IgnoreCase); + if (lcaseMatch.Success) + { + var str = EvaluateExpression(lcaseMatch.Groups[1].Value).ToString(); + return str.ToLower(); + } + + var ltrimMatch = Regex.Match(expression, @"LTRIM\$\s*\(\s*(.+?)\s*\)", RegexOptions.IgnoreCase); + if (ltrimMatch.Success) + { + var str = EvaluateExpression(ltrimMatch.Groups[1].Value).ToString(); + return str.TrimStart(); + } + + var rtrimMatch = Regex.Match(expression, @"RTRIM\$\s*\(\s*(.+?)\s*\)", RegexOptions.IgnoreCase); + if (rtrimMatch.Success) + { + var str = EvaluateExpression(rtrimMatch.Groups[1].Value).ToString(); + return str.TrimEnd(); + } + + // Enhanced math functions + var roundMatch = Regex.Match(expression, @"ROUND\s*\(\s*(.+?)\s*(,\s*(.+?))?\s*\)", RegexOptions.IgnoreCase); + if (roundMatch.Success) + { + var value = EvaluateNumericExpression(roundMatch.Groups[1].Value); + var decimals = roundMatch.Groups[3].Success ? (int)EvaluateNumericExpression(roundMatch.Groups[3].Value) : 0; + return Math.Round(value, Math.Max(0, decimals)); + } + + var fixMatch = Regex.Match(expression, @"FIX\s*\(\s*(.+?)\s*\)", RegexOptions.IgnoreCase); + if (fixMatch.Success) + { + var value = EvaluateNumericExpression(fixMatch.Groups[1].Value); + return value >= 0 ? Math.Floor(value) : Math.Ceiling(value); + } + + var cintMatch = Regex.Match(expression, @"CINT\s*\(\s*(.+?)\s*\)", RegexOptions.IgnoreCase); + if (cintMatch.Success) + { + var value = EvaluateNumericExpression(cintMatch.Groups[1].Value); + return (double)(int)Math.Round(value); + } + + var cdblMatch = Regex.Match(expression, @"CDBL\s*\(\s*(.+?)\s*\)", RegexOptions.IgnoreCase); + if (cdblMatch.Success) + { + return EvaluateNumericExpression(cdblMatch.Groups[1].Value); + } + + // Array bound functions + var uboundMatch = Regex.Match(expression, @"UBOUND\s*\(\s*([A-Z][A-Z0-9]*)\s*(,\s*(.+?))?\s*\)", RegexOptions.IgnoreCase); + if (uboundMatch.Success) + { + var arrayName = uboundMatch.Groups[1].Value.ToUpper(); + var dimension = uboundMatch.Groups[3].Success ? (int)EvaluateNumericExpression(uboundMatch.Groups[3].Value) : 0; + return (double)GetArrayUpperBound(arrayName, dimension); + } + + var lboundMatch = Regex.Match(expression, @"LBOUND\s*\(\s*([A-Z][A-Z0-9]*)\s*(,\s*(.+?))?\s*\)", RegexOptions.IgnoreCase); + if (lboundMatch.Success) + { + var arrayName = lboundMatch.Groups[1].Value.ToUpper(); + var dimension = lboundMatch.Groups[3].Success ? (int)EvaluateNumericExpression(lboundMatch.Groups[3].Value) : 0; + return (double)GetArrayLowerBound(arrayName, dimension); + } + + // Handle user-defined functions + var fnMatch = Regex.Match(expression, @"(FN[A-Z][A-Z0-9]*)\s*\(\s*(.+?)\s*\)", RegexOptions.IgnoreCase); + if (fnMatch.Success) + { + var funcName = fnMatch.Groups[1].Value.ToUpper(); + var argument = EvaluateExpression(fnMatch.Groups[2].Value); + + if (_userFunctions.TryGetValue(funcName, out var userFunc)) + { + var savedValue = _variables.TryGetValue(userFunc.Parameter, out var temp) ? temp : null; + _variables[userFunc.Parameter] = argument; + + var result = EvaluateExpression(userFunc.Expression); + + if (savedValue != null) + _variables[userFunc.Parameter] = savedValue; + else + _variables.Remove(userFunc.Parameter); + + return result; + } + + throw new BasicRuntimeException($"Undefined function: {funcName}", _currentLine); + } + + // Handle array access + var arrayMatch = Regex.Match(expression, @"([A-Z][A-Z0-9]*)\s*\(\s*(.+?)\s*\)", RegexOptions.IgnoreCase); + if (arrayMatch.Success) + { + var arrayName = arrayMatch.Groups[1].Value.ToUpper(); + var indicesStr = arrayMatch.Groups[2].Value; + + if (_arrays.TryGetValue(arrayName, out var array)) + { + var indices = indicesStr.Split(',') + .Select(i => (int)EvaluateNumericExpression(i.Trim())) + .ToArray(); + + if (indices.Length != array.Rank) + throw new BasicRuntimeException("Wrong number of array indices", _currentLine); + + for (int i = 0; i < indices.Length; i++) + { + if (indices[i] < 0 || indices[i] >= array.GetLength(i)) + throw new BasicRuntimeException("Array index out of bounds", _currentLine); + } + + return (double)array.GetValue(indices); + } + } + + // Handle variables (including string variables) + if (Regex.IsMatch(expression, @"^[A-Z][A-Z0-9]*\$?$", RegexOptions.IgnoreCase)) + { + return _variables.TryGetValue(expression.ToUpper(), out object value) ? value : + (expression.EndsWith("$") ? "" : 0.0); + } + + // Handle arithmetic operations (improved precedence handling) + return EvaluateArithmeticExpression(expression); + } + + private double EvaluateNumericExpression(string expression) + { + var result = EvaluateExpression(expression); + if (result is string strResult && double.TryParse(strResult, out var numResult)) + { + return numResult; + } + return Convert.ToDouble(result); + } + + private object EvaluateArithmeticExpression(string expression) + { + // Simple arithmetic parser with basic precedence + // This is a simplified version - a full parser would be more complex + + // Handle parentheses first + var parenMatch = Regex.Match(expression, @"\(([^()]+)\)"); + if (parenMatch.Success) + { + var innerResult = EvaluateExpression(parenMatch.Groups[1].Value); + var newExpression = expression.Replace(parenMatch.Value, innerResult.ToString()); + return EvaluateExpression(newExpression); + } + + // Handle multiplication and division + var multDivMatch = Regex.Match(expression, @"(.+?)\s*([*/])\s*(.+)"); + if (multDivMatch.Success) + { + var left = EvaluateNumericExpression(multDivMatch.Groups[1].Value); + var op = multDivMatch.Groups[2].Value; + var right = EvaluateNumericExpression(multDivMatch.Groups[3].Value); + + switch (op) + { + case "*": return left * right; + case "/": return right != 0 ? left / right : throw new BasicRuntimeException("Division by zero", _currentLine); + } + } + + // Handle addition and subtraction + var addSubMatch = Regex.Match(expression, @"(.+?)\s*([+\-])\s*(.+)"); + if (addSubMatch.Success) + { + var left = EvaluateNumericExpression(addSubMatch.Groups[1].Value); + var op = addSubMatch.Groups[2].Value; + var right = EvaluateNumericExpression(addSubMatch.Groups[3].Value); + + switch (op) + { + case "+": return left + right; + case "-": return left - right; + } + } + + return expression; + } + + private bool EvaluateCondition(string condition) + { + // Handle comparison operations + var comparisonOps = new[] { "<=", ">=", "<>", "=", "<", ">" }; + + foreach (var op in comparisonOps) + { + var index = condition.IndexOf(op); + if (index > 0) + { + var left = EvaluateExpression(condition.Substring(0, index).Trim()); + var right = EvaluateExpression(condition.Substring(index + op.Length).Trim()); + + var leftNum = Convert.ToDouble(left); + var rightNum = Convert.ToDouble(right); + + switch (op) + { + case "=": return Math.Abs(leftNum - rightNum) < 0.0001; + case "<>": return Math.Abs(leftNum - rightNum) >= 0.0001; + case "<": return leftNum < rightNum; + case ">": return leftNum > rightNum; + case "<=": return leftNum <= rightNum; + case ">=": return leftNum >= rightNum; + } + } + } + + // If no comparison operator, treat as boolean expression + var result = EvaluateExpression(condition); + return Convert.ToDouble(result) != 0; + } + + public static List GetSupportedFunctions() + { + return new List + { + // Control statements + new BasicFunction { Name = "PRINT", Syntax = "PRINT expression", Description = "Output text or values", Category = "I/O" }, + new BasicFunction { Name = "LET", Syntax = "LET variable = expression", Description = "Assign value to variable", Category = "Control" }, + new BasicFunction { Name = "DIM", Syntax = "DIM array(size1[,size2,...])", Description = "Dimension arrays", Category = "Control" }, + new BasicFunction { Name = "INPUT", Syntax = "INPUT variable[,variable...]", Description = "Input values from user", Category = "I/O" }, + new BasicFunction { Name = "READ", Syntax = "READ variable[,variable...]", Description = "Read values from DATA statements", Category = "I/O" }, + new BasicFunction { Name = "DATA", Syntax = "DATA value[,value...]", Description = "Store data values", Category = "I/O" }, + new BasicFunction { Name = "RESTORE", Syntax = "RESTORE", Description = "Reset DATA pointer", Category = "I/O" }, + new BasicFunction { Name = "DEF", Syntax = "DEF FNname(parameter) = expression", Description = "Define user function", Category = "Control" }, + new BasicFunction { Name = "ON", Syntax = "ON expression GOTO/GOSUB line1[,line2...]", Description = "Computed branching", Category = "Control" }, + new BasicFunction { Name = "FOR", Syntax = "FOR variable = start TO end [STEP increment]", Description = "Start a loop", Category = "Control" }, + new BasicFunction { Name = "NEXT", Syntax = "NEXT [variable]", Description = "End a loop", Category = "Control" }, + new BasicFunction { Name = "IF", Syntax = "IF condition THEN statement", Description = "Conditional execution", Category = "Control" }, + new BasicFunction { Name = "GOTO", Syntax = "GOTO line_number", Description = "Jump to line", Category = "Control" }, + new BasicFunction { Name = "GOSUB", Syntax = "GOSUB line_number", Description = "Call subroutine", Category = "Control" }, + new BasicFunction { Name = "RETURN", Syntax = "RETURN", Description = "Return from subroutine", Category = "Control" }, + new BasicFunction { Name = "END", Syntax = "END", Description = "End program", Category = "Control" }, + new BasicFunction { Name = "REM", Syntax = "REM comment", Description = "Comment/remark", Category = "Control" }, + + // Mathematical functions + new BasicFunction { Name = "ABS", Syntax = "ABS(number)", Description = "Absolute value", Category = "Math" }, + new BasicFunction { Name = "ATN", Syntax = "ATN(number)", Description = "Arctangent", Category = "Math" }, + new BasicFunction { Name = "COS", Syntax = "COS(number)", Description = "Cosine", Category = "Math" }, + new BasicFunction { Name = "EXP", Syntax = "EXP(number)", Description = "Exponential (e^x)", Category = "Math" }, + new BasicFunction { Name = "INT", Syntax = "INT(number)", Description = "Integer part", Category = "Math" }, + new BasicFunction { Name = "LOG", Syntax = "LOG(number)", Description = "Natural logarithm", Category = "Math" }, + new BasicFunction { Name = "RND", Syntax = "RND[(number)]", Description = "Random number 0-1", Category = "Math" }, + new BasicFunction { Name = "SGN", Syntax = "SGN(number)", Description = "Sign (-1, 0, or 1)", Category = "Math" }, + new BasicFunction { Name = "SIN", Syntax = "SIN(number)", Description = "Sine", Category = "Math" }, + new BasicFunction { Name = "SQR", Syntax = "SQR(number)", Description = "Square root", Category = "Math" }, + new BasicFunction { Name = "TAN", Syntax = "TAN(number)", Description = "Tangent", Category = "Math" }, + new BasicFunction { Name = "ROUND", Syntax = "ROUND(number[, decimals])", Description = "Round to decimal places", Category = "Math" }, + new BasicFunction { Name = "FIX", Syntax = "FIX(number)", Description = "Truncate decimal part", Category = "Math" }, + new BasicFunction { Name = "CINT", Syntax = "CINT(number)", Description = "Convert to integer", Category = "Math" }, + new BasicFunction { Name = "CDBL", Syntax = "CDBL(expression)", Description = "Convert to double", Category = "Math" }, + + // String functions + new BasicFunction { Name = "ASC", Syntax = "ASC(string)", Description = "ASCII code of first character", Category = "String" }, + new BasicFunction { Name = "CHR$", Syntax = "CHR$(number)", Description = "Character from ASCII code", Category = "String" }, + new BasicFunction { Name = "LEFT$", Syntax = "LEFT$(string, length)", Description = "Left substring", Category = "String" }, + new BasicFunction { Name = "LEN", Syntax = "LEN(string)", Description = "Length of string", Category = "String" }, + new BasicFunction { Name = "MID$", Syntax = "MID$(string, start[, length])", Description = "Middle substring", Category = "String" }, + new BasicFunction { Name = "RIGHT$", Syntax = "RIGHT$(string, length)", Description = "Right substring", Category = "String" }, + new BasicFunction { Name = "STR$", Syntax = "STR$(number)", Description = "Convert number to string", Category = "String" }, + new BasicFunction { Name = "VAL", Syntax = "VAL(string)", Description = "Convert string to number", Category = "String" }, + new BasicFunction { Name = "INSTR", Syntax = "INSTR(string, substring)", Description = "Find position of substring", Category = "String" }, + new BasicFunction { Name = "SPACE$", Syntax = "SPACE$(count)", Description = "Generate spaces", Category = "String" }, + new BasicFunction { Name = "STRING$", Syntax = "STRING$(count, character)", Description = "Repeat character", Category = "String" }, + new BasicFunction { Name = "UCASE$", Syntax = "UCASE$(string)", Description = "Convert to uppercase", Category = "String" }, + new BasicFunction { Name = "LCASE$", Syntax = "LCASE$(string)", Description = "Convert to lowercase", Category = "String" }, + new BasicFunction { Name = "LTRIM$", Syntax = "LTRIM$(string)", Description = "Remove leading spaces", Category = "String" }, + new BasicFunction { Name = "RTRIM$", Syntax = "RTRIM$(string)", Description = "Remove trailing spaces", Category = "String" }, + + // Program management commands + new BasicFunction { Name = "LIST", Syntax = "LIST [start[-end]]", Description = "List program lines", Category = "Program" }, + new BasicFunction { Name = "NEW", Syntax = "NEW", Description = "Clear current program", Category = "Program" }, + new BasicFunction { Name = "CLEAR", Syntax = "CLEAR", Description = "Clear variables and reset", Category = "Program" }, + new BasicFunction { Name = "RUN", Syntax = "RUN [line]", Description = "Run program from specified line", Category = "Program" }, + new BasicFunction { Name = "STOP", Syntax = "STOP", Description = "Stop program execution", Category = "Program" }, + new BasicFunction { Name = "CONT", Syntax = "CONT", Description = "Continue execution after STOP", Category = "Program" }, + + // I/O formatting functions + new BasicFunction { Name = "TAB", Syntax = "TAB(n)", Description = "Tab to column position", Category = "I/O" }, + new BasicFunction { Name = "SPC", Syntax = "SPC(n)", Description = "Print n spaces", Category = "I/O" }, + new BasicFunction { Name = "FRE", Syntax = "FRE(x)", Description = "Free memory available", Category = "System" }, + new BasicFunction { Name = "POS", Syntax = "POS(x)", Description = "Current print position", Category = "System" }, + + // Graphics commands (when enabled) + new BasicFunction { Name = "PLOT", Syntax = "PLOT x, y", Description = "Plot a point at coordinates", Category = "Graphics" }, + new BasicFunction { Name = "HLIN", Syntax = "HLIN x1, x2 AT y", Description = "Draw horizontal line", Category = "Graphics" }, + new BasicFunction { Name = "VLIN", Syntax = "VLIN y1, y2 AT x", Description = "Draw vertical line", Category = "Graphics" }, + new BasicFunction { Name = "COLOR", Syntax = "COLOR = value", Description = "Set graphics color", Category = "Graphics" }, + + // File I/O commands (when enabled) + new BasicFunction { Name = "OPEN", Syntax = "OPEN \"filename\", #channel[, mode]", Description = "Open virtual file", Category = "File I/O" }, + new BasicFunction { Name = "CLOSE", Syntax = "CLOSE #channel", Description = "Close file channel", Category = "File I/O" }, + new BasicFunction { Name = "PRINT#", Syntax = "PRINT# channel, data", Description = "Write to file", Category = "File I/O" }, + new BasicFunction { Name = "INPUT#", Syntax = "INPUT# channel, variable", Description = "Read from file", Category = "File I/O" }, + + // Memory operations (when enabled) + new BasicFunction { Name = "PEEK", Syntax = "PEEK(address)", Description = "Read from virtual memory", Category = "Memory" }, + new BasicFunction { Name = "POKE", Syntax = "POKE address, value", Description = "Write to virtual memory", Category = "Memory" }, + new BasicFunction { Name = "CALL", Syntax = "CALL address", Description = "Simulate machine language call", Category = "Memory" }, + + // Array functions + new BasicFunction { Name = "UBOUND", Syntax = "UBOUND(array[, dimension])", Description = "Upper bound of array dimension", Category = "Array" }, + new BasicFunction { Name = "LBOUND", Syntax = "LBOUND(array[, dimension])", Description = "Lower bound of array dimension", Category = "Array" } + }; + } +} + +// Additional helper classes for enhanced functionality +public class UserDefinedFunction +{ + public string Name { get; set; } + public string Parameter { get; set; } + public string Expression { get; set; } + public int LineNumber { get; set; } +} + +public class DataStatement +{ + public int LineNumber { get; set; } + public List Values { get; set; } +} + +// Enhanced request/response classes +public class BasicExecutionRequest +{ + public string Code { get; set; } + public bool CaseSensitive { get; set; } = false; + public bool EnableGraphics { get; set; } = false; + public bool EnableFileIO { get; set; } = false; + public bool EnableMemory { get; set; } = false; + public string BasicVersion { get; set; } = "microsoft6502"; + public List InputValues { get; set; } = new List(); + public Dictionary Files { get; set; } = new Dictionary(); + public Dictionary Variables { get; set; } = new Dictionary(); + public int MaxExecutionTimeMs { get; set; } = 120000; + public ResourceLimits ResourceLimits { get; set; } = new ResourceLimits(); +} + +public class BasicExecutionResponse +{ + public string Output { get; set; } + public Dictionary Variables { get; set; } + public List Graphics { get; set; } + public Dictionary Files { get; set; } + public VirtualMemory Memory { get; set; } + public ExecutionStatistics ExecutionStats { get; set; } + public List Errors { get; set; } + public bool Success { get; set; } + public int LinesExecuted { get; set; } + public long MemoryUsedBytes { get; set; } + public int FunctionsCalled { get; set; } +} + +// Helper classes +public class ForLoop +{ + public string Variable { get; set; } + public double EndValue { get; set; } + public double StepValue { get; set; } + public int StartLine { get; set; } +} + +public enum StatementResultType +{ + Continue, + Goto, + End, + Stop +} + +public class StatementResult +{ + public StatementResultType Type { get; set; } + public int TargetLine { get; set; } +} \ No newline at end of file From 90f5bceb7e6575258bd6d769cd4a0644171efdf6 Mon Sep 17 00:00:00 2001 From: Troy Simeon Taylor <44444967+troystaylor@users.noreply.github.com> Date: Mon, 8 Sep 2025 16:24:32 -0400 Subject: [PATCH 2/2] Add files via upload --- tools/paconn-cli/CHANGES.md | 116 +++ tools/paconn-cli/README.md | 709 +++++++++--------- tools/paconn-cli/UPLOAD_GUIDE.md | 29 + tools/paconn-cli/VERIFICATION.md | 45 ++ tools/paconn-cli/paconn/__init__.py | 44 +- tools/paconn-cli/paconn/commands/params.py | 660 ++++++++-------- tools/paconn-cli/paconn/commands/validate.py | 143 ++-- .../paconn/operations/script_validate.py | 192 +++++ tools/paconn-cli/setup.py | 147 ++-- 9 files changed, 1268 insertions(+), 817 deletions(-) create mode 100644 tools/paconn-cli/CHANGES.md create mode 100644 tools/paconn-cli/UPLOAD_GUIDE.md create mode 100644 tools/paconn-cli/VERIFICATION.md create mode 100644 tools/paconn-cli/paconn/operations/script_validate.py diff --git a/tools/paconn-cli/CHANGES.md b/tools/paconn-cli/CHANGES.md new file mode 100644 index 0000000000..cd636d6840 --- /dev/null +++ b/tools/paconn-cli/CHANGES.md @@ -0,0 +1,116 @@ +# PACONN CLI v0.1.0 - C# Script Validation Changes + +## Overview +This package contains all the modified and new files for adding C# script validation to the paconn CLI tool. + +## Version Update +- **Previous Version**: 0.0.21 +- **New Version**: 0.1.0 (minor version bump for new feature) + +## Files Modified/Added + +### Root Level Files +1. **setup.py** - MODIFIED + - Updated version from 0.0.21 to 0.1.0 + - Added regex dependency for enhanced C# script parsing + +2. **README.md** - MODIFIED + - Updated validate command documentation + - Added comprehensive C# script validation examples + - Added new command line arguments documentation + +### paconn/ Directory +3. **paconn/__init__.py** - MODIFIED + - Updated version from 0.0.21 to 0.1.0 + +### paconn/commands/ Directory +4. **paconn/commands/validate.py** - MODIFIED + - Added script parameter to validate function signature + - Implemented mutual exclusion logic (api-def OR script, not both) + - Added comprehensive script validation output formatting + - Enhanced error, warning, and success message formatting + +5. **paconn/commands/params.py** - MODIFIED + - Added SCRIPT parameter to _VALIDATE command arguments + - Added help text for --script parameter + - Included mutual exclusion documentation + +### paconn/operations/ Directory +6. **paconn/operations/script_validate.py** - NEW FILE + - Complete C# script validation engine + - ValidationResult dataclass for structured results + - CSharpScriptValidator class with comprehensive checks: + - Namespace validation (26 approved namespaces) + - Class structure validation (Script : ScriptBase) + - Method signature validation (ExecuteAsync) + - Security constraint validation + - File size validation (1MB limit) + - Best practices validation + - Professional error and warning formatting + +## New Features Added + +### C# Script Validation +- **Always Strict Mode**: No basic/lenient validation option +- **Mutual Exclusion**: Cannot validate both swagger and script simultaneously +- **Comprehensive Checks**: + - Required Script class inheritance from ScriptBase + - Mandatory ExecuteAsync method implementation + - Only 26 allowed namespaces from Microsoft documentation + - File size under 1MB limit + - Security constraints (no file system, network operations) + - Best practice recommendations + +### Command Line Usage +```bash +# Validate C# script (always strict) +paconn validate --script script.csx + +# Validate OpenAPI definition (existing functionality) +paconn validate --api-def swagger.json + +# Using settings file (supports either, not both) +paconn validate --settings settings.json + +# Error when both specified +paconn validate --api-def swagger.json --script script.csx # ERROR + +# Help documentation +paconn validate --help +``` + +### Settings File Support +Scripts can be validated via settings.json: +```json +{ + "script": "path/to/script.csx" +} +``` + +### Enhanced Output Formatting +All validation outputs now have consistent, professional formatting: +- Structured headers and sections +- Numbered errors and warnings +- Clear validation summaries +- Explicit result statements + +## Installation Notes +- Requires Python 3.5+ +- New dependency: regex>=2022.1.18 (automatically installed) +- Backward compatible with existing swagger validation workflows + +## Testing +All functionality has been tested with: +- Valid C# scripts following Power Platform requirements +- Invalid scripts with various error types +- Scripts with warnings for best practices +- Mutual exclusion scenarios +- Settings file integration +- Help text and version display + +## Deployment +1. Replace the modified files in the repository +2. Update version number references if needed +3. Test the package build: `python setup.py sdist bdist_wheel` +4. Publish to PyPI: `twine upload dist/*` +5. Users can upgrade: `pip install --upgrade paconn` \ No newline at end of file diff --git a/tools/paconn-cli/README.md b/tools/paconn-cli/README.md index 4ebc39707f..254da82cb4 100644 --- a/tools/paconn-cli/README.md +++ b/tools/paconn-cli/README.md @@ -1,345 +1,364 @@ -# Microsoft Power Platform Connectors CLI - ->[Note] ->**These release notes describe functionality that may not have been released yet.** To see when this functionality is planned to release, please review [What's new and planned for Common Data Model and Data Integration](https://docs.microsoft.com/en-us/business-applications-release-notes/April19/cdm-data-integration/planned-features). Delivery timelines and projected functionality may change or may not ship (see [Microsoft policy](https://go.microsoft.com/fwlink/p/?linkid=2007332)). - - -The `paconn` command-line tool is designed to aid Microsoft Power Platform custom connectors development. - -## Installing - -1. Install Python 3.5+ from [https://www.python.org/downloads](Python downloads). Select the 'Download' link on any version of Python greater than Python 3.5. For Linux and macOS X, follow the appropriate link on the page. You can also install using an OS-specific package manager of your choice. - -2. Run the installer to begin installation and be sure to check the box 'Add Python X.X to PATH'. - -3. Make sure the installation path is in the PATH variable by running: - - `python --version` - -4. After python is installed, install `paconn` by running: - - `pip install paconn` - - If you get errors saying 'Access is denied', consider using the `--user` option or running the command as an Administrator (Windows). - -## Custom Connector Directory and Files - -A custom connector consists of two to four files: an Open API swagger definition, an API properties file, an optional icon for the connector, and an optional csharp script file. The files are generally located in a directory with the connector ID as the name of the directory. - -Sometimes, the custom connector directory may include a `settings.json` file. Although this file isn't part of the connector definition, it can be used as an argument-store for the CLI. - -### API Definition (Swagger) File - -The API definition file describes the API for the custom connector using the OpenAPI specification. It's also known as the swagger file. For more information about API definitions used to write custom connector, go to [Create a custom connector from an OpenAPI definition](https://docs.microsoft.com/en-us/connectors/custom-connectors/define-openapi-definition). Also review the tutorial in the [Extend an OpenAPI definition for a custom connector](https://docs.microsoft.com/en-us/connectors/custom-connectors/openapi-extensions) article. - -### API Properties File - -The API properties file contains some properties for the custom connector. These properties aren't part of the API definition. It contains information such as the brand color, authentication information, and so on. A typical API properties file looks like the following sample: - -```json -{ - "properties": { - "capabilities": [], - "connectionParameters": { - "api_key": { - "type": "securestring", - "uiDefinition": { - "constraints": { - "clearText": false, - "required": "true", - "tabIndex": 2 - }, - "description": "The KEY for this API", - "displayName": "KEY", - "tooltip": "Provide your KEY" - } - } - }, - "iconBrandColor": "#007EE6", - "scriptOperations": [ - "getCall", - "postCall", - "putCall" - ], - "policyTemplateInstances": [ - { - "title": "MyPolicy", - "templateId": "setqueryparameter", - "parameters": { - "x-ms-apimTemplateParameter.name": "queryParameterName", - "x-ms-apimTemplateParameter.value": "queryParameterValue", - "x-ms-apimTemplateParameter.existsAction": "override" - } - } - ] - } -} -``` - -More information on each of the properties is given below: - -* `properties`: The container for the information. - -* `connectionParameters`: Defines the connection parameter for the service. - -* `iconBrandColor`: The icon brand color in HTML hex code for the custom connector. - -* `scriptOperations`: A list of the operations that are executed with the script file. An empty scriptOperations list indicates that all operations are executed with the script file. - -* `capabilities`: Describes the capabilities for the connector, for example, cloud only, on-prem gateway, and so on. - -* `policyTemplateInstances`: An optional list of policy template instances and values used in the custom connector. - -### Icon File - -The icon file is a small image representing the custom connector icon. - -### Script File - -The script is a CSX script file that is deployed for the custom connector and executed for every call to a subset of the connector's operations. - -### Settings File - -Instead of providing the arguments in the command line, a `settings.json` file can be used to specify them. A typical `settings.json` file looks like the following sample: - -```json -{ - "connectorId": "CONNECTOR-ID", - "environment": "ENVIRONMENT-GUID", - "apiProperties": "apiProperties.json", - "apiDefinition": "apiDefinition.swagger.json", - "icon": "icon.png", - "script": "script.csx", - "powerAppsApiVersion": "2016-11-01", - "powerAppsUrl": "https://api.powerapps.com" -} -``` - -In the settings file, the following items are expected. If an option is missing but required, the console will prompt for the missing information. - -* `connectorId`: The connector ID string for the custom connector. This parameter is required for download and update operations, but not for the create or validate operation. A new custom connector with the new ID will be created for create command. If you need to update a custom connector just created using the same settings file, please make sure the settings file is correctly updated with the new connector ID from the create operation. - -* `environment`: The environment ID string for the custom connector. This parameter is required for all operations, except the validate operation. - -* `apiProperties`: The path to the API properties `apiProperties.json` file. It's required for the create and update operation. When this option is present during the download, the file will be downloaded to the given location, otherwise it will be saved as `apiProperties.json`. - -* `apiDefinition`: The path to the swagger file. It's required for the create, update, and validate operations. When this option is present during the download operation, the file in the given location will be written to, otherwise it will be saved as `apiDefinition.swagger.json`. - -* `icon`: The path to the optional icon file. The create and update operations will use the default icon when this parameter is no specified. When this option is present during the download operation, the file in the given location will be written to, otherwise it will be saved as `icon.png`. - -* `script`: The path to the optional script file. The create and update operations will use only the value within the specified parameter. When this option is present during the download operation, the file in the given location will be written to, otherwise it will be saved as `script.csx`. - -* `powerAppsUrl`: The API URL for Power Apps. This parameter is optional and set to `https://api.powerapps.com` by default. - -* `powerAppsApiVersion`: The API version to use for Power Apps. This parameter is optional and set to `2016-11-01` by default. - -## Command-Line Operations - -### Login - -Log in to Power Platform by running: - -`paconn login` - -This command will ask you to log in using the device code login process. Follow the prompt for the log in. Service Principle authentication is not supported at this point. Please review [a customer workaround posted in the issues page](https://github.com/microsoft/PowerPlatformConnectors/issues/287). - -### Logout - -Logout by running: - -`paconn logout` - -### Download Custom Connector Files - -The connector files are always downloaded into a subdirectory with the connector ID as the directory name. When a destination directory is specified, the subdirectory will be created in the specified one. Otherwise, it will be created in the current directory. In addition to the three connector files, the download operation will also write a fourth file called settings.json containing the parameters used to download the files. - -Download the custom connector files by running: - -`paconn download` - -or - -`paconn download -e [Power Platform Environment GUID] -c [Connector ID]` - -or - -`paconn download -s [Path to settings.json]` - -When the environment or connector ID isn't specified, the command will prompt for the missing argument(s). The command will output the download location for the connector if it successfully downloads. - -All the arguments can be also specified using a [settings.json file](#settings-file). - -``` -Arguments - --cid -c : The custom connector ID. - --dest -d : Destination directory. - --env -e : Power Platform environment GUID. - --overwrite -w : Overwrite all the existing connector and settings files. - --pau -u : Power Platform URL. - --pav -v : Power Platform API version. - --settings -s : A settings file containing required parameters. - When a settings file is specified some command - line parameters are ignored. -``` - -### Create a New Custom Connector - -A new custom connector can be created from the connectors files by running the `create` operation. Create a connector by running: - -`paconn create --api-prop [Path to apiProperties.json] --api-def [Path to apiDefinition.swagger.json]` - -or - -`paconn create -e [Power Platform Environment GUID] --api-prop [Path to apiProperties.json] --api-def [Path to apiDefinition.swagger.json] --icon [Path to icon.png] --secret [The OAuth2 client secret for the connector]` - -or - -`paconn create -s [Path to settings.json] --secret [The OAuth2 client secret for the connector]` - -When the environment isn't specified, the command will prompt for it. However, the API definition and API properties file must be provided as part of the command line argument or a settings file. The OAuth2 secret must be provided for a connector using OAuth2. The command will print the connector ID for the newly created custom connector on successful completion. If you're using a settings.json file for the create command, please make sure to update it with the new connector ID before you update the newly created connector. - -``` -Arguments - --api-def : Location for the Open API definition JSON document. - --api-prop : Location for the API properties JSON document. - --env -e : Power Platform environment GUID. - --icon : Location for the icon file. - --script -x : Location for the script file. - --pau -u : Power Platform URL. - --pav -v : Power Platform API version. - --secret -r : The OAuth2 client secret for the connector. - --settings -s : A settings file containing required parameters. - When a settings file is specified some command - line parameters are ignored. -``` -### Update an Existing Custom Connector - -Like the `create` operation, an existing custom connector can be updated using the `update` operation. Update a connector by running: - -`paconn update --api-prop [Path to apiProperties.json] --api-def [Path to apiDefinition.swagger.json]` - -or - -`paconn update -e [Power Platform Environment GUID] -c [Connector ID] --api-prop [Path to apiProperties.json] --api-def [Path to apiDefinition.swagger.json] --icon [Path to icon.png] --secret [The OAuth2 client secret for the connector]` - -or - -`paconn update -s [Path to settings.json] --secret [The OAuth2 client secret for the connector]` - -When environment or connector ID isn't specified, the command will prompt for the missing argument(s). However, the API definition and API properties file must be provided as part of the command-line argument or a settings file. The OAuth2 secret must be provided for a connector using OAuth2. The command will print the updated connector ID on successful completion. If you're using a settings.json file for the update command, make sure the correct environment and connector ID are specified. - -``` -Arguments - --api-def : Location for the Open API definition JSON document. - --api-prop : Location for the API properties JSON document. - --cid -c : The custom connector ID. - --env -e : Power Platform environment GUID. - --icon : Location for the icon file. - --script -x : Location for the script file. - --pau -u : Power Platform URL. - --pav -v : Power Platform API version. - --secret -r : The OAuth2 client secret for the connector. - --settings -s : A settings file containing required parameters. - When a settings file is specified some command - line parameters are ignored. - ``` - -### Validate a Swagger JSON - -The validate operation takes a swagger file and verfies if it follows all the recommended rules. Validate a swagger file by running: - -`paconn validate --api-def [Path to apiDefinition.swagger.json]` - -or - -`paconn validate -s [Path to settings.json]` - -The command will print the error, warning, or success message depending result of the validation. - -``` -Arguments - --api-def : Location for the Open API definition JSON document. - --pau -u : Power Platform URL. - --pav -v : Power Platform API version. - --settings -s : A settings file containing required parameters. - When a settings file is specified some command - line parameters are ignored. - ``` - - -### Best Practice - -Download all of your connectors and use git or any other source code management system to save the files. In case of an incorrect update, redeploy the connector by rerunning the update command with the correct set of files from the source code management system. - -Please test the custom connector and the settings file in a test environment before deploying in the production environment. Always double check that the environment and connector id are correct. - -## Limitations - -The project is limited to creation, update, and download of a custom connector in the Power Automate and Power Apps environment. When an environment isn't specified, only the Power Automate environments are listed to choose from. For a non-custom connector, the swagger file isn't returned. - -**Stack Owner Property & apiProperties file:** - -Currently, there is a limitation that prevents you from updating your connector's artificats in your environment using Paconn when the `stackOwner` property is present in your `apiProperties.json` file. As a workaround to this, create two versions of your connector artifacts: the first being the version that is submitted to certification and contains the `stackOwner` property, the second having the `stackOwner` property omitted to enable updating within your environment. We are working to remove the limitation and will update this section once complete. - -## Reporting issues and feedback - -If you encounter any bugs with the tool, submit an issue in the [Issues](https://github.com/Microsoft/PowerPlatformConnectors/issues) section of our GitHub repo. - -If you believe you have found a security vulnerability that meets [Microsoft's definition of a security vulnerability](https://docs.microsoft.com/previous-versions/tn-archive/cc751383%28v=technet.10%29), submit a [report to MSRC](https://msrc.microsoft.com/create-report). More information can be found at [MSRC frequently asked questions on reporting](https://www.microsoft.com/msrc/faqs-report-an-issue). - - -## Contributing - -This project welcomes contributions and suggestions. Most contributions require you to agree to a -Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us -the rights to use your contribution. For details, visit https://cla.microsoft.com. - -To contibute a connector to the open source repo, please start by creating a fork on the github repo. -Once you have the fork created, create a new branch on the forked repo. Clone this forked repo on you -local machine, and checkout the branch. Create a folder for your connector under the `connectors` folder -and place the connector files in the sub-folder. Commit and push the changes to your forked branch. -Create a pull request from the forked branch to the main repo to merge your changes into the main repo. -[Please see this document for more information](https://github.com/CoolProp/CoolProp/wiki/Contributing:-git-development-workflow). - -When you submit a pull request, a CLA-bot will automatically determine whether you need to provide -a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions -provided by the bot. You will only need to do this once across all repos using our CLA. - -This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). -For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or -contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments. - -## Legal Notices - -Microsoft and any contributors grant you a license to the Microsoft documentation and other content -in this repository under the [Creative Commons Attribution 4.0 International Public License](https://creativecommons.org/licenses/by/4.0/legalcode), -see the [LICENSE](LICENSE) file, and grant you a license to any code in the repository under the [MIT License](https://opensource.org/licenses/MIT), see the -[LICENSE-CODE](LICENSE-CODE) file. - -Microsoft, Windows, Microsoft Azure and/or other Microsoft products and services referenced in the documentation -may be either trademarks or registered trademarks of Microsoft in the United States and/or other countries. -The licenses for this project do not grant you rights to use any Microsoft names, logos, or trademarks. -Microsoft's general trademark guidelines can be found at http://go.microsoft.com/fwlink/?LinkID=254653. - -Privacy information can be found at https://privacy.microsoft.com/en-us/ - -Microsoft and any contributors reserve all others rights, whether under their respective copyrights, patents, -or trademarks, whether by implication, estoppel or otherwise. - -## License - -``` -Microsoft Power Platform Connectors CLI (paconn) - -Copyright (c) Microsoft Corporation -All rights reserved. - -MIT License - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -``` - +# Microsoft Power Platform Connectors CLI + +>[Note] +>**These release notes describe functionality that may not have been released yet.** To see when this functionality is planned to release, please review [What's new and planned for Common Data Model and Data Integration](https://docs.microsoft.com/en-us/business-applications-release-notes/April19/cdm-data-integration/planned-features). Delivery timelines and projected functionality may change or may not ship (see [Microsoft policy](https://go.microsoft.com/fwlink/p/?linkid=2007332)). + + +The `paconn` command-line tool is designed to aid Microsoft Power Platform custom connectors development. + +## Installing + +1. Install Python 3.5+ from [https://www.python.org/downloads](Python downloads). Select the 'Download' link on any version of Python greater than Python 3.5. For Linux and macOS X, follow the appropriate link on the page. You can also install using an OS-specific package manager of your choice. + +2. Run the installer to begin installation and be sure to check the box 'Add Python X.X to PATH'. + +3. Make sure the installation path is in the PATH variable by running: + + `python --version` + +4. After python is installed, install `paconn` by running: + + `pip install paconn` + + If you get errors saying 'Access is denied', consider using the `--user` option or running the command as an Administrator (Windows). + +## Custom Connector Directory and Files + +A custom connector consists of two to four files: an Open API swagger definition, an API properties file, an optional icon for the connector, and an optional csharp script file. The files are generally located in a directory with the connector ID as the name of the directory. + +Sometimes, the custom connector directory may include a `settings.json` file. Although this file isn't part of the connector definition, it can be used as an argument-store for the CLI. + +### API Definition (Swagger) File + +The API definition file describes the API for the custom connector using the OpenAPI specification. It's also known as the swagger file. For more information about API definitions used to write custom connector, go to [Create a custom connector from an OpenAPI definition](https://docs.microsoft.com/en-us/connectors/custom-connectors/define-openapi-definition). Also review the tutorial in the [Extend an OpenAPI definition for a custom connector](https://docs.microsoft.com/en-us/connectors/custom-connectors/openapi-extensions) article. + +### API Properties File + +The API properties file contains some properties for the custom connector. These properties aren't part of the API definition. It contains information such as the brand color, authentication information, and so on. A typical API properties file looks like the following sample: + +```json +{ + "properties": { + "capabilities": [], + "connectionParameters": { + "api_key": { + "type": "securestring", + "uiDefinition": { + "constraints": { + "clearText": false, + "required": "true", + "tabIndex": 2 + }, + "description": "The KEY for this API", + "displayName": "KEY", + "tooltip": "Provide your KEY" + } + } + }, + "iconBrandColor": "#007EE6", + "scriptOperations": [ + "getCall", + "postCall", + "putCall" + ], + "policyTemplateInstances": [ + { + "title": "MyPolicy", + "templateId": "setqueryparameter", + "parameters": { + "x-ms-apimTemplateParameter.name": "queryParameterName", + "x-ms-apimTemplateParameter.value": "queryParameterValue", + "x-ms-apimTemplateParameter.existsAction": "override" + } + } + ] + } +} +``` + +More information on each of the properties is given below: + +* `properties`: The container for the information. + +* `connectionParameters`: Defines the connection parameter for the service. + +* `iconBrandColor`: The icon brand color in HTML hex code for the custom connector. + +* `scriptOperations`: A list of the operations that are executed with the script file. An empty scriptOperations list indicates that all operations are executed with the script file. + +* `capabilities`: Describes the capabilities for the connector, for example, cloud only, on-prem gateway, and so on. + +* `policyTemplateInstances`: An optional list of policy template instances and values used in the custom connector. + +### Icon File + +The icon file is a small image representing the custom connector icon. + +### Script File + +The script is a CSX script file that is deployed for the custom connector and executed for every call to a subset of the connector's operations. + +### Settings File + +Instead of providing the arguments in the command line, a `settings.json` file can be used to specify them. A typical `settings.json` file looks like the following sample: + +```json +{ + "connectorId": "CONNECTOR-ID", + "environment": "ENVIRONMENT-GUID", + "apiProperties": "apiProperties.json", + "apiDefinition": "apiDefinition.swagger.json", + "icon": "icon.png", + "script": "script.csx", + "powerAppsApiVersion": "2016-11-01", + "powerAppsUrl": "https://api.powerapps.com" +} +``` + +In the settings file, the following items are expected. If an option is missing but required, the console will prompt for the missing information. + +* `connectorId`: The connector ID string for the custom connector. This parameter is required for download and update operations, but not for the create or validate operation. A new custom connector with the new ID will be created for create command. If you need to update a custom connector just created using the same settings file, please make sure the settings file is correctly updated with the new connector ID from the create operation. + +* `environment`: The environment ID string for the custom connector. This parameter is required for all operations, except the validate operation. + +* `apiProperties`: The path to the API properties `apiProperties.json` file. It's required for the create and update operation. When this option is present during the download, the file will be downloaded to the given location, otherwise it will be saved as `apiProperties.json`. + +* `apiDefinition`: The path to the swagger file. It's required for the create, update, and validate operations. When this option is present during the download operation, the file in the given location will be written to, otherwise it will be saved as `apiDefinition.swagger.json`. + +* `icon`: The path to the optional icon file. The create and update operations will use the default icon when this parameter is no specified. When this option is present during the download operation, the file in the given location will be written to, otherwise it will be saved as `icon.png`. + +* `script`: The path to the optional script file. The create and update operations will use only the value within the specified parameter. When this option is present during the download operation, the file in the given location will be written to, otherwise it will be saved as `script.csx`. + +* `powerAppsUrl`: The API URL for Power Apps. This parameter is optional and set to `https://api.powerapps.com` by default. + +* `powerAppsApiVersion`: The API version to use for Power Apps. This parameter is optional and set to `2016-11-01` by default. + +## Command-Line Operations + +### Login + +Log in to Power Platform by running: + +`paconn login` + +This command will ask you to log in using the device code login process. Follow the prompt for the log in. Service Principle authentication is not supported at this point. Please review [a customer workaround posted in the issues page](https://github.com/microsoft/PowerPlatformConnectors/issues/287). + +### Logout + +Logout by running: + +`paconn logout` + +### Download Custom Connector Files + +The connector files are always downloaded into a subdirectory with the connector ID as the directory name. When a destination directory is specified, the subdirectory will be created in the specified one. Otherwise, it will be created in the current directory. In addition to the three connector files, the download operation will also write a fourth file called settings.json containing the parameters used to download the files. + +Download the custom connector files by running: + +`paconn download` + +or + +`paconn download -e [Power Platform Environment GUID] -c [Connector ID]` + +or + +`paconn download -s [Path to settings.json]` + +When the environment or connector ID isn't specified, the command will prompt for the missing argument(s). The command will output the download location for the connector if it successfully downloads. + +All the arguments can be also specified using a [settings.json file](#settings-file). + +``` +Arguments + --cid -c : The custom connector ID. + --dest -d : Destination directory. + --env -e : Power Platform environment GUID. + --overwrite -w : Overwrite all the existing connector and settings files. + --pau -u : Power Platform URL. + --pav -v : Power Platform API version. + --settings -s : A settings file containing required parameters. + When a settings file is specified some command + line parameters are ignored. +``` + +### Create a New Custom Connector + +A new custom connector can be created from the connectors files by running the `create` operation. Create a connector by running: + +`paconn create --api-prop [Path to apiProperties.json] --api-def [Path to apiDefinition.swagger.json]` + +or + +`paconn create -e [Power Platform Environment GUID] --api-prop [Path to apiProperties.json] --api-def [Path to apiDefinition.swagger.json] --icon [Path to icon.png] --secret [The OAuth2 client secret for the connector]` + +or + +`paconn create -s [Path to settings.json] --secret [The OAuth2 client secret for the connector]` + +When the environment isn't specified, the command will prompt for it. However, the API definition and API properties file must be provided as part of the command line argument or a settings file. The OAuth2 secret must be provided for a connector using OAuth2. The command will print the connector ID for the newly created custom connector on successful completion. If you're using a settings.json file for the create command, please make sure to update it with the new connector ID before you update the newly created connector. + +``` +Arguments + --api-def : Location for the Open API definition JSON document. + --api-prop : Location for the API properties JSON document. + --env -e : Power Platform environment GUID. + --icon : Location for the icon file. + --script -x : Location for the script file. + --pau -u : Power Platform URL. + --pav -v : Power Platform API version. + --secret -r : The OAuth2 client secret for the connector. + --settings -s : A settings file containing required parameters. + When a settings file is specified some command + line parameters are ignored. +``` +### Update an Existing Custom Connector + +Like the `create` operation, an existing custom connector can be updated using the `update` operation. Update a connector by running: + +`paconn update --api-prop [Path to apiProperties.json] --api-def [Path to apiDefinition.swagger.json]` + +or + +`paconn update -e [Power Platform Environment GUID] -c [Connector ID] --api-prop [Path to apiProperties.json] --api-def [Path to apiDefinition.swagger.json] --icon [Path to icon.png] --secret [The OAuth2 client secret for the connector]` + +or + +`paconn update -s [Path to settings.json] --secret [The OAuth2 client secret for the connector]` + +When environment or connector ID isn't specified, the command will prompt for the missing argument(s). However, the API definition and API properties file must be provided as part of the command-line argument or a settings file. The OAuth2 secret must be provided for a connector using OAuth2. The command will print the updated connector ID on successful completion. If you're using a settings.json file for the update command, make sure the correct environment and connector ID are specified. + +``` +Arguments + --api-def : Location for the Open API definition JSON document. + --api-prop : Location for the API properties JSON document. + --cid -c : The custom connector ID. + --env -e : Power Platform environment GUID. + --icon : Location for the icon file. + --script -x : Location for the script file. + --pau -u : Power Platform URL. + --pav -v : Power Platform API version. + --secret -r : The OAuth2 client secret for the connector. + --settings -s : A settings file containing required parameters. + When a settings file is specified some command + line parameters are ignored. + ``` + +### Validate a Swagger JSON or C# Script + +The validate operation takes either a swagger file or a C# script file and verifies if it follows all the recommended rules. + +**Validate a swagger file:** + +`paconn validate --api-def [Path to apiDefinition.swagger.json]` + +**Validate a C# script file (always in strict mode):** + +`paconn validate --script [Path to script.csx]` + +**Using settings file:** + +`paconn validate -s [Path to settings.json]` + +Note: You can validate either a swagger file OR a script file, but not both simultaneously. + +The command will print the error, warning, or success message depending on the result of the validation. + +**C# Script Validation Features:** +- Validates required Script class inheritance from ScriptBase +- Checks for mandatory ExecuteAsync method implementation +- Ensures only allowed namespaces are used (21 specific namespaces) +- Validates file size (1MB limit) +- Checks for best practices (ConfigureAwait, Context.SendAsync usage) +- Always runs in strict mode with comprehensive security checks + +``` +Arguments + --api-def : Location for the Open API definition JSON document. + Cannot be used with --script. + --script -x : Location for the C# script file (.csx) to validate. + Validation is always performed in strict mode. + Cannot be used with --api-def. + --pau -u : Power Platform URL. + --pav -v : Power Platform API version. + --settings -s : A settings file containing required parameters. + Settings file must contain either 'apiDefinition' OR 'script', not both. + ``` + + +### Best Practice + +Download all of your connectors and use git or any other source code management system to save the files. In case of an incorrect update, redeploy the connector by rerunning the update command with the correct set of files from the source code management system. + +Please test the custom connector and the settings file in a test environment before deploying in the production environment. Always double check that the environment and connector id are correct. + +## Limitations + +The project is limited to creation, update, and download of a custom connector in the Power Automate and Power Apps environment. When an environment isn't specified, only the Power Automate environments are listed to choose from. For a non-custom connector, the swagger file isn't returned. + +**Stack Owner Property & apiProperties file:** + +Currently, there is a limitation that prevents you from updating your connector's artificats in your environment using Paconn when the `stackOwner` property is present in your `apiProperties.json` file. As a workaround to this, create two versions of your connector artifacts: the first being the version that is submitted to certification and contains the `stackOwner` property, the second having the `stackOwner` property omitted to enable updating within your environment. We are working to remove the limitation and will update this section once complete. + +## Reporting issues and feedback + +If you encounter any bugs with the tool, submit an issue in the [Issues](https://github.com/Microsoft/PowerPlatformConnectors/issues) section of our GitHub repo. + +If you believe you have found a security vulnerability that meets [Microsoft's definition of a security vulnerability](https://docs.microsoft.com/previous-versions/tn-archive/cc751383%28v=technet.10%29), submit a [report to MSRC](https://msrc.microsoft.com/create-report). More information can be found at [MSRC frequently asked questions on reporting](https://www.microsoft.com/msrc/faqs-report-an-issue). + + +## Contributing + +This project welcomes contributions and suggestions. Most contributions require you to agree to a +Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us +the rights to use your contribution. For details, visit https://cla.microsoft.com. + +To contibute a connector to the open source repo, please start by creating a fork on the github repo. +Once you have the fork created, create a new branch on the forked repo. Clone this forked repo on you +local machine, and checkout the branch. Create a folder for your connector under the `connectors` folder +and place the connector files in the sub-folder. Commit and push the changes to your forked branch. +Create a pull request from the forked branch to the main repo to merge your changes into the main repo. +[Please see this document for more information](https://github.com/CoolProp/CoolProp/wiki/Contributing:-git-development-workflow). + +When you submit a pull request, a CLA-bot will automatically determine whether you need to provide +a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions +provided by the bot. You will only need to do this once across all repos using our CLA. + +This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). +For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or +contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments. + +## Legal Notices + +Microsoft and any contributors grant you a license to the Microsoft documentation and other content +in this repository under the [Creative Commons Attribution 4.0 International Public License](https://creativecommons.org/licenses/by/4.0/legalcode), +see the [LICENSE](LICENSE) file, and grant you a license to any code in the repository under the [MIT License](https://opensource.org/licenses/MIT), see the +[LICENSE-CODE](LICENSE-CODE) file. + +Microsoft, Windows, Microsoft Azure and/or other Microsoft products and services referenced in the documentation +may be either trademarks or registered trademarks of Microsoft in the United States and/or other countries. +The licenses for this project do not grant you rights to use any Microsoft names, logos, or trademarks. +Microsoft's general trademark guidelines can be found at http://go.microsoft.com/fwlink/?LinkID=254653. + +Privacy information can be found at https://privacy.microsoft.com/en-us/ + +Microsoft and any contributors reserve all others rights, whether under their respective copyrights, patents, +or trademarks, whether by implication, estoppel or otherwise. + +## License + +``` +Microsoft Power Platform Connectors CLI (paconn) + +Copyright (c) Microsoft Corporation +All rights reserved. + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +``` + diff --git a/tools/paconn-cli/UPLOAD_GUIDE.md b/tools/paconn-cli/UPLOAD_GUIDE.md new file mode 100644 index 0000000000..7d07fcfcc7 --- /dev/null +++ b/tools/paconn-cli/UPLOAD_GUIDE.md @@ -0,0 +1,29 @@ +# File Structure for Repository Upload + +``` +paconn-cli-changes/ +├── setup.py # MODIFIED: Version update + new dependency +├── README.md # MODIFIED: Enhanced documentation +├── CHANGES.md # NEW: Summary of all changes +└── paconn/ + ├── __init__.py # MODIFIED: Version update + ├── commands/ + │ ├── validate.py # MODIFIED: Added script validation + │ └── params.py # MODIFIED: Added --script parameter + └── operations/ + └── script_validate.py # NEW: C# script validation engine +``` + +## Upload Instructions + +1. **Copy these files to your repository** maintaining the exact folder structure +2. **Replace existing files** where marked as MODIFIED +3. **Add new files** where marked as NEW +4. **Commit with version tag**: v0.1.0 + +## Key Changes Summary +- **New Feature**: C# script validation for Power Platform connectors +- **Version**: 0.0.21 → 0.1.0 +- **Always Strict**: Script validation enforces all security and best practices +- **Mutual Exclusion**: Can validate either swagger OR script, not both +- **Professional Output**: Consistent formatting across all validation types \ No newline at end of file diff --git a/tools/paconn-cli/VERIFICATION.md b/tools/paconn-cli/VERIFICATION.md new file mode 100644 index 0000000000..d32a137ce7 --- /dev/null +++ b/tools/paconn-cli/VERIFICATION.md @@ -0,0 +1,45 @@ +# Pre-Upload Verification Checklist + +## ✅ All Modified Files Included + +### Root Level (2 files) +- [x] setup.py - Version 0.1.0, added regex dependency +- [x] README.md - Updated validate command documentation + +### paconn/ Directory (1 file) +- [x] __init__.py - Version 0.1.0 + +### paconn/commands/ Directory (2 files) +- [x] validate.py - Added script validation with mutual exclusion +- [x] params.py - Added --script parameter + +### paconn/operations/ Directory (1 file) +- [x] script_validate.py - NEW: Complete C# validation engine + +## ✅ Documentation Files +- [x] CHANGES.md - Comprehensive change summary +- [x] UPLOAD_GUIDE.md - Repository upload instructions +- [x] VERIFICATION.md - This checklist + +## ✅ Version Consistency +- [x] setup.py: __VERSION__ = '0.1.0' +- [x] paconn/__init__.py: __VERSION__ = '0.1.0' + +## ✅ New Features Verified +- [x] Script validation works independently +- [x] API definition validation still works +- [x] Mutual exclusion enforced +- [x] Settings file support +- [x] Professional output formatting +- [x] Help text updated +- [x] Version command shows 0.1.0 + +## 🚀 Ready for Repository Upload +This folder contains ONLY the changed files in their correct repository structure. +No unchanged files are included to avoid unnecessary commits. + +**Next Steps:** +1. Copy files to repository maintaining folder structure +2. Commit with message: "Add C# script validation v0.1.0" +3. Tag release: v0.1.0 +4. Test package build and publish to PyPI \ No newline at end of file diff --git a/tools/paconn-cli/paconn/__init__.py b/tools/paconn-cli/paconn/__init__.py index d40051dda6..2f7ecb3f05 100644 --- a/tools/paconn-cli/paconn/__init__.py +++ b/tools/paconn-cli/paconn/__init__.py @@ -1,22 +1,22 @@ -# ----------------------------------------------------------------------------- -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. See License.txt in the project root for -# license information. -# ----------------------------------------------------------------------------- - -""" -Initializer -""" - -__VERSION__ = '0.0.21' -__CLI_NAME__ = 'paconn' - -# Commands -_COMMAND_GROUP = '' -_LOGIN = 'login' -_LOGOUT = 'logout' -_DOWNLOAD = 'download' -_CREATE = 'create' -_UPDATE = 'update' -_VALIDATE = 'validate' -_CONVERT = 'convert' +# ----------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# ----------------------------------------------------------------------------- + +""" +Initializer +""" + +__VERSION__ = '0.1.0' +__CLI_NAME__ = 'paconn' + +# Commands +_COMMAND_GROUP = '' +_LOGIN = 'login' +_LOGOUT = 'logout' +_DOWNLOAD = 'download' +_CREATE = 'create' +_UPDATE = 'update' +_VALIDATE = 'validate' +_CONVERT = 'convert' diff --git a/tools/paconn-cli/paconn/commands/params.py b/tools/paconn-cli/paconn/commands/params.py index e93df34d46..1a14b4f829 100644 --- a/tools/paconn-cli/paconn/commands/params.py +++ b/tools/paconn-cli/paconn/commands/params.py @@ -1,327 +1,333 @@ -# ----------------------------------------------------------------------------- -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. See License.txt in the project root for -# license information. -# ----------------------------------------------------------------------------- - -""" -CLI parameter definitions -""" - -from knack.arguments import ArgumentsContext -from paconn import _LOGIN, _DOWNLOAD, _CREATE, _UPDATE, _VALIDATE, _CONVERT - -CLIENT_SECRET = 'client_secret' -CLIENT_SECRET_OPTIONS = ['--secret', '-r'] -CLIENT_SECRET_HELP = 'The OAuth2 client secret for the connector.' - -ENVIRONMENT = 'environment' -ENVIRONMENT_OPTIONS = ['--env', '-e'] -ENVIRONMENT_HELP = 'Power Platform environment ID.' - -CONNECTOR_ID = 'connector_id' -CONNECTOR_ID_OPTIONS = ['--cid', '-c'] -CONNECTOR_ID_HELP = 'The custom connector ID.' - -POWERAPPS_URL = 'powerapps_url' -POWERAPPS_URL_OPTIONS = ['--pau', '-u'] -POWERAPPS_URL_HELP = 'Power Platform URL.' - -POWERAPPS_VERSION = 'powerapps_version' -POWERAPPS_VERSION_OPTIONS = ['--pav', '-v'] -POWERAPPS_VERSION_HELP = 'Power Platform api version.' - -SETTINGS = 'settings_file' -SETTINGS_OPTIONS = ['--settings', '-s'] -SETTINGS_HELP = 'A settings file containing required parameters. When a settings file is specified some commandline parameters are ignored.' # noqa: E501 - -API_PROPERTIES = 'api_properties' -API_PROPERTIES_OPTIONS = ['--api-prop', '-p'] -API_PROPERTIES_HELP = 'Location of the API properties JSON document.' - -API_DEFINITION = 'api_definition' -API_DEFINITION_OPTIONS = ['--api-def', '-d'] -API_DEFINITION_HELP = 'Location of the Open API definition JSON document.' - -ICON = 'icon' -ICON_OPTIONS = ['--icon', '-i'] -ICON_HELP = 'Location for the icon file.' - -SCRIPT = 'script' -SCRIPT_OPTIONS = ['--script', '-x'] -SCRIPT_HELP = 'Location for the script file.' - -OPENAPI_FILE = 'openapi_file' -OPENAPI_FILE_OPTIONS = ['--openapi', '-api'] -OPENAPI_FILE_HELP = 'Location of the OpenAPI 3.0 definition file to convert.' - -DESTINATION = 'destination' -DESTINATION_OPTIONS = ['--dest', '-dst'] -DESTINATION_HELP = 'Destination directory for the converted connector files.' - - -# pylint: disable=unused-argument -def load_arguments(self, command): - """ - Load command line arguments - """ - with ArgumentsContext(self, _LOGIN) as arg_context: - arg_context.argument( - 'client_id', - options_list=['--clid', '-i'], - type=str, - required=False, - help='The client ID.') - arg_context.argument( - 'tenant', - options_list=['--tenant', '-t'], - type=str, - required=False, - help='The tenant.') - arg_context.argument( - 'authority_url', - options_list=['--authority_url', '-a'], - type=str, - required=False, - help='Authority URL for login.') - arg_context.argument( - 'resource', - options_list=['--resource', '-r'], - type=str, - required=False, - help='Resource URL for login.') - arg_context.argument( - SETTINGS, - options_list=SETTINGS_OPTIONS, - type=str, - required=False, - help=SETTINGS_HELP) - arg_context.argument( - 'force', - options_list=['--force', '-f'], - type=bool, - required=False, - nargs='?', - default=False, - const=True, - help='Override a previous login, if exists.') - - with ArgumentsContext(self, _DOWNLOAD) as arg_context: - arg_context.argument( - ENVIRONMENT, - options_list=ENVIRONMENT_OPTIONS, - type=str, - required=False, - help=ENVIRONMENT_HELP) - arg_context.argument( - CONNECTOR_ID, - options_list=CONNECTOR_ID_OPTIONS, - type=str, - required=False, - help=CONNECTOR_ID_HELP) - arg_context.argument( - 'destination', - options_list=['--dest', '-d'], - type=str, - required=False, - help='Destination directory. Non-existent directories will be created.') - arg_context.argument( - POWERAPPS_URL, - options_list=POWERAPPS_URL_OPTIONS, - type=str, - required=False, - help=POWERAPPS_URL_HELP) - arg_context.argument( - POWERAPPS_VERSION, - options_list=POWERAPPS_VERSION_OPTIONS, - type=str, - required=False, - help=POWERAPPS_VERSION_HELP) - arg_context.argument( - SETTINGS, - options_list=SETTINGS_OPTIONS, - type=str, - required=False, - help=SETTINGS_HELP) - arg_context.argument( - 'overwrite', - options_list=['--overwrite', '-w'], - type=bool, - required=False, - nargs='?', - default=False, - const=True, - help='Overwrite all the existing connector and settings files.') - - with ArgumentsContext(self, _CREATE) as arg_context: - arg_context.argument( - ENVIRONMENT, - options_list=ENVIRONMENT_OPTIONS, - type=str, - required=False, - help=ENVIRONMENT_HELP) - arg_context.argument( - API_PROPERTIES, - options_list=API_PROPERTIES_OPTIONS, - type=str, - required=False, - help=API_PROPERTIES_HELP) - arg_context.argument( - API_DEFINITION, - options_list=API_DEFINITION_OPTIONS, - type=str, - required=False, - help=API_DEFINITION_HELP) - arg_context.argument( - ICON, - options_list=ICON_OPTIONS, - type=str, - required=False, - help=ICON_HELP) - arg_context.argument( - SCRIPT, - options_list=SCRIPT_OPTIONS, - type=str, - required=False, - help=SCRIPT_HELP) - arg_context.argument( - POWERAPPS_URL, - options_list=POWERAPPS_URL_OPTIONS, - type=str, - required=False, - help=POWERAPPS_URL_HELP) - arg_context.argument( - POWERAPPS_VERSION, - options_list=POWERAPPS_VERSION_OPTIONS, - type=str, - required=False, - help=POWERAPPS_VERSION_HELP) - arg_context.argument( - CLIENT_SECRET, - options_list=CLIENT_SECRET_OPTIONS, - type=str, - required=False, - help=CLIENT_SECRET_HELP) - arg_context.argument( - SETTINGS, - options_list=SETTINGS_OPTIONS, - type=str, - required=False, - help=SETTINGS_HELP) - arg_context.argument( - 'overwrite_settings', - options_list=['--overwrite-settings', '-w'], - type=bool, - required=False, - nargs='?', - default=False, - const=True, - help='Overwrite the existing settings file.') - - with ArgumentsContext(self, _UPDATE) as arg_context: - arg_context.argument( - ENVIRONMENT, - options_list=ENVIRONMENT_OPTIONS, - type=str, - required=False, - help=ENVIRONMENT_HELP) - arg_context.argument( - API_PROPERTIES, - options_list=API_PROPERTIES_OPTIONS, - type=str, - required=False, - help=API_PROPERTIES_HELP) - arg_context.argument( - API_DEFINITION, - options_list=API_DEFINITION_OPTIONS, - type=str, - required=False, - help=API_DEFINITION_HELP) - arg_context.argument( - ICON, - options_list=ICON_OPTIONS, - type=str, - required=False, - help=ICON_HELP) - arg_context.argument( - SCRIPT, - options_list=SCRIPT_OPTIONS, - type=str, - required=False, - help=SCRIPT_HELP) - arg_context.argument( - CONNECTOR_ID, - options_list=CONNECTOR_ID_OPTIONS, - type=str, - required=False, - help=CONNECTOR_ID_HELP) - arg_context.argument( - POWERAPPS_URL, - options_list=POWERAPPS_URL_OPTIONS, - type=str, - required=False, - help=POWERAPPS_URL_HELP) - arg_context.argument( - POWERAPPS_VERSION, - options_list=POWERAPPS_VERSION_OPTIONS, - type=str, - required=False, - help=POWERAPPS_VERSION_HELP) - arg_context.argument( - CLIENT_SECRET, - options_list=CLIENT_SECRET_OPTIONS, - type=str, - required=False, - help=CLIENT_SECRET_HELP) - arg_context.argument( - SETTINGS, - options_list=SETTINGS_OPTIONS, - type=str, - required=False, - help=SETTINGS_HELP) - - with ArgumentsContext(self, _VALIDATE) as arg_context: - arg_context.argument( - API_DEFINITION, - options_list=API_DEFINITION_OPTIONS, - type=str, - required=False, - help=API_DEFINITION_HELP) - arg_context.argument( - POWERAPPS_URL, - options_list=POWERAPPS_URL_OPTIONS, - type=str, - required=False, - help=POWERAPPS_URL_HELP) - arg_context.argument( - POWERAPPS_VERSION, - options_list=POWERAPPS_VERSION_OPTIONS, - type=str, - required=False, - help=POWERAPPS_VERSION_HELP) - arg_context.argument( - SETTINGS, - options_list=SETTINGS_OPTIONS, - type=str, - required=False, - help=SETTINGS_HELP) - - with ArgumentsContext(self, _CONVERT) as arg_context: - arg_context.argument( - OPENAPI_FILE, - options_list=OPENAPI_FILE_OPTIONS, - type=str, - required=False, - help=OPENAPI_FILE_HELP) - arg_context.argument( - DESTINATION, - options_list=DESTINATION_OPTIONS, - type=str, - required=False, - help=DESTINATION_HELP) - arg_context.argument( - SETTINGS, - options_list=SETTINGS_OPTIONS, - type=str, - required=False, - help=SETTINGS_HELP) +# ----------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# ----------------------------------------------------------------------------- + +""" +CLI parameter definitions +""" + +from knack.arguments import ArgumentsContext +from paconn import _LOGIN, _DOWNLOAD, _CREATE, _UPDATE, _VALIDATE, _CONVERT + +CLIENT_SECRET = 'client_secret' +CLIENT_SECRET_OPTIONS = ['--secret', '-r'] +CLIENT_SECRET_HELP = 'The OAuth2 client secret for the connector.' + +ENVIRONMENT = 'environment' +ENVIRONMENT_OPTIONS = ['--env', '-e'] +ENVIRONMENT_HELP = 'Power Platform environment ID.' + +CONNECTOR_ID = 'connector_id' +CONNECTOR_ID_OPTIONS = ['--cid', '-c'] +CONNECTOR_ID_HELP = 'The custom connector ID.' + +POWERAPPS_URL = 'powerapps_url' +POWERAPPS_URL_OPTIONS = ['--pau', '-u'] +POWERAPPS_URL_HELP = 'Power Platform URL.' + +POWERAPPS_VERSION = 'powerapps_version' +POWERAPPS_VERSION_OPTIONS = ['--pav', '-v'] +POWERAPPS_VERSION_HELP = 'Power Platform api version.' + +SETTINGS = 'settings_file' +SETTINGS_OPTIONS = ['--settings', '-s'] +SETTINGS_HELP = 'A settings file containing required parameters. When a settings file is specified some commandline parameters are ignored.' # noqa: E501 + +API_PROPERTIES = 'api_properties' +API_PROPERTIES_OPTIONS = ['--api-prop', '-p'] +API_PROPERTIES_HELP = 'Location of the API properties JSON document.' + +API_DEFINITION = 'api_definition' +API_DEFINITION_OPTIONS = ['--api-def', '-d'] +API_DEFINITION_HELP = 'Location of the Open API definition JSON document.' + +ICON = 'icon' +ICON_OPTIONS = ['--icon', '-i'] +ICON_HELP = 'Location for the icon file.' + +SCRIPT = 'script' +SCRIPT_OPTIONS = ['--script', '-x'] +SCRIPT_HELP = 'Location for the script file.' + +OPENAPI_FILE = 'openapi_file' +OPENAPI_FILE_OPTIONS = ['--openapi', '-api'] +OPENAPI_FILE_HELP = 'Location of the OpenAPI 3.0 definition file to convert.' + +DESTINATION = 'destination' +DESTINATION_OPTIONS = ['--dest', '-dst'] +DESTINATION_HELP = 'Destination directory for the converted connector files.' + + +# pylint: disable=unused-argument +def load_arguments(self, command): + """ + Load command line arguments + """ + with ArgumentsContext(self, _LOGIN) as arg_context: + arg_context.argument( + 'client_id', + options_list=['--clid', '-i'], + type=str, + required=False, + help='The client ID.') + arg_context.argument( + 'tenant', + options_list=['--tenant', '-t'], + type=str, + required=False, + help='The tenant.') + arg_context.argument( + 'authority_url', + options_list=['--authority_url', '-a'], + type=str, + required=False, + help='Authority URL for login.') + arg_context.argument( + 'resource', + options_list=['--resource', '-r'], + type=str, + required=False, + help='Resource URL for login.') + arg_context.argument( + SETTINGS, + options_list=SETTINGS_OPTIONS, + type=str, + required=False, + help=SETTINGS_HELP) + arg_context.argument( + 'force', + options_list=['--force', '-f'], + type=bool, + required=False, + nargs='?', + default=False, + const=True, + help='Override a previous login, if exists.') + + with ArgumentsContext(self, _DOWNLOAD) as arg_context: + arg_context.argument( + ENVIRONMENT, + options_list=ENVIRONMENT_OPTIONS, + type=str, + required=False, + help=ENVIRONMENT_HELP) + arg_context.argument( + CONNECTOR_ID, + options_list=CONNECTOR_ID_OPTIONS, + type=str, + required=False, + help=CONNECTOR_ID_HELP) + arg_context.argument( + 'destination', + options_list=['--dest', '-d'], + type=str, + required=False, + help='Destination directory. Non-existent directories will be created.') + arg_context.argument( + POWERAPPS_URL, + options_list=POWERAPPS_URL_OPTIONS, + type=str, + required=False, + help=POWERAPPS_URL_HELP) + arg_context.argument( + POWERAPPS_VERSION, + options_list=POWERAPPS_VERSION_OPTIONS, + type=str, + required=False, + help=POWERAPPS_VERSION_HELP) + arg_context.argument( + SETTINGS, + options_list=SETTINGS_OPTIONS, + type=str, + required=False, + help=SETTINGS_HELP) + arg_context.argument( + 'overwrite', + options_list=['--overwrite', '-w'], + type=bool, + required=False, + nargs='?', + default=False, + const=True, + help='Overwrite all the existing connector and settings files.') + + with ArgumentsContext(self, _CREATE) as arg_context: + arg_context.argument( + ENVIRONMENT, + options_list=ENVIRONMENT_OPTIONS, + type=str, + required=False, + help=ENVIRONMENT_HELP) + arg_context.argument( + API_PROPERTIES, + options_list=API_PROPERTIES_OPTIONS, + type=str, + required=False, + help=API_PROPERTIES_HELP) + arg_context.argument( + API_DEFINITION, + options_list=API_DEFINITION_OPTIONS, + type=str, + required=False, + help=API_DEFINITION_HELP) + arg_context.argument( + ICON, + options_list=ICON_OPTIONS, + type=str, + required=False, + help=ICON_HELP) + arg_context.argument( + SCRIPT, + options_list=SCRIPT_OPTIONS, + type=str, + required=False, + help=SCRIPT_HELP) + arg_context.argument( + POWERAPPS_URL, + options_list=POWERAPPS_URL_OPTIONS, + type=str, + required=False, + help=POWERAPPS_URL_HELP) + arg_context.argument( + POWERAPPS_VERSION, + options_list=POWERAPPS_VERSION_OPTIONS, + type=str, + required=False, + help=POWERAPPS_VERSION_HELP) + arg_context.argument( + CLIENT_SECRET, + options_list=CLIENT_SECRET_OPTIONS, + type=str, + required=False, + help=CLIENT_SECRET_HELP) + arg_context.argument( + SETTINGS, + options_list=SETTINGS_OPTIONS, + type=str, + required=False, + help=SETTINGS_HELP) + arg_context.argument( + 'overwrite_settings', + options_list=['--overwrite-settings', '-w'], + type=bool, + required=False, + nargs='?', + default=False, + const=True, + help='Overwrite the existing settings file.') + + with ArgumentsContext(self, _UPDATE) as arg_context: + arg_context.argument( + ENVIRONMENT, + options_list=ENVIRONMENT_OPTIONS, + type=str, + required=False, + help=ENVIRONMENT_HELP) + arg_context.argument( + API_PROPERTIES, + options_list=API_PROPERTIES_OPTIONS, + type=str, + required=False, + help=API_PROPERTIES_HELP) + arg_context.argument( + API_DEFINITION, + options_list=API_DEFINITION_OPTIONS, + type=str, + required=False, + help=API_DEFINITION_HELP) + arg_context.argument( + ICON, + options_list=ICON_OPTIONS, + type=str, + required=False, + help=ICON_HELP) + arg_context.argument( + SCRIPT, + options_list=SCRIPT_OPTIONS, + type=str, + required=False, + help=SCRIPT_HELP) + arg_context.argument( + CONNECTOR_ID, + options_list=CONNECTOR_ID_OPTIONS, + type=str, + required=False, + help=CONNECTOR_ID_HELP) + arg_context.argument( + POWERAPPS_URL, + options_list=POWERAPPS_URL_OPTIONS, + type=str, + required=False, + help=POWERAPPS_URL_HELP) + arg_context.argument( + POWERAPPS_VERSION, + options_list=POWERAPPS_VERSION_OPTIONS, + type=str, + required=False, + help=POWERAPPS_VERSION_HELP) + arg_context.argument( + CLIENT_SECRET, + options_list=CLIENT_SECRET_OPTIONS, + type=str, + required=False, + help=CLIENT_SECRET_HELP) + arg_context.argument( + SETTINGS, + options_list=SETTINGS_OPTIONS, + type=str, + required=False, + help=SETTINGS_HELP) + + with ArgumentsContext(self, _VALIDATE) as arg_context: + arg_context.argument( + API_DEFINITION, + options_list=API_DEFINITION_OPTIONS, + type=str, + required=False, + help=API_DEFINITION_HELP) + arg_context.argument( + SCRIPT, + options_list=SCRIPT_OPTIONS, + type=str, + required=False, + help='Location for the C# script file (.csx) to validate. Cannot be used with --api-def.') + arg_context.argument( + POWERAPPS_URL, + options_list=POWERAPPS_URL_OPTIONS, + type=str, + required=False, + help=POWERAPPS_URL_HELP) + arg_context.argument( + POWERAPPS_VERSION, + options_list=POWERAPPS_VERSION_OPTIONS, + type=str, + required=False, + help=POWERAPPS_VERSION_HELP) + arg_context.argument( + SETTINGS, + options_list=SETTINGS_OPTIONS, + type=str, + required=False, + help=SETTINGS_HELP) + + with ArgumentsContext(self, _CONVERT) as arg_context: + arg_context.argument( + OPENAPI_FILE, + options_list=OPENAPI_FILE_OPTIONS, + type=str, + required=False, + help=OPENAPI_FILE_HELP) + arg_context.argument( + DESTINATION, + options_list=DESTINATION_OPTIONS, + type=str, + required=False, + help=DESTINATION_HELP) + arg_context.argument( + SETTINGS, + options_list=SETTINGS_OPTIONS, + type=str, + required=False, + help=SETTINGS_HELP) diff --git a/tools/paconn-cli/paconn/commands/validate.py b/tools/paconn-cli/paconn/commands/validate.py index 56653065e9..349fc2942e 100644 --- a/tools/paconn-cli/paconn/commands/validate.py +++ b/tools/paconn-cli/paconn/commands/validate.py @@ -1,50 +1,93 @@ -# ----------------------------------------------------------------------------- -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. See License.txt in the project root for -# license information. -# ----------------------------------------------------------------------------- -""" -Validate command. -""" - -from paconn import _VALIDATE - -from paconn.common.util import display -from paconn.settings.util import load_powerapps_and_flow_rp -from paconn.settings.settingsbuilder import SettingsBuilder - -import paconn.operations.validate - - -def validate( - api_definition, - powerapps_url, - powerapps_version, - settings_file): - """ - Validate command. - """ - # Get settings - settings = SettingsBuilder.get_settings( - environment=None, - settings_file=settings_file, - api_properties=None, - api_definition=api_definition, - icon=None, - script=None, - connector_id=None, - powerapps_url=powerapps_url, - powerapps_version=powerapps_version) - - powerapps_rp, _ = load_powerapps_and_flow_rp( - settings=settings, - command_context=_VALIDATE) - - result = paconn.operations.validate.validate( - powerapps_rp=powerapps_rp, - settings=settings) - - if result: - display(result) - else: - display('{} validated successfully.'.format(settings.api_definition)) +# ----------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# ----------------------------------------------------------------------------- +""" +Validate command. +""" + +from paconn import _VALIDATE + +from paconn.common.util import display +from paconn.settings.util import load_powerapps_and_flow_rp +from paconn.settings.settingsbuilder import SettingsBuilder + +import paconn.operations.validate +import paconn.operations.script_validate + + +def validate( + api_definition, + script, + powerapps_url, + powerapps_version, + settings_file): + """ + Validate command - supports either API definition OR script validation (mutually exclusive). + """ + + # Get settings first + settings = SettingsBuilder.get_settings( + environment=None, + settings_file=settings_file, + api_properties=None, + api_definition=api_definition, + icon=None, + script=script, + connector_id=None, + powerapps_url=powerapps_url, + powerapps_version=powerapps_version) + + # Check for mutual exclusion after settings are loaded + has_api_def = settings.api_definition is not None + has_script = settings.script is not None + + if has_api_def and has_script: + display("ERROR: Cannot specify both api_definition and script. Choose one validation type.") + return + + if not has_api_def and not has_script: + display("ERROR: Must specify either api_definition or script for validation.") + return + + if has_api_def: + # Existing OpenAPI validation path + powerapps_rp, _ = load_powerapps_and_flow_rp(settings=settings, command_context=_VALIDATE) + result = paconn.operations.validate.validate(powerapps_rp=powerapps_rp, settings=settings) + + if result: + display(result) + else: + display('{} validated successfully.'.format(settings.api_definition)) + + elif has_script: + # New script validation path (always strict) + result = paconn.operations.script_validate.validate_script(settings.script) + + if result.has_errors: + # Format errors similar to API validation output + error_output = f"Script validation failed for {settings.script}:\n\n" + error_output += "Errors:\n" + error_output += result.format_errors() + error_output += f"\n\nResult: Validation failed. Please fix the errors above." + display(error_output) + elif result.has_warnings: + # Format warnings similar to API validation output + warning_output = f"Script validation completed with warnings for {settings.script}:\n\n" + warning_output += "Warnings:\n" + warning_output += result.format_warnings() + warning_output += f"\n\nResult: {settings.script} validated successfully with warnings." + display(warning_output) + else: + # Format success similar to API validation output + success_output = f"Script validation successful for {settings.script}:\n\n" + success_output += "Validation Summary:\n" + success_output += "✓ Required Script class structure validated\n" + success_output += "✓ ExecuteAsync method signature validated\n" + success_output += "✓ Namespace usage validated (26 approved namespaces)\n" + success_output += "✓ Security constraints validated\n" + success_output += "✓ File size within limits (1MB max)\n" + success_output += "✓ Best practices checked\n" + success_output += f"\nResult: {settings.script} validated successfully." + display(success_output) diff --git a/tools/paconn-cli/paconn/operations/script_validate.py b/tools/paconn-cli/paconn/operations/script_validate.py new file mode 100644 index 0000000000..00b8a4d9a9 --- /dev/null +++ b/tools/paconn-cli/paconn/operations/script_validate.py @@ -0,0 +1,192 @@ +# ----------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# ----------------------------------------------------------------------------- + +""" +C# Script validation for Power Platform connectors. +Always runs in strict mode. +""" + +import os +import re +from dataclasses import dataclass +from typing import List, Optional + + +@dataclass +class ValidationResult: + """Results from script validation""" + errors: List[str] + warnings: List[str] + file_path: str + + @property + def has_errors(self) -> bool: + return len(self.errors) > 0 + + @property + def has_warnings(self) -> bool: + return len(self.warnings) > 0 + + def format_errors(self) -> str: + """Format errors for display""" + if not self.errors: + return "" + formatted = [] + for i, error in enumerate(self.errors, 1): + formatted.append(f" Error {i}: {error}") + return "\n".join(formatted) + + def format_warnings(self) -> str: + """Format warnings for display""" + if not self.warnings: + return "" + formatted = [] + for i, warning in enumerate(self.warnings, 1): + formatted.append(f" Warning {i}: {warning}") + return "\n".join(formatted) + + +class CSharpScriptValidator: + """ + Validates C# script files for Power Platform connectors. + Always runs in strict mode with comprehensive checks. + """ + + # Allowed namespaces from Microsoft documentation + ALLOWED_NAMESPACES = { + 'System', + 'System.Collections', + 'System.Collections.Generic', + 'System.Diagnostics', + 'System.IO', + 'System.IO.Compression', + 'System.Linq', + 'System.Net', + 'System.Net.Http', + 'System.Net.Http.Headers', + 'System.Net.Security', + 'System.Security.Authentication', + 'System.Security.Cryptography', + 'System.Text', + 'System.Text.RegularExpressions', + 'System.Threading', + 'System.Threading.Tasks', + 'System.Web', + 'System.Xml', + 'System.Xml.Linq', + 'System.Drawing', + 'System.Drawing.Drawing2D', + 'System.Drawing.Imaging', + 'Microsoft.Extensions.Logging', + 'Newtonsoft.Json', + 'Newtonsoft.Json.Linq' + } + + MAX_FILE_SIZE = 1048576 # 1MB in bytes + + def validate_script(self, script_path: str) -> ValidationResult: + """ + Validate a C# script file (always strict mode). + """ + errors = [] + warnings = [] + + # File existence and basic checks + if not os.path.exists(script_path): + errors.append(f"Script file not found: {script_path}") + return ValidationResult(errors, warnings, script_path) + + if not script_path.lower().endswith('.csx'): + errors.append(f"Script file must have .csx extension: {script_path}") + + # File size check + file_size = os.path.getsize(script_path) + if file_size > self.MAX_FILE_SIZE: + errors.append(f"Script file size ({file_size} bytes) exceeds 1MB limit") + + # Read and validate content + try: + with open(script_path, 'r', encoding='utf-8') as file: + content = file.read() + except Exception as e: + errors.append(f"Failed to read script file: {e}") + return ValidationResult(errors, warnings, script_path) + + # Content validations + self._validate_namespaces(content, errors) + self._validate_class_structure(content, errors) + self._validate_execute_async_method(content, errors) + self._validate_best_practices(content, warnings) + self._validate_security_patterns(content, errors, warnings) + + return ValidationResult(errors, warnings, script_path) + + def _validate_namespaces(self, content: str, errors: List[str]): + """Validate using statements against allowed namespaces""" + using_pattern = r'^\s*using\s+([^;]+);' + using_statements = re.findall(using_pattern, content, re.MULTILINE) + + for using_stmt in using_statements: + namespace = using_stmt.strip() + if namespace not in self.ALLOWED_NAMESPACES: + errors.append(f"Namespace '{namespace}' is not allowed. Use only the 26 approved namespaces for Power Platform connectors.") + + def _validate_class_structure(self, content: str, errors: List[str]): + """Validate Script class structure""" + # Check for Script class + class_pattern = r'public\s+class\s+Script\s*:\s*ScriptBase' + if not re.search(class_pattern, content): + errors.append("Missing required 'public class Script : ScriptBase' declaration") + + def _validate_execute_async_method(self, content: str, errors: List[str]): + """Validate ExecuteAsync method signature""" + method_pattern = r'public\s+override\s+async\s+Task\s+ExecuteAsync\s*\(\s*\)' + if not re.search(method_pattern, content): + errors.append("Missing required 'public override async Task ExecuteAsync()' method") + + def _validate_best_practices(self, content: str, warnings: List[str]): + """Check for best practices (strict mode warnings)""" + # Check for ConfigureAwait(false) + await_lines = [line for line in content.split('\n') if 'await ' in line] + for line in await_lines: + if 'await ' in line and 'ConfigureAwait(false)' not in line: + warnings.append("Consider using '.ConfigureAwait(false)' with await statements for better performance") + break # Only warn once + + # Check for proper OperationId handling + if 'Context.OperationId' in content and 'base64' not in content.lower(): + warnings.append("Consider implementing base64 decoding for OperationId to handle regional differences") + + # Check for CreateJsonContent usage + if 'new JObject' in content and 'CreateJsonContent' not in content: + warnings.append("Consider using 'CreateJsonContent()' helper method for JSON responses") + + def _validate_security_patterns(self, content: str, errors: List[str], warnings: List[str]): + """Validate security patterns and practices""" + # Check for direct HttpClient usage + if re.search(r'new\s+HttpClient\s*\(', content): + warnings.append("Consider using 'this.Context.SendAsync' instead of direct HttpClient instantiation") + + # Check for proper Context.SendAsync usage + if 'HttpClient' in content and 'Context.SendAsync' not in content: + warnings.append("Use 'this.Context.SendAsync' for HTTP requests instead of direct HttpClient") + + # Check for potentially unsafe operations + if re.search(r'File\.|Directory\.|Path\.', content): + errors.append("File system operations are not allowed in connector scripts") + + # Check for network operations outside of Context.SendAsync + if re.search(r'Socket|TcpClient|UdpClient', content): + errors.append("Direct network operations are not allowed. Use Context.SendAsync for HTTP requests") + + +def validate_script(script_path: str) -> ValidationResult: + """ + Public function to validate a C# script file. + Always runs in strict mode. + """ + validator = CSharpScriptValidator() + return validator.validate_script(script_path) \ No newline at end of file diff --git a/tools/paconn-cli/setup.py b/tools/paconn-cli/setup.py index 289f51a277..32e2ed7239 100644 --- a/tools/paconn-cli/setup.py +++ b/tools/paconn-cli/setup.py @@ -1,73 +1,74 @@ -# ----------------------------------------------------------------------------- -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. See License.txt in the project root for -# license information. -# ----------------------------------------------------------------------------- - -"""Microsoft Power Platform Connectors CLI package that can be installed using setuptools""" - -import os -from setuptools import setup - -__VERSION__ = '0.0.21' - - -def read(fname): - """Local read helper function for long documentation""" - return open(os.path.join(os.path.dirname(__file__), fname)).read() - - -setup( - name='paconn', - version=__VERSION__, - description='Microsoft Power Platform Connectors CLI', - long_description=read('README.md'), - long_description_content_type="text/markdown", - author='Microsoft Corporation', - author_email='connectors@microsoft.com', - license='MIT', - classifiers=[ - 'Development Status :: 4 - Beta', - 'Intended Audience :: Developers', - 'Topic :: Software Development :: Build Tools', - 'Environment :: Console', - 'License :: OSI Approved :: MIT License', - 'Natural Language :: English', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6' - ], - keywords='azure, powerapps, flow, power platform, connectors', - python_requires='>=3.5,<4', - packages=[ - 'paconn', - 'paconn.apimanager', - 'paconn.authentication', - 'paconn.commands', - 'paconn.common', - 'paconn.config', - 'paconn.operations', - 'paconn.settings' - ], - install_requires=[ - 'docutils', - 'flake8', - 'future', - 'knack~=0.5.1', - 'pytest', - 'pytest-xdist', - 'virtualenv', - 'requests', - 'adal', - 'msrestazure', - 'azure-storage-blob>=2.1,<12.0' - ], - extras_require={ - ":python_version<'3.0'": ['pylint~=1.9.2'], - ":python_version>='3.0'": ['pylint~=2.0.0'] - }, - package_data={'paconn.config': ['*.*']}, - include_package_data=True, - entry_points={ - 'console_scripts': ['paconn=paconn.__main__:main'] - } -) +# ----------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# ----------------------------------------------------------------------------- + +"""Microsoft Power Platform Connectors CLI package that can be installed using setuptools""" + +import os +from setuptools import setup + +__VERSION__ = '0.1.0' + + +def read(fname): + """Local read helper function for long documentation""" + return open(os.path.join(os.path.dirname(__file__), fname)).read() + + +setup( + name='paconn', + version=__VERSION__, + description='Microsoft Power Platform Connectors CLI', + long_description=read('README.md'), + long_description_content_type="text/markdown", + author='Microsoft Corporation', + author_email='connectors@microsoft.com', + license='MIT', + classifiers=[ + 'Development Status :: 4 - Beta', + 'Intended Audience :: Developers', + 'Topic :: Software Development :: Build Tools', + 'Environment :: Console', + 'License :: OSI Approved :: MIT License', + 'Natural Language :: English', + 'Programming Language :: Python :: 3.5', + 'Programming Language :: Python :: 3.6' + ], + keywords='azure, powerapps, flow, power platform, connectors', + python_requires='>=3.5,<4', + packages=[ + 'paconn', + 'paconn.apimanager', + 'paconn.authentication', + 'paconn.commands', + 'paconn.common', + 'paconn.config', + 'paconn.operations', + 'paconn.settings' + ], + install_requires=[ + 'docutils', + 'flake8', + 'future', + 'knack~=0.5.1', + 'pytest', + 'pytest-xdist', + 'virtualenv', + 'requests', + 'adal', + 'msrestazure', + 'azure-storage-blob>=2.1,<12.0', + 'regex>=2022.1.18' # Enhanced regex support for C# script validation + ], + extras_require={ + ":python_version<'3.0'": ['pylint~=1.9.2'], + ":python_version>='3.0'": ['pylint~=2.0.0'] + }, + package_data={'paconn.config': ['*.*']}, + include_package_data=True, + entry_points={ + 'console_scripts': ['paconn=paconn.__main__:main'] + } +)