|
1 | | -import * as fs from 'fs'; |
2 | | -import * as path from 'path'; |
3 | | -import * as child_process from 'child_process'; |
4 | | - |
5 | | -import { scip } from './scip'; |
6 | | -import { diffSnapshot, formatSnapshot, writeSnapshot } from './lib'; |
7 | | -import { Input } from './lsif-typescript/Input'; |
8 | | -import { join } from 'path'; |
9 | | -import { IndexOptions, SnapshotOptions, mainCommand } from './MainCommand'; |
10 | | -import { sendStatus, setQuiet, setShowProgressRateLimit } from './status'; |
11 | | -import { Indexer } from './indexer'; |
12 | | -import { exit } from 'process'; |
13 | | - |
14 | | -function indexAction(options: IndexOptions): void { |
15 | | - setQuiet(options.quiet); |
16 | | - if (options.showProgressRateLimit !== undefined) { |
17 | | - setShowProgressRateLimit(options.showProgressRateLimit); |
18 | | - } |
19 | | - |
20 | | - const workspaceRoot = options.cwd; |
21 | | - const snapshotDir = options.snapshotDir; |
22 | | - const environment = options.environment; |
23 | | - |
24 | | - const projectRoot = workspaceRoot; |
25 | | - process.chdir(workspaceRoot); |
26 | | - |
27 | | - // TODO: use setup.py / poetry to determine better projectName |
28 | | - const projectName = options.projectName; |
29 | | - if (!projectName || projectName == '') { |
30 | | - console.warn('Must pass `--project-name`'); |
31 | | - return; |
32 | | - } |
33 | | - |
34 | | - // TODO: Use setup.py / poetry to determine better projectVersion |
35 | | - // for now, the current hash works OK |
36 | | - let projectVersion = options.projectVersion; |
37 | | - if (!projectVersion || projectVersion === '') { |
38 | | - // Default to current git hash |
39 | | - try { |
40 | | - projectVersion = child_process.execSync('git rev-parse HEAD').toString().trim(); |
41 | | - } catch (e) { |
42 | | - console.warn('Must either pass `--project-version` or run from within a git repository'); |
43 | | - return; |
44 | | - } |
45 | | - } |
46 | | - |
47 | | - const outputFile = path.join(projectRoot, options.output); |
48 | | - const output = fs.openSync(outputFile, 'w'); |
49 | | - |
50 | | - sendStatus(`Indexing ${projectRoot} with version ${projectVersion}`); |
51 | | - |
52 | | - try { |
53 | | - let indexer = new Indexer({ |
54 | | - ...options, |
55 | | - workspaceRoot, |
56 | | - projectRoot, |
57 | | - projectName, |
58 | | - projectVersion, |
59 | | - environment, |
60 | | - writeIndex: (partialIndex: scip.Index): void => { |
61 | | - fs.writeSync(output, partialIndex.serializeBinary()); |
62 | | - }, |
63 | | - }); |
64 | | - |
65 | | - indexer.index(); |
66 | | - } catch (e) { |
67 | | - console.warn( |
68 | | - '\n\nExperienced Fatal Error While Indexing:\nPlease create an issue at github.com/sourcegraph/scip-python:', |
69 | | - e |
70 | | - ); |
71 | | - exit(1); |
72 | | - } |
73 | | - |
74 | | - fs.close(output); |
75 | | - |
76 | | - if (snapshotDir) { |
77 | | - sendStatus(`Writing snapshot from index: ${outputFile}`); |
78 | | - |
79 | | - const scipIndex = scip.Index.deserializeBinary(fs.readFileSync(outputFile)); |
80 | | - for (const doc of scipIndex.documents) { |
81 | | - if (doc.relative_path.startsWith('..')) { |
82 | | - console.log('Skipping Doc:', doc.relative_path); |
83 | | - continue; |
84 | | - } |
85 | | - |
86 | | - const inputPath = path.join(projectRoot, doc.relative_path); |
87 | | - const input = Input.fromFile(inputPath); |
88 | | - const obtained = formatSnapshot(input, doc, scipIndex.external_symbols); |
89 | | - const relativeToInputDirectory = path.relative(projectRoot, inputPath); |
90 | | - const outputPath = path.resolve(snapshotDir, relativeToInputDirectory); |
91 | | - writeSnapshot(outputPath, obtained); |
92 | | - } |
93 | | - } |
94 | | -} |
95 | | - |
96 | | -function snapshotAction(snapshotRoot: string, options: SnapshotOptions): void { |
97 | | - setQuiet(options.quiet); |
98 | | - if (options.showProgressRateLimit !== undefined) { |
99 | | - setShowProgressRateLimit(options.showProgressRateLimit); |
100 | | - } |
101 | | - |
102 | | - console.log('... Snapshotting ... '); |
103 | | - const projectName = options.projectName; |
104 | | - const projectVersion = options.projectVersion; |
105 | | - const environment = options.environment ? path.resolve(options.environment) : undefined; |
106 | | - |
107 | | - const snapshotOnly = options.only; |
108 | | - |
109 | | - const inputDirectory = path.resolve(join(snapshotRoot, 'input')); |
110 | | - const outputDirectory = path.resolve(join(snapshotRoot, 'output')); |
111 | | - |
112 | | - // Either read all the directories or just the one passed in by name |
113 | | - let snapshotDirectories = fs.readdirSync(inputDirectory); |
114 | | - if (snapshotOnly) { |
115 | | - snapshotDirectories = [snapshotOnly]; |
116 | | - } |
117 | | - |
118 | | - for (const snapshotDir of snapshotDirectories) { |
119 | | - let projectRoot = join(inputDirectory, snapshotDir); |
120 | | - if (!fs.lstatSync(projectRoot).isDirectory()) { |
121 | | - continue; |
122 | | - } |
123 | | - |
124 | | - projectRoot = path.resolve(projectRoot); |
125 | | - process.chdir(projectRoot); |
126 | | - |
127 | | - const scipBinaryFile = path.join(projectRoot, options.output); |
128 | | - const output = fs.openSync(scipBinaryFile, 'w'); |
129 | | - |
130 | | - if (options.index) { |
131 | | - let indexer = new Indexer({ |
132 | | - ...options, |
133 | | - workspaceRoot: projectRoot, |
134 | | - projectRoot, |
135 | | - projectName, |
136 | | - projectVersion, |
137 | | - environment, |
138 | | - writeIndex: (partialIndex: any): void => { |
139 | | - fs.writeSync(output, partialIndex.serializeBinary()); |
140 | | - }, |
141 | | - }); |
142 | | - indexer.index(); |
143 | | - fs.close(output); |
144 | | - } |
145 | | - |
146 | | - const contents = fs.readFileSync(scipBinaryFile); |
147 | | - const scipIndex = scip.Index.deserializeBinary(contents); |
148 | | - |
149 | | - for (const doc of scipIndex.documents) { |
150 | | - if (doc.relative_path.startsWith('..')) { |
151 | | - continue; |
152 | | - } |
153 | | - |
154 | | - const inputPath = path.join(projectRoot, doc.relative_path); |
155 | | - const input = Input.fromFile(inputPath); |
156 | | - const obtained = formatSnapshot(input, doc, scipIndex.external_symbols); |
157 | | - const relativeToInputDirectory = path.relative(projectRoot, inputPath); |
158 | | - const outputPath = path.resolve(outputDirectory, snapshotDir, relativeToInputDirectory); |
159 | | - |
160 | | - if (options.check) { |
161 | | - diffSnapshot(outputPath, obtained); |
162 | | - } else { |
163 | | - writeSnapshot(outputPath, obtained); |
164 | | - } |
165 | | - } |
166 | | - } |
167 | | -} |
168 | | - |
169 | | -export function main(argv: string[]): void { |
170 | | - const command = mainCommand(indexAction, snapshotAction, (_) => { |
171 | | - throw 'not yet implemented'; |
172 | | - // console.log('ENVIRONMENT OPTIONS', options); |
173 | | - // console.log(getEnvironment(new Set(), '', undefined)); |
174 | | - }); |
175 | | - command.parse(argv); |
176 | | -} |
| 1 | +import { main } from './main-impl'; |
177 | 2 |
|
178 | 3 | main(process.argv); |
0 commit comments