Skip to content

Commit

Permalink
refactor, wip: trying again with using a single object
Browse files Browse the repository at this point in the history
  • Loading branch information
EagleoutIce committed Aug 26, 2023
1 parent 5b08c65 commit f2fb128
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 99 deletions.
20 changes: 4 additions & 16 deletions src/cli/benchmark-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { LimitBenchmarkPool } from '../benchmark/parallel-helper'
import * as os from 'os'
import { guard } from '../util/assert'
import fs from 'fs'
import { register, scripts } from './common/scripts-info'
import { scripts } from './common/scripts-info'

const now = date2string(new Date())

Expand All @@ -22,18 +22,6 @@ export const optionDefinitions: OptionDefinition[] = [
{ name: 'output', alias: 'o', type: String, description: `File to write all the measurements to in a per-file-basis (defaults to {italic benchmark-${now}.json})`, defaultValue: `benchmark-${now}.json`, typeLabel: '{underline file}' }
]

const toolName = 'benchmark'
const description = 'Benchmark the static backwards slicer'

register('benchmark', {
toolName,
target: 'benchmark-app',
description,
type: 'master script',
usageExample: `${toolName} "example-folder/"`,
options: optionDefinitions
})

export interface BenchmarkCliOptions {
verbose: boolean
help: boolean
Expand All @@ -46,14 +34,14 @@ export interface BenchmarkCliOptions {

export const optionHelp = [
{
header: description,
header: scripts.benchmark.description,
content: 'Slice given files with additional benchmark information'
},
{
header: 'Synopsis',
content: [
`$ ${toolName} {italic example-folder/}`,
`$ ${toolName} {bold --help}`
`$ ${scripts.benchmark.toolName} {italic example-folder/}`,
`$ ${scripts.benchmark.toolName} {bold --help}`
]
},
{
Expand Down
21 changes: 4 additions & 17 deletions src/cli/benchmark-helper-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { RParseRequestFromFile } from '../r-bridge'
import fs from 'fs'
import { displayEnvReplacer } from '../util/json'
import { guard } from '../util/assert'
import { register } from './common/scripts-info'
import { scripts } from './common'

export const optionDefinitions: OptionDefinition[] = [
{ name: 'verbose', alias: 'v', type: Boolean, description: 'Run with verbose logging [do not use for the real benchmark as this affects the time measurements, but only to find errors]' },
Expand All @@ -17,19 +17,6 @@ export const optionDefinitions: OptionDefinition[] = [
{ name: 'output', alias: 'o', type: String, description: `File to write the measurements to (appends a single line in JSON format)`, typeLabel: '{underline file}' },
]

const toolName = 'benchmark-single'
const description = 'Helper Script to Benchmark the Slicer'

register('benchmark-single', {
toolName,
target: 'benchmark-helper-app',
description,
usageExample: `${toolName} "example.R" --output "example.json"`,
options: optionDefinitions,
type: 'helper script',
masterScripts: [ 'benchmark' ]
})

export interface SingleBenchmarkCliOptions {
verbose: boolean
help: boolean
Expand All @@ -40,14 +27,14 @@ export interface SingleBenchmarkCliOptions {

export const optionHelp = [
{
header: description,
header: scripts['benchmark-helper'].description,
content: 'Will slice for all possible variables, signal by exit code if slicing was successful, and can be run standalone'
},
{
header: 'Synopsis',
content: [
`$ ${toolName} {italic example-file.R} --output {italic output.json}`,
`$ ${toolName} {bold --help}`
`$ ${scripts['benchmark-helper'].toolName} {italic example-file.R} --output {italic output.json}`,
`$ ${scripts['benchmark-helper'].toolName} {bold --help}`
]
},
{
Expand Down
2 changes: 2 additions & 0 deletions src/cli/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './script'
export * from './scripts-info'
67 changes: 62 additions & 5 deletions src/cli/common/scripts-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
*/
import { MergeableRecord } from '../../util/objects'
import { OptionDefinition } from 'command-line-usage'
import { DeepReadonly } from 'ts-essentials'
import { optionDefinitions as benchmarkOptions } from '../benchmark-app'
import { optionDefinitions as benchmarkHelperOptions } from '../benchmark-helper-app'
import { optionDefinitions as slicerOptions } from '../slicer-app'
import { optionDefinitions as summarizerOptions } from '../summarizer-app'
import { optionDefinitions as quadsOptions } from '../export-quads-app'
import { optionDefinitions as statsOptions } from '../../statistics'


interface BaseScriptInformation extends MergeableRecord {
Expand All @@ -26,10 +33,60 @@ export interface HelperScriptInformation extends BaseScriptInformation {

export type ScriptInformation = MasterScriptInformation | HelperScriptInformation

/**
* We hold `_scripts` internally, as the modifiable variant and export the readonly scripts
*/
const _scripts = {
'slicer': {
toolName: 'slicer',
target: 'slicer-app',
description: 'Static backwards executable slicer for R',
options: slicerOptions,
usageExample: 'slicer -c "12@product" test/testfiles/example.R',
type: 'master script',
},
'benchmark': {
toolName: 'benchmark',
target: 'benchmark-app',
description: 'Benchmark the static backwards slicer',
type: 'master script',
usageExample: 'benchmark "example-folder/"',
options: benchmarkOptions
},
'benchmark-helper': {
toolName: 'benchmark-single',
target: 'benchmark-helper-app',
description: 'Helper Script to Benchmark the Slicer',
usageExample: 'benchmark-single "example.R" --output "example.json"',
options: benchmarkHelperOptions,
type: 'helper script',
masterScripts: [ 'benchmark' ]
},
'summarizer': {
toolName: 'summarizer',
target: 'summarizer-app',
description: 'Summarize the results of the benchmark',
options: summarizerOptions,
usageExample: 'summarizer "benchmark.json"',
type: 'master script',
},
'export-quads': {
toolName: 'export-quads',
target: 'export-quads-app',
description: 'Export quads of the normalized AST of a given R code file',
usageExample: 'export-quads "example.R" --output "example.quads"',
options: quadsOptions,
type: 'master script',
},
'stats': {
toolName: 'stats',
target: 'statistics-app',
description: 'Generate usage Statistics for R scripts',
options: statsOptions,
usageExample: 'stats -i example.R --output-dir "output-folder/"',
type: 'master script',
}
} as const

export const scripts = new Map<string, ScriptInformation>()

export function register(name: string, information: ScriptInformation) {
scripts.set(name, information)
}
export const scripts = _scripts as DeepReadonly<Record<keyof typeof _scripts, ScriptInformation>>

20 changes: 4 additions & 16 deletions src/cli/export-quads-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import commandLineUsage, { OptionDefinition } from 'command-line-usage'
import { serialize2quads } from '../util/quads'
import fs from 'fs'
import { allRFilesFrom } from '../util/files'
import { register } from './common/scripts-info'
import { scripts } from './common'


export const optionDefinitions: OptionDefinition[] = [
Expand All @@ -16,18 +16,6 @@ export const optionDefinitions: OptionDefinition[] = [
{ name: 'output', alias: 'o', type: String, description: 'File to write all the generated quads to (defaults to {italic out.quads})', typeLabel: '{underline file}' },
]

const toolName = 'export-quads'
const description = 'Export quads of the normalized AST of a given R code file'

register('export-quads', {
toolName,
target: 'export-quads-app',
description,
usageExample: `${toolName} "example.R" --output "example.quads"`,
options: optionDefinitions,
type: 'master script',
})

export interface QuadsCliOptions {
verbose: boolean
help: boolean
Expand All @@ -38,14 +26,14 @@ export interface QuadsCliOptions {

export const optionHelp = [
{
header: description,
header: scripts['export-quads'].description,
content: 'Generate RDF N-Quads from the AST of a given R script'
},
{
header: 'Synopsis',
content: [
`$ ${toolName} {bold -i} {italic example.R} {bold --output} {italic "example.quads"}`,
`$ ${toolName} {bold --help}`
`$ ${scripts['export-quads'].toolName} {bold -i} {italic example.R} {bold --output} {italic "example.quads"}`,
`$ ${scripts['export-quads'].toolName} {bold --help}`
]
},
{
Expand Down
22 changes: 5 additions & 17 deletions src/cli/slicer-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { guard } from '../util/assert'
import { SingleSlicingCriterion, SlicingCriteria } from '../slicing'
import { BenchmarkSlicer, stats2string, summarizeSlicerStats } from '../benchmark'
import { NodeId } from '../r-bridge'
import { register, scripts } from './common/scripts-info'
import { scripts } from './common'

export const optionDefinitions: OptionDefinition[] = [
{ name: 'verbose', alias: 'v', type: Boolean, description: 'Run with verbose logging' },
Expand All @@ -18,18 +18,6 @@ export const optionDefinitions: OptionDefinition[] = [
{ name: 'output', alias: 'o', type: String, description: 'File to write all the generated quads to (defaults to the commandline)', typeLabel: '{underline file}' },
]

const toolName = 'slicer'
const description = 'Static backwards executable slicer for R'

register('slicer', {
toolName,
target: 'slicer-app',
description,
options: optionDefinitions,
usageExample: `${toolName} -c "12@product" test/testfiles/example.R`,
type: 'master script',
})

export interface SlicerCliOptions {
verbose: boolean
help: boolean
Expand All @@ -42,15 +30,15 @@ export interface SlicerCliOptions {

export const optionHelp = [
{
header: description,
header: scripts.slicer.description,
content: 'Slice R code based on a given slicing criterion'
},
{
header: 'Synopsis',
content: [
`$ ${toolName} {bold -c} {italic "12@product"} {italic test/testfiles/example.R}`,
`$ ${toolName} {bold -i} {italic example.R} {bold --stats} {bold --criterion} {italic "8:3;3:1;12@product"}`,
`$ ${toolName} {bold --help}`
`$ ${scripts.slicer.toolName} {bold -c} {italic "12@product"} {italic test/testfiles/example.R}`,
`$ ${scripts.slicer.toolName} {bold -i} {italic example.R} {bold --stats} {bold --criterion} {italic "8:3;3:1;12@product"}`,
`$ ${scripts.slicer.toolName} {bold --help}`
]
},
{
Expand Down
12 changes: 1 addition & 11 deletions src/cli/statistics-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,14 @@ import {
printFeatureStatistics,
initFileProvider,
setFormatter,
voidFormatter, ContextsWithCount, toolName, description
voidFormatter, ContextsWithCount,
} from '../statistics'
import { log, LogLevel } from '../util/log'
import commandLineArgs from 'command-line-args'
import commandLineUsage from 'command-line-usage'
import { guard } from '../util/assert'
import { allRFilesFrom, writeTableAsCsv } from '../util/files'
import { DefaultMap } from '../util/defaultmap'
import { register } from './common/scripts-info'

register('stats', {
toolName,
target: 'statistics-app',
description,
options: optionDefinitions,
usageExample: `${toolName} -i example.R --output-dir "output-folder/"`,
type: 'master script',
})

const options = commandLineArgs(optionDefinitions) as StatsCliOptions

Expand Down
20 changes: 4 additions & 16 deletions src/cli/summarizer-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import fs from 'fs'
import { SlicingCriteria } from '../slicing'
import { escape } from '../statistics'
import { displayEnvReplacer } from '../util/json'
import { register } from './common/scripts-info'
import { scripts } from './common'

export const optionDefinitions: OptionDefinition[] = [
{ name: 'verbose', alias: 'v', type: Boolean, description: 'Run with verbose logging' },
Expand All @@ -24,18 +24,6 @@ export const optionDefinitions: OptionDefinition[] = [
{ name: 'output', alias: 'o', type: String, description: `Basename of the summaries (defaults to {italic <input>-summary})`, typeLabel: '{underline file}' },
]

const toolName = 'summarizer'
const description = 'Summarize the results of the benchmark'

register('summarizer', {
toolName,
target: 'summarizer-app',
description,
options: optionDefinitions,
usageExample: `${toolName} "benchmark.json"`,
type: 'master script',
})

export interface BenchmarkCliOptions {
verbose: boolean
help: boolean
Expand All @@ -46,14 +34,14 @@ export interface BenchmarkCliOptions {

export const optionHelp = [
{
header: description,
header: scripts.summarizer.description,
content: 'Summarize and explain the results of the benchmark tool. Summarizes in two stages: first per-request, and then overall'
},
{
header: 'Synopsis',
content: [
`$ ${toolName} {italic benchmark.json}`,
`$ ${toolName} {bold --help}`
`$ ${scripts.summarizer.toolName} {italic benchmark.json}`,
`$ ${scripts.summarizer.toolName} {bold --help}`
]
},
{
Expand Down
3 changes: 2 additions & 1 deletion src/flowr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { bold, ColorEffect, Colors, FontStyles, formatter, italic, setFormatter,
import { repl } from './cli/repl/core'
import { ScriptInformation, scripts } from './cli/common/scripts-info'
import { waitOnScript } from './cli/repl'
import { DeepReadonly } from 'ts-essentials'

const scriptsText = Array.from(Object.entries(scripts).filter(([, {type}]) => type === 'master script'), ([k,]) => k).join(', ')

Expand Down Expand Up @@ -62,7 +63,7 @@ if(options['no-ansi']) {

async function main() {
if(options.script) {
let target = scripts.get(options.script)?.target
let target = (scripts as DeepReadonly<Record<string, ScriptInformation>>)[options.script].target as string | undefined
guard(target !== undefined, `Unknown script ${options.script}, pick one of ${scriptsText}.`)
console.log(`Running script '${formatter.format(options.script, { style: FontStyles.bold })}'`)
target = `cli/${target}`
Expand Down

0 comments on commit f2fb128

Please sign in to comment.