-
Notifications
You must be signed in to change notification settings - Fork 215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Import typespec project into a single file #4383
Draft
timotheeguerin
wants to merge
25
commits into
microsoft:main
Choose a base branch
from
timotheeguerin:import-single-file
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
0f3b89c
Import as single file initial
timotheeguerin c22b89d
Fix
timotheeguerin 9ae1564
Try stuff
timotheeguerin 5c95afe
tweaks
timotheeguerin 1ccb4b6
Merge branch 'main' of https://github.com/Microsoft/typespec into imp…
timotheeguerin 2b8e2aa
Migrate to source loader
timotheeguerin 1a93dd4
Setup debugger
timotheeguerin 3044f7d
fix source loader
timotheeguerin 2a6af84
Progress
timotheeguerin cae4de4
Generate
timotheeguerin f38faa8
Tweaks
timotheeguerin 7865753
tweaks
timotheeguerin 3eba979
Fix printing of raw text
timotheeguerin 1ada5db
Fix
timotheeguerin 25234d8
fix usings
timotheeguerin a437f30
Importer in playgroud
timotheeguerin cc02138
Add to the playground
timotheeguerin a50b535
tweaks
timotheeguerin d243bf1
cleanup
timotheeguerin 198d7ef
Merge branch 'main' of https://github.com/Microsoft/typespec into imp…
timotheeguerin af0f383
lock
timotheeguerin ec421a2
fix
timotheeguerin 9120da5
fix
timotheeguerin d8a9d00
cleanup
timotheeguerin a247fef
Merge branch 'main' into import-single-file
timotheeguerin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -520,7 +520,7 @@ export function printComment( | |
case SyntaxKind.BlockComment: | ||
return printBlockComment(commentPath as AstPath<BlockComment>, options); | ||
case SyntaxKind.LineComment: | ||
return `${options.originalText.slice(comment.pos, comment.end).trimEnd()}`; | ||
return `${getRawText(comment, options).trimEnd()}`; | ||
default: | ||
throw new Error(`Not a comment: ${JSON.stringify(comment)}`); | ||
} | ||
|
@@ -1660,7 +1660,7 @@ function printNumberLiteral( | |
options: TypeSpecPrettierOptions, | ||
): Doc { | ||
const node = path.node; | ||
return getRawText(node, options); | ||
return node.valueAsString; | ||
} | ||
|
||
function printBooleanLiteral( | ||
|
@@ -1975,7 +1975,10 @@ function printItemList<T extends Node>( | |
* @param options Prettier options | ||
* @returns Raw text in the file for the given node. | ||
*/ | ||
function getRawText(node: TextRange, options: TypeSpecPrettierOptions) { | ||
function getRawText(node: TextRange, options: TypeSpecPrettierOptions): string { | ||
if ("rawText" in node) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added this way of setting the syntax for basic nodes where they can have |
||
return node.rawText as string; | ||
} | ||
return options.originalText.slice(node.pos, node.end); | ||
} | ||
|
||
|
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,5 @@ | ||
/** | ||
* File serving as an entrypoint to resolve a local tsp install from a global install. | ||
* DO NOT MOVE or this will create a breaking change for user of global cli. | ||
*/ | ||
import "../dist/cli.js"; |
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,60 @@ | ||
{ | ||
"name": "@typespec/importer", | ||
"private": true, | ||
"version": "0.0.1", | ||
"author": "Microsoft Corporation", | ||
"description": "Package to import TypeSpec files into a single one", | ||
"homepage": "https://typespec.io", | ||
"readme": "https://github.com/microsoft/typespec/blob/main/README.md", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/microsoft/typespec.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/microsoft/typespec/issues" | ||
}, | ||
"keywords": [ | ||
"typespec" | ||
], | ||
"type": "module", | ||
"main": "dist/src/index.js", | ||
"exports": { | ||
".": "./dist/index.js" | ||
}, | ||
"bin": "dist/src/cli.js", | ||
"engines": { | ||
"node": ">=18.0.0" | ||
}, | ||
"scripts": { | ||
"bundle": "node ./dist/src/cli.js", | ||
"clean": "rimraf ./dist ./temp", | ||
"build": "tsc -p .", | ||
"watch": "tsc -p . --watch", | ||
"test": "vitest run", | ||
"test:ui": "vitest --ui", | ||
"test:ci": "vitest run --coverage --reporter=junit --reporter=default", | ||
"lint": "eslint . --max-warnings=0", | ||
"lint:fix": "eslint . --fix" | ||
}, | ||
"files": [ | ||
"lib/*.tsp", | ||
"dist/**", | ||
"!dist/test/**" | ||
], | ||
"dependencies": { | ||
"@typespec/compiler": "workspace:~", | ||
"picocolors": "~1.1.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "~22.5.4", | ||
"@vitest/coverage-v8": "^2.1.0", | ||
"@vitest/ui": "^2.1.0", | ||
"c8": "^10.1.2", | ||
"rimraf": "~6.0.1", | ||
"source-map-support": "~0.5.21", | ||
"typescript": "~5.6.2", | ||
"vite": "^5.4.4", | ||
"vitest": "^2.1.0" | ||
} | ||
} |
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,42 @@ | ||
try { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
await import("source-map-support/register.js"); | ||
} catch { | ||
// package only present in dev. | ||
} | ||
|
||
import { getDirectoryPath, logDiagnostics, NodeHost, normalizePath } from "@typespec/compiler"; | ||
import { mkdir, writeFile } from "fs/promises"; | ||
import { resolve } from "path"; | ||
import { parseArgs } from "util"; | ||
import { ImporterHost } from "./importer-host.js"; | ||
import { combineProjectIntoFile } from "./importer.js"; | ||
|
||
function log(...args: any[]) { | ||
// eslint-disable-next-line no-console | ||
console.log(...args); | ||
} | ||
const args = parseArgs({ | ||
options: {}, | ||
args: process.argv.slice(2), | ||
allowPositionals: true, | ||
}); | ||
|
||
const rawEntrypoint = normalizePath(resolve(args.positionals[0])); | ||
|
||
const { content, diagnostics } = await combineProjectIntoFile(ImporterHost, rawEntrypoint); | ||
|
||
if (diagnostics.length > 0) { | ||
logDiagnostics(diagnostics, NodeHost.logSink); | ||
process.exit(1); | ||
} | ||
if (content) { | ||
const outputFile = "tsp-output/main.tsp"; | ||
await mkdir(getDirectoryPath(outputFile), { recursive: true }); | ||
log(`Writing output to ${outputFile}`); | ||
await writeFile(outputFile, content); | ||
process.exit(0); | ||
} else { | ||
process.exit(1); | ||
} |
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,10 @@ | ||
import { NodeHost, type CompilerHost } from "@typespec/compiler"; | ||
import { createRemoteHost } from "./remote-host.js"; | ||
|
||
/** | ||
* Special host that tries to load data from additional locations | ||
*/ | ||
export const ImporterHost: CompilerHost = { | ||
...NodeHost, | ||
...createRemoteHost(NodeHost), | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not possible to check with urls and doesn't bring too much values apart from figuring out you passed the wrong value when developping.