Skip to content

Commit

Permalink
Merge pull request #69 from franneck94/fixEmptyArgs
Browse files Browse the repository at this point in the history
Fix empty args
  • Loading branch information
franneck94 authored Mar 22, 2022
2 parents ae2a987 + 2beba76 commit 7259d85
Show file tree
Hide file tree
Showing 47 changed files with 242 additions and 435 deletions.
24 changes: 0 additions & 24 deletions .github/workflows/unitTest.yml

This file was deleted.

29 changes: 1 addition & 28 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,6 @@
"args": ["--extensionDevelopmentPath=${workspaceFolder}"],
"outFiles": ["${workspaceFolder}/dist/**/*.js"],
"preLaunchTask": "npm: compile"
},
{
"name": "Unit Test Extension",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}",
"--extensionTestsPath=${workspaceFolder}/dist/test/unitTest/suite/index"
],
"outFiles": ["${workspaceFolder}/dist/**/*.js"],
"preLaunchTask": "npm: pretest"
},
{
"name": "Integration Test Extension",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"${workspaceFolder}/test/integrationTest/testAssets/testCpp",
"--extensionDevelopmentPath=${workspaceFolder}",
"--extensionTestsPath=${workspaceFolder}/dist/test/integrationTests/tests/index"
],
"stopOnEntry": false,
"sourceMaps": true,
"outFiles": ["${workspaceFolder}/dist/**/*.js"],
"preLaunchTask": "npm: pretest",
}
}
]
}
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# C/C++ Runner Change Log

## Version 3.2.3: March 22, 2022

- **Bugfix**: Fixed cmd argument bugs
- **Bugfix**: Fixed double folder entries in folder selection

## Version 3.2.2: March 6, 2022

- **Internal**: Removed task provider
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ These arguments will be stored in the launch.json config for debugging the binar

If you now run or debug your program these values will be fed into **argc**/**argv**.
The stored arguments will be reset after selecting a new active folder.
Note: For strings with whitespaces please use \"\".
Note: For strings with whitespaces please use \" (not single \').

### Include & Exclude Folders for Selection

Expand Down
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "c-cpp-runner",
"displayName": "C/C++ Runner",
"description": "🚀 Compile, run and debug single or multiple C/C++ files with ease. 🚀",
"version": "3.2.2",
"version": "3.2.3",
"publisher": "franneck94",
"license": "MIT",
"icon": "icon.png",
Expand Down Expand Up @@ -348,10 +348,7 @@
"compile": "npm run webpack",
"compile-watch": "npm run webpack-watch",
"watch": "tsc -watch -p ./",
"lint": "eslint src --ext ts",
"pretest": "tsc -p ./ && tsc -p test.tsconfig.json",
"test": "node ./dist/test/unitTest/runTest.js",
"integrationTest": "node ./dist/test/integrationTest/runTest.js"
"lint": "eslint src --ext ts"
},
"devDependencies": {
"@types/glob": "^7.1.3",
Expand Down
2 changes: 1 addition & 1 deletion src/executor/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export async function executeRunTask(
}

if (argumentsString) {
commandLine += ` ${argumentsString}`;
commandLine += ` ${argumentsString.replace(/"/g, '')}`;
}

if (!commandLine) return;
Expand Down
3 changes: 3 additions & 0 deletions src/handler/folderHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export async function folderHandler(
);
}

// XXX: Quick fix for double folder names due to include pattern
const foldersSet = new Set(foldersList);
foldersList = Array.from(foldersSet);
foldersList = naturalSort(foldersList);
});

Expand Down
66 changes: 50 additions & 16 deletions src/provider/launchProvider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as path from 'path';

import {
getOccurenceIndicies,
pathExists,
readJsonFile,
replaceBackslashes,
Expand Down Expand Up @@ -125,21 +126,54 @@ export class LaunchProvider extends FileProvider {
}

public updateArgumentsData(argumentsString: string | undefined) {
if (argumentsString !== undefined) {
if (argumentsString.includes(' ')) {
if (!argumentsString.includes('"')) {
this.argumentsString = argumentsString.split(' ');
} else {
this.argumentsString = argumentsString.split('" ');
}
if (argumentsString === undefined) return;

this.argumentsString = this.argumentsString.map((arg: string) =>
arg.replace('"', ''),
);
} else {
this.argumentsString = [argumentsString];
}
if (argumentsString === '') {
this.argumentsString = [];
return;
}

if (!argumentsString.includes(' ')) {
this.argumentsString = [argumentsString];
}

if (!argumentsString.includes('"')) {
this.argumentsString = argumentsString.split(' ');
return;
}

const indicies = getOccurenceIndicies(argumentsString, '"');
this.argumentsString = [];

const strsToReplace: string[] = [];
if (!indicies || indicies.length < 2) return;

for (let i = 0; i < indicies.length; i += 2) {
const sub = argumentsString.slice(
(indicies[i] as number) + 1,
indicies[i + 1],
);
this.argumentsString.push(sub);
strsToReplace.push(sub);
}

for (const strReplace of strsToReplace) {
argumentsString = argumentsString.replace(strReplace, '');
}

argumentsString = argumentsString.replace(/"/g, '');
if (argumentsString.startsWith(' ')) {
argumentsString = argumentsString.slice(1);
}
if (argumentsString.endsWith(' ')) {
argumentsString = argumentsString.slice(0, argumentsString.length - 2);
}

this.argumentsString.push(
...argumentsString.split(' ').filter((str: string) => Boolean(str)),
);

this.argumentsString.map((str: string) => str.replace(/"/g, ''));
}

public changeCallback() {
Expand Down Expand Up @@ -181,7 +215,7 @@ export class LaunchProvider extends FileProvider {
if (this.argumentsString) {
launchTemplate.configurations[0].args = this.argumentsString;
} else {
launchTemplate.configurations[0].args = [''];
launchTemplate.configurations[0].args = [];
}

launchTemplate.configurations[0].cwd = replaceBackslashes(
Expand Down Expand Up @@ -234,10 +268,10 @@ export class LaunchProvider extends FileProvider {
}
}

if (this.argumentsString) {
if (this.argumentsString && this.argumentsString.length > 0) {
launchTemplate.configurations[0].args = this.argumentsString;
} else {
launchTemplate.configurations[0].args = [''];
launchTemplate.configurations[0].args = [];
}

if (this.settings.operatingSystem === OperatingSystems.windows) {
Expand Down
159 changes: 159 additions & 0 deletions src/provider/taskProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import * as path from 'path';
import * as vscode from 'vscode';

import { extensionPath } from '../extension';
import { readJsonFile, writeJsonFile } from '../utils/fileUtils';
import {
Builds,
JsonConfiguration,
JsonTask,
OperatingSystems,
Task,
} from '../utils/types';
import { getLaunchConfigIndex } from '../utils/vscodeUtils';
import { SettingsProvider } from './settingsProvider';

const EXTENSION_NAME = 'C_Cpp_Runner';
const CONFIG_NAME = 'C/C++ Runner: Debug Session';

export class TaskProvider implements vscode.TaskProvider {
private readonly _tasksFile: string;
public tasks: Task[] | undefined;

constructor(
private readonly _settingsProvider: SettingsProvider,
public workspaceFolder: string | undefined,
public activeFolder: string | undefined,
public buildMode: Builds,
) {
const templateDirectory = path.join(
extensionPath ? extensionPath : '',
'templates',
);
this._tasksFile = path.join(templateDirectory, 'tasks_template.json');

this.getTasks();
}

public async resolveTask(task: Task) {
return task;
}

public provideTasks() {
return this.getTasks();
}

public getTasks() {
if (!this.activeFolder) return [];

this.setTasksDefinition();

if (!this.tasks) return [];

return this.tasks;
}

private setTasksDefinition() {
const taskType = 'shell';
const configJson: JsonTask = readJsonFile(this._tasksFile);

if (!configJson) {
return [];
}

this.tasks = [];

for (const taskJson of configJson.tasks) {
if (taskJson.type !== taskType) {
continue;
}
if (taskJson.options) {
if (taskJson.options.hide) {
continue;
}
}

const shellCommand = `${taskJson.command} ${taskJson.args.join(' ')}`;

const definition = {
type: taskType,
task: taskJson.label,
};
const problemMatcher = '$gcc';
const scope = vscode.TaskScope.Workspace;
let execution: vscode.ShellExecution;

if (this._settingsProvider.operatingSystem === OperatingSystems.windows) {
const shellOptions: vscode.ShellExecutionOptions = {
executable: 'C:/Windows/System32/cmd.exe',
shellArgs: ['/d', '/c'],
};
execution = new vscode.ShellExecution(shellCommand, shellOptions);
} else {
execution = new vscode.ShellExecution(shellCommand);
}

const task = new Task(
definition,
scope,
taskJson.label,
EXTENSION_NAME,
execution,
problemMatcher,
);

this.tasks.push(task);
}

return this.tasks;
}

public updateModeData(buildMode: Builds) {
this.buildMode = buildMode;
}

public updateFolderData(
workspaceFolder: string | undefined,
activeFolder: string | undefined,
) {
this.resetArguments();

this.workspaceFolder = workspaceFolder;
this.activeFolder = activeFolder;
}

private resetArguments() {
if (this.workspaceFolder) {
const launchPath = path.join(
this.workspaceFolder,
'.vscode',
'launch.json',
);

const configJson: JsonConfiguration | undefined = readJsonFile(
launchPath,
);

if (configJson) {
const configIdx = getLaunchConfigIndex(configJson, CONFIG_NAME);

if (configIdx === undefined) return;

configJson.configurations[configIdx].args = [];
writeJsonFile(launchPath, configJson);
}
}
}

public getProjectFolder() {
if (this.activeFolder) {
return this.activeFolder;
}

if (this.workspaceFolder) {
return this.workspaceFolder;
}

return undefined;
}
}
9 changes: 9 additions & 0 deletions src/utils/fileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,12 @@ export function getBasename(pathStr: string) {

return pathStr;
}

export function getOccurenceIndicies(text: string, char: string) {
const indices = [];
for (let i = 0; i < text.length; i++) {
if (text[i] === char) indices.push(i);
}

return indices;
}
Loading

0 comments on commit 7259d85

Please sign in to comment.