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