-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
278 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import os from 'os' | ||
import { Component, Terminal } from '../../core' | ||
import { compareVersions, httpsGetJson, nullTerminal, run } from '../../core/utils' | ||
|
||
const TS4_PKG = 'tonos-ts4' | ||
const PYPI = `https://pypi.org/pypi/${TS4_PKG}/json` | ||
const currentOS = os.type() | ||
const [PYTHON, PIP] = ['Linux', 'Darwin'].includes(currentOS) | ||
? ['python3', 'pip3'] | ||
: ['python', 'pip'] | ||
|
||
export const components = { | ||
ts4: new class extends Component { | ||
async getCurrentVersion(): Promise<string> { | ||
let version | ||
|
||
try { | ||
const output = await run(PIP, ['show', TS4_PKG], {}, nullTerminal) | ||
version = output.split(os.EOL).find(line => (/^Version:/).exec(line)) | ||
} catch { | ||
// TODO handle the lack of 'pip' | ||
console.debug(`Package ${TS4_PKG} not found`) | ||
} | ||
return version ? version.split(/:\s*/)[1] : '' | ||
} | ||
|
||
async ensureVersion( | ||
terminal: Terminal, | ||
force: boolean, | ||
requiredVersion?: string, | ||
): Promise<boolean> { | ||
const current = await this.getCurrentVersion(); | ||
if (!force && current !== "" && !requiredVersion) { | ||
return false; | ||
} | ||
let version = (requiredVersion ?? "latest").toLowerCase(); | ||
if (!force && version === current) { | ||
return false; | ||
} | ||
const available = await this.loadAvailableVersions(); | ||
if (version === "latest") { | ||
version = available[0]; | ||
} else { | ||
if (!available.includes(version)) { | ||
throw new Error(`Invalid ${this.name} version ${version}`); | ||
} | ||
} | ||
if (!force && version === current) { | ||
return false; | ||
} | ||
const pkg = TS4_PKG + (version ? `==${version}` : '') | ||
const output = await run(PIP, ['install', '-U', pkg], {}, nullTerminal) | ||
const successPattern = `Successfully installed ${TS4_PKG}-${version}` | ||
const isOk = output.split(os.EOL).find(line => line === successPattern) | ||
|
||
if (!isOk) { | ||
terminal.writeError(output) | ||
return false | ||
} else { | ||
terminal.log(successPattern) | ||
} | ||
|
||
return true | ||
} | ||
|
||
async loadAvailableVersions(): Promise<string[]> { | ||
const info = await httpsGetJson(PYPI) | ||
const versions = Object.keys(info.releases) | ||
.filter(v => (/^(\d+\.){2}\d+$/).test(v)) | ||
.sort(compareVersions) | ||
.reverse() | ||
return versions.length < 10 ? versions : [...versions.slice(0, 10), "..."] | ||
} | ||
}('', PYTHON, { | ||
executable: true, | ||
globally: true, | ||
}), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import path from 'path' | ||
import fs from 'fs' | ||
import { Command, Component, Terminal, ToolController } from '../../core' | ||
import { uniqueFilePath } from '../../core/utils' | ||
import { components } from './components' | ||
import { BasicTest } from './snippets' | ||
|
||
export const ts4VersionCommand: Command = { | ||
name: "version", | ||
title: "Show installed and available versions", | ||
async run(terminal: Terminal, _args: {}): Promise<void> { | ||
terminal.log(await Component.getInfoAll(components)); | ||
}, | ||
} | ||
|
||
export const ts4InstallCommand: Command = { | ||
name: "install", | ||
title: "Install latest release of TestSuite4", | ||
args: [{ | ||
isArg: true, | ||
name: 'version', | ||
type: 'string', | ||
title: 'TestSuite4 version (semver compatible)', | ||
}], | ||
async run(terminal: Terminal, args: { version: string }) { | ||
const versions: { | ||
ts4?: string; | ||
} = { | ||
...(args.version !== "" ? { ts4: args.version } : {}), | ||
} | ||
await Component.setVersions(terminal, false, components, versions) | ||
}, | ||
} | ||
|
||
export const ts4UpdateCommand: Command = { | ||
name: "update", | ||
title: "Update to the latest version", | ||
async run(terminal: Terminal, _args: {}): Promise<void> { | ||
await Component.updateAll(terminal, true, components) | ||
}, | ||
} | ||
|
||
export const ts4CreateCommand: Command = { | ||
name: "create", | ||
title: "Create TestSuite4 test", | ||
args: [{ | ||
isArg: true, | ||
name: "name", | ||
title: "Test script name", | ||
type: "string", | ||
defaultValue: "Test", | ||
}, { | ||
name: "folder", | ||
type: "folder", | ||
title: "Target folder (current is default)", | ||
}], | ||
async run(terminal: Terminal, args: { name: string, folder: string }) { | ||
const filePath = uniqueFilePath(args.folder, `${args.name}{}.py`) | ||
const text = BasicTest.split("{name}").join(args.name) | ||
fs.writeFileSync(filePath, text) | ||
terminal.log(`TestSuite4 test script ${path.basename(filePath)} created.`) | ||
}, | ||
} | ||
|
||
export const ts4RunCommand: Command = { | ||
name: 'run', | ||
title: 'Run TestSuite4\'s test', | ||
args: [{ | ||
isArg: true, | ||
name: 'file', | ||
type: 'file', | ||
title: 'Test', | ||
nameRegExp: /\.py$/i, | ||
}], | ||
async run(terminal: Terminal, args: { file: string }): Promise<void> { | ||
const ext = path.extname(args.file) | ||
if (ext !== ".py") { | ||
terminal.log(`Choose file *.py`) | ||
return | ||
} | ||
await Component.ensureInstalledAll(terminal, components) | ||
const fileDir = path.dirname(args.file) | ||
const fileName = path.basename(args.file) | ||
|
||
await components.ts4.run(terminal, fileDir, [fileName]) | ||
}, | ||
} | ||
|
||
export const TestSuite4: ToolController = { | ||
name: "ts4", | ||
title: "TestSuite4 framework", | ||
commands: [ | ||
ts4VersionCommand, | ||
ts4InstallCommand, | ||
ts4UpdateCommand, | ||
ts4CreateCommand, | ||
ts4RunCommand, | ||
], | ||
} |
Oops, something went wrong.