Skip to content

Commit

Permalink
First commit for Liquifier, the Liquibase change log converter.
Browse files Browse the repository at this point in the history
  • Loading branch information
daquino committed Jun 26, 2015
0 parents commit 3f05cc0
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.iml
build
.gradle
.idea
35 changes: 35 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
plugins {
id 'com.github.johnrengelman.shadow' version '1.2.1'
}

group 'com.refactify'
version '1.0-SNAPSHOT'

apply plugin: 'java'

sourceCompatibility = 1.6

repositories {
mavenLocal()
mavenCentral()
}

shadowJar {
manifest {
attributes 'Main-Class': 'com.refactify.Liquify'
}
}

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile "org.liquibase:liquibase-core:3.4.0"
compile "org.yaml:snakeyaml:1.13"
}

task dist(dependsOn: ['clean', 'shadowJar'], type: Copy) {
from 'src/main/shell/'
from ('build/libs/') {
rename 'liquify(.*).jar', 'liquify.jar'
}
into 'build/dist/'
}
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rootProject.name = 'liquify'

55 changes: 55 additions & 0 deletions src/main/java/com/refactify/Liquify.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.refactify;

import com.refactify.arguments.ConversionArguments;
import com.refactify.arguments.ConversionArgumentsParser;
import com.refactify.printer.UsagePrinter;
import liquibase.changelog.ChangeSet;
import liquibase.changelog.DatabaseChangeLog;
import liquibase.exception.LiquibaseException;
import liquibase.parser.ChangeLogParser;
import liquibase.parser.ChangeLogParserFactory;
import liquibase.resource.FileSystemResourceAccessor;
import liquibase.resource.ResourceAccessor;
import liquibase.serializer.ChangeLogSerializer;
import liquibase.serializer.ChangeLogSerializerFactory;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Liquify {
private final static ConversionArgumentsParser parser = new ConversionArgumentsParser();
private final static UsagePrinter usagePrinter = new UsagePrinter();
public static void main(final String[] args) {
ConversionArguments conversionArguments = parser.parseArguments(args);

if(conversionArguments.areValid()) {
convertDatabaseChangeLog(conversionArguments);
}
else {
usagePrinter.printUsage();
}
}

private static void convertDatabaseChangeLog(final ConversionArguments conversionArguments) {
try {
ResourceAccessor resourceAccessor = new FileSystemResourceAccessor(System.getProperty("user.dir"));
ChangeLogParser parser = ChangeLogParserFactory.getInstance().getParser(conversionArguments.getSource(), resourceAccessor);
DatabaseChangeLog changeLog = parser.parse(conversionArguments.getSource(), null, resourceAccessor);
ChangeLogSerializer serializer = ChangeLogSerializerFactory.getInstance().getSerializer(conversionArguments.getDestination());
for (ChangeSet set : changeLog.getChangeSets()) {
set.setFilePath(conversionArguments.getDestination());
}
serializer.write(changeLog.getChangeSets(), new FileOutputStream(conversionArguments.getDestination()));
}
catch (LiquibaseException e) {
e.printStackTrace();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
42 changes: 42 additions & 0 deletions src/main/java/com/refactify/arguments/ConversionArguments.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.refactify.arguments;

public class ConversionArguments {
private String source;
private String destination;
private static final String[] validFileExtensions = new String[] {"xml", "yaml", "json", "sql"};

public String getSource() {
return source;
}

public void setSource(final String source) {
this.source = source;
}

public String getDestination() {
return destination;
}

public void setDestination(final String destination) {
this.destination = destination;
}

public boolean areValid() {
return isValidPath(source) && isValidPath(destination);
}

private boolean isValidPath(final String path) {
return path != null && hasValidExtension(path);
}

private boolean hasValidExtension(final String path) {
boolean validExtension = false;
for(String fileExtension: validFileExtensions) {
if(path.endsWith(fileExtension)) {
validExtension = true;
break;
}
}
return validExtension;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.refactify.arguments;

import java.util.ArrayList;
import java.util.List;

public class ConversionArgumentsParser {

public ConversionArguments parseArguments(String[] arguments) {
ConversionArguments conversionArguments = new ConversionArguments();
try {
for (int i = 0; i < arguments.length; i++) {
String argument = arguments[i];
if (argument.equals("-s") || argument.equals("--source")) {
String value = arguments[++i];
conversionArguments.setSource(value);
}
else if (argument.equals("-d") || argument.equals("--dest")) {
String value = arguments[++i];
conversionArguments.setDestination(value);
}
}
}
catch(ArrayIndexOutOfBoundsException exc) {
System.out.println("Invalid arguments.");
}
return conversionArguments;
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/refactify/printer/UsagePrinter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.refactify.printer;

public class UsagePrinter {
public void printUsage() {
System.out.println("Liquify can be use to convert Liquibase changelog " +
"files between the supported file formats: xml,yaml,json,sql.");
System.out.println("====================================");
System.out.println("Usage: ");
System.out.println(" liquify -s <source> -d <dest>");
System.out.println("");
System.out.println(" Options:");
System.out.println(" -s,--source Database changelog source file.");
System.out.println(" -d,--dest Database changelog destination file.");
}
}
2 changes: 2 additions & 0 deletions src/main/shell/liquify
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
java -jar liquify.jar "$@"

0 comments on commit 3f05cc0

Please sign in to comment.