Skip to content

Commit 2c8f24f

Browse files
refactor: Move project name/version logic to Indexer (#112)
1 parent 5c02051 commit 2c8f24f

File tree

3 files changed

+18
-24
lines changed

3 files changed

+18
-24
lines changed

packages/pyright-scip/src/indexer.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as child_process from 'child_process';
12
import * as path from 'path';
23
import { Event } from 'vscode-languageserver/lib/common/api';
34

@@ -40,6 +41,17 @@ export class Indexer {
4041
// private _getConfigOptions(host: Host, commandLineOptions: CommandLineOptions): ConfigOptions {
4142
let fs = new PyrightFileSystem(createFromRealFileSystem());
4243

44+
if (
45+
scipConfig.infer.projectVersionFromCommit &&
46+
(!scipConfig.projectVersion || scipConfig.projectVersion === '')
47+
) {
48+
try {
49+
scipConfig.projectVersion = child_process.execSync('git rev-parse HEAD').toString().trim();
50+
} catch (e) {
51+
scipConfig.projectVersion = '';
52+
}
53+
}
54+
4355
let config = new ScipPyrightConfig(scipConfig, fs);
4456
this.pyrightConfig = config.getConfigOptions();
4557

packages/pyright-scip/src/lib.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export interface ScipConfig extends IndexOptions {
1818

1919
projectRoot: string;
2020

21+
infer: { projectVersionFromCommit: boolean };
22+
2123
writeIndex: (index: scip.Index) => void;
2224
}
2325

packages/pyright-scip/src/main-impl.ts

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
3-
import * as child_process from 'child_process';
43

54
import { scip } from './scip';
65
import { diffSnapshot, formatSnapshot, writeSnapshot } from './lib';
@@ -24,39 +23,23 @@ function indexAction(options: IndexOptions): void {
2423
const projectRoot = workspaceRoot;
2524
process.chdir(workspaceRoot);
2625

27-
// TODO: use setup.py / poetry to determine better projectName
28-
const projectName = options.projectName;
29-
30-
// TODO: Use setup.py / poetry to determine better projectVersion
31-
// for now, the current hash works OK
32-
let projectVersion = options.projectVersion;
33-
if (!projectVersion || projectVersion === '') {
34-
// Default to current git hash
35-
try {
36-
projectVersion = child_process.execSync('git rev-parse HEAD').toString().trim();
37-
} catch (e) {
38-
projectVersion = '';
39-
}
40-
}
41-
4226
const outputFile = path.join(projectRoot, options.output);
4327
const output = fs.openSync(outputFile, 'w');
4428

45-
sendStatus(`Indexing ${projectRoot} with version ${projectVersion}`);
46-
4729
try {
4830
let indexer = new Indexer({
4931
...options,
5032
workspaceRoot,
5133
projectRoot,
52-
projectName,
53-
projectVersion,
5434
environment,
35+
infer: { projectVersionFromCommit: true },
5536
writeIndex: (partialIndex: scip.Index): void => {
5637
fs.writeSync(output, partialIndex.serializeBinary());
5738
},
5839
});
5940

41+
sendStatus(`Indexing ${projectRoot} with version ${indexer.scipConfig.projectVersion}`);
42+
6043
indexer.index();
6144
} catch (e) {
6245
console.warn(
@@ -95,8 +78,6 @@ function snapshotAction(snapshotRoot: string, options: SnapshotOptions): void {
9578
}
9679

9780
console.log('... Snapshotting ... ');
98-
const projectName = options.projectName;
99-
const projectVersion = options.projectVersion;
10081
const environment = options.environment ? path.resolve(options.environment) : undefined;
10182

10283
const snapshotOnly = options.only;
@@ -127,9 +108,8 @@ function snapshotAction(snapshotRoot: string, options: SnapshotOptions): void {
127108
...options,
128109
workspaceRoot: projectRoot,
129110
projectRoot,
130-
projectName,
131-
projectVersion,
132111
environment,
112+
infer: { projectVersionFromCommit: false },
133113
writeIndex: (partialIndex: any): void => {
134114
fs.writeSync(output, partialIndex.serializeBinary());
135115
},

0 commit comments

Comments
 (0)