├── .gitignore ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | .advplc* 3 | *.prw 4 | .vscode/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danielflira/advplc/8390b041e1a9ccc3a13bc1149b1fb4998052d6dd/README.md -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var fs = require('fs'); 4 | var path = require('path'); 5 | var program = require('commander') 6 | var child_process = require('child_process'); 7 | var prompt_wizard = require('prompt-wizard'); 8 | 9 | var advplc = { 10 | 11 | 'configure': null, 12 | 13 | 'getBridgePath': function() { 14 | let modules = require.main.paths; 15 | let binary_path = path.join('advpl-vscode', 'bin'); 16 | let module_path = ""; 17 | 18 | for ( let i = 0; i < modules.length; i++ ) { 19 | module_path = path.join(modules[i], binary_path); 20 | try { 21 | fs.accessSync(module_path); 22 | break; 23 | } catch(err) { 24 | module_path = null; 25 | }; 26 | } 27 | 28 | return module_path; 29 | }, 30 | 31 | 'getBridgeBin': function() { 32 | if ( process.platform == 'darwin' ) { 33 | return 'AdvplDebugBridgeMac'; 34 | } else if ( process.platform == 'win32' ) { 35 | return 'AdvplDebugBridge.exe'; 36 | } else { 37 | return ''; 38 | } 39 | }, 40 | 41 | 'getBridge': function() { 42 | return path.join(this.getBridgePath(), this.getBridgeBin()); 43 | }, 44 | 45 | 'cipherPassword': function(password, callback) { 46 | let args = []; 47 | let child = null; 48 | let last_data = ''; 49 | 50 | args.push('--CipherPassword='.concat(password)); 51 | child = child_process.spawn(this.getBridge(), args); 52 | 53 | child.stdout.on('data', function(data) { 54 | last_data += data; 55 | }); 56 | 57 | child.stdout.on('end', function() { 58 | callback(last_data); 59 | }); 60 | }, 61 | 62 | 'compileFile': function(filename, callback) { 63 | let args = []; 64 | let child = null; 65 | let bridge = this.getBridge(); 66 | var last_data = ''; 67 | 68 | if ( fs.statSync(filename).isDirectory() ) { 69 | filename += String.fromCharCode(170) + 70 | configure.compileFolderRegex; 71 | } 72 | 73 | args.push('--compileInfo='.concat(this.getCompileInfo())); 74 | args.push('--source='.concat(filename)); 75 | child = child_process.spawn(bridge, args); 76 | 77 | child.stdout.on('data', function(data) { 78 | last_data += data; 79 | }); 80 | 81 | child.on('exit', function() { 82 | if ( last_data ) { 83 | callback(JSON.parse(last_data)); 84 | } else { 85 | console.log('ended with no response from bridge ' 86 | .concat(child.exitCode)); 87 | } 88 | }); 89 | }, 90 | 91 | 'loadConfigure': function (next) { 92 | fs.readFile('.advplc', function(err, data) { 93 | if ( data ) { 94 | configure = JSON.parse(data); 95 | } 96 | 97 | if ( err ) { 98 | console.log(err); 99 | return; 100 | } 101 | 102 | if ( next ) { 103 | next(configure); 104 | } 105 | }); 106 | }, 107 | 108 | 'getCompileInfo': function() { 109 | return JSON.stringify(configure); 110 | } 111 | }; 112 | 113 | 114 | function compileResource(parameters, configure, resource) { 115 | if ( ! parameters.env ) { 116 | console.log('you must inform environment to compile, see --env') 117 | } 118 | 119 | configure.selectedEnvironment = parameters.env; 120 | 121 | advplc.compileFile(path.resolve(resource), function(result) { 122 | for ( let i = 0; i < result.msgs.length; i++ ) { 123 | let msg = result.msgs[i]; 124 | console.log(msg); 125 | } 126 | }); 127 | } 128 | 129 | 130 | function wizardCfg(parameters, configure) { 131 | if ( ! parameters.cfg ) { 132 | return wizardAdd1(parameters, configure); 133 | } 134 | 135 | var wizard = prompt_wizard.create([ 136 | { 137 | 'prompt': 'authorization generation date', 138 | 'key': 'authorization_generation', 139 | 'default': configure.authorization_generation 140 | }, 141 | { 142 | 'prompt': 'authorization validation date', 143 | 'key': 'authorization_validation', 144 | 'default': configure.authorization_validation 145 | }, 146 | { 147 | 'prompt': 'authorization permission', 148 | 'key': 'authorization_permission', 149 | 'default': configure.authorization_permission 150 | }, 151 | { 152 | 'prompt': 'authorization code', 153 | 'key': 'authorization_code', 154 | 'default': configure.authorization_code 155 | }, 156 | { 157 | 'prompt': 'start program from smartclient', 158 | 'key': 'startProgram', 159 | 'default': configure.startProgram ? 160 | configure.startProgram : 161 | "sigaadv" 162 | }, 163 | { 164 | 'prompt': 'compile files regex', 165 | 'key': 'compileFolderRegex', 166 | 'default': configure.compileFolderRegex ? 167 | configure.compileFolderRegex : 168 | '.*\\.(prw|prx|apw|aph|tres|png|bmp|tres)' 169 | } 170 | ]); 171 | 172 | wizard.execute().then(function wizardCfgQuiz(quiz) { 173 | Object.assign(configure, quiz); 174 | return wizardAdd1(parameters, configure); 175 | }); 176 | } 177 | 178 | 179 | function wizardAdd1(parameters, configure) { 180 | if ( ! parameters.add ) { 181 | return saveConfigure(configure); 182 | } 183 | 184 | var wizard = prompt_wizard.create([ 185 | { 186 | 'prompt': 'environment name', 187 | 'key': 'environment', 188 | 'default': 'environment' 189 | } 190 | ]); 191 | 192 | wizard.execute().then(function wizardAdd1Quiz(quiz) { 193 | let environment = {}; 194 | let add = true; 195 | 196 | if ( ! configure.environments ) { 197 | configure.environments = []; 198 | } 199 | 200 | for ( let i = 0; i < configure.environments.length; i++ ) { 201 | let cfgenv = configure.environments[i]; 202 | if ( cfgenv.environment == quiz.environment ) { 203 | environment = cfgenv; 204 | add = false; 205 | break; 206 | } 207 | } 208 | 209 | if ( add ) { 210 | environment = quiz; 211 | configure.environments.push(environment); 212 | } 213 | 214 | return wizardAdd2(parameters, configure, environment); 215 | }); 216 | } 217 | 218 | 219 | function wizardAdd2(parameters, configure, environment) { 220 | var actualPassword = environment.passwordCipher; 221 | 222 | var wizard = prompt_wizard.create([ 223 | { 224 | 'prompt': 'server binary version', 225 | 'key': 'serverVersion', 226 | 'default': environment.serverVersion ? 227 | environment.serverVersion : 228 | '131227A' 229 | }, 230 | { 231 | 'prompt': 'server address', 232 | 'key': 'server', 233 | 'default': environment.server ? 234 | environment.server : 235 | 'localhost' 236 | }, 237 | { 238 | 'prompt': 'server port', 239 | 'key': 'port', 240 | 'default': environment.port ? 241 | environment.port : 242 | '5555' 243 | }, 244 | { 245 | 'prompt': 'environment language', 246 | 'key': 'language', 247 | 'default': environment.language ? 248 | environment.language : 249 | 'PORTUGUESE' 250 | }, 251 | { 252 | 'prompt': 'object repository type', 253 | 'key': 'rpoType', 254 | 'default': environment.rpoType ? 255 | environment.rpoType : 256 | 'TOP' 257 | }, 258 | { 259 | 'prompt': 'environment user', 260 | 'key': 'user', 261 | 'default': environment.user ? 262 | environment.user : 263 | 'admin' 264 | }, 265 | { 266 | 'prompt': 'environment password', 267 | 'key': 'passwordCipher', 268 | 'default': environment.passwordCipher ? 269 | environment.passwordCipher : 270 | '' 271 | }, 272 | { 273 | 'prompt': 'environment default start program', 274 | 'key': 'startProgram', 275 | 'default': environment.startProgram ? 276 | environment.startProgram : 277 | 'sigaadv' 278 | }, 279 | { 280 | 'prompt': 'smartclient path', 281 | 'key': 'smartClientPath', 282 | 'default': environment.smartClientPath ? 283 | environment.smartClientPath : 284 | 'C:\\TOTVS\\Protheus\\bin\\smartclient\\' 285 | }, 286 | { 287 | 'prompt': 'include list', 288 | 'key': 'includeList', 289 | 'default': environment.includeList ? 290 | environment.includeList : 291 | 'C:\\caminho1\\include\\;C:\\caminho2\\include\\' 292 | }, 293 | ]); 294 | 295 | wizard.execute().then(function wizardAdd2Quiz(quiz) { 296 | Object.assign(environment, quiz); 297 | 298 | // password has changed 299 | if ( quiz.passwordCipher !== actualPassword ) { 300 | advplc.cipherPassword(quiz.passwordCipher, function(password) { 301 | quiz.passwordCipher = password; 302 | saveConfigure(configure); 303 | }); 304 | } 305 | 306 | // just save file 307 | else { 308 | saveConfigure(configure); 309 | } 310 | }); 311 | } 312 | 313 | 314 | function saveConfigure(configure) { 315 | fs.writeFile('.advplc', JSON.stringify(configure, null, '\t'), 316 | function(err) { 317 | if ( err ) { 318 | console.log(err); 319 | } else { 320 | console.log('configure changed successfully'); 321 | } 322 | }); 323 | } 324 | 325 | 326 | program 327 | .option('-e, --env ', 'environment from .advplc to compile') 328 | .option('--cfg', 'configure .advplc generic options') 329 | .option('--add', 'add .advplc environment options') 330 | .arguments('[resource]') 331 | .action(function(resource) { 332 | advplc.loadConfigure(function(configure) { 333 | if ( program.add || program.cfg ) { 334 | wizardCfg(program, configure); 335 | } else { 336 | compileResource(program, configure, resource); 337 | } 338 | }); 339 | }) 340 | .parse(process.argv) -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "advplc", 3 | "version": "1.0.0", 4 | "description": "advpl compiler wrap", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "install": "napa https://github.com/killerall/advpl-vscode" 9 | }, 10 | "author": "Daniel Lira", 11 | "license": "ISC", 12 | "dependencies": { 13 | "commander": "^2.10.0", 14 | "napa": "^3.0.0", 15 | "prompt-wizard": "^1.2.1" 16 | }, 17 | "bin": { 18 | "advplc": "./index.js" 19 | } 20 | } 21 | --------------------------------------------------------------------------------