Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RPG parser CLI #360

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions cli/rpgparse/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import glob from "glob";
import Parser from "../../language/parser";
import { readFileSync, writeFileSync, existsSync, mkdirSync } from "fs";
import path from "path";

main();

function printHelp() {
console.log(`
Usage: "" [options]

Options:
-f, --files <glob> Specify the files to scan (required)
-c, --cwd <directory> Specify the current working directory
-d, --outdir <directory> Specify the output directory
-o, --output <file> Specify the output file
-h, --help Display this help message
`);
}

async function main() {
const parms = process.argv.slice(2);

let cwd = process.cwd();
let scanGlob = `**/*.{SQLRPGLE,sqlrpgle,RPGLE,rpgle}`;
let filesProvided: boolean = false;
let output: string = undefined;
let outDir: string = undefined;

for (let i = 0; i < parms.length; i++) {
switch (parms[i]) {
case `-f`:
case `--files`:
scanGlob = parms[i + 1];
filesProvided = true;
i++;
break;
case `-c`:
case `--cwd`:
cwd = parms[i + 1];
i++;
break;

case `-d`:
case `--outdir`:
outDir = parms[i + 1];
i++;
break;

case `-o`:
case `--output`:
output = parms[i + 1];
i++;
break;

case `-h`:
case `--help`:
printHelp();
process.exit(0);
}
}

if (!filesProvided) {
console.log(`files not provided`);
process.exit(1);
}

let parser: Parser = new Parser();
let files: string[];

try {
files = getFiles(cwd, scanGlob);
} catch (e) {
error(e.message || e);
process.exit(1);
}

for (const file of files) {
try {
const content = readFileSync(file, { encoding: `utf-8` });

if (
content.length > 6 &&
content.substring(0, 6).toLowerCase() === `**free`
) {
const docs = await parser.getDocs(file, content, {
withIncludes: true,
collectReferences: true,
});

const filterdDocs = JSON.stringify(docs.filterCache(), null, 2);

if (output) {
const outputPath = path.join(outDir ?? cwd, output);
try {
if (outDir) {
if (!existsSync(outDir)) {
mkdirSync(outDir, { recursive: true });
}
}
writeFileSync(outputPath, filterdDocs, {
encoding: `utf-8`,
flag: `w`,
});
} catch (err) {
console.log(err);
}
} else {
console.log(filterdDocs);
}
}
} catch (e) {
error(`failed to parse ${file}: ${e.message || e}`);
process.exit(1);
}
}
} // end of main

function getFiles(cwd: string, globPath: string): string[] {
return glob.sync(globPath, {
cwd,
absolute: true,
nocase: true,
});
}

function error(line: string) {
process.stdout.write(line + `\n`);
}
22 changes: 22 additions & 0 deletions cli/rpgparse/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "@code4i/rpgparse-cli",
"version": "1.0.0",
"description": "A CLI tool for parsing RPG files",
"bin": {
"rpgparse": "./dist/index.js"
},
"scripts": {
"webpack:dev": "webpack --mode none --config ./webpack.config.js",
"webpack": "webpack --mode production --config ./webpack.config.js",
"test": "webpack --mode none --config ./webpack.config.js && node ./dist/index.js",
"build": "tsc",
"start": "node dist/index.js"
},
"dependencies": {
},
"devDependencies": {
"typescript": "^4.8.4"
},
"author": "Code4i",
"license": "MIT"
}
5 changes: 5 additions & 0 deletions cli/rpgparse/test/copy1.rpgle
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
**FREE

Dcl-Pr theExtProcedure ExtProc;
theNewValue Char(20);
End-Pr;
28 changes: 28 additions & 0 deletions cli/rpgparse/test/test.rpgle
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
**FREE
Ctl-Opt Main(MainLine);
/// -------------------------------------
// Main
/// -------------------------------------
Dcl-Proc MainLine;
Dcl-Pi MainLine Extpgm('MAINTLINE');
Iof Char(1);
End-Pi;
Dcl-S myString Varchar(20);

myString = CvtToMixed(myString);
End-Proc;

/// -------------------------------------
// CvtToMixed
// Convert the passed string to mixed case or
// what is normally called Title case.
// @param Source string
// @return Title cased string
/// -------------------------------------
Dcl-Proc CvtToMixed;
Dcl-Pi CvtToMixed Extpgm('MAINTLINE');
theString Varchar(100);
End-Pi;

return theString;
End-Proc;
13 changes: 13 additions & 0 deletions cli/rpgparse/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "ES2019",
"noImplicitAny": false,
"noUnusedParameters": false,
"strict": false,
"allowJs": true,
"outDir": "./dist",
"esModuleInterop": true,
"sourceMap": true
}
}
27 changes: 27 additions & 0 deletions cli/rpgparse/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

// @ts-nocheck

'use strict';

const withDefaults = require(`../../shared.webpack.config`);
const path = require(`path`);
const webpack = require(`webpack`);

module.exports = withDefaults({
context: path.join(__dirname),
entry: {
extension: `./index.ts`,
},
output: {
filename: `index.js`,
path: path.join(__dirname, `dist`)
},
// Other stuff
plugins: [
new webpack.BannerPlugin({banner: `#! /usr/bin/env node`, raw: true})
]
});
13 changes: 13 additions & 0 deletions language/models/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ export default class Cache {
return (lines.length >= 1 ? lines[0] : 0);
}

/**
* Filters the cache to include only indicators that have references.
*
* @returns {Cache} A new Cache instance with filtered indicators.
*/
filterCache(): Cache {
const filteredIndicators = this.indicators.filter(indicator => indicator.references.length > 0);
return new Cache({
...this,
indicators: filteredIndicators
});
}

find(name: string, includeProcedure?: string): Declaration|undefined {
name = name.toUpperCase();

Expand Down