Skip to content

Commit

Permalink
Refactor to use version object #59
Browse files Browse the repository at this point in the history
Enable writing safer version aware logic.
  • Loading branch information
undergroundwires committed Feb 26, 2022
1 parent d6bc33e commit eeb1d5b
Show file tree
Hide file tree
Showing 14 changed files with 198 additions and 35 deletions.
4 changes: 3 additions & 1 deletion src/application/Parser/ProjectInformationParser.ts
Original file line number Diff line number Diff line change
@@ -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,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/domain/IProjectInformation.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
10 changes: 6 additions & 4 deletions src/domain/ProjectInformation.ts
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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 {
Expand Down
24 changes: 24 additions & 0 deletions src/domain/Version.ts
Original file line number Diff line number Diff line change
@@ -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}`;
}
}
2 changes: 1 addition & 1 deletion src/presentation/components/TheFooter/TheFooter.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 14 additions & 6 deletions src/presentation/electron/Update/ManualUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -17,19 +19,25 @@ 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) {
await download(info, project);
}
}

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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ describe('ProjectInformationParser', () => {
describe('parseProjectInformation', () => {
it('parses expected repository version', () => {
// arrange
const expected = 'expected-version';
const expected = '0.11.3';
const env = getProcessEnvironmentStub();
env.VUE_APP_VERSION = expected;
// act
const info = parseProjectInformation(env);
// assert
expect(info.version).to.be.equal(expected);
const actual = info.version.toString();
expect(actual).to.be.equal(expected);
});
it('parses expected repository url', () => {
// arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ describe('CodeSubstituter', () => {
// arrange
const info = new ProjectInformationStub();
const date = new Date();
const testCases = [
const testCases: Array<{ parameter: string, argument: string }> = [
{
parameter: 'homepage',
argument: info.homepage,
},
{
parameter: 'version',
argument: info.version,
argument: info.version.toString(),
},
{
parameter: 'date',
Expand Down
27 changes: 14 additions & 13 deletions tests/unit/domain/ProjectInformation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,31 @@ import { expect } from 'chai';
import { ProjectInformation } from '@/domain/ProjectInformation';
import { OperatingSystem } from '@/domain/OperatingSystem';
import { EnumRangeTestRunner } from '@tests/unit/application/Common/EnumRangeTestRunner';
import { VersionStub } from '@tests/unit/shared/Stubs/VersionStub';

describe('ProjectInformation', () => {
it('sets name as expected', () => {
// arrange
const expected = 'expected-name';
const sut = new ProjectInformation(expected, 'version', 'repositoryUrl', 'homepage');
const sut = new ProjectInformation(expected, new VersionStub('0.7.2'), 'repositoryUrl', 'homepage');
// act
const actual = sut.name;
// assert
expect(actual).to.equal(expected);
});
it('sets version as expected', () => {
// arrange
const expected = 'expected-version';
const expected = new VersionStub('0.11.3');
const sut = new ProjectInformation('name', expected, 'repositoryUrl', 'homepage');
// act
const actual = sut.version;
// assert
expect(actual).to.equal(expected);
expect(actual).to.deep.equal(expected);
});
it('sets repositoryUrl as expected', () => {
// arrange
const expected = 'expected-repository-url';
const sut = new ProjectInformation('name', 'version', expected, 'homepage');
const sut = new ProjectInformation('name', new VersionStub('0.7.2'), expected, 'homepage');
// act
const actual = sut.repositoryUrl;
// assert
Expand All @@ -36,7 +37,7 @@ describe('ProjectInformation', () => {
it('sets repositoryUrl when it does not end with .git', () => {
// arrange
const expected = 'expected-repository-url';
const sut = new ProjectInformation('name', 'version', expected, 'homepage');
const sut = new ProjectInformation('name', new VersionStub('0.7.2'), expected, 'homepage');
// act
const actual = sut.repositoryWebUrl;
// assert
Expand All @@ -45,7 +46,7 @@ describe('ProjectInformation', () => {
it('removes ".git" from the end when it ends with ".git"', () => {
// arrange
const expected = 'expected-repository-url';
const sut = new ProjectInformation('name', 'version', `${expected}.git`, 'homepage');
const sut = new ProjectInformation('name', new VersionStub('0.7.2'), `${expected}.git`, 'homepage');
// act
const actual = sut.repositoryWebUrl;
// assert
Expand All @@ -55,7 +56,7 @@ describe('ProjectInformation', () => {
it('sets homepage as expected', () => {
// arrange
const expected = 'expected-homepage';
const sut = new ProjectInformation('name', 'version', 'repositoryUrl', expected);
const sut = new ProjectInformation('name', new VersionStub('0.7.2'), 'repositoryUrl', expected);
// act
const actual = sut.homepage;
// assert
Expand All @@ -65,7 +66,7 @@ describe('ProjectInformation', () => {
// arrange
const repositoryUrl = 'https://github.com/undergroundwires/privacy.sexy.git';
const expected = 'https://github.com/undergroundwires/privacy.sexy/issues';
const sut = new ProjectInformation('name', 'version', repositoryUrl, 'homepage');
const sut = new ProjectInformation('name', new VersionStub('0.7.2'), repositoryUrl, 'homepage');
// act
const actual = sut.feedbackUrl;
// assert
Expand All @@ -74,7 +75,7 @@ describe('ProjectInformation', () => {
it('sets releaseUrl to github releases page', () => {
// arrange
const repositoryUrl = 'https://github.com/undergroundwires/privacy.sexy.git';
const version = '0.7.2';
const version = new VersionStub('0.7.2');
const expected = 'https://github.com/undergroundwires/privacy.sexy/releases/tag/0.7.2';
const sut = new ProjectInformation('name', version, repositoryUrl, 'homepage');
// act
Expand All @@ -87,7 +88,7 @@ describe('ProjectInformation', () => {
// arrange
const expected = 'https://github.com/undergroundwires/privacy.sexy/releases/download/0.7.2/privacy.sexy-0.7.2.dmg';
const repositoryUrl = 'https://github.com/undergroundwires/privacy.sexy.git';
const version = '0.7.2';
const version = new VersionStub('0.7.2');
const sut = new ProjectInformation('name', version, repositoryUrl, 'homepage');
// act
const actual = sut.getDownloadUrl(OperatingSystem.macOS);
Expand All @@ -98,7 +99,7 @@ describe('ProjectInformation', () => {
// arrange
const expected = 'https://github.com/undergroundwires/privacy.sexy/releases/download/0.7.2/privacy.sexy-0.7.2.AppImage';
const repositoryUrl = 'https://github.com/undergroundwires/privacy.sexy.git';
const version = '0.7.2';
const version = new VersionStub('0.7.2');
const sut = new ProjectInformation('name', version, repositoryUrl, 'homepage');
// act
const actual = sut.getDownloadUrl(OperatingSystem.Linux);
Expand All @@ -109,7 +110,7 @@ describe('ProjectInformation', () => {
// arrange
const expected = 'https://github.com/undergroundwires/privacy.sexy/releases/download/0.7.2/privacy.sexy-Setup-0.7.2.exe';
const repositoryUrl = 'https://github.com/undergroundwires/privacy.sexy.git';
const version = '0.7.2';
const version = new VersionStub('0.7.2');
const sut = new ProjectInformation('name', version, repositoryUrl, 'homepage');
// act
const actual = sut.getDownloadUrl(OperatingSystem.Windows);
Expand All @@ -118,7 +119,7 @@ describe('ProjectInformation', () => {
});
describe('throws when os is invalid', () => {
// arrange
const sut = new ProjectInformation('name', 'version', 'repositoryUrl', 'homepage');
const sut = new ProjectInformation('name', new VersionStub(), 'repositoryUrl', 'homepage');
// act
const act = (os: OperatingSystem) => sut.getDownloadUrl(os);
// assert
Expand Down
Loading

0 comments on commit eeb1d5b

Please sign in to comment.