Skip to content

Commit f3c8657

Browse files
committed
export after compile
1 parent 7b0fdc5 commit f3c8657

12 files changed

+155
-3574
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Change Log
22

3+
## [3.1.0]
4+
### Added
5+
- Export after compile
6+
7+
38
## [0.2.3]
49
### Fixed
510
- Remove unused command

commands/compile/index.js

Lines changed: 0 additions & 70 deletions
This file was deleted.

commands/currentdoc/index.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* Import and compile current file.
3+
*/
4+
const clsRegexName = require( './cls-regex-name' )
5+
const clsCheckNames = require( './cls-check-names' )
6+
const rtnRegexName = require( './rtn-regex-name' )
7+
const rtnCheckNames = require( './rtn-check-names' )
8+
9+
// extract and verify name of active document
10+
// env - environment { window, languages, log }
11+
// return { name, content, error }
12+
const CurrentDoc = env => () => {
13+
14+
/*
15+
env ={
16+
window = {},
17+
languages = [ 'cacheobjectscript', 'cacheobjectscriptinclude' ],
18+
log = data => console.log( 'cos.compile:', JSON.stringify( data ) )
19+
}
20+
*/
21+
const { window, languages, log } = env
22+
const editor = window.activeTextEditor
23+
if ( !editor ) return {
24+
error: 'No active editor, open one at first'
25+
}
26+
27+
const doc = editor.document
28+
if ( !doc ) return {
29+
error: 'Open ObjectScript file first.'
30+
}
31+
32+
const fullname = doc.fileName
33+
if ( !fullname ) return {
34+
error: 'You must save the document first'
35+
}
36+
37+
if ( !~languages.indexOf( doc.languageId ) ) return {
38+
error: `${ fullname } has unsupported type ${ language }`
39+
}
40+
41+
let file = ( fullname.match( /[^\\\/]+$/ ) || [] )[ 0 ] || '' //only filename without folders
42+
let code = doc.getText().replace( /\/\/[^\r\n]*\r?\n/g, '' ) // normalize EOL?
43+
let name, ext, codename //server side name
44+
45+
const cdnm = ({name,ext})=>[name, ext].join('.')
46+
47+
if ( /\.cls$/i.test( fullname ) ) { // is class?
48+
49+
( { name, ext } = clsRegexName( code ) )
50+
codename = cdnm({ name, ext })
51+
if ( !clsCheckNames( { code: codename, file, log } ) ) return {
52+
error: 'check names'
53+
}
54+
55+
} else { // routines
56+
57+
( { name, ext } = rtnRegexName( code ) )
58+
codename = cdnm({ name, ext })
59+
if ( !rtnCheckNames( { code: codename, file, log } ) ) return {
60+
error: 'check names'
61+
}
62+
}
63+
64+
return {
65+
name: codename,
66+
content: code.split( /\r?\n/g ), // get code lines array
67+
error: ''
68+
}
69+
70+
}
71+
72+
module.exports = { CurrentDoc }

commands/export/index.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ let doc2file = docname => docname
2222
const ExportDoc = ( doc, next ) => ({ error, data }) => {
2323

2424
if ( error ){
25-
log( `GETDOC ${ doc.name }: ${ JSON.stringify( error ) }\n` )
25+
log( `${ JSON.stringify( error ) }\n` )
2626
return
2727
}
2828
const { content, status } = data.result
@@ -35,10 +35,11 @@ const ExportDoc = ( doc, next ) => ({ error, data }) => {
3535
if ( !fs.existsSync( folders ) ) mkdir( folders )
3636
fs.writeFileSync( fullname, ( content || [] ).join( '\n' ) )
3737
log( `${ doc.name } -> ${ fullname }. ${ status } ` )
38-
next()
38+
if ( next && ( typeof next === 'function')) next()
3939

4040
}
4141

42+
4243
const doclist = ( { error, data } ) => {
4344

4445
if ( error ) return log( `DOCLIST: ${ JSON.stringify( error ) }` )
@@ -69,29 +70,29 @@ const doclist = ( { error, data } ) => {
6970
/**
7071
* Export all classes/routines in a namespace to working directory.
7172
*/
72-
module.exports = environment => {
73+
module.exports = env => {
7374

7475
//reassign module variables
75-
( { api, log, options } = environment );
76+
( { api, log, options } = env ); //env - environment
7677
( { root, folder, atelier } = options );
7778
if ( atelier ) doc2file = doc => asAtelier( doc )
7879

7980
// doclist options
8081
const { category, generated, filter } = options;
81-
82-
return () => {
82+
const exportAll = () => {
8383

8484
if ( !root ){
8585
log( `COS.EXPORT: Open folder before export - Ctrl+K, Ctrl+O` )
8686
return
8787
}
88-
88+
8989
log( '\nLoad documents list ...' )
9090
api.getDocNames(
9191
{ category, generated, filter }, //doclist options
9292
( error, data ) => doclist( { error, data } ) //callback wrapper
9393
)
94-
9594
}
9695

96+
return { exportAll, ExportDoc }
97+
9798
}

extension.js

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
const vscode = require('vscode')
22
const { workspace, window } = vscode
3+
const http = require('http')
4+
35
const API = require('cos-api4node')
46
const LOG = require( './log' )
57
const languages = require('./package.json')[ 'contributes' ][ 'languages' ].map( lang => lang.id )
68
const panel = require( './status-bar-panel' )
79
const CmdExport = require( './commands/export' )
8-
const CmdCompile = require( './commands/compile' )
10+
const { CurrentDoc }= require( './commands/currentdoc' )
11+
const IsApiError = require( './is-api-error' )
12+
13+
914

1015
const activate = context => {
1116

@@ -26,7 +31,7 @@ const activate = context => {
2631

2732
api.headServer( err => {
2833

29-
if ( !!err ) return log( 'Connection FAILED: ' + conn )
34+
if ( err ) return log( 'Connection FAILED: ' + conn )
3035
log( 'Connected ' + conn )
3136
panel.set( conn )
3237

@@ -35,12 +40,67 @@ const activate = context => {
3540
const exportTo = options.get( 'export' )
3641
exportTo.root = workspace.rootPath
3742

38-
const cosExport = CmdExport({ api, log, options: exportTo })
39-
const cosCompile = CmdCompile({ window, api, log, languages })
43+
const { exportAll, ExportDoc } = CmdExport({ api, log, options: exportTo })
44+
const currentDoc = CurrentDoc({ window, languages, log })
45+
46+
const Save = ({ name, log }) => ( err, data ) => {
47+
48+
// IsApiError, ExportDoc - global
49+
const isGetDocError = IsApiError( name, 'getDoc', log )
50+
if ( isGetDocError({ err, data }) ) return
51+
52+
const completed = () => log( 'Completed.' )
53+
const exportDoc = ExportDoc( { name, cat: data.result.cat }, completed )
54+
55+
exportDoc( { err, data } )
56+
}
57+
58+
const Export = ( { api, name, log } ) => ( err, data ) => {
59+
// IsApiError, Save - from upper scope
60+
const isCompileError = IsApiError( name, 'compile', log )
61+
if ( isCompileError({ err, data }) ) return;
62+
// after compilation API returns updated storage definition
63+
// but, currently, we don`t have any AST implementation
64+
// so, just export again
65+
data.console.forEach( ci => log( ci ) ) //output compilation log
66+
//log( ` Export ${ name }` )
67+
const save = Save( { name, log } )
68+
api.getDoc( name, save )
69+
}
70+
71+
const Compile = ( { api, name, log } ) => ( err, data ) => {
72+
73+
// IsApiError, Export
74+
const isImportError = IsApiError( name, 'import', log )
75+
if ( isImportError({ err, data }) ) return;
76+
77+
const exportCurrent = Export( { api, name, log } )
78+
//log( `Compile ${ name }` )
79+
api.compile( name, exportCurrent )
80+
81+
}
82+
83+
// import -> compile -> export
84+
// save to server, compile, export to disk
85+
const importCompileExport = () => {
86+
87+
// api, Compile, log
88+
const { name, content, error } = currentDoc()
89+
if ( error ) return log( error )
90+
91+
const compile = Compile({ api, name, log } )
92+
//log( ` Import ${ name }` )
93+
api.putDoc( name,
94+
{ enc: false, content },
95+
{ ignoreConflict: true },
96+
compile
97+
)
98+
99+
}
40100

41101
context.subscriptions.push(
42-
vscode.commands.registerCommand( 'cos.export', cosExport )
43-
, vscode.commands.registerCommand( 'cos.compile', cosCompile )
102+
vscode.commands.registerCommand( 'cos.compile', importCompileExport ),
103+
vscode.commands.registerCommand( 'cos.export', exportAll )
44104
)
45105

46106
}

commands/compile/is-error.js renamed to is-api-error.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = ({ name, action, log }) => ( err, data ) => {
1+
module.exports = ({ name, action, log }) => ({ err, data }) => {
22

33
const { result, status, console } = data
44

0 commit comments

Comments
 (0)