@@ -3,33 +3,19 @@ import * as path from 'path';
33import * as os from 'os' ;
44import inquirer from 'inquirer' ;
55import { logger , LogStep , showErrorMessages , SkipStep } from "../../core/logger" ;
6- import { VM_VERSION , GLOBAL_BLUESCRIPT_PATH , GlobalConfigHandler } from "../../config/global-config" ;
76import { BoardName } from "../../config/board-utils" ;
87import { exec } from '../../core/shell' ;
98import * as fs from '../../core/fs' ;
109import chalk from "chalk" ;
10+ import { CommandHandler } from "../command" ;
11+ import { GLOBAL_SETTINGS } from "../../config/constants" ;
1112
1213
13- const RUNTIME_ZIP_URL = `https://github.com/csg-tokyo/bluescript/releases/download/v${ VM_VERSION } /release-microcontroller-v${ VM_VERSION } .zip` ;
14- const RUNTIME_DIR = path . join ( GLOBAL_BLUESCRIPT_PATH , 'microcontroller' ) ;
15-
16- const ESP_IDF_VERSION = 'v5.4' ;
17- const ESP_IDF_GIT_REPO = 'https://github.com/espressif/esp-idf.git' ;
18- const ESP_ROOT_DIR = path . join ( GLOBAL_BLUESCRIPT_PATH , 'esp' ) ;
19- const ESP_IDF_EXPORT_FILE = path . join ( ESP_ROOT_DIR , 'esp-idf/export.sh' ) ;
20- const ESP_IDF_INSTALL_FILE = path . join ( ESP_ROOT_DIR , 'esp-idf/install.sh' ) ;
21-
22-
23- abstract class SetupHandler {
24- protected globalConfigHandler : GlobalConfigHandler ;
25-
26- constructor ( ) {
27- this . globalConfigHandler = GlobalConfigHandler . load ( ) ;
28- }
14+ abstract class SetupHandler extends CommandHandler {
2915
3016 getSetupPlan ( ) : string [ ] {
3117 const plan : string [ ] = [ ] ;
32- plan . push ( `Download BlueScript runtime from ${ RUNTIME_ZIP_URL } ` ) ;
18+ plan . push ( `Download BlueScript runtime from ${ GLOBAL_SETTINGS . RUNTIME_ZIP_URL } ` ) ;
3319 plan . push ( ...this . getBoardSetupPlan ( ) ) ;
3420 return plan ;
3521 }
@@ -42,8 +28,8 @@ abstract class SetupHandler {
4228 }
4329
4430 private ensureBlueScriptDir ( ) {
45- if ( ! fs . exists ( GLOBAL_BLUESCRIPT_PATH ) ) {
46- fs . makeDir ( GLOBAL_BLUESCRIPT_PATH ) ;
31+ if ( ! fs . exists ( GLOBAL_SETTINGS . BLUESCRIPT_DIR ) ) {
32+ fs . makeDir ( GLOBAL_SETTINGS . BLUESCRIPT_DIR ) ;
4733 }
4834 }
4935
@@ -56,18 +42,16 @@ abstract class SetupHandler {
5642 if ( ! this . needToDownloadBlueScriptRuntime ( ) ) {
5743 throw new SkipStep ( 'already downloaded.' , undefined ) ;
5844 }
59- if ( fs . exists ( RUNTIME_DIR ) ) {
60- fs . removeDir ( RUNTIME_DIR ) ;
45+ if ( fs . exists ( GLOBAL_SETTINGS . RUNTIME_DIR ) ) {
46+ fs . removeDir ( GLOBAL_SETTINGS . RUNTIME_DIR ) ;
6147 }
6248
63- await fs . downloadAndUnzip ( RUNTIME_ZIP_URL , GLOBAL_BLUESCRIPT_PATH ) ;
64- this . globalConfigHandler . setRuntimeDir ( RUNTIME_DIR ) ;
49+ await fs . downloadAndUnzip ( GLOBAL_SETTINGS . RUNTIME_ZIP_URL , GLOBAL_SETTINGS . BLUESCRIPT_DIR ) ;
50+ this . globalConfigHandler . setRuntimeDir ( GLOBAL_SETTINGS . RUNTIME_DIR ) ;
6551 }
6652
6753 abstract needSetup ( ) : boolean ;
68-
6954 abstract getBoardSetupPlan ( ) : string [ ] ;
70-
7155 abstract setupBoard ( ) : Promise < void > ;
7256}
7357
@@ -103,7 +87,7 @@ export class ESP32SetupHandler extends SetupHandler {
10387 } else {
10488 throw new Error ( 'Unknown OS.' ) ;
10589 }
106- plan . push ( `Clone ESP-IDF ${ ESP_IDF_VERSION } from ${ ESP_IDF_GIT_REPO } .` ) ;
90+ plan . push ( `Clone ESP-IDF ${ GLOBAL_SETTINGS . ESP_IDF_VERSION } from ${ GLOBAL_SETTINGS . ESP_IDF_GIT_REPO } .` ) ;
10791 plan . push ( 'Run ESP-IDF install script.' ) ;
10892 return plan ;
10993 }
@@ -115,9 +99,9 @@ export class ESP32SetupHandler extends SetupHandler {
11599 await this . runEspIdfInstallScript ( ) ;
116100
117101 this . globalConfigHandler . updateBoardConfig ( this . boardName , {
118- idfVersion : ESP_IDF_VERSION ,
119- rootDir : ESP_ROOT_DIR ,
120- exportFile : ESP_IDF_EXPORT_FILE ,
102+ idfVersion : GLOBAL_SETTINGS . ESP_IDF_VERSION ,
103+ rootDir : GLOBAL_SETTINGS . ESP_ROOT_DIR ,
104+ exportFile : GLOBAL_SETTINGS . ESP_IDF_EXPORT_FILE ,
121105 xtensaGccDir : await this . getXtensaGccDir ( ) ,
122106 } ) ;
123107 }
@@ -179,28 +163,29 @@ export class ESP32SetupHandler extends SetupHandler {
179163 }
180164
181165 @LogStep (
182- `Cloning ESP-IDF ${ ESP_IDF_VERSION } from ${ ESP_IDF_GIT_REPO } ... It may take a while.`
166+ `Cloning ESP-IDF ${ GLOBAL_SETTINGS . ESP_IDF_VERSION } from ${ GLOBAL_SETTINGS . ESP_IDF_GIT_REPO } ... It may take a while.`
183167 )
184168 private async cloneEspIdf ( ) {
185- if ( fs . exists ( ESP_ROOT_DIR ) ) {
186- fs . removeDir ( ESP_ROOT_DIR ) ;
169+ if ( fs . exists ( GLOBAL_SETTINGS . ESP_ROOT_DIR ) ) {
170+ fs . removeDir ( GLOBAL_SETTINGS . ESP_ROOT_DIR ) ;
187171 }
188172 if ( ! ( await this . isPackageInstalled ( 'git' ) ) ) {
189173 throw new Error ( 'Cannot find git command. Please install git and try again.' ) ;
190174 }
191175
192- fs . makeDir ( ESP_ROOT_DIR ) ;
193- await exec ( `git clone --depth 1 -b ${ ESP_IDF_VERSION } --recursive ${ ESP_IDF_GIT_REPO } ` , { cwd : ESP_ROOT_DIR } ) ;
176+ fs . makeDir ( GLOBAL_SETTINGS . ESP_ROOT_DIR ) ;
177+ await exec ( `git clone --depth 1 -b ${ GLOBAL_SETTINGS . ESP_IDF_VERSION } --recursive ${ GLOBAL_SETTINGS . ESP_IDF_GIT_REPO } ` ,
178+ { cwd : GLOBAL_SETTINGS . ESP_ROOT_DIR } ) ;
194179 }
195180
196181 @LogStep ( 'Running ESP-IDF install script...' )
197182 private async runEspIdfInstallScript ( ) {
198- await exec ( ESP_IDF_INSTALL_FILE ) ;
183+ await exec ( GLOBAL_SETTINGS . ESP_IDF_INSTALL_FILE ) ;
199184 }
200185
201186 private async getXtensaGccDir ( ) {
202187 try {
203- const gccPath = await exec ( `source ${ ESP_IDF_EXPORT_FILE } > /dev/null 2>&1 && which xtensa-esp32-elf-gcc` , { silent :true } ) ;
188+ const gccPath = await exec ( `source ${ GLOBAL_SETTINGS . ESP_IDF_EXPORT_FILE } > /dev/null 2>&1 && which xtensa-esp32-elf-gcc` , { silent :true } ) ;
204189 return path . dirname ( gccPath ) ;
205190 } catch ( error ) {
206191 throw new Error ( 'Failed to get xtensa gcc path.' , { cause : error } ) ;
@@ -249,7 +234,7 @@ export async function handleSetupCommand(board: string) {
249234 await setupHandler . setup ( ) ;
250235
251236 logger . br ( ) ;
252- logger . success ( `Success to se tup ${ board } ` ) ;
237+ logger . success ( `Success to set up ${ board } ` ) ;
253238 logger . info ( `Next step: run ${ chalk . yellow ( `bscript board flash-runtime ${ board } ` ) } ` ) ;
254239
255240 } catch ( error ) {
0 commit comments