Skip to content

Commit 479bca2

Browse files
committed
refactor: get repo info.
1 parent e3a1c74 commit 479bca2

File tree

5 files changed

+387
-277
lines changed

5 files changed

+387
-277
lines changed

package.json

+88-88
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,98 @@
11
{
2-
"name": "coding-plugin",
3-
"description": "Coding plugin for VS Code.",
4-
"version": "0.0.1",
5-
"publisher": "alcheung",
6-
"license": "MIT",
7-
"engines": {
8-
"vscode": "^1.47.0"
9-
},
10-
"categories": [
11-
"Other"
12-
],
13-
"activationEvents": [
14-
"onCommand:codingPlugin.show",
15-
"onCommand:codingPlugin.login",
16-
"onWebviewPanel:codingPlugin",
17-
"onView:treeviewSample"
18-
],
19-
"repository": {
20-
"type": "git",
21-
"url": "https://github.com/microsoft/vscode-extension-samples.git"
22-
},
23-
"main": "./out/extension.js",
24-
"contributes": {
25-
"commands": [
26-
{
27-
"command": "codingPlugin.show",
28-
"title": "Start coding session",
29-
"category": "Coding plugin"
30-
},
31-
{
32-
"command": "codingPlugin.login",
33-
"title": "Login coding.net",
34-
"category": "Coding plugin"
35-
}
2+
"name": "coding-plugin",
3+
"description": "Coding plugin for VS Code.",
4+
"version": "0.0.1",
5+
"publisher": "alcheung",
6+
"license": "MIT",
7+
"engines": {
8+
"vscode": "^1.47.0"
9+
},
10+
"categories": [
11+
"Other"
12+
],
13+
"activationEvents": [
14+
"onCommand:codingPlugin.show",
15+
"onCommand:codingPlugin.login",
16+
"onWebviewPanel:codingPlugin",
17+
"onView:treeviewSample"
18+
],
19+
"repository": {
20+
"type": "git",
21+
"url": "https://github.com/microsoft/vscode-extension-samples.git"
22+
},
23+
"main": "./out/extension.js",
24+
"contributes": {
25+
"commands": [
26+
{
27+
"command": "codingPlugin.show",
28+
"title": "Start coding session",
29+
"category": "Coding plugin"
30+
},
31+
{
32+
"command": "codingPlugin.login",
33+
"title": "Login coding.net",
34+
"category": "Coding plugin"
35+
}
3636
],
3737
"viewsWelcome": [
3838
{
3939
"view": "treeviewSample",
4040
"contents": "Please login first.\n[Login](command:codingPlugin.login)"
4141
}
4242
],
43-
"viewsContainers": {
44-
"activitybar": [
45-
{
46-
"id": "treeview-sample",
47-
"title": "Treeview Sample",
43+
"viewsContainers": {
44+
"activitybar": [
45+
{
46+
"id": "treeview-sample",
47+
"title": "Treeview Sample",
4848
"icon": "src/assets/coding.svg"
49-
}
50-
]
51-
},
52-
"views": {
53-
"treeview-sample": [
54-
{
55-
"id": "treeviewSample",
56-
"name": "Treeview Sample",
57-
"icon": "src/assets/coding.svg",
58-
"contextualTitle": "List"
59-
}
60-
]
61-
}
62-
},
63-
"scripts": {
64-
"postinstall": "cd src/typings && npx vscode-dts master && npx vscode-dts dev master",
65-
"vscode:prepublish": "npm run compile",
66-
"compile": "npm-run-all -p compile:*",
67-
"compile:extension": "tsc -p ./src",
68-
"compile:webviews": "webpack --config webpack.config.js",
69-
"watch": "npm-run-all -p watch:*",
70-
"watch:extension": "tsc -watch -p ./src",
71-
"watch:webviews": "webpack --watch --mode development",
72-
"lint": "eslint . --ext .ts,.tsx"
73-
},
74-
"dependencies": {
75-
"@risingstack/react-easy-state": "^6.3.0",
76-
"got": "^11.7.0",
77-
"keytar": "^7.0.0",
78-
"ky": "^0.24.0",
79-
"nanoid": "^3.1.16",
80-
"react": "^17.0.0",
81-
"react-dom": "^17.0.0",
82-
"simple-git": "^2.21.0"
83-
},
84-
"devDependencies": {
85-
"@types/react": "^16.9.53",
86-
"@types/react-dom": "^16.9.8",
87-
"@typescript-eslint/eslint-plugin": "^3.0.2",
88-
"@typescript-eslint/parser": "^3.0.2",
89-
"css-loader": "^5.0.0",
90-
"eslint": "^7.1.0",
91-
"npm-run-all": "^4.1.5",
92-
"style-loader": "^2.0.0",
93-
"ts-loader": "^8.0.5",
94-
"typescript": "^4.0.3",
95-
"webpack": "4",
96-
"webpack-cli": "^4.0.0"
97-
}
49+
}
50+
]
51+
},
52+
"views": {
53+
"treeview-sample": [
54+
{
55+
"id": "treeviewSample",
56+
"name": "Treeview Sample",
57+
"icon": "src/assets/coding.svg",
58+
"contextualTitle": "List"
59+
}
60+
]
61+
}
62+
},
63+
"scripts": {
64+
"postinstall": "cd src/typings && npx vscode-dts master && npx vscode-dts dev master",
65+
"vscode:prepublish": "npm run compile",
66+
"compile": "npm-run-all -p compile:*",
67+
"compile:extension": "tsc -p ./src",
68+
"compile:webviews": "webpack --config webpack.config.js",
69+
"watch": "npm-run-all -p watch:*",
70+
"watch:extension": "tsc -watch -p ./src",
71+
"watch:webviews": "webpack --watch --mode development",
72+
"lint": "eslint . --ext .ts,.tsx"
73+
},
74+
"dependencies": {
75+
"@risingstack/react-easy-state": "^6.3.0",
76+
"got": "^11.7.0",
77+
"keytar": "^7.0.0",
78+
"ky": "^0.24.0",
79+
"nanoid": "^3.1.16",
80+
"react": "^17.0.0",
81+
"react-dom": "^17.0.0",
82+
"simple-git": "^2.21.0"
83+
},
84+
"devDependencies": {
85+
"@types/react": "^16.9.53",
86+
"@types/react-dom": "^16.9.8",
87+
"@typescript-eslint/eslint-plugin": "^3.0.2",
88+
"@typescript-eslint/parser": "^3.0.2",
89+
"css-loader": "^5.0.0",
90+
"eslint": "^7.1.0",
91+
"npm-run-all": "^4.1.5",
92+
"style-loader": "^2.0.0",
93+
"ts-loader": "^8.0.5",
94+
"typescript": "^4.0.3",
95+
"webpack": "4",
96+
"webpack-cli": "^4.0.0"
97+
}
9898
}

src/codingServer.ts

+3-9
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,10 @@ export class CodingServer {
117117
}
118118

119119
public static async getRepoParams() {
120-
const gitSrv = await GitService.getBuiltInGitApi();
120+
const urls = await GitService.getRemoteURLs();
121121
// TODO: multiple working repos
122-
const repoInstance = gitSrv?.repositories[0];
123-
124-
if (!repoInstance) {
125-
return null;
126-
}
127-
128-
const cloneUrl = await repoInstance.getConfig(`remote.origin.url`);
129-
return parseCloneUrl(cloneUrl);
122+
const url = urls?.[0];
123+
return parseCloneUrl(url || ``);
130124
}
131125

132126
public async getMRList(

src/common/gitService.ts

+25-11
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,36 @@
11
import * as vscode from 'vscode';
2-
import { API as BuiltInGitApi, GitExtension, Repository } from '../typings/git';
2+
import * as cp from 'child_process';
3+
import { promisify } from 'util';
4+
5+
import { GitExtension } from '../typings/git';
6+
7+
const exec = promisify(cp.exec);
38

49
export class GitService {
5-
static async getBuiltInGitApi(): Promise<BuiltInGitApi | undefined> {
10+
static async getRemoteURLs(): Promise<string[] | null> {
611
try {
7-
const extension = vscode.extensions.getExtension('vscode.git') as vscode.Extension<GitExtension>;
12+
const extension = vscode.extensions.getExtension(`vscode.git`) as vscode.Extension<GitExtension>;
813
if (extension !== undefined) {
914
const gitExtension = extension.isActive ? extension.exports : await extension.activate();
15+
const model = gitExtension.getAPI(1);
1016

11-
return gitExtension.getAPI(1);
12-
}
13-
} catch { }
17+
if (vscode.workspace.workspaceFolders?.length) {
18+
const tasks = vscode.workspace.workspaceFolders.map(f =>
19+
exec(`${model.git.path} config --get remote.origin.url`, {
20+
cwd: f.uri.path,
21+
}));
22+
const result = await Promise.all(tasks);
23+
const urls = result.map(({ stdout, stderr }) => {
24+
return stdout.trim();
25+
});
1426

15-
return;
16-
}
27+
return urls;
28+
}
29+
}
30+
} catch (err) {
31+
console.error(err);
32+
}
1733

18-
static async getRemoteUrl(repo: Repository): Promise<string> {
19-
const url = await repo.getConfig(`remote.origin.url`);
20-
return url;
34+
return null;
2135
}
2236
}

src/extension.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import * as vscode from 'vscode';
22

3-
import Logger from './common/logger';
3+
// import Logger from './common/logger';
44
import { uriHandler, CodingServer } from './codingServer';
5-
import { CodingAuthenticationProvider } from './coding'
5+
import { CodingAuthenticationProvider } from './coding';
66
import { Panel } from './panel';
77
import { ListProvider } from './tree';
88

99
export async function activate(context: vscode.ExtensionContext) {
10-
console.log(`actived`);
10+
console.log(`activated`);
1111
const repoInfo = await CodingServer.getRepoParams();
1212
console.log(`repo `, repoInfo);
1313
const codingAuth = new CodingAuthenticationProvider(repoInfo);
@@ -19,38 +19,38 @@ export async function activate(context: vscode.ExtensionContext) {
1919
context.subscriptions.push(
2020
vscode.commands.registerCommand('codingPlugin.show', () => {
2121
Panel.createOrShow(context);
22-
})
22+
}),
2323
);
2424

2525
context.subscriptions.push(
2626
vscode.commands.registerCommand('codingPlugin.openConvertPage', k => {
2727
Panel.createOrShow(context);
2828
Panel.currentPanel?.broadcast(`UPDATE_CURRENCY`, k);
29-
})
29+
}),
3030
);
3131

3232
context.subscriptions.push(
3333
vscode.commands.registerCommand('codingPlugin.login', async () => {
3434
const session = await codingAuth.login(repoInfo?.team || ``);
3535
if (!session?.accessToken) {
36-
console.error(`No token provided.`)
36+
console.error(`No token provided.`);
3737
}
3838

3939
console.log(`session: `, session);
40-
})
40+
}),
4141
);
4242

4343
vscode.window.registerTreeDataProvider(
4444
`treeviewSample`,
45-
new ListProvider(context, service, repoInfo)
45+
new ListProvider(context, service, repoInfo),
4646
);
4747

4848
if (vscode.window.registerWebviewPanelSerializer) {
4949
// Make sure we register a serializer in activation event
5050
vscode.window.registerWebviewPanelSerializer(Panel.viewType, {
5151
async deserializeWebviewPanel(webviewPanel: vscode.WebviewPanel, state: any) {
5252
Panel.revive(webviewPanel, context.extensionUri, context.extensionPath);
53-
}
53+
},
5454
});
5555
}
5656
}

0 commit comments

Comments
 (0)