-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First commit for Liquifier, the Liquibase change log converter.
- Loading branch information
0 parents
commit 3f05cc0
Showing
8 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
*.iml | ||
build | ||
.gradle | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
rootProject.name = 'liquify' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
42
src/main/java/com/refactify/arguments/ConversionArguments.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/refactify/arguments/ConversionArgumentsParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/sh | ||
java -jar liquify.jar "$@" |