11import { join } from "path" ;
22import { outputFile , pathExists , readFile } from "fs-extra" ;
33import { dump as dumpYaml , load as loadYaml } from "js-yaml" ;
4- import { CancellationToken , Uri , window } from "vscode" ;
5- import { CodeQLCliServer , QlpacksInfo } from "../codeql-cli/cli" ;
4+ import { Uri } from "vscode" ;
5+ import { CodeQLCliServer } from "../codeql-cli/cli" ;
66import { getOnDiskWorkspaceFolders } from "../common/vscode/workspace-folders" ;
77import { ProgressCallback } from "../common/vscode/progress" ;
88import { DatabaseItem } from "../databases/local-databases" ;
99import { getQlPackPath , QLPACK_FILENAMES } from "../common/ql" ;
1010import { getErrorMessage } from "../common/helpers-pure" ;
1111import { ExtensionPack } from "./shared/extension-pack" ;
1212import { NotificationLogger , showAndLogErrorMessage } from "../common/logging" ;
13- import {
14- disableAutoNameExtensionPack ,
15- getExtensionsDirectory ,
16- } from "../config" ;
13+ import { getExtensionsDirectory } from "../config" ;
1714import {
1815 autoNameExtensionPack ,
1916 ExtensionPackName ,
2017 formatPackName ,
21- parsePackName ,
22- validatePackName ,
2318} from "./extension-pack-name" ;
24- import {
25- askForWorkspaceFolder ,
26- autoPickExtensionsDirectory ,
27- } from "./extensions-workspace-folder" ;
19+ import { autoPickExtensionsDirectory } from "./extensions-workspace-folder" ;
2820
2921const maxStep = 3 ;
3022
@@ -33,7 +25,6 @@ export async function pickExtensionPack(
3325 databaseItem : Pick < DatabaseItem , "name" | "language" > ,
3426 logger : NotificationLogger ,
3527 progress : ProgressCallback ,
36- token : CancellationToken ,
3728) : Promise < ExtensionPack | undefined > {
3829 progress ( {
3930 message : "Resolving extension packs..." ,
@@ -52,182 +43,14 @@ export async function pickExtensionPack(
5243 true ,
5344 ) ;
5445
55- if ( ! disableAutoNameExtensionPack ( ) ) {
56- progress ( {
57- message : "Creating extension pack..." ,
58- step : 2 ,
59- maxStep,
60- } ) ;
61-
62- return autoCreateExtensionPack (
63- databaseItem . name ,
64- databaseItem . language ,
65- extensionPacksInfo ,
66- logger ,
67- ) ;
68- }
69-
70- if ( Object . keys ( extensionPacksInfo ) . length === 0 ) {
71- return pickNewExtensionPack ( databaseItem , token ) ;
72- }
73-
74- const extensionPacks = (
75- await Promise . all (
76- Object . entries ( extensionPacksInfo ) . map ( async ( [ name , paths ] ) => {
77- if ( paths . length !== 1 ) {
78- void showAndLogErrorMessage (
79- logger ,
80- `Extension pack ${ name } resolves to multiple paths` ,
81- {
82- fullMessage : `Extension pack ${ name } resolves to multiple paths: ${ paths . join (
83- ", " ,
84- ) } `,
85- } ,
86- ) ;
87-
88- return undefined ;
89- }
90-
91- const path = paths [ 0 ] ;
92-
93- let extensionPack : ExtensionPack ;
94- try {
95- extensionPack = await readExtensionPack ( path , databaseItem . language ) ;
96- } catch ( e : unknown ) {
97- void showAndLogErrorMessage (
98- logger ,
99- `Could not read extension pack ${ name } ` ,
100- {
101- fullMessage : `Could not read extension pack ${ name } at ${ path } : ${ getErrorMessage (
102- e ,
103- ) } `,
104- } ,
105- ) ;
106-
107- return undefined ;
108- }
109-
110- return extensionPack ;
111- } ) ,
112- )
113- ) . filter ( ( info ) : info is ExtensionPack => info !== undefined ) ;
114-
115- const extensionPacksForLanguage = extensionPacks . filter (
116- ( pack ) =>
117- pack . extensionTargets [ `codeql/${ databaseItem . language } -all` ] !==
118- undefined ,
119- ) ;
120-
121- const options : Array < {
122- label : string ;
123- description : string | undefined ;
124- detail : string | undefined ;
125- extensionPack : ExtensionPack | null ;
126- } > = extensionPacksForLanguage . map ( ( pack ) => ( {
127- label : pack . name ,
128- description : pack . version ,
129- detail : pack . path ,
130- extensionPack : pack ,
131- } ) ) ;
132- options . push ( {
133- label : "Create new extension pack" ,
134- description : undefined ,
135- detail : undefined ,
136- extensionPack : null ,
137- } ) ;
138-
13946 progress ( {
140- message : "Choosing extension pack..." ,
47+ message : "Creating extension pack..." ,
14148 step : 2 ,
14249 maxStep,
14350 } ) ;
14451
145- const extensionPackOption = await window . showQuickPick (
146- options ,
147- {
148- title : "Select extension pack to use" ,
149- } ,
150- token ,
151- ) ;
152- if ( ! extensionPackOption ) {
153- return undefined ;
154- }
155-
156- if ( ! extensionPackOption . extensionPack ) {
157- return pickNewExtensionPack ( databaseItem , token ) ;
158- }
159-
160- return extensionPackOption . extensionPack ;
161- }
162-
163- async function pickNewExtensionPack (
164- databaseItem : Pick < DatabaseItem , "name" | "language" > ,
165- token : CancellationToken ,
166- ) : Promise < ExtensionPack | undefined > {
167- const workspaceFolder = await askForWorkspaceFolder ( ) ;
168- if ( ! workspaceFolder ) {
169- return undefined ;
170- }
171-
172- const examplePackName = autoNameExtensionPack (
173- databaseItem . name ,
174- databaseItem . language ,
175- ) ;
176-
177- const name = await window . showInputBox (
178- {
179- title : "Create new extension pack" ,
180- prompt : "Enter name of extension pack" ,
181- placeHolder : examplePackName
182- ? `e.g. ${ formatPackName ( examplePackName ) } `
183- : "" ,
184- validateInput : async ( value : string ) : Promise < string | undefined > => {
185- const message = validatePackName ( value ) ;
186- if ( message ) {
187- return message ;
188- }
189-
190- const packName = parsePackName ( value ) ;
191- if ( ! packName ) {
192- return "Invalid pack name" ;
193- }
194-
195- const packPath = join ( workspaceFolder . uri . fsPath , packName . name ) ;
196- if ( await pathExists ( packPath ) ) {
197- return `A pack already exists at ${ packPath } ` ;
198- }
199-
200- return undefined ;
201- } ,
202- } ,
203- token ,
204- ) ;
205- if ( ! name ) {
206- return undefined ;
207- }
208-
209- const packName = parsePackName ( name ) ;
210- if ( ! packName ) {
211- return undefined ;
212- }
213-
214- const packPath = join ( workspaceFolder . uri . fsPath , packName . name ) ;
215-
216- if ( await pathExists ( packPath ) ) {
217- return undefined ;
218- }
219-
220- return writeExtensionPack ( packPath , packName , databaseItem . language ) ;
221- }
222-
223- async function autoCreateExtensionPack (
224- name : string ,
225- language : string ,
226- extensionPacksInfo : QlpacksInfo ,
227- logger : NotificationLogger ,
228- ) : Promise < ExtensionPack | undefined > {
22952 // Get the `codeQL.model.extensionsDirectory` setting for the language
230- const userExtensionsDirectory = getExtensionsDirectory ( language ) ;
53+ const userExtensionsDirectory = getExtensionsDirectory ( databaseItem . language ) ;
23154
23255 // If the setting is not set, automatically pick a suitable directory
23356 const extensionsDirectory = userExtensionsDirectory
@@ -239,11 +62,14 @@ async function autoCreateExtensionPack(
23962 }
24063
24164 // Generate the name of the extension pack
242- const packName = autoNameExtensionPack ( name , language ) ;
65+ const packName = autoNameExtensionPack (
66+ databaseItem . name ,
67+ databaseItem . language ,
68+ ) ;
24369 if ( ! packName ) {
24470 void showAndLogErrorMessage (
24571 logger ,
246- `Could not automatically name extension pack for database ${ name } ` ,
72+ `Could not automatically name extension pack for database ${ databaseItem . name } ` ,
24773 ) ;
24874
24975 return undefined ;
@@ -259,7 +85,7 @@ async function autoCreateExtensionPack(
25985 try {
26086 extensionPack = await readExtensionPack (
26187 existingExtensionPackPaths [ 0 ] ,
262- language ,
88+ databaseItem . language ,
26389 ) ;
26490 } catch ( e : unknown ) {
26591 void showAndLogErrorMessage (
@@ -309,7 +135,7 @@ async function autoCreateExtensionPack(
309135 return undefined ;
310136 }
311137
312- return writeExtensionPack ( packPath , packName , language ) ;
138+ return writeExtensionPack ( packPath , packName , databaseItem . language ) ;
313139}
314140
315141async function writeExtensionPack (
0 commit comments