@@ -10,14 +10,21 @@ module.exports = class Status {
1010 * @param {vscode.ExtensionContext } context
1111 */
1212 constructor ( context ) {
13+ //Used for git status
1314 this . status = undefined ;
1415
16+ //Used for moving members to streamfiles and vice-versa
17+ /** @type {{library: string, ifsPath: string, asp?: string}[]|undefined } */
18+ this . gitLibraries = undefined ;
19+
1520 this . emitter = new vscode . EventEmitter ( ) ;
1621 this . onDidChangeTreeData = this . emitter . event ;
1722
1823 context . subscriptions . push (
1924 vscode . workspace . onDidChangeConfiguration ( async event => {
2025 if ( event . affectsConfiguration ( `code-for-ibmi.connectionSettings` ) ) {
26+ this . gitLibraries = undefined ;
27+ this . fetchGitLibs ( ) ;
2128 this . refresh ( ) ;
2229 }
2330 } ) ,
@@ -54,13 +61,35 @@ module.exports = class Status {
5461
5562 vscode . commands . registerCommand ( `git-client-ibmi.status.restore` , async ( node ) => {
5663 const connection = instance . getConnection ( ) ;
64+ const content = instance . getContent ( ) ;
5765 const repoPath = connection . config . homeDirectory ;
5866 const repo = new Git ( repoPath ) ;
5967
6068 if ( node ) {
6169 if ( repo . canUseGit ( ) && await repo . isGitRepo ( ) ) {
6270 await repo . restore ( node . path ) ;
6371 this . refresh ( ) ;
72+
73+ await this . fetchGitLibs ( ) ;
74+ if ( this . gitLibraries ) {
75+ const libConfig = this . gitLibraries . find ( setting => setting . ifsPath . toUpperCase ( ) . startsWith ( repoPath . toUpperCase ( ) ) ) ;
76+
77+ if ( libConfig ) {
78+ const pathParts = node . path . split ( `/` ) ;
79+
80+ const library = libConfig . library . toUpperCase ( ) ;
81+ const sourceFile = pathParts [ pathParts . length - 2 ] . toUpperCase ( ) ;
82+ let memberName = pathParts [ pathParts . length - 1 ] . toUpperCase ( ) ;
83+ memberName = memberName . substring ( 0 , memberName . lastIndexOf ( `.` ) ) ;
84+
85+ try {
86+ const fileContent = await content . downloadStreamfile ( path . posix . join ( repoPath , node . path ) ) ;
87+ await content . uploadMemberContent ( libConfig . asp , library , sourceFile , memberName , fileContent ) ;
88+ } catch ( e ) {
89+ vscode . window . showErrorMessage ( `Error copying back to source member ${ library } /${ sourceFile } /${ memberName } . ${ e } ` )
90+ }
91+ }
92+ }
6493 }
6594 }
6695 } ) ,
@@ -130,15 +159,42 @@ module.exports = class Status {
130159 }
131160 } ) ,
132161
133- vscode . workspace . onDidSaveTextDocument ( ( document ) => {
134- if ( document . uri . scheme === `streamfile` ) {
135- const connection = instance . getConnection ( ) ;
162+ vscode . workspace . onDidSaveTextDocument ( async ( document ) => {
163+ const connection = instance . getConnection ( ) ;
164+
165+ if ( connection ) {
166+ const repoPath = connection . config . homeDirectory ;
167+ const content = instance . getContent ( ) ;
168+
169+ switch ( document . uri . scheme ) {
170+ case `member` :
171+ await this . fetchGitLibs ( ) ;
172+
173+ if ( this . gitLibraries ) {
174+ let memberParts = document . uri . path . split ( `/` ) ;
175+ const library = memberParts [ memberParts . length - 3 ] . toUpperCase ( ) ;
176+ const sourceFile = memberParts [ memberParts . length - 2 ] . toLowerCase ( ) ;
177+ const baseName = memberParts [ memberParts . length - 1 ] . toLowerCase ( ) ;
178+
179+ const libConfig = this . gitLibraries . find ( setting => setting . library . toUpperCase ( ) === library ) ;
180+
181+ if ( libConfig ) {
182+ await connection . paseCommand ( `mkdir ${ path . posix . join ( libConfig . ifsPath , sourceFile ) } ` , `.` , 1 ) ;
183+ await content . writeStreamfile ( path . posix . join ( libConfig . ifsPath , sourceFile , baseName ) , document . getText ( ) ) ;
184+
185+ if ( libConfig . ifsPath . toUpperCase ( ) === repoPath . toUpperCase ( ) ) {
186+ this . refresh ( ) ;
187+ }
188+ }
189+ }
190+ break ;
136191
137- if ( connection ) {
138- const repoPath = connection . config . homeDirectory ;
192+ case `streamfile` :
139193 if ( document . uri . path . startsWith ( repoPath ) ) {
140194 this . refresh ( ) ;
141195 }
196+ break ;
197+
142198 }
143199 }
144200 } )
@@ -149,6 +205,50 @@ module.exports = class Status {
149205 this . emitter . fire ( ) ;
150206 }
151207
208+ async fetchGitLibs ( ) {
209+ const connection = instance . getConnection ( ) ;
210+ if ( connection ) {
211+ const content = instance . getContent ( ) ;
212+
213+ if ( this . gitLibraries === undefined ) {
214+ /** @type {string } */
215+ let jsonContent ;
216+ let gitlibs ;
217+
218+ try {
219+ jsonContent = await content . downloadStreamfile ( `/.gitlibs.json` ) ;
220+ } catch ( e ) {
221+ this . gitLibraries = false ;
222+ //Okay.. file doesn't exist probs
223+ }
224+
225+ if ( jsonContent ) {
226+ try {
227+ gitlibs = JSON . parse ( jsonContent ) ;
228+ } catch ( e ) {
229+ vscode . window . showErrorMessage ( `Unable to read .gitlibs.json. Invalid JSON.` ) ;
230+ }
231+ }
232+
233+ if ( gitlibs ) {
234+ let isValid = true ;
235+
236+ for ( const configItem of gitlibs ) {
237+ if ( typeof configItem . library !== `string` || typeof configItem . ifsPath !== `string` ) {
238+ isValid = false ;
239+ }
240+ }
241+
242+ if ( isValid ) {
243+ this . gitLibraries = gitlibs ;
244+ } else {
245+ vscode . window . showErrorMessage ( `.gitlibs.json in incorrect format.` ) ;
246+ }
247+ }
248+ }
249+ }
250+ }
251+
152252 /**
153253 *
154254 * @param {vscode.TreeItem } element
0 commit comments