Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 50 additions & 5 deletions src/storage/common_storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import { createGeneratorContext } from '../editor/generator_context';

// Types, constants, and functions related to modules, regardless of where the modules are stored.

export const CURRENT_VERSION = '0.1';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should use the blockly version instead of having a version specific for the file format


export type Module = {
modulePath: string,
moduleType: string,
Expand Down Expand Up @@ -90,11 +92,13 @@ const MARKER_BLOCKS_CONTENT = 'blocksContent: ';
const MARKER_METHODS = 'methods: ';
const MARKER_MODULE_TYPE = 'moduleType: ';
const MARKER_COMPONENTS = 'components: ';
const MARKER_VERSION = 'version: ';
const PARTS_INDEX_BLOCKS_CONTENT = 0;
const PARTS_INDEX_METHODS = 1;
const PARTS_INDEX_MODULE_TYPE = 2;
const PARTS_INDEX_COMPONENTS = 3;
const NUMBER_OF_PARTS = 4;
const PARTS_INDEX_VERSION = 4;
const NUMBER_OF_PARTS = 5;

export const UPLOAD_DOWNLOAD_FILE_EXTENSION = '.blocks';

Expand Down Expand Up @@ -572,19 +576,26 @@ export function makeModuleContent(
pythonCode: string,
blocksContent: string,
methodsContent: string,
componentsContent: string): string {
componentsContent: string,
optVersionContent?: string): string {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure we want this optional. When one is uploaded without it, we are giving it a default value.


const versionContent = optVersionContent ? optVersionContent : CURRENT_VERSION;

let delimiter = DELIMITER_PREFIX;
while (module.moduleType.includes(delimiter)
|| blocksContent.includes(delimiter)
|| methodsContent.includes(delimiter)
|| componentsContent.includes(delimiter)) {
|| componentsContent.includes(delimiter)
|| versionContent.includes(delimiter)) {
delimiter += '.';
}
return (
'# This file was generated by the Blocks editor.\n\n' +
pythonCode + '\n\n\n' +
'"""\n' +
delimiter + '\n' +
MARKER_VERSION + versionContent + '\n' +
delimiter + '\n' +
MARKER_COMPONENTS + componentsContent + '\n' +
delimiter + '\n' +
MARKER_MODULE_TYPE + module.moduleType + '\n' +
Expand Down Expand Up @@ -639,6 +650,10 @@ function getParts(moduleContent: string): string[] {
// This module was saved without components.
parts.push(MARKER_COMPONENTS + '[]');
}
if (parts.length <= PARTS_INDEX_VERSION) {
// This module was saved without version.
parts.push(MARKER_VERSION + '0.0');
}
return parts;
}

Expand Down Expand Up @@ -692,6 +707,18 @@ export function extractComponents(moduleContent: string): Component[] {
return components;
}

/**
* Extract the version from the given module content.
*/
export function extractVersion(moduleContent: string): string {
const parts = getParts(moduleContent);
let versionContent = parts[PARTS_INDEX_VERSION];
if (versionContent.startsWith(MARKER_VERSION)) {
versionContent = versionContent.substring(MARKER_VERSION.length);
}
return versionContent;
}

/**
* Produce the blob for downloading a project.
*/
Expand Down Expand Up @@ -737,6 +764,10 @@ function _processModuleContentForDownload(
if (componentsContent.startsWith(MARKER_COMPONENTS)) {
componentsContent = componentsContent.substring(MARKER_COMPONENTS.length);
}
let versionContent = parts[PARTS_INDEX_VERSION];
if (versionContent.startsWith(MARKER_VERSION)) {
versionContent = versionContent.substring(MARKER_VERSION.length);
}

const module: Module = {
modulePath: makeModulePath(projectName, moduleName),
Expand All @@ -750,7 +781,12 @@ function _processModuleContentForDownload(
// Clear out the python content.
const pythonCode = '';
return makeModuleContent(
module, pythonCode, blocksContent, methodsContent, componentsContent);
module,
pythonCode,
blocksContent,
methodsContent,
componentsContent,
versionContent);
}

/**
Expand Down Expand Up @@ -826,6 +862,10 @@ export function _processUploadedModule(
projectName: string, filename: string, uploadedContent: string)
: [string, string, string] {
const parts = getParts(uploadedContent);
let versionContent = parts[PARTS_INDEX_VERSION];
if (versionContent.startsWith(MARKER_VERSION)) {
versionContent = versionContent.substring(MARKER_VERSION.length);
}
let blocksContent = parts[PARTS_INDEX_BLOCKS_CONTENT];
if (blocksContent.startsWith(MARKER_BLOCKS_CONTENT)) {
blocksContent = blocksContent.substring(MARKER_BLOCKS_CONTENT.length);
Expand Down Expand Up @@ -869,6 +909,11 @@ export function _processUploadedModule(
headlessBlocklyWorkspace, generatorContext);

const moduleContent = makeModuleContent(
module, pythonCode, blocksContent, methodsContent, componentsContent);
module,
pythonCode,
blocksContent,
methodsContent,
componentsContent,
versionContent);
return [moduleName, moduleType, moduleContent];
}