Skip to content

Commit

Permalink
system info
Browse files Browse the repository at this point in the history
  • Loading branch information
lancejpollard committed Jan 19, 2024
1 parent d3e179f commit eb6f22c
Show file tree
Hide file tree
Showing 15 changed files with 284 additions and 69 deletions.
47 changes: 46 additions & 1 deletion code/cli/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import stripAnsi from 'strip-ansi'
import tint, { Tint } from '@termsurf/tint-text'
import ora from 'ora'
import { log, logWithSpace } from '../shared/logger.js'
import _ from 'lodash'

const M: Tint = { tone: 'magenta' }
const C: Tint = { tone: 'cyan' }
const B: Tint = { tone: 'white' }
const B: Tint = { tone: 'blackBright' }
const W: Tint = { tone: 'white' }
const WB: Tint = { tone: 'whiteBright' }
const G: Tint = { tone: 'green' }
const R: Tint = { tone: 'red' }

Expand Down Expand Up @@ -68,3 +71,45 @@ export function renderProgress(text: string, color = true) {
return text
}
}

export function logTree(json) {
const text: Array<string> = []

traverse(json)

console.log(text.join('\n'))

function traverse(json, depth = 1) {
for (const name in json) {
const val = json[name]
if (val) {
if (_.isObject(val)) {
text.push(
ident(depth) +
tint('o', B) +
' ' +
tint(_.kebabCase(name), W),
)
traverse(val, depth + 1)
} else {
text.push(
ident(depth) +
tint('o', B) +
' ' +
tint(_.kebabCase(name), W),
)
text.push(
ident(depth + 1) +
tint(' o <', B) +
tint(val, C) +
tint('>', B),
)
}
}
}
}
}

function ident(size: number) {
return ''.padStart(size * 2, ' ')
}
11 changes: 11 additions & 0 deletions code/cli/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
logOutput,
logOutputError,
logStart,
logTree,
renderProgress,
} from './logging.js'
import {
Expand All @@ -56,6 +57,7 @@ import {
transferInput,
} from './parse.js'
import { formatRust } from '../node/code.js'
import { inspectSystem } from '../node/system.js'

export const CONVERT_KEY: Record<string, Link> = {
i: { line: ['input', 'file', 'path'] },
Expand Down Expand Up @@ -303,6 +305,15 @@ export async function call(task: Task, source) {
return
}
}
case 'inspect': {
const thing = source.object[0] as string
if (thing === 'system') {
const info = await inspectSystem()
logTree(info)
return
}
break
}
}

throw kink('task_not_implemented', {
Expand Down
132 changes: 66 additions & 66 deletions code/node/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,73 +12,73 @@ import {
CompileToAst,
} from '../shared/index.js'
import { handleCommand } from './command.js'
import Parser from 'tree-sitter'
import JavaScript from 'tree-sitter-javascript'
import Swift from 'tree-sitter-swift'
import fsp from 'fs/promises'

// compileToAsm({
// input: {
// format: 'js',
// file: { path: 'test/file/code/quicksort/quicksort.js' },
// },
// })

// compileToAsm({
// input: {
// format: 'js',
// file: { path: 'test/file/code/quicksort/quicksort.swift' },
// },
// })

const TREE_SITTER_LANGUAGE: Record<string, any> = {
js: JavaScript,
swift: Swift,
}
// import Parser from 'tree-sitter'
// import JavaScript from 'tree-sitter-javascript'
// import Swift from 'tree-sitter-swift'
// import fsp from 'fs/promises'

// // compileToAsm({
// // input: {
// // format: 'js',
// // file: { path: 'test/file/code/quicksort/quicksort.js' },
// // },
// // })

// // compileToAsm({
// // input: {
// // format: 'js',
// // file: { path: 'test/file/code/quicksort/quicksort.swift' },
// // },
// // })

// const TREE_SITTER_LANGUAGE: Record<string, any> = {
// js: JavaScript,
// swift: Swift,
// }

export async function compileToAsm(input: CompileToAst) {
const parser = new Parser()
parser.setLanguage(TREE_SITTER_LANGUAGE[input.input.format])
const code = await fsp.readFile(input.input.file.path, 'utf-8')
const tree = parser.parse(code)
return traverse(tree.rootNode)

function traverse(node) {
const out: any = {
form: node.type,
base: node.startPosition,
head: node.endPosition,
}
if (node.type.match(/^[\w_]+$/)) {
switch (node.type) {
case 'identifier':
case 'number':
case 'string':
case 'comment':
case 'string_fragment':
case 'template_string':
case 'simple_identifier':
case 'var':
case 'let':
case 'const':
case 'true':
case 'return':
case 'false':
out.text = node.text
break
}
} else {
out.text = node.text
}
if (node.children?.length) {
out.nest = []
for (const child of node.children) {
out.nest.push(traverse(child))
}
}
return out
}
}
// export async function compileToAsm(input: CompileToAst) {
// const parser = new Parser()
// parser.setLanguage(TREE_SITTER_LANGUAGE[input.input.format])
// const code = await fsp.readFile(input.input.file.path, 'utf-8')
// const tree = parser.parse(code)
// return traverse(tree.rootNode)

// function traverse(node) {
// const out: any = {
// form: node.type,
// base: node.startPosition,
// head: node.endPosition,
// }
// if (node.type.match(/^[\w_]+$/)) {
// switch (node.type) {
// case 'identifier':
// case 'number':
// case 'string':
// case 'comment':
// case 'string_fragment':
// case 'template_string':
// case 'simple_identifier':
// case 'var':
// case 'let':
// case 'const':
// case 'true':
// case 'return':
// case 'false':
// out.text = node.text
// break
// }
// } else {
// out.text = node.text
// }
// if (node.children?.length) {
// out.nest = []
// for (const child of node.children) {
// out.nest.push(traverse(child))
// }
// }
// return out
// }
// }

// function define(
// lang: string,
Expand Down
34 changes: 34 additions & 0 deletions code/node/system.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import si from 'systeminformation'
import _ from 'lodash'

export async function inspectSystem() {
return await inspectBasicSystem()
}

export async function inspectBasicSystem() {
const os = _.omit(await si.osInfo(), [
'serial',
'servicepack',
'logofile',
'fqdn',
'uefi',
])
const system = _.omit(await si.system(), [
'serial',
'uuid',
'sku',
'uuid',
])
const cpu = _.omit(await si.cpu(), [
'cache',
'governor',
'flags',
'virtualization',
'revision',
'voltage',
'vendor',
'speedMin',
'speedMax',
])
return { os, system, cpu }
}
12 changes: 12 additions & 0 deletions code/shared/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,21 @@ import {
FormatPython,
FormatRuby,
FormatRust,
FormatSqlWithContent,
FormatSwift,
LLVM_ARCHITECTURE_CONTENT,
} from './type/index.js'
import { format as _formatSql } from 'sql-formatter'
import decodeUtf8 from 'decode-utf8'

// https://github.com/sql-formatter-org/sql-formatter?tab=readme-ov-file
export function formatSqlWithContent(input: FormatSqlWithContent) {
const text =
input.input.file.content instanceof ArrayBuffer
? decodeUtf8(input.input.file.content)
: input.input.file.content
return _formatSql(text)
}

export function buildCommandToCompileSwift(input: CompileSwift) {
const cmd = getCommand(`swiftc`)
Expand Down
10 changes: 10 additions & 0 deletions code/shared/type/cast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92225,6 +92225,15 @@ export type FormatRust = {
}
}

export type FormatSqlWithContent = {
input: {
format: string
file: {
content: ArrayBuffer | string
}
}
}

export type FormatSwift = {
input: {
format: string
Expand Down Expand Up @@ -108104,6 +108113,7 @@ export const TASK = [
'read',
'add',
'verify',
'inspect',
] as const

export type Task = (typeof TASK)[number]
Expand Down
17 changes: 17 additions & 0 deletions code/shared/type/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ export const transform_input_output_file: Make = {
},
}

export const validate_input_file_path_or_content: Make = {
form: 'make',
make: (v: Record<string, any>, ctx: RefinementCtx) => {
if (!v.input.file.path) {
if (!v.input.file.content) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Must specify either input file path or content.`,
path: ['input', 'file'],
})
}
}

return v
},
}

export const test_time_string: Test = {
form: 'test',
test: (bond: string, name: string) =>
Expand Down
18 changes: 18 additions & 0 deletions code/shared/type/source/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -737,3 +737,21 @@ export const compile_to_ast: Form = {
},
},
}

export const format_sql_with_content: Form = {
form: 'form',
link: {
input: {
link: {
format: { like: 'string', name: { mark: 'I' } },
file: {
link: {
content: {
case: [{ like: 'ArrayBuffer' }, { like: 'string' }],
},
},
},
},
},
},
}
1 change: 1 addition & 0 deletions code/shared/type/source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export const task: List = {
'read',
'add',
'verify',
'inspect',
],
}

Expand Down
10 changes: 10 additions & 0 deletions code/shared/type/take.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,16 @@ export const FormatRustModel: z.ZodType<Cast.FormatRust> = z.object({
}),
})

export const FormatSqlWithContentModel: z.ZodType<Cast.FormatSqlWithContent> =
z.object({
input: z.object({
format: z.string(),
file: z.object({
content: z.union([z.instanceof(ArrayBuffer), z.string()]),
}),
}),
})

export const FormatSwiftModel: z.ZodType<Cast.FormatSwift> = z.object({
input: z.object({
format: z.string(),
Expand Down
1 change: 0 additions & 1 deletion code/shared/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
CompressMp4WithFfmpegModel,
ConvertMp4ToGifWithFfmpegModel,
ConvertVideoWithFfmpeg,
ConvertVideoWithFfmpegModel,
FfmpegCodecAudio,
IOPath,
} from './type/index.js'
Expand Down
Loading

0 comments on commit eb6f22c

Please sign in to comment.