Skip to content

Commit

Permalink
Remove non existent app icons from icon stores (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverschwendener committed Dec 5, 2018
1 parent 4e19eff commit 1fa3fea
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 26 deletions.
44 changes: 31 additions & 13 deletions src/ts/icon-service/mac-os-app-icon-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { AppIconStoreHelpers } from "../helpers/app-icon-store-helpers";

export class MacOsAppIconStore implements AppIconStore {
private readonly storePath: string;
private readonly icons: ApplicationIcon[] = [];
private icons: ApplicationIcon[] = [];
private readonly iconSet: IconSet;

constructor(storePath: string, iconSet: IconSet) {
Expand All @@ -26,22 +26,40 @@ export class MacOsAppIconStore implements AppIconStore {
}

public init(searchResultItems: SearchResultItem[]): void {
this.icons = [];

if (!existsSync(this.storePath)) {
mkdirSync(this.storePath);
}

searchResultItems.filter((searchResultItem: SearchResultItem) => {
return searchResultItem.icon === this.iconSet.appIcon;
}).forEach((searchResultItem: SearchResultItem) => {
const appFilePath = normalize(searchResultItem.executionArgument);
const appName = searchResultItem.name;
const outFilePath = join(this.storePath, `${AppIconStoreHelpers.buildIconFileName(appName)}.png`);

convert(appFilePath, outFilePath).then(() => {
this.addIcon({ name: appName, PNGFilePath: outFilePath });
}).catch(() => {
// do nothing
searchResultItems
.filter((searchResultItem: SearchResultItem) => {
return searchResultItem.icon === this.iconSet.appIcon;
}).forEach((searchResultItem: SearchResultItem) => {
const appFilePath = normalize(searchResultItem.executionArgument);
const appName = searchResultItem.name;
const outFilePath = join(this.storePath, `${AppIconStoreHelpers.buildIconFileName(appName)}.png`);

convert(appFilePath, outFilePath).then(() => {
this.addIcon({ name: appName, PNGFilePath: outFilePath });
}).catch((err) => {
// tslint:disable-next-line:no-console
console.log(`Error while generating app icon: ${err}`);
});
});
});

this.removeNonExistentIcons();
}

private removeNonExistentIcons(): void {
const indexesToDelete = [];

for (let i = 0; i < this.icons.length; i++) {
if (!existsSync(this.icons[i].PNGFilePath)) {
indexesToDelete.push(i);
}
}

indexesToDelete.forEach((i) => this.icons.splice(i, 1));
}
}
42 changes: 29 additions & 13 deletions src/ts/icon-service/windows-app-icon-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { AppIconStoreHelpers } from "../helpers/app-icon-store-helpers";
import { existsSync, mkdirSync } from "fs";

export class WindowsAppIconStore implements AppIconStore {
private readonly icons: ApplicationIcon[] = [];
private icons: ApplicationIcon[] = [];
private readonly storePath: string;
private readonly iconSet: IconSet;

Expand All @@ -26,6 +26,8 @@ export class WindowsAppIconStore implements AppIconStore {
}

public init(searchResultItems: SearchResultItem[]): void {
this.icons = [];

if (!existsSync(this.storePath)) {
mkdirSync(this.storePath);
}
Expand All @@ -38,29 +40,43 @@ export class WindowsAppIconStore implements AppIconStore {

ps.addCommand(`Add-Type -AssemblyName System.Drawing`);

const apps = searchResultItems.filter((searchResultItem: SearchResultItem) => {
return searchResultItem.icon === this.iconSet.appIcon;
});
searchResultItems
.filter((searchResultItem: SearchResultItem) => {
return searchResultItem.icon === this.iconSet.appIcon;
}).filter((app) => {
const inFilePath = normalize(app.executionArgument);
const appName = app.name;
const outFilePath = join(this.storePath, `${AppIconStoreHelpers.buildIconFileName(appName)}.png`);

for (const app of apps) {
const inFilePath = normalize(app.executionArgument);
const appName = app.name;
const outFilePath = join(this.storePath, `${AppIconStoreHelpers.buildIconFileName(appName)}.png`);
ps.addCommand(`$fileExists = Test-Path -Path "${inFilePath}";`);
ps.addCommand(`if($fileExists) { $icon = [System.Drawing.Icon]::ExtractAssociatedIcon("${inFilePath}"); }`);
ps.addCommand(`if($fileExists) { $bitmap = $icon.ToBitmap().save("${outFilePath}", [System.Drawing.Imaging.ImageFormat]::Png); }`);

ps.addCommand(`$icon = [System.Drawing.Icon]::ExtractAssociatedIcon("${inFilePath}")`);
ps.addCommand(`$bitmap = $icon.ToBitmap().save("${outFilePath}", [System.Drawing.Imaging.ImageFormat]::Png);`);

this.addIcon({ name: app.name, PNGFilePath: outFilePath });
}
this.addIcon({ name: app.name, PNGFilePath: outFilePath });
});

ps.invoke().then(() => {
// tslint:disable-next-line:no-console
console.log("Sucessfully generated all app icons");
this.removeNonExistentIcons();
ps.dispose();
}).catch((err) => {
// tslint:disable-next-line:no-console
console.log(`Error while generating app icon: ${err}`);
this.removeNonExistentIcons();
ps.dispose();
});
}

private removeNonExistentIcons(): void {
const indexesToDelete = [];

for (let i = 0; i < this.icons.length; i++) {
if (!existsSync(this.icons[i].PNGFilePath)) {
indexesToDelete.push(i);
}
}

indexesToDelete.forEach((i) => this.icons.splice(i, 1));
}
}

0 comments on commit 1fa3fea

Please sign in to comment.