Skip to content

Commit

Permalink
improve encapsulation of CommandContext
Browse files Browse the repository at this point in the history
  • Loading branch information
Ievgenii.Mykhalevskyi authored and Ievgenii.Mykhalevskyi committed Sep 29, 2024
1 parent db8e63b commit 8936047
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/BuildTaskProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export class BuildTaskProvider implements vscode.TaskProvider {
onDidWrite: writeEmitter.event,
onDidClose: closeEmitter.event,
close: async () => {
commandContext?.cancellationToken.cancel();
commandContext?.cancel();
},
};
resolved(pty);
Expand Down
12 changes: 6 additions & 6 deletions src/CommandManagement/AtomicCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ export class AtomicCommand {
try {
if (this._mutex.isLocked()) {
if (this._executingCommand == "autowatcher") {
this._prevCommandContext?.cancellationToken.cancel();
this._prevCommandContext?.cancel();
this._mutex.cancel();
} else {
throw UserCommandIsExecuting;
}
} else {
this._prevCommandContext?.cancellationToken.cancel();
this._prevCommandContext?.cancel();
}
release = await this._mutex.acquire();
if (currentOperationID !== this.latestOperationID)
Expand All @@ -75,11 +75,11 @@ export class AtomicCommand {
}
}

private async withCancellation(closure: () => Promise<void>, cancellation: vscode.CancellationTokenSource) {
private async withCancellation(closure: () => Promise<void>, cancellation: vscode.CancellationToken) {
let dis: vscode.Disposable;
return new Promise<void>(async (resolve, reject) => {
try {
dis = cancellation.token.onCancellationRequested(e => {
dis = cancellation.onCancellationRequested(e => {
dis.dispose();
reject(UserTerminatedError);
})
Expand Down Expand Up @@ -107,13 +107,13 @@ export class AtomicCommand {
);
}
if (choice === "Terminate") {
this._prevCommandContext?.cancellationToken.cancel();
this._prevCommandContext?.cancel();
this._mutex.cancel();
} else {
throw UserCommandIsExecuting;
}
} else {
this._prevCommandContext?.cancellationToken.cancel();
this._prevCommandContext?.cancel();
}
releaser = await this._mutex.acquire();
if (currentOperationID !== this.latestOperationID)
Expand Down
23 changes: 17 additions & 6 deletions src/CommandManagement/CommandContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@ import { Executor, ExecutorMode, ExecutorReturnType } from "../execShell";
export const UserTerminatedError: Error = new Error("Terminated");

export class CommandContext {
cancellationToken: vscode.CancellationTokenSource;
private executor: Executor;

private _cancellationTokenSource: vscode.CancellationTokenSource;
private _executor: Executor;
public get executor(): Executor {
return this.executor;
}
public get cancellationToken(): vscode.CancellationToken {
return this._cancellationTokenSource.token;
}

constructor(cancellationToken: vscode.CancellationTokenSource, executor: Executor) {
this.cancellationToken = cancellationToken;
this.executor = executor;
this._cancellationTokenSource = cancellationToken;
this._executor = executor;
}

public async execShell(
Expand All @@ -20,8 +27,8 @@ export class CommandContext {
returnType = ExecutorReturnType.statusCode,
mode: ExecutorMode = ExecutorMode.verbose
): Promise<boolean | string> {
return await this.executor.execShell(
this.cancellationToken.token,
return await this._executor.execShell(
this._cancellationTokenSource.token,
commandName,
fileOrCommand,
args,
Expand All @@ -30,4 +37,8 @@ export class CommandContext {
mode
)
}

public cancel() {
this._cancellationTokenSource.cancel();
}
}
2 changes: 1 addition & 1 deletion src/Debug/DebugAdapterTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class DebugAdapterTracker implements vscode.DebugAdapterTracker {
await DebugAdapterTracker.updateStatus(this.sessionID, "stopped");
const terminationContext = new CommandContext(new vscode.CancellationTokenSource(), new Executor());
await terminateCurrentIOSApp(terminationContext, this.sessionID, true);
this.commandContext?.cancellationToken.cancel();
this.commandContext?.cancel();
} finally {
try {
await vscode.debug.stopDebugging(this.debugSession);
Expand Down
11 changes: 5 additions & 6 deletions src/Debug/DebugConfigurationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class DebugConfigurationProvider implements vscode.DebugConfigurationProv
});
}

async startIOSTestsDebugger(isDebuggable: boolean) {
async startIOSTestsDebugger(isDebuggable: boolean, testRun: vscode.TestRun) {
const appSessionId = getSessionId(`All tests: ${isDebuggable}${this.counterID}`);
let debugSession: vscode.DebugConfiguration = {
type: "xcode-lldb",
Expand All @@ -76,20 +76,19 @@ export class DebugConfigurationProvider implements vscode.DebugConfigurationProv
if (e === appSessionId)
resolve(true);
});
vscode.debug.startDebugging(undefined, debugSession);
vscode.debug.startDebugging(undefined, debugSession, { testRun: testRun });
});
}

async startIOSTestsForCurrentFileDebugger(tests: string[], isDebuggable: boolean) {
async startIOSTestsForCurrentFileDebugger(tests: string[], isDebuggable: boolean, testRun: vscode.TestRun) {
const appSessionId = `${getSessionId(tests.join(","))}_${isDebuggable}${this.counterID}`;
let debugSession: vscode.DebugConfiguration = {
type: "xcode-lldb",
name: "iOS: Run Tests & Debug: Current File",
request: "launch",
target: "testsForCurrentFile",
isDebuggable: isDebuggable,
appSessionId: appSessionId,
testsToRun: tests
appSessionId: appSessionId
};

let dis: vscode.Disposable | undefined;
Expand All @@ -98,7 +97,7 @@ export class DebugConfigurationProvider implements vscode.DebugConfigurationProv
if (e === appSessionId)
resolve(true);
});
vscode.debug.startDebugging(undefined, debugSession);
vscode.debug.startDebugging(undefined, debugSession, { testRun: testRun });
});
}

Expand Down
6 changes: 3 additions & 3 deletions src/TestsProvider/TestProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import { error } from 'console';

export class TestProvider {
projectManager: ProjectManager
executeTests: (tests: string[] | undefined, isDebuggable: boolean) => Promise<boolean>;
executeTests: (tests: string[] | undefined, isDebuggable: boolean, testRun: vscode.TestRun) => Promise<boolean>;
context = new TestTreeContext();
asyncParser = new TestCaseAsyncParser()
asyncTestCaseParser = new TestCaseProblemParser();

constructor(projectManager: ProjectManager, executeTests: (tests: string[] | undefined, isDebuggable: boolean) => Promise<boolean>) {
constructor(projectManager: ProjectManager, executeTests: (tests: string[] | undefined, isDebuggable: boolean, testRun: vscode.TestRun) => Promise<boolean>) {
this.projectManager = projectManager;
this.executeTests = executeTests;
}
Expand Down Expand Up @@ -97,7 +97,7 @@ export class TestProvider {
}
console.log("log");
});
await this.executeTests(request.include === undefined ? undefined : tests, request.profile?.kind === vscode.TestRunProfileKind.Debug);
await this.executeTests(request.include === undefined ? undefined : tests, request.profile?.kind === vscode.TestRunProfileKind.Debug, run);
}
catch (err) {
console.log(`Run with error: ${err}`);
Expand Down
9 changes: 4 additions & 5 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ async function initialize(atomicCommand: AtomicCommand, projectManager: ProjectM
}
}

const projectExecutor = new Executor();
const atomicCommand = new AtomicCommand(projectExecutor);
const atomicCommand = new AtomicCommand(new Executor());
const problemDiagnosticResolver = new ProblemDiagnosticResolver();

let debugConfiguration: DebugConfigurationProvider;
Expand Down Expand Up @@ -131,11 +130,11 @@ export async function activate(context: vscode.ExtensionContext) {
debugSessionEndEvent.event
);

testProvider = new TestProvider(projectManager, async (tests, isDebuggable) => {
testProvider = new TestProvider(projectManager, async (tests, isDebuggable, testRun) => {
if (tests) {
return await debugConfiguration.startIOSTestsForCurrentFileDebugger(tests, isDebuggable);
return await debugConfiguration.startIOSTestsForCurrentFileDebugger(tests, isDebuggable, testRun);
} else {
return await debugConfiguration.startIOSTestsDebugger(isDebuggable);
return await debugConfiguration.startIOSTestsDebugger(isDebuggable, testRun);
}
});
testProvider.activateTests(context);
Expand Down

0 comments on commit 8936047

Please sign in to comment.