Skip to content

Commit

Permalink
Added CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
apryamostanov committed Feb 25, 2020
1 parent bce9d5f commit cfad769
Show file tree
Hide file tree
Showing 10 changed files with 250 additions and 7 deletions.
100 changes: 100 additions & 0 deletions cobol-cli/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
File
https://github.com/virtualdogbert/GroovyConfigWriter/blob/master/build.gradle
has been used as basis for this file.
*/
plugins {
id "groovy"
id "maven-publish"
id "idea"
id "jacoco"
id "org.springframework.boot" version "2.1.1.RELEASE"
id "io.spring.dependency-management" version "1.0.6.RELEASE"
id "enterprise.groovy.plugin" version "1.0.4"
}

apply plugin: 'com.jfrog.bintray'
bootJar {
baseName = 'cobol-cli'
version = '1.0.0'
}

ext['groovy.version'] = '2.5.4'
sourceCompatibility = 1.8
group = "io.i-t"
version = "1.0.0"
description = "Groovy Cobol Transpiler, Runtime environment and API"

repositories {
jcenter()
maven {
url "https://dl.bintray.com/infinite-technology/io.i-t"
}
}

sourceSets {
main.groovy.srcDirs = ["src/main/groovy"]
test.groovy.srcDirs = ["src/test/groovy"]
}

dependencies {
compile project(':cobol-lib')
compile 'info.picocli:picocli:4.2.0'
}

task sourceJar(type: Jar) {
from sourceSets.main.allSource
classifier "sources"
}

ext {
theTitle = "Groovy Cobol Transpiler, Runtime environment and API"
titleForDocumentation = archivesBaseName + " " + version
packageTitle = group
description = "Groovy Cobol Transpiler, Runtime environment and API"
}

publishing {
publications {
groovyMaven(MavenPublication) {
from components.java
artifact(sourceJar) {
classifier "sources"
}
}
}
}

task jarSrc(type: Jar, dependsOn: classes) {
classifier = "sources"
from sourceSets.main.allSource
}

artifacts {
archives(jarSrc)
}

bintray {
user = System.getenv("bintrayuser")
key = System.getenv("bintraypassword")
System.out.println(user)
System.out.println(key)
publications = ["groovyMaven"]
def projectName = project.name
def projectDescription = project.description
pkg {
userOrg = "infinite-technology"
repo = "io.i-t"
name = "cobol-cli"
desc = "Groovy Cobol Transpiler, Runtime environment and API"
issueTrackerUrl = "https://github.com/INFINITE-TECHNOLOGY/COBOL/issues"
vcsUrl = "https://github.com/INFINITE-TECHNOLOGY/COBOL.git"
websiteUrl = "https://i-t.io/Cobol"
licenses = ["Apache-2.0"]
}
}

configurations {
providedRuntime
compile.exclude(group: 'ch.qos.logback')
}
33 changes: 33 additions & 0 deletions cobol-cli/conventions.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

Map conventions = [
disable : false,
whiteListScripts : true,

disableDynamicCompile : false,
dynamicCompileWhiteList : [],

compileStaticExtensions : [],
limitCompileStaticExtensions: false,

defAllowed : false,
skipDefaultPackage : false
]
System.setProperty('enterprise.groovy.conventions', "conventions=${conventions.inspect()}")
29 changes: 29 additions & 0 deletions cobol-cli/src/main/groovy/io/infinite/cobol/LineBreakEnum.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.infinite.cobol

enum LineBreakEnum {

CRLF([(byte) 13, (byte) 10], "CRLF"),
CR([(byte) 13], "CR"),
LF([(byte) 10], "LF"),
LFCR([(byte) 10, (byte) 13], "LFCR"),
NL([(byte) 21], "NL"),
NONE([], "NONE")

private final String label

private final List<Byte> bytes

LineBreakEnum(List<Byte> bytes, String label) {
this.label = label
this.bytes = bytes
}

List<Byte> asBytes() {
return bytes
}

String asLabel() {
return label
}

}
59 changes: 59 additions & 0 deletions cobol-cli/src/main/groovy/io/infinite/cobol/Main.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.infinite.cobol

import groovy.util.logging.Slf4j
import io.infinite.blackbox.BlackBox
import io.proleap.cobol.preprocessor.CobolPreprocessor
import picocli.CommandLine

import java.util.concurrent.Callable

@BlackBox
@Slf4j
@CommandLine.Command(name = "COBOL-CLI", mixinStandardHelpOptions = true, description = "Convert COBOL data file to XML.", version = "1.0.0")
class Main implements Callable<Integer> {

@CommandLine.Parameters(index = "0", description = "Copybook file.")
File copybookFile

@CommandLine.Parameters(index = "1", description = "COBOL data file")
File dataFile

@CommandLine.Parameters(index = "2", description = "Result XML file")
File resultFile

@CommandLine.Option(names = ["-e", "--encoding"], description = "COBOL data file character encoding, i.e. 037 (EBCDIC), ASCII, etc", defaultValue = "037", showDefaultValue = CommandLine.Help.Visibility.ALWAYS)
String encoding

@CommandLine.Option(names = ["-s", "--structure"], description = "Copybook structure.\nUse if your file has header and/or trailer.\nDefault: \${DEFAULT-VALUE}\nSupported: \${COMPLETION-CANDIDATES}")
CopybookStructureEnum structure = CopybookStructureEnum.DETAILS

@CommandLine.Option(names = ["-l", "--lineBreak"], description = "Specify line break sequence\nUse if COBOL data file is already split by lines.\nDefault: \${DEFAULT-VALUE}\nSupported: \${COMPLETION-CANDIDATES}")
LineBreakEnum lineBreak = LineBreakEnum.NONE

@CommandLine.Option(names = ["-f", "--copybookFormat"], description = "Copybook format\nDefault: \${DEFAULT-VALUE}\nSupported: \${COMPLETION-CANDIDATES}")
CobolPreprocessor.CobolSourceFormatEnum copybookFormat = CobolPreprocessor.CobolSourceFormatEnum.FIXED

static void main(String[] args) {
log.info("Welcome to Infinite Technology COBOL.")
log.info("This is Open Source software, free for usage and modification.")
log.info("By using this software you accept License and User Agreement.")
log.info("Visit our web site: https://i-t.io/Cobol")
int exitCode = new CommandLine(new Main()).execute(args)
System.exit(exitCode)
}

Integer call() throws Exception {
CobolCompiler cobolCompiler = new CobolCompiler()
CobolRuntime cobolRuntime = cobolCompiler.compileCobol(copybookFile.getText(), copybookFormat)
cobolRuntime.run(
dataFile.length(),
dataFile.newInputStream(),
encoding,
lineBreak.asBytes(),
new CobolApiXml(resultFile.newOutputStream()),
structure
)
return 0
}

}
1 change: 1 addition & 0 deletions cobol-lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ bintray {
configurations {
providedRuntime
compile.exclude(group: 'ch.qos.logback')
compile.exclude(group: 'info.picocli')
}

dependencyManagement {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ class CobolRuntime {
if (totalSize < (totalRead + totalSkipped)) {
throw new CobolExceptionRuntime("Over reading. Internal error (bug) - this should not happen. Investigation is required.")
}
if ([CopybookStructureEnum.THREE_RECORD, CopybookStructureEnum.TWO_RECORD_H].contains(copybookStructure) && isFirstRecord) {
if ([CopybookStructureEnum.HEADER_DETAILS_TRAILER, CopybookStructureEnum.HEADER_DETAILS].contains(copybookStructure) && isFirstRecord) {
//DISABLED DEBUG:log.debug("Copybook structure with header. Header read - proceeding to read details.")
isFirstRecord = false
break
}
if ([CopybookStructureEnum.THREE_RECORD, CopybookStructureEnum.TWO_RECORD_T].contains(copybookStructure) && !isLastRecord) {
if ([CopybookStructureEnum.HEADER_DETAILS_TRAILER, CopybookStructureEnum.DETAILS_TRAILER].contains(copybookStructure) && !isLastRecord) {
Long lastRecordSize = totalRead - initialTotalRead
//DISABLED DEBUG:log.debug("Last record/trailer check: totalSize=${totalSize}, ${totalRead} + ${lastRecordSize} = ${totalRead + lastRecordSize}")
if (totalSize <= totalRead + lastRecordSize) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package io.infinite.cobol

enum CopybookStructureEnum {

ONE_RECORD("ONE_RECORD"),
TWO_RECORD_H("TWO_RECORD_H"),
TWO_RECORD_T("TWO_RECORD_T"),
THREE_RECORD("THREE_RECORD")
DETAILS("DETAILS"),
HEADER_DETAILS("HEADER_DETAILS"),
DETAILS_TRAILER("DETAILS_TRAILER"),
HEADER_DETAILS_TRAILER("HEADER_DETAILS_TRAILER")

private final String copybookStructure

Expand Down
20 changes: 20 additions & 0 deletions cobol-test/copybook.cbl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
000010 IDENTIFICATION DIVISION. XXXXXXXX
PROGRAM-ID. UnstringSample. XXXXXXXX
ENVIRONMENT DIVISION. XXXXXXXX
CONFIGURATION SECTION. XXXXXXXX
SPECIAL-NAMES. DECIMAL-POINT IS COMMA. XXXXXXXX
INPUT-OUTPUT SECTION. XXXXXXXX
DATA DIVISION. XXXXXXXX
WORKING-STORAGE SECTION. XXXXXXXX
01 ABCDE-RECORD. XXXXXXXX
XXXXXX 02 ABCDE-REC. XXXXXXXX
03 ABCDE-COMMON. XXXXXXXX
05 ABCDE-DETAILS. XXXXXXXX
10 ABCDE-RECORD-ABC. XXXXXXXX
15 ABCDE-PRI-ABC. XXXXXXXX
20 ABCDE-ABC-AAAAAAAA PIC X(02). XXXXXXXX
20 ABCDE-ABC-ACCT-ABCS. XXXXXXXX
25 ABCDE-ABC-ABC-1 PIC X(02). XXXXXXXX
25 ABCDE-ABC-ABC-2 PIC X(03). XXXXXXXX
25 ABCDE-ABC-ABC-3 PIC X(03). XXXXXXXX
25 ABCDE-ABC-ABC-4 PIC X(04). XXXXXXXX
Empty file added cobol-test/datafile.bin
Empty file.
3 changes: 2 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ include 'services:webservice'

rootProject.name = 'cobol'

include 'cobol-lib'
include 'cobol-lib'
include 'cobol-cli'

0 comments on commit cfad769

Please sign in to comment.