1+ /**
2+ * @author Jared Wolff <[email protected] > 3+ * @copyright Circuit Dojo LLC
4+ * @license Apache 2.0
5+ */
6+
7+ // The module 'vscode' contains the VS Code extensibility API
8+ // Import the module and reference it with the alias vscode in your code below
9+ import * as vscode from 'vscode' ;
10+ import * as cp from 'child_process' ;
11+ import * as util from 'util' ;
12+ import * as os from 'os' ;
13+ import * as fs from 'fs-extra' ;
14+ import * as path from 'path' ;
15+ import * as unzip from 'node-stream-zip' ;
16+
17+ import * as extension from './extension' ;
18+ import * as helper from './helper' ;
19+
20+ async function copyFilesRecursively ( source : string , destination : string ) {
21+ const files = fs . readdirSync ( source ) ;
22+ for ( const file of files ) {
23+ const sourcePath = path . join ( source , file ) ;
24+ const destinationPath = path . join ( destination , file ) ;
25+ console . log ( "target: " + destinationPath ) ;
26+ const stats = fs . statSync ( sourcePath ) ;
27+ if ( stats . isDirectory ( ) ) {
28+ console . log ( "making dir: " + destinationPath ) ;
29+
30+ let exists = await fs . pathExists ( destinationPath ) ;
31+ if ( ! exists ) {
32+ fs . mkdirSync ( destinationPath ) ;
33+ }
34+
35+ await copyFilesRecursively ( sourcePath , destinationPath ) ;
36+ } else if ( ! fs . existsSync ( destinationPath ) ) {
37+ console . log ( "copying file: " + destinationPath ) ;
38+ const contents = fs . readFileSync ( sourcePath , 'utf8' ) ;
39+ fs . writeFileSync ( destinationPath , contents , 'utf8' ) ;
40+ }
41+ }
42+ }
43+
44+ export async function create_new ( context : vscode . ExtensionContext , config : extension . GlobalConfig , _dest : vscode . Uri | undefined ) {
45+
46+ // Pop up asking for location
47+ let dest = await helper . get_dest ( _dest ) ;
48+
49+ if ( dest === null || dest === undefined || dest . toString ( ) === "" ) {
50+ vscode . window . showErrorMessage ( "Invalid destination" ) ;
51+ return ;
52+ }
53+
54+ // Merge path
55+ let app_dest = path . join ( dest . fsPath , "app" ) ;
56+
57+ console . log ( "dest: " + app_dest ) ;
58+
59+ // Create app folder
60+ let exists = await fs . pathExists ( app_dest ) ;
61+ if ( ! exists ) {
62+ console . log ( `${ app_dest } not found` ) ;
63+ // Otherwise create home directory
64+ await fs . mkdirp ( app_dest ) ;
65+ }
66+
67+ // Popup asking for which SDK (vanilla vs NCS)
68+ const choices = [ "Vanilla" , "NRF Connect SDK" ] ;
69+ const templates = [ "vanilla" , "ncs" ] ;
70+ const sdk = await vscode . window . showQuickPick ( choices , {
71+ title : "Pick your Zephyr SDK variant." ,
72+ placeHolder : choices [ 0 ] ,
73+ ignoreFocusOut : true ,
74+ } ) ?? choices [ 0 ] ;
75+
76+ let templateSubPath = "" ;
77+ for ( let i = 0 ; i < choices . length ; i ++ ) {
78+ if ( choices [ i ] === sdk ) {
79+ templateSubPath = templates [ i ] ;
80+ }
81+ }
82+
83+ if ( templateSubPath === "" ) {
84+ vscode . window . showErrorMessage ( 'Invalid SDK choice.' ) ;
85+ return undefined ;
86+ }
87+
88+ // Get the static files
89+ const extensionPath = context . extensionPath ;
90+ copyFilesRecursively ( path . join ( extensionPath , "templates" , templateSubPath ) , app_dest ) ;
91+
92+ // Promisified exec
93+ let exec = util . promisify ( cp . exec ) ;
94+
95+ // Init git repo
96+ await exec ( "git init " + app_dest , { env : config . env } ) ;
97+
98+ // West init
99+ let init_cmd = `west init -l ${ app_dest } ` ;
100+ await exec ( init_cmd , { env : config . env } ) ;
101+
102+ console . log ( "init_cmd: " + init_cmd ) ;
103+
104+ // Init the rest of the way
105+ await extension . initRepo ( config , context , dest ) ;
106+
107+ }
0 commit comments