Skip to content
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

fix: setup of CLI #698

Merged
merged 3 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ jobs:
run: npx semantic-release
env:
GITHUB_TOKEN: ${{ secrets.PAT || secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
VSCE_PAT: ${{ secrets.VSCE_PAT }}
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions packages/safe-ds-cli/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
# Safe-DS CLI

```txt
Usage: cli [options] [command]

Options:
-V, --version output the version number
-h, --help display help for command

Commands:
generate [options] <file> generate Python code
help [command] display help for command
```
1 change: 0 additions & 1 deletion packages/safe-ds-cli/esbuild.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ const ctx = await esbuild.context({
'.js': '.cjs',
},
loader: { '.ts': 'ts' },
external: ['vscode'],
platform: 'node',
sourcemap: !minify,
minify,
Expand Down
13 changes: 5 additions & 8 deletions packages/safe-ds-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "@safe-ds/cli",
"version": "0.2.0",
"private": true,
"version": "0.2.2",
"description": "A command line interface for the Safe-DS DSL.",
"author": {
"name": "Lars Reimann",
Expand All @@ -26,22 +25,20 @@
},
"type": "module",
"bin": {
"safe-ds": "./bin/cli"
"safe-ds": "./bin/cli.js"
},
"exports": null,
"files": [
"bin",
"dist",
"src"
"dist"
],
"scripts": {
"clean": "shx rm -rf dist lib tsconfig.tsbuildinfo",
"build": "tsc -b tsconfig.json && node esbuild.mjs",
"build:clean": "npm run clean && npm run build",
"watch": "concurrently -n tsc,esbuild -c blue,yellow \"tsc -b tsconfig.json --watch\" \"node esbuild.mjs --watch\"",
"start": "node ./bin/cli"
"watch": "concurrently -n tsc,esbuild -c blue,yellow \"tsc -b tsconfig.json --watch\" \"node esbuild.mjs --watch\""
},
"dependencies": {
"devDependencies": {
"chalk": "^5.3.0",
"commander": "^11.1.0",
"esbuild": "^0.19.5",
Expand Down
4 changes: 2 additions & 2 deletions packages/safe-ds-cli/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import { createSafeDsServices } from 'safe-ds';
export const generate = async (fileName: string, opts: GenerateOptions): Promise<void> => {
const services = createSafeDsServices(NodeFileSystem).SafeDs;
const document = await extractDocument(fileName, services);
const generatedFiles = services.generation.PythonGenerator.generate(document, opts.destination);
const destination = opts.destination ?? path.join(path.dirname(fileName), 'generated');
const generatedFiles = services.generation.PythonGenerator.generate(document, URI.file(path.resolve(destination)));

for (const file of generatedFiles) {
const fsPath = URI.parse(file.uri).fsPath;
const parentDirectoryPath = path.dirname(fsPath);

if (!fs.existsSync(parentDirectoryPath)) {
fs.mkdirSync(parentDirectoryPath, { recursive: true });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,32 +100,31 @@ export class SafeDsPythonGenerator {
this.partialEvaluator = services.evaluation.PartialEvaluator;
}

generate(document: LangiumDocument, destination: string | undefined): TextDocument[] {
generate(document: LangiumDocument, destination: URI): TextDocument[] {
const node = document.parseResult.value;

// Do not generate stub files
if (isStubFile(document) || !isSdsModule(node)) {
return [];
}

const filePath = document.uri.fsPath;
const data = extractDestinationAndName(filePath, destination);
const name = path.parse(document.uri.fsPath).name;
const pythonModuleName = this.builtinAnnotations.getPythonModule(node);
const packagePath = pythonModuleName === undefined ? node.name.split('.') : [pythonModuleName];
const parentDirectoryPath = path.join(data.destination, ...packagePath);
const parentDirectoryPath = path.join(destination.fsPath, ...packagePath);

const generatedFiles = new Map<string, string>();
generatedFiles.set(
`${path.join(parentDirectoryPath, this.formatGeneratedFileName(data.name))}.py`,
`${path.join(parentDirectoryPath, this.formatGeneratedFileName(name))}.py`,
this.generateModule(node),
);
for (const pipeline of streamAllContents(node).filter(isSdsPipeline)) {
const entryPointFilename = `${path.join(
parentDirectoryPath,
`${this.formatGeneratedFileName(data.name)}_${this.getPythonNameOrDefault(pipeline)}`,
`${this.formatGeneratedFileName(name)}_${this.getPythonNameOrDefault(pipeline)}`,
)}.py`;
const entryPointContent = expandToStringWithNL`from ${this.formatGeneratedFileName(
data.name,
name,
)} import ${this.getPythonNameOrDefault(
pipeline,
)}\n\nif __name__ == '__main__':\n${PYTHON_INDENT}${this.getPythonNameOrDefault(pipeline)}()`;
Expand Down Expand Up @@ -600,16 +599,3 @@ class GenerationInfoFrame {
return `${BLOCK_LAMBDA_PREFIX}${this.blockLambdaManager.assignId(lambda)}`;
}
}

interface FilePathData {
destination: string;
name: string;
}

const extractDestinationAndName = function (filePath: string, destination: string | undefined): FilePathData {
const baseFilePath = path.basename(filePath, path.extname(filePath)).replace(/[.-]/gu, '');
return {
destination: destination ?? path.join(path.dirname(baseFilePath), 'generated'),
name: path.basename(baseFilePath),
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('generation', async () => {

// Generate code for all documents
const actualOutputs = stream(documents)
.flatMap((document) => pythonGenerator.generate(document, test.actualOutputRoot.fsPath))
.flatMap((document) => pythonGenerator.generate(document, test.actualOutputRoot))
.map((textDocument) => [textDocument.uri, textDocument.getText()])
.toMap(
(entry) => entry[0],
Expand Down