Skip to content

Commit 180e5d3

Browse files
committed
allow create project by clone git template
1 parent 6d29c64 commit 180e5d3

3 files changed

Lines changed: 150 additions & 74 deletions

File tree

src/OperationExplorer.ts

Lines changed: 147 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ import {
4444
view_str$prompt$select_file, view_str$prompt$select_folder, view_str$prompt$select_file_or_folder, view_str$prompt$select_tool_install_mode,
4545
view_str$prompt$tool_install_mode_online, view_str$prompt$tool_install_mode_local, view_str$operation$empty_anygcc_prj, view_str$operation$setupUtilTools,
4646
view_str$prompt$setupToolchainPrefix,
47-
view_str$prompt$needReloadToUpdateEnv
47+
view_str$prompt$needReloadToUpdateEnv,
48+
view_str$operation$create_prj_done
4849
} from './StringTable';
4950
import { CreateOptions, ImportOptions, ProjectType } from './EIDETypeDefine';
5051
import { File } from '../lib/node-utility/File';
@@ -63,6 +64,7 @@ import * as NodePath from 'path';
6364
import { ResInstaller, ExternalToolName } from './ResInstaller';
6465
import { AbstractProject } from './EIDEProject';
6566
import { WorkspaceManager } from './WorkspaceManager';
67+
import * as child_process from 'child_process';
6668

6769
interface TemplatePickItem extends vscode.QuickPickItem, TemplateInfo {
6870
cacheFileName: string | undefined;
@@ -1091,6 +1093,7 @@ export class OperationExplorer {
10911093
const remoteUrl = acToken ? rawUrl : utility.redirectHost(rawUrl); // if token is enabled, not proxy
10921094

10931095
let targetTempFile: File | undefined;
1096+
let targetCloneUrl: string | undefined;
10941097

10951098
// get template from Github
10961099
{
@@ -1257,6 +1260,7 @@ export class OperationExplorer {
12571260
version: templateInfo.version,
12581261
category: templateInfo.category,
12591262
download_url: gitFileInfo?.download_url || templateInfo.download_url,
1263+
git_clone_url: templateInfo.git_clone_url,
12601264
size: gitFileInfo?.size || templateInfo.size,
12611265
disabled: templateInfo.disabled,
12621266
upload_time: templateInfo.upload_time,
@@ -1282,6 +1286,10 @@ export class OperationExplorer {
12821286
desc_list.push(`${size_str} KB`);
12831287
}
12841288

1289+
if (tPickItem.git_clone_url) {
1290+
desc_list.push(`Git Repo`);
1291+
}
1292+
12851293
const detail_list: string[] = [];
12861294

12871295
if (tPickItem.author) {
@@ -1296,6 +1304,10 @@ export class OperationExplorer {
12961304
detail_list.push(`Version: ${tPickItem.version}`);
12971305
}
12981306

1307+
if (tPickItem.git_clone_url) {
1308+
detail_list.push(`Git-Url: ${tPickItem.git_clone_url}`);
1309+
}
1310+
12991311
// set descriptions
13001312
tPickItem.description = desc_list.join(', ');
13011313
tPickItem.detail = detail_list.join(', ');
@@ -1313,104 +1325,166 @@ export class OperationExplorer {
13131325
return;
13141326
}
13151327

1316-
if (temp_sel_item.download_url === undefined) {
1317-
this.locked = false;
1318-
GlobalEvent.emit('msg', newMessage('Warning', `error !, download url is null !`));
1319-
return;
1328+
// use git clone
1329+
if (temp_sel_item.git_clone_url) {
1330+
targetCloneUrl = temp_sel_item.git_clone_url;
13201331
}
13211332

1322-
// found cache, use cache install
1323-
if (temp_sel_item.cacheFileName) {
1324-
targetTempFile = resManager.getCachedFileByName(temp_sel_item.cacheFileName);
1325-
}
1333+
// use template file
1334+
else if (temp_sel_item.download_url) {
13261335

1327-
// has no cache, redownload it
1328-
else {
1329-
// create cache file
1330-
targetTempFile = resManager.getCachedFileByName(temp_sel_item.file_name);
1331-
1332-
const done = await vscode.window.withProgress({
1333-
location: vscode.ProgressLocation.Notification,
1334-
title: 'Downloading template',
1335-
cancellable: true
1336-
}, (progress, token): Thenable<boolean> => {
1337-
return new Promise(async (resolve) => {
1338-
1339-
const res = await utility.downloadFileWithProgress(
1340-
<string>temp_sel_item.download_url, temp_sel_item.file_name, progress, token);
1341-
1342-
if (res instanceof Buffer) {
1343-
fs.writeFileSync((<File>targetTempFile).path, res);
1344-
resManager.addCache({
1345-
name: (<File>targetTempFile).name,
1346-
size: res.length,
1347-
version: temp_sel_item.version
1348-
});
1349-
resolve(true);
1350-
return;
1351-
}
1352-
1353-
else if (res instanceof Error) {
1354-
GlobalEvent.emit('msg', ExceptionToMessage(res, 'Warning'));
1336+
// found cache, use cache install
1337+
if (temp_sel_item.cacheFileName) {
1338+
targetTempFile = resManager.getCachedFileByName(temp_sel_item.cacheFileName);
1339+
}
1340+
// has no cache, redownload it
1341+
else {
1342+
// create cache file
1343+
targetTempFile = resManager.getCachedFileByName(temp_sel_item.file_name);
1344+
1345+
const done = await vscode.window.withProgress({
1346+
location: vscode.ProgressLocation.Notification,
1347+
title: 'Downloading template',
1348+
cancellable: true
1349+
}, (progress, token): Thenable<boolean> => {
1350+
return new Promise(async (resolve) => {
1351+
1352+
const res = await utility.downloadFileWithProgress(
1353+
<string>temp_sel_item.download_url, temp_sel_item.file_name, progress, token);
1354+
1355+
if (res instanceof Buffer) {
1356+
fs.writeFileSync((<File>targetTempFile).path, res);
1357+
resManager.addCache({
1358+
name: (<File>targetTempFile).name,
1359+
size: res.length,
1360+
version: temp_sel_item.version
1361+
});
1362+
resolve(true);
1363+
return;
1364+
}
1365+
1366+
else if (res instanceof Error) {
1367+
GlobalEvent.emit('msg', ExceptionToMessage(res, 'Warning'));
1368+
resolve(false);
1369+
return;
1370+
}
1371+
1372+
// res is undefined, operation canceled
13551373
resolve(false);
1356-
return;
1357-
}
1358-
1359-
// res is undefined, operation canceled
1360-
resolve(false);
1374+
});
13611375
});
1362-
});
13631376

1364-
// download failed, reset targetTempFile to undefined
1365-
if (done === false) {
1366-
targetTempFile = undefined;
1377+
// download failed, reset targetTempFile to undefined
1378+
if (done === false) {
1379+
targetTempFile = undefined;
1380+
}
13671381
}
13681382
}
13691383

1384+
// error
1385+
else {
1386+
this.locked = false;
1387+
GlobalEvent.emit('msg', newMessage('Warning', `'download_url' and 'git_clone_url' can not be null !`));
1388+
}
1389+
13701390
} catch (error) {
13711391
GlobalEvent.emit('msg', ExceptionToMessage(error, 'Warning'));
13721392
this.locked = false;
13731393
return;
13741394
}
13751395
}
13761396

1377-
//====================================================
1397+
//
1398+
// start create project
1399+
//
13781400

1379-
if (targetTempFile === undefined) {
1401+
if (targetTempFile == undefined &&
1402+
targetCloneUrl == undefined) {
13801403
this.locked = false;
13811404
return;
13821405
}
13831406

1384-
const name = await vscode.window.showInputBox({
1385-
placeHolder: input_project_name,
1386-
ignoreFocusOut: true,
1387-
validateInput: (name) => AbstractProject.validateProjectName(name)
1388-
});
1407+
// do create project by template file
1408+
if (targetTempFile) {
13891409

1390-
if (name === undefined) {
1391-
this.locked = false;
1392-
return;
1393-
}
1410+
const projectname = await vscode.window.showInputBox({
1411+
placeHolder: input_project_name,
1412+
ignoreFocusOut: true,
1413+
validateInput: (name) => AbstractProject.validateProjectName(name)
1414+
});
13941415

1395-
const outDir = await vscode.window.showOpenDialog({
1396-
openLabel: select_out_dir,
1397-
canSelectFiles: false,
1398-
canSelectFolders: true,
1399-
canSelectMany: false
1400-
});
1416+
if (projectname === undefined) {
1417+
this.locked = false;
1418+
return;
1419+
}
14011420

1402-
if (outDir === undefined) {
1403-
this.locked = false;
1404-
return;
1421+
const outDir = await vscode.window.showOpenDialog({
1422+
openLabel: select_out_dir,
1423+
canSelectFiles: false,
1424+
canSelectFolders: true,
1425+
canSelectMany: false
1426+
});
1427+
1428+
if (outDir === undefined) {
1429+
this.locked = false;
1430+
return;
1431+
}
1432+
1433+
const outputDirPath = outDir[0].fsPath;
1434+
1435+
this.emit('request_create_from_template', {
1436+
type: <any>'null', // ignore it
1437+
name: projectname,
1438+
templateFile: targetTempFile,
1439+
outDir: new File(outputDirPath)
1440+
});
14051441
}
14061442

1407-
// notify
1408-
this.emit('request_create_from_template', {
1409-
type: <any>'null', // ignore it
1410-
name: name,
1411-
templateFile: targetTempFile,
1412-
outDir: new File(outDir[0].fsPath)
1413-
});
1443+
// clone git repo now
1444+
else if (targetCloneUrl) {
1445+
1446+
const outDir = await vscode.window.showOpenDialog({
1447+
openLabel: select_out_dir,
1448+
canSelectFiles: false,
1449+
canSelectFolders: true,
1450+
canSelectMany: false
1451+
});
1452+
1453+
if (outDir === undefined) {
1454+
this.locked = false;
1455+
return;
1456+
}
1457+
1458+
const outputDirPath = outDir[0].fsPath;
1459+
1460+
const done = await vscode.window.withProgress({
1461+
location: vscode.ProgressLocation.Notification,
1462+
title: `Cloning Template`,
1463+
cancellable: true,
1464+
}, (progress, cancel): Promise<boolean> => {
1465+
return new Promise(async (resolve) => {
1466+
progress.report({ message: targetCloneUrl });
1467+
const done = await utility.execInternalCommand(`git clone ${targetCloneUrl}`, outputDirPath, cancel);
1468+
if (done) {
1469+
try {
1470+
child_process.execSync(`git remote remove origin`, { cwd: outputDirPath });
1471+
} catch (error) {
1472+
// nothing todo
1473+
}
1474+
}
1475+
resolve(done);
1476+
});
1477+
});
1478+
1479+
if (done) {
1480+
const item = await vscode.window.showInformationMessage(view_str$operation$create_prj_done, 'Yes', 'Later');
1481+
if (item == 'Yes') {
1482+
WorkspaceManager.getInstance().openWorkspace(new File(outputDirPath));
1483+
}
1484+
} else {
1485+
GlobalEvent.emit('msg', newMessage('Warning', `Clone project failed !, Check log in 'eide.log' panel`));
1486+
}
1487+
}
14141488

14151489
this.locked = false;
14161490
}

src/WebInterface/WebInterface.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ export interface TemplateInfo {
4545

4646
download_url: string | undefined;
4747

48+
git_clone_url: string | undefined;
49+
4850
size: number | undefined;
4951

5052
disabled: boolean | undefined;

src/WorkspaceManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export class WorkspaceManager {
8888
}
8989

9090
/**
91-
* @param workspaceFile '.code-workspace' file obj
91+
* @param workspaceFile '.code-workspace' file or a folder
9292
*/
9393
openWorkspace(workspaceFile: File) {
9494
vscode.commands.executeCommand('vscode.openFolder', vscode.Uri.file(workspaceFile.path));

0 commit comments

Comments
 (0)