Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for Python Upgrade Rules #9776

Merged
merged 3 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions localtypings/pxtarget.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,7 @@ declare namespace ts.pxtc {
flashChecksumAddr?: number;
ramSize?: number;
patches?: pxt.Map<UpgradePolicy[]>; // semver range -> upgrade policies
pyPatches?: pxt.Map<UpgradePolicy[]>; // semver range -> upgrade policies
openocdScript?: string;
uf2Family?: string;
onStartText?: boolean;
Expand Down
19 changes: 19 additions & 0 deletions pxtcompiler/simpledriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ namespace pxt {
return mainPkg.getCompileOptionsAsync(target)
}).then(opts => {
patchTS(mainPkg.targetVersion(), opts)
if (mainPkg.getPreferredEditor() === pxt.PYTHON_PROJECT_NAME) {
patchPY(mainPkg.targetVersion(), opts)
}
prepPythonOptions(opts)
return opts
})
Expand Down Expand Up @@ -209,6 +212,22 @@ namespace pxt {
}
}

export function patchPY(version: string, opts: pxtc.CompileOptions) {
if (!version)
return
pxt.debug(`applying PY patches relative to ${version}`)
for (let fn of Object.keys(opts.fileSystem)) {
if (fn.indexOf("/") == -1 && U.endsWith(fn, ".py")) {
const initial = opts.fileSystem[fn]
const patched = pxt.patching.patchPython(version, initial)
Copy link
Contributor

Choose a reason for hiding this comment

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

If this is being used by both Typescript and Python, should this be more generic? Because if computePyPatches returns undefined for TypeScript, that means it won't compute any upgrades, right? It would just return fileContents?

Copy link
Contributor

Choose a reason for hiding this comment

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

Or should there just also be patchJavascript here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, good catch. I may need to split this out after all and not re-use...

Copy link
Member

Choose a reason for hiding this comment

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

yeah, looks like this needs something like a const patchFunction = extension == ".ts" ? pxt.patching.patchJavaScript : pxt.patching.patchPython; then call that here to me?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I opted to just keep them separate. Seemed simpler at this point...

if (initial != patched) {
pxt.debug(`applying PY patch to ${fn}`)
opts.fileSystem[fn] = patched
}
}
}
}

// eslint-disable-next-line no-var
declare var global: any;
// eslint-disable-next-line no-var
Expand Down
19 changes: 19 additions & 0 deletions pxtlib/patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ namespace pxt.patching {
export function computePatches(version: string, kind?: string): ts.pxtc.UpgradePolicy[] {
const patches = pxt.appTarget.compile ? pxt.appTarget.compile.patches : undefined;
if (!patches) return undefined;
return parsePatches(version, patches, kind);
}

export function computePyPatches(version: string, kind?: string): ts.pxtc.UpgradePolicy[] {
const patches = pxt.appTarget.compile ? pxt.appTarget.compile.pyPatches : undefined;
if (!patches) return undefined;
return parsePatches(version, patches, kind);
}

function parsePatches(version: string, patches: Map<ts.pxtc.UpgradePolicy[]>, kind?: string): ts.pxtc.UpgradePolicy[] {
const v = pxt.semver.tryParse(version || "0.0.0") || pxt.semver.tryParse("0.0.0");
let r: ts.pxtc.UpgradePolicy[] = [];
Object.keys(patches)
Expand Down Expand Up @@ -30,6 +40,15 @@ namespace pxt.patching {

export function patchJavaScript(pkgTargetVersion: string, fileContents: string): string {
const upgrades = pxt.patching.computePatches(pkgTargetVersion);
return patchTextCode(pkgTargetVersion, fileContents, upgrades);
}

export function patchPython(pkgTargetVersion: string, fileContents: string): string {
const upgrades = pxt.patching.computePyPatches(pkgTargetVersion);
return patchTextCode(pkgTargetVersion, fileContents, upgrades);
}

function patchTextCode(pkgTargetVersion: string, fileContents: string, upgrades: pxtc.UpgradePolicy[]): string {
let updatedContents = fileContents;
if (upgrades) {
upgrades.filter(u => u.type === "api").forEach(rule => {
Expand Down
70 changes: 54 additions & 16 deletions webapp/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,12 @@ export function applyUpgradesAsync(): Promise<UpgradeResult> {
});
}

const upgradeOp = epkg.header.editor !== pxt.BLOCKS_PROJECT_NAME ? upgradeFromTSAsync : upgradeFromBlocksAsync;
const upgradeOp =
epkg.header.editor !== pxt.BLOCKS_PROJECT_NAME
? epkg.header.editor !== pxt.PYTHON_PROJECT_NAME
? upgradeFromTSAsync
: upgradeFromPythonAsync
: upgradeFromBlocksAsync;

let projectNeverCompiled = false;

Expand Down Expand Up @@ -922,7 +927,7 @@ function upgradeFromBlocksAsync(): Promise<UpgradeResult> {
});
}

function upgradeFromTSAsync(): Promise<UpgradeResult> {
async function upgradeFromTSAsync(): Promise<UpgradeResult> {
const mainPkg = pkg.mainPkg;
const project = pkg.getEditorPkg(mainPkg);
const targetVersion = project.header.targetVersion;
Expand All @@ -937,20 +942,49 @@ function upgradeFromTSAsync(): Promise<UpgradeResult> {

pxt.debug("Applying upgrades to TypeScript")

return checkPatchAsync(patchedFiles)
.then(() => {
return {
success: true,
editor: pxt.JAVASCRIPT_PROJECT_NAME,
patchedFiles
};
})
.catch(e => {
return {
success: false,
errorCodes: e.errorCodes
};
});
try {
await checkPatchAsync(patchedFiles)
return {
success: true,
editor: pxt.JAVASCRIPT_PROJECT_NAME,
patchedFiles
};
} catch (e) {
return {
success: false,
errorCodes: e.errorCodes
};
};
}

async function upgradeFromPythonAsync(): Promise<UpgradeResult> {
const mainPkg = pkg.mainPkg;
const project = pkg.getEditorPkg(mainPkg);
const targetVersion = project.header.targetVersion;

const patchedFiles: pxt.Map<string> = {};
pxt.Util.values(project.files).filter(isPyFile).forEach(file => {
const patched = pxt.patching.patchPython(targetVersion, file.content);
if (patched != file.content) {
patchedFiles[file.name] = patched;
}
});

pxt.debug("Applying upgrades to Python")
srietkerk marked this conversation as resolved.
Show resolved Hide resolved

try {
await checkPatchAsync(patchedFiles);
return {
success: true,
editor: pxt.PYTHON_PROJECT_NAME,
patchedFiles
};
} catch (e) {
return {
success: false,
errorCodes: e.errorCodes
};
}
}

interface UpgradeError extends Error {
Expand Down Expand Up @@ -1000,6 +1034,10 @@ function isTsFile(file: pkg.File) {
return pxt.Util.endsWith(file.getName(), ".ts");
}

function isPyFile(file: pkg.File) {
return pxt.Util.endsWith(file.getName(), ".py");
}

export function updatePackagesAsync(packages: pkg.EditorPackage[], token?: pxt.Util.CancellationToken): Promise<boolean> {
const epkg = pkg.mainEditorPkg();
let backup: pxt.workspace.Header;
Expand Down
Loading