Skip to content

Commit ed38f58

Browse files
committed
Add Phpactor Update command
1 parent f1b1d53 commit ed38f58

File tree

4 files changed

+79
-20
lines changed

4 files changed

+79
-20
lines changed

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
],
2727
"contributes": {
2828
"commands": [
29+
{
30+
"command": "phpactor.update",
31+
"title": "Update Phpactor to the latest version"
32+
},
2933
{
3034
"command": "phpactor.reindex",
3135
"title": "Phpactor: Reindex the project."
@@ -47,8 +51,8 @@
4751
"title": "phpactor config",
4852
"properties": {
4953
"phpactor.path": {
50-
"type": "string",
51-
"default": "phpactor",
54+
"type": "string|null",
55+
"default": null,
5256
"description": "phpactor bin path"
5357
},
5458
"phpactor.enable": {

src/extension.ts

Lines changed: 70 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import {
55
} from "vscode-languageclient";
66

77
import * as vscode from "vscode";
8+
import { spawn } from "child_process";
9+
import { existsSync, mkdir, mkdirSync } from "fs";
10+
import { basename, dirname } from "path";
811

912
const LanguageID = 'php';
1013

@@ -15,10 +18,13 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
1518
const config = workspaceConfig.get("phpactor") as any;
1619
const enable = config.enable;
1720

21+
if (!config.path) {
22+
config.path = await installPhpactor(context.globalStoragePath)
23+
}
24+
1825
if (enable === false) return;
1926

2027
languageClient = createClient(config);
21-
2228
languageClient.start();
2329
}
2430

@@ -30,12 +36,6 @@ export function deactivate() {
3036
}
3137

3238
function createClient(config: any): LanguageClient {
33-
if (!config.path) {
34-
vscode.window.showWarningMessage(
35-
'The path to Phpactor must be configured (e.g. phpactor, or /path/to/phpactor)"
36-
);
37-
return;
38-
}
3939
let serverOptions: ServerOptions = {
4040
run: {
4141
command: config.path,
@@ -66,26 +66,27 @@ function createClient(config: any): LanguageClient {
6666
clientOptions
6767
);
6868

69-
7069
vscode.commands.registerCommand('phpactor.reindex', reindex);
7170
vscode.commands.registerCommand('phpactor.config.dump', dumpConfig);
7271
vscode.commands.registerCommand('phpactor.services.list', servicesList);
7372
vscode.commands.registerCommand('phpactor.status', status);
73+
const updateConfig = {cwd: dirname(dirname(config.path))}
74+
vscode.commands.registerCommand('phpactor.update', updatePhpactor, updateConfig);
7475

7576
return languageClient;
7677
}
7778

7879
function reindex(): void {
79-
if(!languageClient) {
80-
return;
80+
if(!languageClient) {
81+
return;
8182
}
8283

8384
languageClient.sendRequest('indexer/reindex');
8485
}
8586

8687
async function dumpConfig(): Promise<void> {
87-
if(!languageClient) {
88-
return;
88+
if(!languageClient) {
89+
return;
8990
}
9091

9192
const channel = vscode.window.createOutputChannel('Phpactor Config')
@@ -95,20 +96,73 @@ async function dumpConfig(): Promise<void> {
9596
}
9697

9798
function servicesList(): void {
98-
if(!languageClient) {
99-
return;
99+
if(!languageClient) {
100+
return;
100101
}
101102

102103
languageClient.sendRequest('service/running');
103104
}
104105

105106
async function status(): Promise<any> {
106-
if(!languageClient) {
107-
return;
107+
if(!languageClient) {
108+
return;
108109
}
109110

110111
const channel = vscode.window.createOutputChannel('Phpactor Status')
111112
const result = await languageClient.sendRequest<string>('phpactor/status');
112113
channel.append(result)
113114
channel.show()
114115
}
116+
117+
async function installPhpactor(storagePath: string): Promise<string> {
118+
const channel = vscode.window.createOutputChannel("Phpactor Installation")
119+
if (!existsSync(storagePath)) {
120+
mkdirSync(storagePath)
121+
}
122+
123+
const path = `${storagePath}/phpactor`
124+
125+
if (!existsSync(path)) {
126+
await exec(channel, 'git', ['clone', 'https://github.com/phpactor/phpactor'], storagePath)
127+
await exec(channel, 'composer', ['install'], path)
128+
vscode.window.showInformationMessage(`Phpactor installed at ${path}`)
129+
}
130+
131+
return `${storagePath}/phpactor/bin/phpactor`
132+
}
133+
134+
export async function updatePhpactor(): Promise<void> {
135+
const channel = vscode.window.createOutputChannel('Phpactor Update')
136+
channel.appendLine(this.cwd)
137+
await exec(channel, 'git', ['pull'], this.cwd)
138+
await exec(channel, 'composer', ['install'], this.cwd)
139+
channel.appendLine("Phpactor updated")
140+
vscode.window.showInformationMessage("Phpactor updated")
141+
}
142+
143+
144+
function exec(channel: vscode.OutputChannel, command: string, args: string[], cwd: string): Promise<void> {
145+
return new Promise(function (resolve, reject) {
146+
147+
const child = spawn(command, args, {
148+
cwd: cwd,
149+
timeout: 30000,
150+
})
151+
child.stdout.on('data', function (data) {
152+
channel.append(data.toString('utf8'))
153+
})
154+
child.stderr.on('data', function (data) {
155+
channel.append(data.toString('utf8'))
156+
})
157+
child.on('close', function (code) {
158+
if (code !== 0) {
159+
reject(`Expected git to exit with code 0 got "${code}"`)
160+
}
161+
resolve()
162+
});
163+
164+
child.on('error', function (err) {
165+
reject(err)
166+
});
167+
})
168+
}

src/test/suite/extension.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as assert from 'assert';
33
// You can import and use all API from the 'vscode' module
44
// as well as import your extension to test it
55
import * as vscode from 'vscode';
6-
// import * as myExtension from '../../extension';
6+
import * as myExtension from '../../extension';
77

88
suite('Extension Test Suite', () => {
99
test('Extension is present', async () => {

src/test/suite/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import glob from 'glob';
55
export function run(): Promise<void> {
66
// Create the mocha test
77
const mocha = new Mocha({
8-
ui: 'tdd'
8+
ui: 'tdd',
9+
timeout: 10000
910
});
1011

1112
const testsRoot = path.resolve(__dirname, '..');

0 commit comments

Comments
 (0)