|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Porpoiseful LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +/** |
| 19 | + * @fileoverview Functions for generating Python code from Blockly projects. |
| 20 | + * This module uses headless Blockly to convert block-based projects into |
| 21 | + * Python code files and provides utilities for packaging them into downloadable zip files. |
| 22 | + */ |
| 23 | + |
| 24 | +import * as Blockly from 'blockly/core'; |
| 25 | +import { extendedPythonGenerator } from '../editor/extended_python_generator'; |
| 26 | +import { Project, Module, Storage, parseModuleContentText } from './common_storage'; |
| 27 | +import JSZip from 'jszip'; |
| 28 | +import { GeneratorContext } from '../editor/generator_context'; |
| 29 | + |
| 30 | +/** Result of Python code generation for a single module */ |
| 31 | +export interface ModulePythonResult { |
| 32 | + moduleName: string; |
| 33 | + pythonCode: string; |
| 34 | + success: boolean; |
| 35 | + error?: string; |
| 36 | +} |
| 37 | + |
| 38 | +/** Result of Python code generation for an entire project */ |
| 39 | +export interface ProjectPythonResult { |
| 40 | + projectName: string; |
| 41 | + modules: ModulePythonResult[]; |
| 42 | + success: boolean; |
| 43 | + errorCount: number; |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Generate Python code for a single module using headless Blockly |
| 48 | + * @param module The module containing Blockly JSON |
| 49 | + * @param storage The storage interface to fetch module content |
| 50 | + * @returns Result containing generated Python code or error |
| 51 | + */ |
| 52 | +export async function generatePythonForModule(module: Module, storage: Storage): Promise<ModulePythonResult> { |
| 53 | + try { |
| 54 | + // Fetch the module content from storage |
| 55 | + const moduleContentText = await storage.fetchModuleContentText(module.modulePath); |
| 56 | + const moduleContent = parseModuleContentText(moduleContentText); |
| 57 | + |
| 58 | + // Create a headless workspace |
| 59 | + const workspace = new Blockly.Workspace(); |
| 60 | + |
| 61 | + // Parse and load the JSON into the workspace |
| 62 | + const blocks = moduleContent.getBlocks(); |
| 63 | + |
| 64 | + Blockly.serialization.workspaces.load(blocks, workspace); |
| 65 | + |
| 66 | + |
| 67 | + // Create and set up generator context like the editor does |
| 68 | + const generatorContext = new GeneratorContext(); |
| 69 | + generatorContext.setModule(module); |
| 70 | + |
| 71 | + // Initialize the generator if not already done |
| 72 | + if (!extendedPythonGenerator.isInitialized) { |
| 73 | + extendedPythonGenerator.init(workspace); |
| 74 | + } |
| 75 | + |
| 76 | + // Generate Python code using the same method as the editor |
| 77 | + const pythonCode = extendedPythonGenerator.mrcWorkspaceToCode(workspace, generatorContext); |
| 78 | + |
| 79 | + // Clean up the workspace |
| 80 | + workspace.dispose(); |
| 81 | + |
| 82 | + return { |
| 83 | + moduleName: module.moduleName, |
| 84 | + pythonCode, |
| 85 | + success: true, |
| 86 | + }; |
| 87 | + } catch (error) { |
| 88 | + console.error('Error generating Python for module', module.moduleName, ':', error); |
| 89 | + return { |
| 90 | + moduleName: module.moduleName, |
| 91 | + pythonCode: '', |
| 92 | + success: false, |
| 93 | + error: error instanceof Error ? error.message : String(error), |
| 94 | + }; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Generate Python code for all modules in a project using headless Blockly |
| 100 | + * @param project The project containing multiple modules |
| 101 | + * @param storage The storage interface to fetch module content |
| 102 | + * @returns Result containing Python code for all modules |
| 103 | + */ |
| 104 | +export async function generatePythonForProject(project: Project, storage: Storage): Promise<ProjectPythonResult> { |
| 105 | + const moduleResults: ModulePythonResult[] = []; |
| 106 | + let errorCount = 0; |
| 107 | + |
| 108 | + // Process the robot module |
| 109 | + const robotResult = await generatePythonForModule(project.robot, storage); |
| 110 | + moduleResults.push(robotResult); |
| 111 | + if (!robotResult.success) { |
| 112 | + errorCount++; |
| 113 | + } |
| 114 | + |
| 115 | + // Process all mechanism modules |
| 116 | + for (const mechanism of project.mechanisms) { |
| 117 | + const result = await generatePythonForModule(mechanism, storage); |
| 118 | + moduleResults.push(result); |
| 119 | + if (!result.success) { |
| 120 | + errorCount++; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + // Process all opmode modules |
| 125 | + for (const opMode of project.opModes) { |
| 126 | + const result = await generatePythonForModule(opMode, storage); |
| 127 | + moduleResults.push(result); |
| 128 | + if (!result.success) { |
| 129 | + errorCount++; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + return { |
| 134 | + projectName: project.projectName, |
| 135 | + modules: moduleResults, |
| 136 | + success: errorCount === 0, |
| 137 | + errorCount, |
| 138 | + }; |
| 139 | +} |
| 140 | + |
| 141 | +/** |
| 142 | + * Generate Python files content as a map for easy file creation |
| 143 | + * @param project The project containing multiple modules |
| 144 | + * @param storage The storage interface to fetch module content |
| 145 | + * @returns Map of filename to Python code content |
| 146 | + */ |
| 147 | +export async function generatePythonFilesMap(project: Project, storage: Storage): Promise<Map<string, string>> { |
| 148 | + const filesMap = new Map<string, string>(); |
| 149 | + const result = await generatePythonForProject(project, storage); |
| 150 | + |
| 151 | + for (const moduleResult of result.modules) { |
| 152 | + if (moduleResult.success) { |
| 153 | + const filename = `${moduleResult.moduleName}.py`; |
| 154 | + filesMap.set(filename, moduleResult.pythonCode); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + return filesMap; |
| 159 | +} |
| 160 | + |
| 161 | +/** |
| 162 | + * Generate Python files for a project and create a downloadable zip blob |
| 163 | + * @param project The project containing multiple modules |
| 164 | + * @param storage The storage interface to fetch module content |
| 165 | + * @returns Promise that resolves to a blob URL for downloading the zip file |
| 166 | + */ |
| 167 | +export async function producePythonProjectBlob(project: Project, storage: Storage): Promise<string> { |
| 168 | + // Initialize the generator first |
| 169 | + initializeHeadlessBlockly(); |
| 170 | + |
| 171 | + const pythonFilesMap = await generatePythonFilesMap(project, storage); |
| 172 | + |
| 173 | + |
| 174 | + if (pythonFilesMap.size === 0) { |
| 175 | + throw new Error('No Python files were generated successfully'); |
| 176 | + } |
| 177 | + |
| 178 | + const zip = new JSZip(); |
| 179 | + for (const [filename, pythonCode] of pythonFilesMap) { |
| 180 | + zip.file(filename, pythonCode); |
| 181 | + } |
| 182 | + |
| 183 | + const content = await zip.generateAsync({ type: "blob" }); |
| 184 | + const blobUrl = URL.createObjectURL(content); |
| 185 | + return blobUrl; |
| 186 | +} |
| 187 | + |
| 188 | +/** |
| 189 | + * Initialize Blockly for headless operation |
| 190 | + * This should be called once before using the generation functions |
| 191 | + */ |
| 192 | +export function initializeHeadlessBlockly(): void { |
| 193 | + // Initialize Blockly for headless operation |
| 194 | + // This ensures all necessary generators and blocks are loaded |
| 195 | + extendedPythonGenerator.init(new Blockly.Workspace()); |
| 196 | +} |
0 commit comments