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

Adding support for Rascal Project creation PR-redo #248

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
14 changes: 7 additions & 7 deletions rascal-vscode-extension/package-lock.json

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

7 changes: 6 additions & 1 deletion rascal-vscode-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
],
"activationEvents": [
"onCommand:rascalmpl.createTerminal",
"onCommand:rascalmpl.createProject",
"onLanguage:rascalmpl"
],
"main": "./dist/extension.js",
Expand All @@ -46,6 +47,10 @@
{
"command": "rascalmpl.importMain",
"title": "Start Rascal Terminal and Import this module"
},
{
"command": "rascalmpl.createProject",
"title": "Create new Rascal project"
}
],
"languages": [
Expand Down Expand Up @@ -112,7 +117,7 @@
"devDependencies": {
"@types/glob": "^7.2.0",
"@types/mocha": "^9.1.1",
"@types/node": "^16.x",
"@types/node": "^16.18.23",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a specific reason to pin point this?

"@types/node-fetch": "^2.5.12",
"@types/tar": "^6.1.1",
"@types/temp": "^0.9.1",
Expand Down
136 changes: 136 additions & 0 deletions rascal-vscode-extension/src/RascalExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,18 @@ import * as os from 'os';
import * as path from 'path';
import * as vscode from 'vscode';

import { writeFileSync } from 'fs';
Copy link
Member Author

@DavyLandman DavyLandman Apr 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two concerns:

  • we try to avoid "native" parts of node. since those will prevent us from running as a browser extension, if we ever get that far.
  • VS Code doesn't like it if we run stuff in sync. Since all the extensions in vscode share the same node-process, we have to be good neighours, and write our code in an async way. luckily, with promises this has gotten a lot more readable.
  • VS Code has a Virtual File System, we should try our best to keep within that, and not escape it more than needed.


import { integer } from 'vscode-languageclient/node';
import { getJavaExecutable } from './auto-jvm/JavaLookup';
import { RascalLanguageServer } from './lsp/RascalLanguageServer';
import { LanguageParameter, ParameterizedLanguageServer } from './lsp/ParameterizedLanguageServer';
import { RascalTerminalLinkProvider } from './RascalTerminalLinkProvider';
import { VSCodeUriResolverServer } from './fs/VSCodeURIResolver';

const vsfs = vscode.workspace.fs;
const URI = vscode.Uri;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't this be done at import time? import { Uri } from 'vscode'?


export class RascalExtension implements vscode.Disposable {
private readonly vfsServer: VSCodeUriResolverServer;
private readonly dsls:ParameterizedLanguageServer;
Expand All @@ -50,6 +55,7 @@ export class RascalExtension implements vscode.Disposable {
this.registerTerminalCommand();
this.registerMainRun();
this.registerImportModule();
this.createProject();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick, let's rename it to registerCreateProject as it's not actually creating a project.


vscode.window.registerTerminalLinkProvider(new RascalTerminalLinkProvider(this.rascal.rascalClient));
}
Expand Down Expand Up @@ -87,6 +93,34 @@ export class RascalExtension implements vscode.Disposable {
);
}

private createProject() {
this.context.subscriptions.push(
vscode.commands.registerCommand("rascalmpl.createProject",
async () => {
try {
const filePath = await vscode.window.showOpenDialog({
canSelectFiles: false,
canSelectFolders: true,
canSelectMany: false,
title: "New Rascal project",
});

if (filePath) {
const dest = filePath[0].path;
const destUri = URI.file(dest);
const projectName = dest.split("/").at(-1) as string;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on windows the path won't be seperated by / . The module path in node has logic that helps us deal with paths. If we do it on URI paths, we have to use posix from the path module. The RascalMFValidator has some example of this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do like the idea of forcing the projectName as a directory name in this explicit manner.

We could also use the inverse, and popup some input boxes, and then ask for the root directory.


newRascalProject(dest, projectName);

// Open a workspace window
await vscode.commands.executeCommand("vscode.openFolder", destUri, true);
}
} catch (e) {
console.error(e);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if we fail the command, we should show a message, as otherwise the command has no output, it just didn't do anything observable.

Using the vscode.window.withProgress will also help give some feedback to what is happening.

}
})
);
}

private registerImportModule() {
this.context.subscriptions.push(
Expand Down Expand Up @@ -183,3 +217,105 @@ function calculateRascalREPLMemory() {
}
return "-Xmx800M";
}

async function newRascalProject(dest: string, name: string) {

const baseDest = URI.file(dest);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before calling the function, we already calculated the Uri, we could move this up, so that this can also work for non-file uri's (like virtual file systems).

vsfs.createDirectory(baseDest);

const metaPath = URI.joinPath(baseDest, "META-INF");
await vsfs.createDirectory(metaPath);

const srcPath = URI.joinPath(baseDest, "src/main/rascal/");
await vsfs.createDirectory(srcPath);

const rascalMFPath = URI.joinPath(metaPath, "RASCAL.MF");
writeFileSync(rascalMFPath.fsPath, rascalMF(name));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed earlier, we should do await vsfs.writeFile (which takes a byte array as argument, so we have to produce the utf8 encoded variant of the string). But that both allows us to work with any virtual file system, and be a better VS Code neighbor.


const pomPath = URI.joinPath(baseDest, "pom.xml");
writeFileSync(pomPath.fsPath, pomXML(name));

const mainPath = URI.joinPath(srcPath, "Main.rsc");
writeFileSync(mainPath.fsPath, emptyModule);

}

function rascalMF(name: string): string {
return "Manifest-Version: 0.0.1\n" +
`Project-Name: ${name}\n` +
"Source: src/main/rascal\n" +
"Require-Libraries: \n"
;
}

function pomXML(name: string, group="org.rascalmpl", version="0.1.0-SNAPSHOT"): string {
return `<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>${group}</groupId>
<artifactId>${name}</artifactId>
<version>${version}</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<repositories>
<repository>
<id>usethesource</id>
<url>https://releases.usethesource.io/maven/</url>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>usethesource</id>
<url>https://releases.usethesource.io/maven/</url>
</pluginRepository>
</pluginRepositories>

<dependencies>
<dependency>
<groupId>org.rascalmpl</groupId>
<artifactId>rascal</artifactId>
<version><getRascalVersion()></version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<compilerArgument>-parameters</compilerArgument>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.rascalmpl</groupId>
<artifactId>rascal-maven-plugin</artifactId>
<version>0.8.2</version>
<configuration>
<errorsAsWarnings>true</errorsAsWarnings>
<bin>\${project.build.outputDirectory}</bin>
<srcs>
<src>$\{project.basedir}/src/main/rascal</src>
</srcs>
</configuration>
</plugin>
</plugins>
</build>
</project>
`;
}

const emptyModule = "module Main\n" +
"import IO;\n\n"+
"int main(int testArgument=0) {\n" +
` println("argument: <testArgument>");\n` +
" return testArgument;\n"+
"}";