Skip to content

Commit

Permalink
Merge pull request #62 from NicolasConstant/develop
Browse files Browse the repository at this point in the history
1.3.0 PR
  • Loading branch information
NicolasConstant authored Feb 24, 2022
2 parents 9cc5a2d + 161cf77 commit 1de6f1a
Show file tree
Hide file tree
Showing 23 changed files with 478 additions and 96 deletions.
6 changes: 3 additions & 3 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ try {
await download(win, info.url, info.properties)
.then(dl => event.sender.send('download-success', dl.getSavePath()));
} catch (err) {
event.sender.send('log-error', err);
event.sender.send('log-error', { info: info, error: err});
}
})();
});
Expand All @@ -103,7 +103,7 @@ try {

event.sender.send('extract-success', info);
} catch (err) {
event.sender.send('log-error', err);
event.sender.send('log-error', { info: info, error: err});
}
})();
});
Expand All @@ -120,7 +120,7 @@ try {
await pro;
event.sender.send('copy-folder-success', info);
} catch (err) {
event.sender.send('log-error', err);
event.sender.send('log-error', { info: info, error: err});
}
})();
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "msfs-community-downloader",
"version": "1.2.2",
"version": "1.3.0",
"description": "MSFS addin downloader client",
"homepage": "https://github.com/nicolasconstant/msfs-community-downloader",
"author": {
Expand Down
18 changes: 18 additions & 0 deletions src/app/core/models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export class FilePackageInfo {
packageId: string;
}

export class FileExtractedInfo extends FilePackageInfo {
filePath: string;
extractFolder: string;
}

export class FileDownloadInfo extends FilePackageInfo {
url: string;
properties: any;
filePath: string;
}

export class FileDownloadUpdate extends FilePackageInfo {
downloadedData: number;
}
129 changes: 111 additions & 18 deletions src/app/core/services/domain.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Injectable, ApplicationRef } from '@angular/core';
import { Subject, Subscription } from 'rxjs';

import { FilesystemService, LocalState, CopyFolderInfo } from './filesystem.service';
import { GithubService, PackageInfo } from './github.service';
import { Package, InstallStatusEnum, PackagesService } from './packages.service';
import { DownloaderService, FileDownloadInfo, FileDownloadUpdate } from './downloader.service';
import { Subscription } from 'rxjs';
import { ExtractorService, FileExtractedInfo } from './extractor.service';
import { BranchInfo, GithubService, PackageInfo } from './github.service';
import { Package, InstallStatusEnum, PackagesService, ReleaseTypeEnum } from './packages.service';
import { DownloaderService } from './downloader.service';
import { ExtractorService } from './extractor.service';
import { SettingsService } from './settings.service';
import { FileDownloadInfo, FileDownloadUpdate, FileExtractedInfo, FilePackageInfo } from '../models';
import { ElectronService } from './electron/electron.service';

@Injectable({
providedIn: 'root'
Expand All @@ -17,14 +20,17 @@ export class DomainService {
private extractSub: Subscription;
private copySub: Subscription;

public errorSubject = new Subject<string>();

constructor(
private app: ApplicationRef,
private packageService: PackagesService,
private filesystemService: FilesystemService,
private githubService: GithubService,
private downloaderService: DownloaderService,
private extractorService: ExtractorService,
private settingsService: SettingsService
private settingsService: SettingsService,
private electronService: ElectronService
) {
this.downloadSub = downloaderService.fileDownloaded.subscribe(r => {
if (r) {
Expand All @@ -46,6 +52,24 @@ export class DomainService {
this.processCopiedFolder(r);
}
});

this.electronService.ipcRenderer.on('log-error', (event, arg) => {
if (arg) {
const error = arg.error;
const info: FilePackageInfo = arg.info;

this.processPackageError(info);

console.error('Node error');
console.error(error);

if (error.message) {
this.errorSubject.next(error.message);
} else {
this.errorSubject.next(error);
}
}
});
}

analysePackages(packages: Package[]): Promise<any> {
Expand All @@ -71,41 +95,79 @@ export class DomainService {

private analysePackage(p: Package): Promise<any> {
const localPromise = this.filesystemService.retrievePackageInfo(p);
const githubPromise = this.githubService.retrievePackageInfo(p);

return Promise.all([localPromise, githubPromise])
let githubPromise: Promise<PackageInfo> = Promise.resolve(null);
if (p.releaseType === ReleaseTypeEnum.release) {
githubPromise = this.githubService.retrievePackageInfo(p);
} else if (p.releaseType === ReleaseTypeEnum.releaseFromBranch) {
githubPromise = this.githubService.retrievePackageInfoFromUniqueTag(p);
}

let branchPromise: Promise<BranchInfo> = Promise.resolve(null);
if (p.releaseType === ReleaseTypeEnum.branch || p.releaseType === ReleaseTypeEnum.releaseFromBranch) {
branchPromise = this.githubService.retrieveBranchInfo(p);
}

return Promise.all([localPromise, githubPromise, branchPromise])
.then(result => {
const local = result[0];
const remote = result[1];
const branch = result[2];

if (local) {
p.localVersion = local.version;
}

if (remote) {
p.availableVersion = remote.availableVersion;
// if (remote || p.releaseType === ReleaseTypeEnum.branch) {
if (p.releaseType === ReleaseTypeEnum.branch) {
p.assetDownloadUrl = branch.downloadUrl;
} else {
p.assetDownloadUrl = remote.downloadUrl;
}

if (p.releaseType === ReleaseTypeEnum.branch || p.releaseType === ReleaseTypeEnum.releaseFromBranch) {
p.availableVersion = branch.hashVersion;
} else {
p.availableVersion = remote.availableVersion;
}

if (p.releaseType === ReleaseTypeEnum.release || p.releaseType === ReleaseTypeEnum.releaseFromBranch) {
p.publishedAt = remote.publishedAt;
p.html_url = remote.html_url;
p.fileSize = remote.fileSize;
} else if (p.releaseType === ReleaseTypeEnum.branch) {
p.publishedAt = branch.publishedAt;
}

p.state = this.getState(p, local, remote);
// }
p.state = this.getState(p, local);
});
}

private getState(p: Package, local: LocalState, info: PackageInfo): InstallStatusEnum {
private getDifferenceInSeconds(date1: Date, date2: Date) {
const diffInMs = Math.abs(date2.getTime() - date1.getTime());
const result = diffInMs / 1000;
return result;
}

private getState(p: Package, local: LocalState): InstallStatusEnum {
if (p.state === InstallStatusEnum.error) return InstallStatusEnum.error;
if (p.state === InstallStatusEnum.downloading) return InstallStatusEnum.downloading;
if (p.state === InstallStatusEnum.extracting) return InstallStatusEnum.extracting;
if (p.state === InstallStatusEnum.installing) return InstallStatusEnum.installing;

if (local && local.untrackedFolderFound) return InstallStatusEnum.untrackedPackageFound;
if (local && !local.folderFound) return InstallStatusEnum.notFound;
if (local && local.version && info && info.availableVersion) {
if (local.version === info.availableVersion) return InstallStatusEnum.installed;
if (local.version !== info.availableVersion) return InstallStatusEnum.updateAvailable;
if (local && local.version && p && p.availableVersion) {
if (p.releaseType === ReleaseTypeEnum.release) {
if (local.version === p.availableVersion) return InstallStatusEnum.installed;
if (local.version !== p.availableVersion) return InstallStatusEnum.updateAvailable;
} else {
if (local.version !== p.availableVersion) return InstallStatusEnum.updateAvailable;
if (this.getDifferenceInSeconds(new Date(local.publishedAt), new Date(p.publishedAt)) > 5) return InstallStatusEnum.updateAvailable;
if (local.version === p.availableVersion) return InstallStatusEnum.installed;
}
}

if (p.state === InstallStatusEnum.error) return InstallStatusEnum.error;
return InstallStatusEnum.unknown;
}

Expand All @@ -118,6 +180,13 @@ export class DomainService {
this.extractorService.extract(downloadedPackage.id, r.filePath);
}

processPackageError(r: FilePackageInfo) {
const errorPackage = this.packages.find(x => x.id === r.packageId);
errorPackage.state = InstallStatusEnum.error;
errorPackage.downloaded = null;
this.app.tick();
}

processDownloadedUpdate(r: FileDownloadUpdate): void {
const downloadedPackage = this.packages.find(x => x.id === r.packageId);
downloadedPackage.downloaded = r.downloadedData;
Expand Down Expand Up @@ -164,7 +233,7 @@ export class DomainService {
this.filesystemService.deleteFolder(p.tempWorkingDir);
p.tempWorkingDir = null;
}
this.filesystemService.writeVersionFile(r.target, p.availableVersion);
this.filesystemService.writeVersionFile(r.target, p);
p.localVersion = p.availableVersion;
p.state = InstallStatusEnum.installed;
this.app.tick();
Expand Down Expand Up @@ -248,6 +317,9 @@ export class DomainService {
toUpdate.illustration = p.illustration;
toUpdate.webpageUrl = p.webpageUrl;
toUpdate.versionPatternToRemove = p.versionPatternToRemove;
toUpdate.releaseType = p.releaseType;
toUpdate.branchName = p.branchName;
toUpdate.releaseBranchTag = p.releaseBranchTag;

this.settingsService.saveSettings(settings);

Expand All @@ -265,6 +337,9 @@ export class DomainService {
localToUpdate.illustration = p.illustration;
localToUpdate.webpageUrl = p.webpageUrl;
localToUpdate.versionPatternToRemove = p.versionPatternToRemove;
localToUpdate.releaseType = p.releaseType;
localToUpdate.branchName = p.branchName;
localToUpdate.releaseBranchTag = p.releaseBranchTag;
}

updateOnlinePackage(p: Package): void {
Expand All @@ -286,6 +361,9 @@ export class DomainService {
toUpdate.webpageUrl = p.webpageUrl;
toUpdate.versionPatternToRemove = p.versionPatternToRemove;
toUpdate.onlineVersion = p.onlineVersion;
toUpdate.releaseType = p.releaseType;
toUpdate.branchName = p.branchName;
toUpdate.releaseBranchTag = p.releaseBranchTag;

this.settingsService.saveSettings(settings);

Expand All @@ -304,6 +382,9 @@ export class DomainService {
localToUpdate.webpageUrl = p.webpageUrl;
localToUpdate.versionPatternToRemove = p.versionPatternToRemove;
localToUpdate.onlineVersion = p.onlineVersion;
localToUpdate.releaseType = p.releaseType;
localToUpdate.branchName = p.branchName;
localToUpdate.releaseBranchTag = p.releaseBranchTag;
}

removeCustomPackage(p: Package): void {
Expand Down Expand Up @@ -342,6 +423,18 @@ export class DomainService {
this.install(p);
}

resetPackage(packageId: string): void {
const p = this.packages.find(x => x.id === packageId);
p.state = InstallStatusEnum.unknown;
this.analysePackage(p)
.catch(err => {
console.error(err);
})
.then(_ => {
this.app.tick();
});
}

remove(p: Package): void {
const communityDir = this.settingsService.getSettings().communityPath;

Expand Down
16 changes: 3 additions & 13 deletions src/app/core/services/downloader.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Injectable } from '@angular/core';
import { ElectronService } from './electron/electron.service';
import { BehaviorSubject, Subject } from 'rxjs';

import { FileDownloadInfo, FileDownloadUpdate } from '../models';
import { ElectronService } from './electron/electron.service';

@Injectable({
providedIn: 'root'
})
Expand Down Expand Up @@ -43,15 +45,3 @@ export class DownloaderService {
this.electronService.ipcRenderer.send('download-item', info);
}
}

export class FileDownloadInfo {
packageId: string;
url: string;
properties: any;
filePath: string;
}

export class FileDownloadUpdate {
packageId: string;
downloadedData: number;
}
8 changes: 2 additions & 6 deletions src/app/core/services/extractor.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';

import { FileExtractedInfo } from '../models';
import { ElectronService } from './electron/electron.service';

@Injectable({
Expand Down Expand Up @@ -29,9 +31,3 @@ export class ExtractorService {
this.electronService.ipcRenderer.send('extract-item', info);
}
}

export class FileExtractedInfo {
packageId: string;
filePath: string;
extractFolder: string;
}
Loading

0 comments on commit 1de6f1a

Please sign in to comment.