Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Commit

Permalink
Enable read/write ability to remote repositories (#130)
Browse files Browse the repository at this point in the history
* Add parse command to context menu

Signed-off-by: Ayman161803 <[email protected]>

* Add webview to take in user input for parse command

Signed-off-by: Ayman161803 <[email protected]>

* Enable file read/write for remote repositories

Signed-off-by: Ayman161803 <[email protected]>
  • Loading branch information
Ayman161803 authored Aug 9, 2022
1 parent 5b47a84 commit 800fbca
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 33 deletions.
12 changes: 12 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@
"type": "npm",
"script": "watch"
}
},
{
"name": "Run Web Extension in VS Code",
"type": "extensionHost",
"debugWebWorkerHost": true,
"request": "launch",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}",
"--extensionDevelopmentKind=web"
],
"outFiles": ["${workspaceFolder}/dist/web/**/*.js"],
"preLaunchTask": "npm: watch-web"
},
{
"type": "node",
Expand Down
7 changes: 7 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
"problemMatcher": [
"$tsc-watch"
]
},
{
"type": "npm",
"script": "watch-web",
"group": "build",
"isBackground": true,
"problemMatcher": ["$ts-webpack-watch"]
}
]
}
8 changes: 4 additions & 4 deletions client/src/filewriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

'use strict';

import { URI } from 'vscode-uri';
import { URI, Utils } from 'vscode-uri';

const vscode = require('vscode');
const path = require('path');
Expand Down Expand Up @@ -105,12 +105,12 @@ export default class FileWriter extends Writer {

let filePath = this.outputDirectory;
if (this.relativeDir) {
filePath = path.resolve(filePath, this.relativeDir);
filePath = Utils.resolvePath(filePath, this.relativeDir);
}
filePath = path.resolve(filePath, this.fileName);
filePath = Utils.resolvePath(filePath, this.fileName);

//console.log('Writing to ' + filePath );
vscode.workspace.fs.writeFile(URI.file(filePath), Buffer.from(this.getBuffer(),'utf-8'));
vscode.workspace.fs.writeFile(filePath, Buffer.from(this.getBuffer(),'utf-8'));

this.fileName = null;
this.relativeDir = null;
Expand Down
26 changes: 13 additions & 13 deletions client/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const languageTagRegex = require('ietf-language-tag-regex');

const vscode = require('vscode')
import { FileType } from 'vscode';
import { URI } from 'vscode-uri';
import { URI, Utils } from 'vscode-uri';
const templateLoader = require('@accordproject/cicero-core').TemplateLoader;

// Matches 'sample.md' or 'sample_TAG.md' where TAG is an IETF language tag (BCP 47)
Expand All @@ -23,17 +23,17 @@ const SAMPLE_FILE_REGEXP = xregexp('text[/\\\\]sample(_(' + IETF_REGEXP + '))?.m
*/
async function loadFilesContents(path, regex) {

const subdirs = await vscode.workspace.fs.readDirectory(URI.parse(path));
const subdirs = await vscode.workspace.fs.readDirectory(path);

const result = await Promise.all(subdirs.map(async (subdir) => {
// subdir an array where subdir[0] is string representing path to file/directory within
// and subdir[1] represents whether the path leads to a folder or a file
const res = fsPath.resolve(path, subdir[0]);
const res = Utils.resolvePath(path, subdir[0]);

// if the subdir is a directory then we call this function recursively on that directory
// else we return the file contents of the file as result
if(subdir[1]==FileType.Directory){
if( /.*node_modules$/.test(res) === false) {
if( /.*node_modules$/.test(res.fsPath) === false) {
return loadFilesContents(res, regex);
}
else {
Expand All @@ -43,8 +43,8 @@ async function loadFilesContents(path, regex) {
else {
if(regex.test(res)) {
return {
name: res,
contents: await loadFileContents(path, res, false, true)
name: res.fsPath,
contents: await loadFileContents(path, res.fsPath, false, true)
};
}
else {
Expand All @@ -66,10 +66,10 @@ async function loadFilesContents(path, regex) {
* it does not exist and required is false
*/
async function loadFileBuffer(path, fileName, required=false) {
const filePath = fsPath.resolve(path, fileName);
const filePath = Utils.resolvePath(path, fileName);

try{
return await vscode.workspace.fs.readFile(URI.file(filePath));
return await vscode.workspace.fs.readFile(filePath);
}
catch(e) {
if(required){
Expand All @@ -90,10 +90,10 @@ async function loadFileBuffer(path, fileName, required=false) {
* required is false
*/
async function loadFileContents(path, fileName, json=false, required=false) {
const filePath = fsPath.resolve(path, fileName);
const filePath = Utils.resolvePath(path, fileName);

try {
const contents = Buffer.from(await vscode.workspace.fs.readFile(URI.file(filePath))).toString();
const contents = Buffer.from(await vscode.workspace.fs.readFile(filePath)).toString();
if(json && contents) {
return JSON.parse(contents);
}
Expand Down Expand Up @@ -156,7 +156,7 @@ export async function fromDirectory(Template, path, options = {offline:false}) {
const externalModelFiles = await template.getModelManager().addAPModelFiles(modelFiles, modelFileNames, options && options.offline);
if(!options || !options.offline){
externalModelFiles.forEach(function (file) {
vscode.workspace.fs.writeFile(URI.file(path + '/model/' + file.name), file.content);
vscode.workspace.fs.writeFile(Utils.resolvePath(path, '/model/', file.name), file.content);
});
}

Expand All @@ -176,7 +176,7 @@ export async function fromDirectory(Template, path, options = {offline:false}) {
if(template.getMetadata().getRuntime() === 'ergo') {
const ergoFiles = await loadFilesContents(path, /logic[/\\].*\.ergo$/);
ergoFiles.forEach((file) => {
const resolvedPath = slash(fsPath.resolve(path));
const resolvedPath = slash(fsPath.resolve(path.fsPath));
const resolvedFilePath = slash(fsPath.resolve(file.name));
const truncatedPath = resolvedFilePath.replace(resolvedPath+'/', '');
template.getLogicManager().addLogicFile(file.contents, truncatedPath);
Expand All @@ -185,7 +185,7 @@ export async function fromDirectory(Template, path, options = {offline:false}) {
// load and add compiled JS files - we assume all runtimes are JS based (review!)
const jsFiles = await loadFilesContents(path, /logic[/\\].*\.js$/);
jsFiles.forEach((file) => {
const resolvedPath = slash(fsPath.resolve(path));
const resolvedPath = slash(fsPath.resolve(path.fsPath));
const resolvedFilePath = slash(fsPath.resolve(file.name));
const truncatedPath = resolvedFilePath.replace(resolvedPath+'/', '');
template.getLogicManager().addLogicFile(file.contents, truncatedPath);
Expand Down
29 changes: 14 additions & 15 deletions client/src/webCommandHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ async function getModelManager(ctoFile: vscode.Uri) {

export async function downloadModels(file: vscode.Uri) {
try {
const outputPath = Utils.resolvePath(file,'..');
const outputPath = Utils.dirname(file);
const modelManager = await getModelManager(file)

if( modelManager ) {
Expand All @@ -118,7 +118,7 @@ export async function downloadModels(file: vscode.Uri) {
}
// Always assume file names have been normalized from `\` to `/`
const modelFilename = (modelFile.fileName).split('/').pop();
const modelFileUri = outputPath.with({ path: path.join(outputPath.path, modelFilename) });
const modelFileUri = Utils.resolvePath(outputPath, modelFilename);
await vscode.workspace.fs.writeFile(modelFileUri, Buffer.from(modelFile.definitions, 'utf8'));
});
}
Expand All @@ -134,7 +134,7 @@ export async function downloadModels(file: vscode.Uri) {

export async function exportClassDiagram(file: vscode.Uri) {
try {
const outputPath = path.dirname(file.path);
const outputPath = Utils.dirname(file);
const modelManager = await getModelManager(file);

if(modelManager) {
Expand All @@ -145,7 +145,7 @@ export async function exportClassDiagram(file: vscode.Uri) {
parameters.fileWriter = new FileWriter(outputPath);
modelManager.accept(visitor, parameters);

vscode.window.showInformationMessage(`Exported class diagram to ${outputPath}`);
vscode.window.showInformationMessage(`Exported class diagram to ${outputPath.fsPath}`);
return true;
}
} catch (error) {
Expand Down Expand Up @@ -327,14 +327,14 @@ export async function parseClause(file: vscode.Uri) {
}
);

panel.webview.html = await getParseWebviewContent(path.relative(templateDirectory.fsPath,file.path));
panel.webview.html = await getParseWebviewContent(path.relative(templateDirectory.fsPath,file.fsPath));

panel.webview.onDidReceiveMessage(
async (message) => {
const {samplePath,outputPath,utcOffset,currentTime} = message;
const template = await fromDirectory(Template,templateDirectory.fsPath);
const template = await fromDirectory(Template,templateDirectory);
const clause = new Clause(template);
const sampleText = Buffer.from(await vscode.workspace.fs.readFile(URI.file(path.resolve(templateDirectory.fsPath,samplePath)))).toString();
const sampleText = Buffer.from(await vscode.workspace.fs.readFile(Utils.resolvePath(templateDirectory,samplePath))).toString();

clause.parse(sampleText, currentTime, utcOffset, path.resolve(templateDirectory.fsPath,samplePath));

Expand All @@ -345,7 +345,7 @@ export async function parseClause(file: vscode.Uri) {
outputChannel.appendLine(JSON.stringify(clause.getData(),null,2));
outputChannel.appendLine('');

await vscode.workspace.fs.writeFile( URI.file(path.resolve(templateDirectory.fsPath,outputPath)), Buffer.from(JSON.stringify(clause.getData(),null,2),'utf-8'));
await vscode.workspace.fs.writeFile( Utils.resolvePath(templateDirectory,outputPath), Buffer.from(JSON.stringify(clause.getData(),null,2),'utf-8'));
outputChannel.appendLine(`Output written to ${outputPath}`);
outputChannel.appendLine('');
},
Expand All @@ -361,10 +361,9 @@ export async function parseClause(file: vscode.Uri) {
}

async function checkTemplate(file: vscode.Uri) {
const packageJsonPath = path.join(file.fsPath, 'package.json');
const packageJsonPath = Utils.resolvePath(file, 'package.json');
// vscode.window.showInformationMessage(`Loading template from ${packageJsonPath}`);

const packageJsonContents = Buffer.from(await vscode.workspace.fs.readFile(URI.file(packageJsonPath)));
const packageJsonContents = Buffer.from(await vscode.workspace.fs.readFile(packageJsonPath));

if(!packageJsonContents) {
vscode.window.showErrorMessage('Template package.json file was not found.');
Expand Down Expand Up @@ -393,13 +392,13 @@ export async function exportArchive(file: vscode.Uri) {
}

await vscode.workspace.saveAll();
const template = await fromDirectory(Template,file.fsPath);;
const template = await fromDirectory(Template,file);;
const archive = await template.toArchive('ergo');

const outputPath = path.join(file.path, `${template.getIdentifier()}.cta`);
const outputPath = Utils.resolvePath(file, `${template.getIdentifier()}.cta`);

await vscode.workspace.fs.writeFile( URI.file(outputPath), Buffer.from(archive));
vscode.window.showInformationMessage(`Created archive ${outputPath}`);
await vscode.workspace.fs.writeFile( outputPath, Buffer.from(archive));
vscode.window.showInformationMessage(`Created archive ${outputPath.fsPath}`);
return true;
} catch (error) {
vscode.window.showErrorMessage( `Failed to export archive ${error}`);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@
"compile": "tsc -b",
"watch": "tsc -b -w",
"web-compile": "webpack",
"web-watch": "webpack --watch",
"watch-web": "webpack --watch",
"lint": "eslint ./client/src ./server/src --ext .ts,.tsx",
"postinstall": "cd client && npm install && cd ../server && npm install && cd ..",
"test": "sh ./scripts/e2e.sh",
Expand Down

0 comments on commit 800fbca

Please sign in to comment.