Commit 4498e177 authored by wudizhanche1000's avatar wudizhanche1000

运行和卸载mod

parent 6a4f6518
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
class="progress progress-striped progress-animated" class="progress progress-striped progress-animated"
value="{{currentApp.status.progress}}" max="{{currentApp.status.total}}"></progress> value="{{currentApp.status.progress}}" max="{{currentApp.status.total}}"></progress>
<div *ngIf="currentApp.status.status==='ready' && (currentApp.id != 'ygopro')"> <div *ngIf="currentApp.status.status==='ready' && (currentApp.id != 'ygopro')">
<button (click)="startApp(currentApp)" type="button" class="btn btn-primary">运行</button> <button (click)="runApp(currentApp)" type="button" class="btn btn-primary">运行</button>
<button type="button" data-toggle="modal" data-target="#settings-modal" class="btn btn-secondary">设置</button> <button type="button" data-toggle="modal" data-target="#settings-modal" class="btn btn-secondary">设置</button>
<!--<button (click)="appsService.browse(currentApp)" type="button" class="btn btn-secondary">游览本地文件</button>--> <!--<button (click)="appsService.browse(currentApp)" type="button" class="btn btn-secondary">游览本地文件</button>-->
...@@ -114,7 +114,7 @@ ...@@ -114,7 +114,7 @@
<div *ngIf="currentApp.isInstalled()"> <div *ngIf="currentApp.isInstalled()">
<h2>本地文件</h2> <h2>本地文件</h2>
<button (click)="appsService.browse(currentApp)" type="button" class="btn btn-secondary">浏览本地文件</button> <button (click)="appsService.browse(currentApp)" type="button" class="btn btn-secondary">浏览本地文件</button>
<button (click)="uninstall(currentApp.id)" type="button" class="btn btn-secondary"> <button (click)="uninstall(currentApp)" type="button" class="btn btn-secondary">
{{'uninstall'|translate}} {{'uninstall'|translate}}
</button> </button>
</div> </div>
...@@ -202,7 +202,8 @@ ...@@ -202,7 +202,8 @@
<h4>{{'additions'|translate}}</h4> <h4>{{'additions'|translate}}</h4>
<div *ngFor="let reference of installConfig.references"> <div *ngFor="let reference of installConfig.references">
<label> <label>
<input type="checkbox" [(ngModel)]="reference.install" name="references" value="{{reference.app.id}}"> <input type="checkbox" [(ngModel)]="reference.install" name="references"
value="{{reference.app.id}}">
{{reference.app.name}} {{reference.app.name}}
</label> </label>
</div> </div>
......
...@@ -30,11 +30,6 @@ export class AppDetailComponent implements OnInit { ...@@ -30,11 +30,6 @@ export class AppDetailComponent implements OnInit {
} }
ngOnInit() { ngOnInit() {
// this.updateInstallConfig();
ipcRenderer.on('download-message-reply', (event, arg)=> {
console.log(arg);
});
ipcRenderer.send("download-message", "ping")
} }
updateInstallConfig() { updateInstallConfig() {
...@@ -54,14 +49,6 @@ export class AppDetailComponent implements OnInit { ...@@ -54,14 +49,6 @@ export class AppDetailComponent implements OnInit {
return this.currentApp.news; return this.currentApp.news;
} }
get friends() {
return false;
}
get achievement() {
return false;
}
get mods() { get mods() {
// let contains = ["optional", "language", "emulator"]; // let contains = ["optional", "language", "emulator"];
// //
...@@ -95,15 +82,11 @@ export class AppDetailComponent implements OnInit { ...@@ -95,15 +82,11 @@ export class AppDetailComponent implements OnInit {
return []; return [];
} }
uninstalling: boolean;
uninstall(id: string) { async uninstall(app: App) {
if (confirm("确认删除?")) { if (confirm("确认删除?")) {
this.uninstalling = true; await this.installService.uninstall(app);
// this.appsService.uninstall(id).then(()=> { this.currentApp.status.status = "init";
// this.uninstalling = false;
// }
// );
} }
} }
...@@ -148,9 +131,7 @@ export class AppDetailComponent implements OnInit { ...@@ -148,9 +131,7 @@ export class AppDetailComponent implements OnInit {
return this.installService.add(completeApp, options); return this.installService.add(completeApp, options);
}); });
})); }));
console.log("before")
await this.installService.getComplete(currentApp); await this.installService.getComplete(currentApp);
console.log("install complete");
currentApp.status.status = "ready"; currentApp.status.status = "ready";
this.ref.detectChanges(); this.ref.detectChanges();
} catch (e) { } catch (e) {
...@@ -167,42 +148,8 @@ export class AppDetailComponent implements OnInit { ...@@ -167,42 +148,8 @@ export class AppDetailComponent implements OnInit {
return dir[0]; return dir[0];
} }
startApp(app: App) { runApp(app: App) {
let execute = path.join(app.local.path, app.actions.get("main").execute); this.appsService.runApp(app);
let args = app.actions.get("main").args;
let env = app.actions.get("main").env;
let opt = {
cwd: app.local.path,
env: env
};
let open = '';
let openApp = app.actions.get("main").open;
if (openApp) {
open = path.join(openApp.local.path, openApp.actions.get("main").execute);
args.push(execute);
} else {
//没有需要通过open启动依赖,直接启动程序
open = execute;
}
let handle = child_process.spawn(open, args, opt);
handle.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
handle.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
handle.on('close', (code) => {
console.log(`child process exited with code ${code}`);
remote.getCurrentWindow().restore();
});
remote.getCurrentWindow().minimize();
} }
copy(text) { copy(text) {
......
...@@ -55,7 +55,7 @@ export class App { ...@@ -55,7 +55,7 @@ export class App {
status: AppStatus; status: AppStatus;
isInstalled(): boolean { isInstalled(): boolean {
return this.local !== undefined; return !!this.local;
} }
constructor(app) { constructor(app) {
......
...@@ -35,6 +35,8 @@ Sudo.prototype.fork = function (modulePath, args, options) { ...@@ -35,6 +35,8 @@ Sudo.prototype.fork = function (modulePath, args, options) {
@Injectable() @Injectable()
export class AppsService { export class AppsService {
private apps: Map<string,App>;
constructor(private http: Http, private settingsService: SettingsService, private ref: ApplicationRef,) { constructor(private http: Http, private settingsService: SettingsService, private ref: ApplicationRef,) {
} }
...@@ -44,7 +46,8 @@ export class AppsService { ...@@ -44,7 +46,8 @@ export class AppsService {
.toPromise() .toPromise()
.then((response)=> { .then((response)=> {
let data = response.json(); let data = response.json();
return this.loadAppsList(data); this.apps = this.loadAppsList(data);
return this.apps;
}); });
} }
...@@ -123,55 +126,52 @@ export class AppsService { ...@@ -123,55 +126,52 @@ export class AppsService {
return apps; return apps;
}; };
findChildren(app: App): App[] {
deleteFile(path: string): Promise<string> { let children = [];
return new Promise((resolve, reject)=> { for (let child of this.apps.values()) {
fs.lstat(path, (err, stats)=> { if (child.parent === app) {
if (err) return resolve(path); children.push(child);
if (stats.isDirectory()) { }
fs.rmdir(path, (err)=> { }
resolve(path); return children;
});
} else {
fs.unlink(path, (err)=> {
resolve(path);
});
}
});
})
} }
saveAppLocal(app: App, appLocal: AppLocal) { runApp(app: App) {
localStorage.setItem(app.id, JSON.stringify(appLocal)); let children = this.findChildren(app);
} let cwd = app.local.path;
let action = app.actions.get('main');
let args = [];
let env = {};
for (let child of children) {
action = child.actions.get('main');
}
let execute = path.join(cwd, action.execute);
if (action.open) {
let openAction = action.open.actions.get('main');
args = args.concat(openAction.args);
args.push(action.execute);
execute = path.join(action.open.local.path, openAction.execute);
env = Object.assign(env, openAction.env);
}
args = args.concat(action.args);
env = Object.assign(env, action.env);
let handle = child_process.spawn(execute, args, {env: env, cwd: cwd});
install(config: InstallConfig) { handle.stdout.on('data', (data) => {
let app = config.app; console.log(`stdout: ${data}`);
} });
uninstall(id: string) { handle.stderr.on('data', (data) => {
// //let current = this; console.log(`stderr: ${data}`);
// if (this.checkInstall(id)) { });
// let files: string[] = this.searchApp(id).local.files.sort().reverse();
// // 删除本目录
// files.push('.');
// let install_dir = this.searchApp(id).local.path;
// return files
// .map((file)=>
// ()=>path.join(install_dir, file)
// )
// .reduce((promise: Promise<string>, task)=>
// promise.then(task).then(this.deleteFile)
// , Promise.resolve(''))
// .then((value)=> {
// this.searchApp(id).local = null;
// localStorage.setItem("localAppData", JSON.stringify(this.data));
// return Promise.resolve()
// });
// }
} handle.on('close', (code) => {
console.log(`child process exited with code ${code}`);
remote.getCurrentWindow().restore();
});
remote.getCurrentWindow().minimize();
}
browse(app: App) { browse(app: App) {
remote.shell.showItemInFolder(app.local.path); remote.shell.showItemInFolder(app.local.path);
......
...@@ -14,6 +14,7 @@ import {EventEmitter} from "events"; ...@@ -14,6 +14,7 @@ import {EventEmitter} from "events";
import {AppLocal} from "./app-local"; import {AppLocal} from "./app-local";
import {Http} from "@angular/http"; import {Http} from "@angular/http";
import ReadableStream = NodeJS.ReadableStream; import ReadableStream = NodeJS.ReadableStream;
import {AppsService} from "./apps.service";
@Injectable() @Injectable()
export class InstallService { export class InstallService {
...@@ -25,7 +26,7 @@ export class InstallService { ...@@ -25,7 +26,7 @@ export class InstallService {
checksumUri = "http://thief.mycard.moe/checksums/"; checksumUri = "http://thief.mycard.moe/checksums/";
constructor(private http: Http) { constructor(private http: Http, private appsService: AppsService) {
if (process.platform === "win32") { if (process.platform === "win32") {
this.tarPath = path.join(process.resourcesPath, 'bin/tar.exe'); this.tarPath = path.join(process.resourcesPath, 'bin/tar.exe');
} else { } else {
...@@ -43,7 +44,6 @@ export class InstallService { ...@@ -43,7 +44,6 @@ export class InstallService {
getComplete(app: App): Promise<App> { getComplete(app: App): Promise<App> {
return new Promise((resolve, reject)=> { return new Promise((resolve, reject)=> {
this.eventEmitter.once(app.id, (complete)=> { this.eventEmitter.once(app.id, (complete)=> {
console.log("receive ",app.id);
resolve(); resolve();
}); });
}); });
...@@ -103,9 +103,7 @@ export class InstallService { ...@@ -103,9 +103,7 @@ export class InstallService {
saveAppLocal(app: App) { saveAppLocal(app: App) {
if (app.local) { if (app.local) {
let a = JSON.stringify(app.local) localStorage.setItem(app.id, JSON.stringify(app.local));
console.log(a);
localStorage.setItem(app.id, a);
} }
} }
...@@ -164,7 +162,6 @@ export class InstallService { ...@@ -164,7 +162,6 @@ export class InstallService {
local.version = app.version; local.version = app.version;
app.local = local; app.local = local;
this.saveAppLocal(app); this.saveAppLocal(app);
console.log("send ",app.id);
this.eventEmitter.emit(app.id, 'install complete'); this.eventEmitter.emit(app.id, 'install complete');
this.installQueue.delete(app); this.installQueue.delete(app);
this.installingQueue.delete(app); this.installingQueue.delete(app);
...@@ -184,4 +181,55 @@ export class InstallService { ...@@ -184,4 +181,55 @@ export class InstallService {
} }
} }
deleteFile(file: string): Promise<string> {
return new Promise((resolve, reject)=> {
fs.lstat(file, (err, stats)=> {
if (err) return resolve(path);
if (stats.isDirectory()) {
fs.rmdir(file, (err)=> {
resolve(file);
});
} else {
fs.unlink(file, (err)=> {
resolve(file);
});
}
});
})
}
async uninstall(app: App) {
if (!app.parent) {
let children = this.appsService.findChildren(app);
for (let child of children) {
if(child.isInstalled()) {
await this.uninstall(child);
}
}
}
let files = Array.from(app.local.files.keys()).sort().reverse();
console.log(files);
for (let file of files) {
let oldFile = file;
if (!path.isAbsolute(file)) {
oldFile = path.join(app.local.path, file);
}
await this.deleteFile(oldFile);
if (app.parent) {
let backFile = path.join(app.local.path, "backup", file);
await new Promise((resolve, reject)=> {
fs.rename(backFile, oldFile, resolve);
});
}
}
if (app.parent) {
await this.deleteFile(path.join(app.local.path, "backup"));
} else {
await this.deleteFile(app.local.path);
}
app.local = null;
localStorage.removeItem(app.id);
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment