import * as path from "path" ; import { exec , parseJSONFileIfExists } from "../utils" ; import { Targets , Dependencies , BuildOptions , Tests , ProjectInfo } from "./types" ; async function introspectMeson < T > ( buildDir , filename , introspectSwitch ) { const parsed = await parseJSONFileIfExists < T > ( path . join ( buildDir , path . join ( "meson-info" , filename ) ) ) ; if ( parsed ) { return parsed ; } const { stdout } = await exec ( "meson" , [ "introspect" , introspectSwitch ] , { cwd : buildDir } ) ; return JSON . parse ( stdout ) as T ; } export async function getMesonTargets ( buildDir ) { const parsed = await introspectMeson < Targets > ( buildDir , "intro-targets.json" , "--targets" ) ; if ( getMesonVersion ( ) [ 1 ] < 50 ) { return parsed . map ( t => { if ( typeof t . filename === "string" ) t . filename = [ t . filename ] ; return t ; } ) ; } return parsed ; } export async function getMesonBuildOptions ( buildDir ) { return introspectMeson < BuildOptions > ( buildDir , "intro-buildoptions.json" , "--buildoptions" ) ; } export async function getMesonProjectInfo ( buildDir ) { return introspectMeson < ProjectInfo > ( buildDir , "intro-projectinfo.json" , "--project-info" ) ; } export async function getMesonDependencies ( buildDir ) { return introspectMeson < Dependencies > ( buildDir , "intro-dependencies.json" , "--dependencies" ) ; } export async function getMesonTests ( buildDir ) { return introspectMeson < Tests > ( buildDir , "intro-tests.json" , "--tests" ) ; } export async function getMesonBenchmarks ( buildDir ) { return introspectMeson < Tests > ( buildDir , "intro-benchmarks.json" , "--benchmarks" ) ; } export async function getMesonVersion ( ) { const MESON_VERSION_REGEX = /^(\d+)\.(\d+)\.(\d+)/g ; const { stdout } = await exec ( "meson" , [ "--version" ] ) ; const match = stdout . trim ( ) . match ( MESON_VERSION_REGEX ) ; if ( match ) { return match . slice ( 1 , 3 ) . map ( s => Number . parseInt ( s ) ) as [ number , number , number ] ; } else throw new Error ( "Meson version doesn't match expected output: " + stdout . trim ( ) ) ; }