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

Update "deploy to playground" integration #307

Merged
merged 5 commits into from
Nov 8, 2024
Merged
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
263 changes: 210 additions & 53 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
"name": "vscode-motoko",
"displayName": "Motoko",
"description": "Motoko language support",
"version": "0.16.11",
"version": "0.16.12",
"publisher": "dfinity-foundation",
"repository": "https://github.com/dfinity/vscode-motoko",
"engines": {
@@ -171,9 +171,9 @@
"execa": "4.1.0",
"fast-glob": "3.2.12",
"ic-mops": "0.45.3",
"ic0": "0.2.7",
"ic0": "0.3.1",
"mnemonist": "0.39.5",
"motoko": "3.8.4",
"motoko": "3.9.0",
"prettier": "2.8.0",
"prettier-plugin-motoko": "0.9.2",
"semver": "7.6.3",
6 changes: 6 additions & 0 deletions src/server/context.ts
Original file line number Diff line number Diff line change
@@ -71,6 +71,12 @@ function requestMotokoInstance(uri: string, version: Version): Motoko {
motoko = require(motokoPath).default;
}
}
// Required for deploying to Motoko Playground
motoko.setPublicMetadata([
'candid:service',
'candid:args',
'motoko:stable-types',
]);
motoko.loadPackage(baseLibrary);
return motoko;
}
65 changes: 31 additions & 34 deletions src/server/playground.ts
Original file line number Diff line number Diff line change
@@ -98,41 +98,37 @@ export async function deployPlayground(
wasm: Uint8Array,
profiling: ProfilingConfig | null,
): Promise<CanisterInfo> {
try {
let updatedState: CanisterInfo | null = null;
if (!canisterInfo) {
if (mode !== 'install') {
throw new Error(`Cannot '${mode}' for new canister`);
}
canisterInfo = await createCanister();
notify('Deploying...');
updatedState = await install(
canisterInfo,
wasm,
args,
'install',
profiling,
);
} else {
if (mode !== 'reinstall' && mode !== 'upgrade') {
throw new Error(`Unknown mode '${mode}'`);
}
notify('Deploying...');
updatedState = await install(
canisterInfo,
wasm,
args,
mode,
profiling,
);
let updatedState: CanisterInfo | null = null;
if (!canisterInfo) {
if (mode !== 'install') {
throw new Error(`Cannot '${mode}' for new canister`);
}
canisterInfo = await createCanister();
notify(`Deploying ${canisterInfo.id}...`);
updatedState = await install(
canisterInfo,
wasm,
args,
'install',
profiling,
);
} else {
if (mode !== 'reinstall' && mode !== 'upgrade') {
throw new Error(`Unknown mode '${mode}'`);
}
//updatedState.candid = candid_source;
updatedState.name = canisterName;
return updatedState;
} catch (err) {
// logger.log(err.message);
throw err;
notify(`Deploying ${canisterInfo.id}...`);
updatedState = await install(
canisterInfo,
wasm,
args,
mode,
profiling,
);
}
console.log('Finished deploying canister');
//updatedState.candid = candid_source;
updatedState.name = canisterName;
return updatedState;
}

async function createCanister(): Promise<CanisterInfo> {
@@ -158,7 +154,7 @@ export async function deployPlayground(
): Promise<CanisterInfo> {
notify('Installing WebAssembly...');
if (!canisterInfo) {
throw new Error('No canister id');
throw new Error('No canister ID');
}
const canisterId = canisterInfo.id;
const installArgs = {
@@ -181,6 +177,7 @@ export async function deployPlayground(
installConfig,
);
canisterInfo = newInfo;
console.log(`Code installed at canister ID: ${canisterInfo.id}`);
return canisterInfo;
}

29 changes: 23 additions & 6 deletions src/server/server.ts
Original file line number Diff line number Diff line change
@@ -228,6 +228,7 @@ async function getPackageSources(
let loadingPackages = false;
let packageConfigChangeTimeout: ReturnType<typeof setTimeout>;
function notifyPackageConfigChange(reuseCached = false) {
isWorkspaceReady = false;
if (!reuseCached) {
packageSourceCache.clear();
}
@@ -363,6 +364,7 @@ function notifyPackageConfigChange(reuseCached = false) {
let dfxResolver: DfxResolver | undefined;
let dfxChangeTimeout: ReturnType<typeof setTimeout>;
function notifyDfxChange() {
isWorkspaceReady = false;
clearTimeout(dfxChangeTimeout);
dfxChangeTimeout = setTimeout(async () => {
try {
@@ -751,6 +753,7 @@ function unscheduleCheck(uri: string) {
}
}

let isWorkspaceReady = false;
let previousCheckedFiles: string[] = [];
let checkWorkspaceTimeout: ReturnType<typeof setTimeout>;
/**
@@ -815,6 +818,7 @@ function checkWorkspace() {
checkedFiles.forEach((uri) => notify(uri));
checkedFiles.forEach((uri) => scheduleCheck(uri));
previousCheckedFiles = checkedFiles;
isWorkspaceReady = true;
} catch (err) {
console.error('Error while finding dfx canister paths');
console.error(err);
@@ -1449,7 +1453,7 @@ connection.onReferences(

// Run a file which is recognized as a unit test
connection.onRequest(TEST_FILE_REQUEST, async (event): Promise<TestResult> => {
while (loadingPackages) {
while (!isWorkspaceReady) {
// Load all packages before running tests
await new Promise((resolve) => setTimeout(resolve, 500));
}
@@ -1521,11 +1525,24 @@ connection.onRequest(TEST_FILE_REQUEST, async (event): Promise<TestResult> => {
});

// Deploy to Motoko Playground
connection.onRequest(DEPLOY_PLAYGROUND, (params) =>
deployPlayground(params, (message) =>
connection.sendNotification(DEPLOY_PLAYGROUND_MESSAGE, { message }),
),
);
connection.onRequest(DEPLOY_PLAYGROUND, async (params) => {
const notify = (message: string) => {
console.log(message);
connection.sendNotification(DEPLOY_PLAYGROUND_MESSAGE, { message });
};
try {
if (!isWorkspaceReady) {
notify('Loading workspace...');
while (!isWorkspaceReady) {
await new Promise((resolve) => setTimeout(resolve, 200));
}
}
return deployPlayground(params, notify);
} catch (err) {
console.error(err);
throw err;
}
});

// Install and import mops package
connection.onRequest(IMPORT_MOPS_PACKAGE, async (params) => {