Refactor to use version object #59
Enable writing safer version aware logic.
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
import { IProjectInformation } from '@/domain/IProjectInformation';
|
||||
import { ProjectInformation } from '@/domain/ProjectInformation';
|
||||
import { Version } from '@/domain/Version';
|
||||
|
||||
export function parseProjectInformation(
|
||||
environment: NodeJS.ProcessEnv,
|
||||
): IProjectInformation {
|
||||
const version = new Version(environment.VUE_APP_VERSION);
|
||||
return new ProjectInformation(
|
||||
environment.VUE_APP_NAME,
|
||||
environment.VUE_APP_VERSION,
|
||||
version,
|
||||
environment.VUE_APP_REPOSITORY_URL,
|
||||
environment.VUE_APP_HOMEPAGE_URL,
|
||||
);
|
||||
|
||||
@@ -22,7 +22,7 @@ export class CodeSubstituter implements ICodeSubstituter {
|
||||
const substitute = (name: string, value: string) => args
|
||||
.addArgument(new FunctionCallArgument(name, value));
|
||||
substitute('homepage', info.homepage);
|
||||
substitute('version', info.version);
|
||||
substitute('version', info.version.toString());
|
||||
substitute('date', this.date.toUTCString());
|
||||
const compiledCode = this.compiler.compileExpressions(code, args);
|
||||
return compiledCode;
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { OperatingSystem } from './OperatingSystem';
|
||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||
import { Version } from '@/domain/Version';
|
||||
|
||||
export interface IProjectInformation {
|
||||
readonly name: string;
|
||||
readonly version: string;
|
||||
readonly version: Version;
|
||||
readonly repositoryUrl: string;
|
||||
readonly homepage: string;
|
||||
readonly feedbackUrl: string;
|
||||
|
||||
@@ -1,21 +1,22 @@
|
||||
import { assertInRange } from '@/application/Common/Enum';
|
||||
import { IProjectInformation } from './IProjectInformation';
|
||||
import { OperatingSystem } from './OperatingSystem';
|
||||
import { Version } from './Version';
|
||||
|
||||
export class ProjectInformation implements IProjectInformation {
|
||||
public readonly repositoryWebUrl: string;
|
||||
|
||||
constructor(
|
||||
public readonly name: string,
|
||||
public readonly version: string,
|
||||
public readonly version: Version,
|
||||
public readonly repositoryUrl: string,
|
||||
public readonly homepage: string,
|
||||
) {
|
||||
if (!name) {
|
||||
throw new Error('name is undefined');
|
||||
}
|
||||
if (!version || +version <= 0) {
|
||||
throw new Error('version should be higher than zero');
|
||||
if (!version) {
|
||||
throw new Error('undefined version');
|
||||
}
|
||||
if (!repositoryUrl) {
|
||||
throw new Error('repositoryUrl is undefined');
|
||||
@@ -27,7 +28,8 @@ export class ProjectInformation implements IProjectInformation {
|
||||
}
|
||||
|
||||
public getDownloadUrl(os: OperatingSystem): string {
|
||||
return `${this.repositoryWebUrl}/releases/download/${this.version}/${getFileName(os, this.version)}`;
|
||||
const fileName = getFileName(os, this.version.toString());
|
||||
return `${this.repositoryWebUrl}/releases/download/${this.version}/${fileName}`;
|
||||
}
|
||||
|
||||
public get feedbackUrl(): string {
|
||||
|
||||
24
src/domain/Version.ts
Normal file
24
src/domain/Version.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
export class Version {
|
||||
public readonly major: number;
|
||||
|
||||
public readonly minor: number;
|
||||
|
||||
public readonly patch: number;
|
||||
|
||||
public constructor(semanticVersion: string) {
|
||||
if (!semanticVersion) {
|
||||
throw new Error('empty version');
|
||||
}
|
||||
if (!semanticVersion.match(/^\d+\.\d+\.\d+$/g)) {
|
||||
throw new Error(`invalid version: ${semanticVersion}`);
|
||||
}
|
||||
const [major, minor, patch] = semanticVersion.split('.');
|
||||
this.major = parseInt(major, 10);
|
||||
this.minor = parseInt(minor, 10);
|
||||
this.patch = parseInt(patch, 10);
|
||||
}
|
||||
|
||||
public toString(): string {
|
||||
return `${this.major}.${this.minor}.${this.patch}`;
|
||||
}
|
||||
}
|
||||
@@ -77,7 +77,7 @@ export default class TheFooter extends Vue {
|
||||
|
||||
private initialize(app: IApplication) {
|
||||
const { info } = app;
|
||||
this.version = info.version;
|
||||
this.version = info.version.toString();
|
||||
this.homepageUrl = info.homepage;
|
||||
this.repositoryUrl = info.repositoryWebUrl;
|
||||
this.releaseUrl = info.releaseUrl;
|
||||
|
||||
@@ -6,6 +6,8 @@ import log from 'electron-log';
|
||||
import fetch from 'cross-fetch';
|
||||
import { ProjectInformation } from '@/domain/ProjectInformation';
|
||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||
import { Version } from '@/domain/Version';
|
||||
import { parseProjectInformation } from '@/application/Parser/ProjectInformationParser';
|
||||
import { UpdateProgressBar } from './UpdateProgressBar';
|
||||
|
||||
export function requiresManualUpdate(): boolean {
|
||||
@@ -17,12 +19,7 @@ export async function handleManualUpdate(info: UpdateInfo) {
|
||||
if (result === ManualDownloadDialogResult.NoAction) {
|
||||
return;
|
||||
}
|
||||
const project = new ProjectInformation(
|
||||
process.env.VUE_APP_NAME,
|
||||
info.version,
|
||||
process.env.VUE_APP_REPOSITORY_URL,
|
||||
process.env.VUE_APP_HOMEPAGE_URL,
|
||||
);
|
||||
const project = getTargetProject(info.version);
|
||||
if (result === ManualDownloadDialogResult.VisitReleasesPage) {
|
||||
await shell.openExternal(project.releaseUrl);
|
||||
} else if (result === ManualDownloadDialogResult.UpdateNow) {
|
||||
@@ -30,6 +27,17 @@ export async function handleManualUpdate(info: UpdateInfo) {
|
||||
}
|
||||
}
|
||||
|
||||
function getTargetProject(targetVersion: string) {
|
||||
const existingProject = parseProjectInformation(process.env);
|
||||
const targetProject = new ProjectInformation(
|
||||
existingProject.name,
|
||||
new Version(targetVersion),
|
||||
existingProject.repositoryUrl,
|
||||
existingProject.homepage,
|
||||
);
|
||||
return targetProject;
|
||||
}
|
||||
|
||||
enum ManualDownloadDialogResult {
|
||||
NoAction = 0,
|
||||
UpdateNow = 1,
|
||||
|
||||
Reference in New Issue
Block a user