11import { commands , window } from "vscode" ;
2- import { join , resolve } from "path" ;
2+ import { join } from "path" ;
33import {
44 getProjectPath ,
5- getVsCodeUserPath ,
5+ getStubsPathForVersion ,
6+ getStubsPathForVersionPosix ,
67 recommendedExtensions ,
8+ settingsStubsPathForVersion ,
79 shouldRecommendExtensions ,
810} from "./api.mjs" ;
9- import { mkdir , readdir , symlink } from "fs/promises" ;
11+ import { mkdir , readdir } from "fs/promises" ;
1012import {
1113 pathExists ,
1214 readJsonFile ,
@@ -19,6 +21,7 @@ import _ from "lodash";
1921import which from "which" ;
2022import { execSync } from "child_process" ;
2123import axios , { HttpStatusCode } from "axios" ;
24+ import type Settings from "./settings.mjs" ;
2225
2326export default class Stubs {
2427 private logger : Logger ;
@@ -27,13 +30,17 @@ export default class Stubs {
2730 this . logger = new Logger ( "Stubs" ) ;
2831 }
2932
30- public async update ( ) : Promise < void > {
31- const configFolder = getVsCodeUserPath ( ) ;
32- const installedStubsFolder = join ( configFolder , "Pico-W-Stub" ) ;
33+ /**
34+ * Update localy installed included stubs to the latest version.
35+ *
36+ * @returns
37+ */
38+ public async update ( settings : Settings ) : Promise < void > {
39+ const installedStubsFolder = getStubsPathForVersion ( "included" ) ;
3340
3441 if ( ! ( await pathExists ( join ( installedStubsFolder , "version.json" ) ) ) ) {
3542 // ensure config folder exists
36- await mkdir ( configFolder , { recursive : true } ) ;
43+ await mkdir ( installedStubsFolder , { recursive : true } ) ;
3744
3845 let installedVersion = "" ;
3946 let currentVersion = "" ;
@@ -64,6 +71,12 @@ export default class Stubs {
6471 if ( installedVersion === currentVersion ) {
6572 this . logger . info ( "Installed stubs are already up to date!" ) ;
6673
74+ // TODO: remove in future versions, only to convert legacy projects
75+ const workspace = getProjectPath ( ) ;
76+ if ( workspace ) {
77+ await this . removeLegacyStubs ( join ( workspace , ".vscode" ) , settings ) ;
78+ }
79+
6780 return ;
6881 }
6982 }
@@ -74,6 +87,12 @@ export default class Stubs {
7487 await copy ( join ( __dirname , ".." , "mpy_stubs" ) , installedStubsFolder ) ;
7588
7689 this . logger . info ( "Updated stubs successfully!" ) ;
90+
91+ // TODO: remove in future versions, only to convert legacy projects
92+ const workspace = getProjectPath ( ) ;
93+ if ( workspace ) {
94+ await this . removeLegacyStubs ( join ( workspace , ".vscode" ) , settings ) ;
95+ }
7796 } catch ( error ) {
7897 const msg : string =
7998 typeof error === "string" ? error : ( error as Error ) . message ;
@@ -104,7 +123,7 @@ export default class Stubs {
104123 await mkdir ( vsc ) ;
105124 }
106125
107- await this . addStubs ( vsc ) ;
126+ await this . removeLegacyStubs ( vsc ) ;
108127 await this . addExtensions ( vsc ) ;
109128 await this . addSettings ( vsc ) ;
110129 await this . addProjectFile ( workspace ) ;
@@ -119,19 +138,21 @@ export default class Stubs {
119138 }
120139
121140 /**
122- * Add stubs to the VS Code user folder
141+ * Remove stubs junction from the VS Code workspace folder
123142 *
124143 * @param vsc The path to the vscode config folder in current workspace
125144 */
126- private async addStubs ( vsc : string ) : Promise < void > {
145+ private async removeLegacyStubs (
146+ vsc : string ,
147+ settings : Settings | undefined = undefined
148+ ) : Promise < void > {
127149 const stubsPath = join ( vsc , "Pico-W-Stub" ) ;
128- if ( ! ( await pathExists ( stubsPath ) ) ) {
129- const configFolder = getVsCodeUserPath ( ) ;
130- await symlink (
131- resolve ( join ( configFolder , "Pico-W-Stub" ) ) ,
132- stubsPath ,
133- "junction"
134- ) ;
150+ if ( await pathExists ( stubsPath ) ) {
151+ await removeJunction ( stubsPath ) ;
152+
153+ if ( settings ) {
154+ await installIncludedStubs ( settings ) ;
155+ }
135156 }
136157 }
137158
@@ -155,7 +176,7 @@ export default class Stubs {
155176 justUpdate : boolean = false
156177 ) : Promise < void > {
157178 const settingsFilePath = join ( vsc , "settings.json" ) ;
158- const stubsPath = join ( ".vscode" , "Pico-W-Stub ") ;
179+ const stubsPath = settingsStubsPathForVersion ( "included ") ;
159180 const defaultSettings : { [ key : string ] : string | boolean | object } = {
160181 // eslint-disable-next-line @typescript-eslint/naming-convention
161182 "python.linting.enabled" : true ,
@@ -214,22 +235,8 @@ export default class Stubs {
214235 *
215236 * @returns
216237 */
217- export async function installIncludedStubs ( ) : Promise < void > {
218- const workspaceFolder = getProjectPath ( ) ;
219- if ( workspaceFolder === undefined ) {
220- return ;
221- }
222- const vsc = join ( workspaceFolder , ".vscode" ) ;
223- const stubsPath = join ( vsc , "Pico-W-Stub" ) ;
224- if ( await pathExists ( stubsPath ) ) {
225- await removeJunction ( stubsPath ) ;
226- }
227- const configFolder = getVsCodeUserPath ( ) ;
228- await symlink (
229- resolve ( join ( configFolder , "Pico-W-Stub" ) ) ,
230- stubsPath ,
231- "junction"
232- ) ;
238+ export async function installIncludedStubs ( settings : Settings ) : Promise < void > {
239+ await settings . updateStubsPath ( settingsStubsPathForVersion ( "included" ) ) ;
233240}
234241
235242enum StubPorts {
@@ -269,7 +276,8 @@ export function displayStringToStubPort(displayString: string): string {
269276// TODO: option to choose stubs distrbution
270277export async function installStubsByVersion (
271278 version : string ,
272- port : string
279+ port : string ,
280+ settings : Settings
273281) : Promise < boolean > {
274282 // check if pip is available
275283 const pip : string | null = await which ( "pip" , { nothrow : true } ) ;
@@ -283,35 +291,21 @@ export async function installStubsByVersion(
283291 return false ;
284292 }
285293
286- const configFolder = getVsCodeUserPath ( ) ;
287- const target = resolve (
288- join ( configFolder , "MicroPico-Stubs" , `${ port } ==${ version } ` )
289- ) ;
294+ const folderName = `${ port } ==${ version } ` ;
295+ const target = getStubsPathForVersionPosix ( folderName ) ;
290296 mkdirpSync ( target ) ;
291297
292298 // install stubs with pip vscode user directory
293299 const result = execSync (
294- `&"${ pip } " install ${ port } ==${ version } ` + `--target "${ target } " --no-user` ,
300+ `${
301+ process . platform === "win32" ? "&" : ""
302+ } "${ pip } " install ${ port } ==${ version } ` + `--target "${ target } " --no-user` ,
295303 process . platform === "win32" ? { shell : "powershell" } : { }
296304 ) ;
297305
298306 // check result
299307 if ( result . toString ( "utf-8" ) . includes ( "Successfully installed" ) ) {
300- const workspaceFolder = getProjectPath ( ) ;
301- if ( workspaceFolder === undefined ) {
302- return false ;
303- }
304- const vsc = join ( workspaceFolder , ".vscode" ) ;
305- const stubsPath = join ( vsc , "Pico-W-Stub" ) ;
306- // delete stubsPath folder if it exists
307- if ( await pathExists ( stubsPath ) ) {
308- await removeJunction ( stubsPath ) ;
309- }
310-
311- // relink
312- await symlink ( target , stubsPath , "junction" ) ;
313-
314- return true ;
308+ return settings . updateStubsPath ( settingsStubsPathForVersion ( folderName ) ) ;
315309 }
316310
317311 return false ;
0 commit comments