microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/npm/qsharp/generate_samples_content.js
65lines · modeblame
02831734Bill Ticehurst3 years ago | 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. | |
| 3 | | |
| 4 | // @ts-check | |
| 5 | | |
| 6 | import { readFileSync, writeFileSync } from "node:fs"; | |
| 7 | import { dirname, join } from "node:path"; | |
| 8 | import { fileURLToPath } from "node:url"; | |
| 9 | | |
9da4acf2Scott Carda1 years ago | 10 | import qSharpSampleList from "../../../samples/samples.mjs"; |
| 11 | import openQasmSampleList from "../../../samples/OpenQASM/samples.mjs"; | |
02831734Bill Ticehurst3 years ago | 12 | |
| 13 | const thisDir = dirname(fileURLToPath(import.meta.url)); | |
9da4acf2Scott Carda1 years ago | 14 | const qSharpSampleDir = join(thisDir, "..", "..", "..", "samples"); |
| 15 | const openQasmSampleDir = join( | |
| 16 | thisDir, | |
| 17 | "..", | |
| 18 | "..", | |
| 19 | "..", | |
| 20 | "samples", | |
| 21 | "OpenQASM", | |
| 22 | ); | |
f11770aaDmitryVasilevsky1 years ago | 23 | |
| 24 | const tsDir = join(thisDir, "src"); | |
| 25 | const qSharpGeneratedTsPath = join(tsDir, "samples.generated.ts"); | |
| 26 | const openQasmGeneratedTsPath = join(tsDir, "openqasm-samples.generated.ts"); | |
| 27 | | |
| 28 | embedSampleContentsInTsFile( | |
| 29 | qSharpSampleList, | |
| 30 | qSharpSampleDir, | |
| 31 | qSharpGeneratedTsPath, | |
| 32 | ); | |
| 33 | embedSampleContentsInTsFile( | |
| 34 | openQasmSampleList, | |
| 35 | openQasmSampleDir, | |
| 36 | openQasmGeneratedTsPath, | |
02831734Bill Ticehurst3 years ago | 37 | ); |
f11770aaDmitryVasilevsky1 years ago | 38 | |
| 39 | /** | |
| 40 | * @param {any[]} sampleList | |
| 41 | * @param {string} sampleDir | |
| 42 | * @param {import("fs").PathOrFileDescriptor} generatedTsFileName | |
| 43 | */ | |
| 44 | function embedSampleContentsInTsFile( | |
| 45 | sampleList, | |
| 46 | sampleDir, | |
| 47 | generatedTsFileName, | |
| 48 | ) { | |
| 49 | const result = sampleList.map((sample) => { | |
| 50 | const samplePath = join(sampleDir, sample.file); | |
| 51 | const sampleText = readFileSync(samplePath, "utf8"); | |
| 52 | return { | |
| 53 | title: sample.title, | |
| 54 | shots: sample.shots, | |
| 55 | code: sampleText, | |
| 56 | omitFromTests: sample.omitFromTests, | |
| 57 | }; | |
| 58 | }); | |
| 59 | | |
| 60 | writeFileSync( | |
| 61 | generatedTsFileName, | |
| 62 | `export default ${JSON.stringify(result, undefined, 2)}`, | |
| 63 | "utf-8", | |
| 64 | ); | |
| 65 | } |