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

Commit

Permalink
persist temp dir
Browse files Browse the repository at this point in the history
  • Loading branch information
0xch4z committed Sep 10, 2018
1 parent bf3174b commit 1664a15
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/debugAdapter/goDebug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { basename, dirname, extname } from 'path';
import { spawn, ChildProcess, execSync, spawnSync, execFile } from 'child_process';
import { Client, RPCConnection } from 'json-rpc2';
import { parseEnvFile, getBinPathWithPreferredGopath, resolveHomeDir, getInferredGopath, getCurrentGoWorkspaceFromGOPATH, envPath, fixDriveCasingInWindows } from '../goPath';
import { getTempFile } from '../util';
import { TempFileProvider } from '../util';
import * as logger from 'vscode-debug-logger';

require('console-stamp')(console);
Expand Down Expand Up @@ -532,7 +532,7 @@ class GoDebugSession extends DebugSession {
this.delve = null;
this.breakpoints = new Map<string, DebugBreakpoint[]>();

const logPath = getTempFile('vscode-go-debug.txt');
const logPath = TempFileProvider.instance.getFilePath('vscode-go-debug.txt');
logger.init(e => this.sendEvent(e), logPath, isServer);
}

Expand Down
4 changes: 2 additions & 2 deletions src/goBuild.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path = require('path');
import vscode = require('vscode');
import { getToolsEnvVars, runTool, ICheckResult, handleDiagnosticErrors, getWorkspaceFolderPath, getCurrentGoPath, getUserNameHash, getTempFile } from './util';
import { getToolsEnvVars, runTool, ICheckResult, handleDiagnosticErrors, getWorkspaceFolderPath, getCurrentGoPath, TempFileProvider } from './util';
import { outputChannel } from './goStatus';
import os = require('os');
import { getNonVendorPackages } from './goPackages';
Expand Down Expand Up @@ -70,7 +70,7 @@ export function goBuild(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfigura
}

const buildEnv = Object.assign({}, getToolsEnvVars());
const tmpPath = getTempFile('go-code-check.' + getUserNameHash());
const tmpPath = TempFileProvider.instance.getFilePath('go-code-check');
const isTestFile = fileUri && fileUri.fsPath.endsWith('_test.go');
const buildFlags: string[] = isTestFile ? getTestFlags(goConfig, null) : (Array.isArray(goConfig['buildFlags']) ? [...goConfig['buildFlags']] : []);
const buildArgs: string[] = isTestFile ? ['test', '-c'] : ['build'];
Expand Down
4 changes: 2 additions & 2 deletions src/goCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import os = require('os');
import { getCoverage } from './goCover';
import { outputChannel, diagnosticsStatusBarItem } from './goStatus';
import { goTest } from './testUtils';
import { ICheckResult, getBinPath, getTempFile } from './util';
import { ICheckResult, getBinPath, TempFileProvider } from './util';
import { goLint } from './goLint';
import { goVet } from './goVet';
import { goBuild } from './goBuild';
Expand Down Expand Up @@ -69,7 +69,7 @@ export function check(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfigurati

let args = [...buildFlags];
if (goConfig['coverOnSave']) {
tmpCoverPath = getTempFile('go-code-cover');
tmpCoverPath = TempFileProvider.instance.getFilePath('go-code-cover');
args = ['-coverprofile=' + tmpCoverPath, ...buildFlags];
}

Expand Down
4 changes: 2 additions & 2 deletions src/goCover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import vscode = require('vscode');
import path = require('path');
import os = require('os');
import fs = require('fs');
import { getTempFile } from './util';
import { TempFileProvider } from './util';
import { showTestOutput, goTest } from './testUtils';
import rl = require('readline');

Expand Down Expand Up @@ -113,7 +113,7 @@ export function toggleCoverageCurrentPackage() {
let cwd = path.dirname(editor.document.uri.fsPath);

let buildFlags = goConfig['testFlags'] || goConfig['buildFlags'] || [];
let tmpCoverPath = getTempFile('go-code-cover');
let tmpCoverPath = TempFileProvider.instance.getFilePath('go-code-cover');
let args = ['-coverprofile=' + tmpCoverPath, ...buildFlags];
return goTest({
goConfig: goConfig,
Expand Down
3 changes: 2 additions & 1 deletion src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { showTestOutput, cancelRunningTests } from './testUtils';
import * as goGenerateTests from './goGenerateTests';
import { addImport, addImportToWorkspace } from './goImport';
import { installAllTools, checkLanguageServer } from './goInstallTools';
import { isGoPathSet, getBinPath, sendTelemetryEvent, getExtensionCommands, getGoVersion, getCurrentGoPath, getToolsGopath, handleDiagnosticErrors, disposeTelemetryReporter, getToolsEnvVars } from './util';
import { isGoPathSet, getBinPath, sendTelemetryEvent, getExtensionCommands, getGoVersion, getCurrentGoPath, getToolsGopath, handleDiagnosticErrors, disposeTelemetryReporter, getToolsEnvVars, TempFileProvider } from './util';
import { LanguageClient, RevealOutputChannelOn, FormattingOptions, ProvideDocumentFormattingEditsSignature, ProvideCompletionItemsSignature } from 'vscode-languageclient';
import { clearCacheForTools, fixDriveCasingInWindows } from './goPath';
import { addTags, removeTags } from './goModifytags';
Expand Down Expand Up @@ -83,6 +83,7 @@ export function activate(ctx: vscode.ExtensionContext): void {
});
}
ctx.globalState.update('goroot', currentGoroot);
TempFileProvider.instance.registerStore(ctx.globalState);

offerToInstallTools();
if (checkLanguageServer()) {
Expand Down
4 changes: 2 additions & 2 deletions src/goTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import path = require('path');
import vscode = require('vscode');
import os = require('os');
import { getTempFile } from './util';
import { TempFileProvider } from './util';
import { goTest, TestConfig, getTestFlags, getTestFunctions, getBenchmarkFunctions, extractInstanceTestName, findAllTestSuiteRuns } from './testUtils';
import { getCoverage } from './goCover';

Expand Down Expand Up @@ -213,7 +213,7 @@ function makeCoverData(goConfig: vscode.WorkspaceConfiguration, confFlag: string
let tmpCoverPath = '';
let testFlags = getTestFlags(goConfig, args) || [];
if (goConfig[confFlag] === true) {
tmpCoverPath = getTempFile('go-code-cover');
tmpCoverPath = TempFileProvider.instance.getFilePath('go-code-cover');
testFlags.push('-coverprofile=' + tmpCoverPath);
}

Expand Down
52 changes: 39 additions & 13 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -844,19 +844,45 @@ export function makeMemoizedByteOffsetConverter(buffer: Buffer): (byteOffset: nu
};
}

export const getTempDir = (() => {
let dir: string | undefined;
return (): string => {
if (!dir) {
dir = fs.mkdtempSync(os.tmpdir() + '/vscode-go');
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
export class TempFileProvider {
private constructor() {}
private static _instance: TempFileProvider;

static get instance() {
if (!TempFileProvider._instance) {
TempFileProvider._instance = new TempFileProvider();
}
return dir;
};
})();
return TempFileProvider._instance;
}

private store: vscode.Memento;

/**
* register store to provider for persistance
*/
registerStore(store: vscode.Memento) {
this.store = store;
}

get dir(): string {
let tempDir = this.store.get<string>('tempDir');

export function getTempFile(name: string): string {
return path.normalize(path.join(getTempDir(), name));
if (!tempDir) {
tempDir = fs.mkdtempSync(os.tmpdir() + path.sep + 'vscode-go');
this.store.update('tempDir', tempDir);
}

if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir);
}

return tempDir;
}

/**
* returns path to temp file with name
*/
getFilePath(name: string): string {
return path.normalize(path.join(this.dir, name));
}
}

0 comments on commit 1664a15

Please sign in to comment.