Skip to content

Commit 5b20f41

Browse files
monsterboomzhaozhuobin
authored andcommitted
Merge pull request #82 from LISTENAI/feature/opt
Feature/opt
2 parents c82dde6 + 7fa65ea commit 5b20f41

File tree

6 files changed

+130
-14
lines changed

6 files changed

+130
-14
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lisa-plugin/zephyr",
3-
"version": "1.6.4",
3+
"version": "1.6.5",
44
"description": "LISA plugin for Zephyr",
55
"main": "./lib/main.js",
66
"engines": {

src/models/sdk.ts

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import { pathExists, mkdirp, writeFile } from 'fs-extra';
2+
import { undertake } from "../main";
3+
import Lisa from '@listenai/lisa_core';
4+
import { venvScripts } from '../venv';
5+
6+
import { resolve, join } from "path";
7+
import { PACKAGE_HOME, loadBundles, getEnv, invalidateEnv } from "../env";
8+
import { get, set } from "../env/config";
9+
10+
export default class SDK {
11+
12+
constructor() {
13+
14+
}
15+
16+
static async clear() {
17+
await set("sdk", undefined);
18+
await invalidateEnv();
19+
}
20+
21+
async zephyrbase() {
22+
const env = await getEnv();
23+
return env["ZEPHYR_BASE"] ?? '';
24+
}
25+
26+
async westConfigPath() {
27+
const zephyrbase = await this.zephyrbase();
28+
return zephyrbase ? resolve(zephyrbase, "../.west/config") : '';
29+
}
30+
31+
async manifestPath(basicPath: string) {
32+
const { stdout } = await Lisa.cmd(
33+
"python",
34+
["-m", "west", "config", "manifest.path"],
35+
{
36+
env: await getEnv(),
37+
cwd: basicPath,
38+
}
39+
);
40+
return join(basicPath, stdout);
41+
}
42+
43+
private async _checkZephyrBase(zephyrbase: string, westConfigPath: string) {
44+
if (!zephyrbase) {
45+
throw new Error(`需要设置 SDK (lisa zep use-sdk [path])`);
46+
}
47+
if (!(await pathExists(westConfigPath))) {
48+
throw new Error(
49+
`当前 SDK 未初始化,需要设置 SDK (lisa zep use-sdk [directory] [--from-git URL#TAG] [--manifest PATH])`
50+
);
51+
}
52+
}
53+
54+
async changeVersion(version: string, install: boolean = false) {
55+
const zephyrbase = await this.zephyrbase();
56+
await this._checkZephyrBase(zephyrbase, await this.westConfigPath());
57+
const {cmd} = Lisa;
58+
const env = await getEnv();
59+
try {
60+
await cmd("git", ["fetch", "origin"], {
61+
env,
62+
cwd: zephyrbase,
63+
});
64+
await cmd("git", ["checkout", version], {
65+
env,
66+
cwd: zephyrbase,
67+
});
68+
await cmd("git", ["pull", "origin", version], {
69+
stdio: "inherit",
70+
env,
71+
cwd: zephyrbase,
72+
});
73+
await cmd("python", ["-m", "west", "update"], {
74+
stdio: "inherit",
75+
env,
76+
cwd: zephyrbase,
77+
});
78+
if (install) {
79+
await this.installRequirement(zephyrbase)
80+
}
81+
} catch (e: any) {
82+
const { stderr } = e;
83+
throw new Error(
84+
(stderr && JSON.stringify(stderr)) || JSON.stringify(e)
85+
);
86+
}
87+
}
88+
89+
async installRequirement(zephyrbase: string) {
90+
const {cmd} = Lisa;
91+
await cmd("python", [
92+
"-m",
93+
"pip",
94+
"install",
95+
"-r",
96+
join(zephyrbase, "scripts", "requirements.txt"),
97+
],
98+
{
99+
stdio: "inherit",
100+
env: await getEnv(),
101+
}
102+
);
103+
}
104+
105+
}

src/tasks/create.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default ({ application, cmd }: LisaType) => {
3030
if (args["from-git"]) {
3131
task.title = "";
3232
const fromGit = args["from-git"];
33-
const targetDir = join(
33+
const targetDir = workspace() || join(
3434
process.cwd(),
3535
await task.prompt({
3636
type: "Input",
@@ -42,7 +42,7 @@ export default ({ application, cmd }: LisaType) => {
4242
try {
4343
await cmd('git', ['clone', fromGit, targetDir], {
4444
stdio: 'inherit',
45-
})
45+
})
4646
} catch (error: any) {
4747
process.exit(error.exitCode);
4848
// console.log(error)
@@ -129,11 +129,11 @@ export default ({ application, cmd }: LisaType) => {
129129

130130
// 根据sampleListJson ux.select 嵌套
131131
application.debug(sampleListJson);
132-
133132
const answers = await inquirer.prompt([
134133
{
135134
type: 'file-tree-selection',
136135
name: 'file',
136+
message: '选择sample. (`左右键/空格键` 展开文件夹,`回车键` 确定选择)',
137137
root: join(sdk, 'samples'),
138138
onlyShowValid: true,
139139
validate: (item) => {
@@ -184,7 +184,7 @@ export default ({ application, cmd }: LisaType) => {
184184
}
185185

186186
if (!val) {
187-
throw new Error('请选择选项为[xx]的项目sample,例如: [hello_world]')
187+
throw new Error('请选择选项为[xx]的项目sample,例如: [hello_world]。注意:`左右键/空格键` 展开文件夹,`回车键` 确定选择。')
188188
}
189189
from = selected;
190190
}
@@ -193,7 +193,6 @@ export default ({ application, cmd }: LisaType) => {
193193

194194
// const selected = await promptDir([], sampleListJson, task);
195195
// const selectedSample = join(sdk, "samples", ...selected);
196-
197196
const targetDir = workspace() || join(
198197
process.cwd(),
199198
await task.prompt({

src/tasks/environment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ export default ({ application, cmd, got }: LisaType) => {
164164
env,
165165
cwd: manifestPath,
166166
});
167-
await cmd("git", ["fetch", "origin", branchName], {
167+
await cmd("git", ["pull", "origin", branchName], {
168168
stdio: "inherit",
169169
env,
170170
cwd: manifestPath,

src/tasks/sdk.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { get } from "../env/config";
77
import { zephyrVersion, sdkTag } from "../utils/sdk";
88
import extendExec from "../utils/extendExec";
99
const path7za = require('7zip-bin').path7za;
10+
import SDK from "../models/sdk";
11+
1012
async function checkZephyrBase(ZEPHYR_BASE: string, westConfigPath: string) {
1113
if (!ZEPHYR_BASE) {
1214
return false;
@@ -21,6 +23,7 @@ export default ({ application, cmd, got, fs, cli }: LisaType) => {
2123
title: "SDK 设置",
2224
async task(_ctx, task) {
2325
task.title = "";
26+
const Sdk = new SDK();
2427
const exec = extendExec(cmd, { task });
2528
const argv = application.argv as ParsedArgs;
2629
const args = argv._[1];
@@ -185,9 +188,13 @@ export default ({ application, cmd, got, fs, cli }: LisaType) => {
185188
throw new Error(`当前 SDK 已经在此分支`);
186189
}
187190
console.log(`正在为您切换分支(${tagName})`);
188-
await cmd("lisa", ["zep", "use-sdk", "--mr", tagName], {
189-
stdio: "inherit",
190-
});
191+
try {
192+
await Sdk.changeVersion(tagName);
193+
} catch (error: any) {
194+
task.title = `SDK 版本切换失败`;
195+
throw Error(error ? error : `SDK 版本切换失败`);
196+
}
197+
task.title = `SDK 分支已切换到 ${tagName}`;
191198
}
192199
if (args === '7z') {
193200
const addArgs = process.argv.slice(5);

src/tasks/west.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,15 @@ export default ({ application, cmd }: LisaType) => {
7373
const command = execArgs.shift();
7474
if (!command) return;
7575

76-
await cmd(command, execArgs, {
77-
stdio: "inherit",
78-
env: await getEnv(),
79-
});
76+
try {
77+
await cmd(command, execArgs, {
78+
stdio: "inherit",
79+
env: await getEnv(),
80+
});
81+
} catch (error) {
82+
task.title = 'exec exit';
83+
throw new Error(`Command failed : ${command} ${execArgs.join(' ')}`)
84+
}
8085

8186
task.title = 'exec exit';
8287
}

0 commit comments

Comments
 (0)