forked from prospero238/liquibase-runner
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JENKINS-23908 - created external invoker
- Loading branch information
1 parent
4b64c53
commit ceb9be7
Showing
6 changed files
with
199 additions
and
36 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
src/main/java/org/jenkinsci/plugins/liquibase/builder/Annotator.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,29 @@ | ||
package org.jenkinsci.plugins.liquibase.builder; | ||
|
||
import hudson.console.LineTransformationOutputStream; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.nio.ByteBuffer; | ||
import java.nio.charset.Charset; | ||
|
||
import org.jenkinsci.plugins.liquibase.external.ChangeSetNote; | ||
|
||
public class Annotator extends LineTransformationOutputStream { | ||
private final OutputStream out; | ||
private final Charset charset; | ||
|
||
public Annotator(OutputStream out, Charset charset) { | ||
this.out = out; | ||
this.charset = charset; | ||
} | ||
@Override | ||
protected void eol(byte[] b, int len) throws IOException { | ||
String line = trimEOL(charset.decode(ByteBuffer.wrap(b, 0, len)).toString()); | ||
|
||
if (ChangeSetNote.doesLineHaveChangeset(line)) { | ||
new ChangeSetNote().encodeTo(out); | ||
|
||
} | ||
out.write(b, 0, len); | ||
}} |
60 changes: 60 additions & 0 deletions
60
src/main/java/org/jenkinsci/plugins/liquibase/builder/Invoker.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,60 @@ | ||
package org.jenkinsci.plugins.liquibase.builder; | ||
|
||
import hudson.Launcher; | ||
import hudson.model.AbstractBuild; | ||
import hudson.model.BuildListener; | ||
import hudson.model.Result; | ||
import hudson.util.ArgumentListBuilder; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.Properties; | ||
|
||
import org.jenkinsci.plugins.liquibase.installation.LiquibaseInstallation; | ||
|
||
import com.google.common.base.Strings; | ||
|
||
public class Invoker { | ||
|
||
private static final String DEFAULT_LOG_LEVEL = "info"; | ||
|
||
public boolean invokeLiquibase(Properties properties, | ||
BuildListener listener, | ||
Launcher launcher, | ||
AbstractBuild<?, ?> build, | ||
LiquibaseBuilder liquibaseBuilder, | ||
Annotator annotator) { | ||
ArgumentListBuilder cliCommand = new ArgumentListBuilder(); | ||
String execName; | ||
if (launcher.isUnix()) { | ||
execName = LiquibaseInstallation.UNIX_EXEC_NAME; | ||
} else { | ||
execName = LiquibaseInstallation.WINDOWS_EXEC_NAME; | ||
} | ||
|
||
|
||
cliCommand.add(new File(liquibaseBuilder.getInstallation().getHome(), execName)); | ||
cliCommand.add("--" + LiquibaseProperty.LOG_LEVEL.getOption(), DEFAULT_LOG_LEVEL); | ||
addOptionIfPresent(cliCommand, LiquibaseProperty.DEFAULTS_FILE, liquibaseBuilder.getLiquibasePropertiesPath()); | ||
|
||
cliCommand.add("update"); | ||
|
||
try { | ||
launcher.launch().cmds(cliCommand).stderr(annotator).stdout(annotator).pwd(build.getWorkspace()).join(); | ||
} catch (IOException e) { | ||
e.printStackTrace(listener.getLogger()); | ||
build.setResult(Result.FAILURE); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(listener.getLogger()); | ||
build.setResult(Result.FAILURE); | ||
} | ||
return true; | ||
|
||
} | ||
|
||
private static void addOptionIfPresent(ArgumentListBuilder cmdExecArgs, LiquibaseProperty liquibaseProperty, String value) { | ||
if (!Strings.isNullOrEmpty(value)) { | ||
cmdExecArgs.add("--" + liquibaseProperty.getOption(), value); | ||
} | ||
} | ||
} |
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
34 changes: 34 additions & 0 deletions
34
src/main/java/org/jenkinsci/plugins/liquibase/external/ChangeSetNote.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,34 @@ | ||
package org.jenkinsci.plugins.liquibase.external; | ||
|
||
import hudson.MarkupText; | ||
import hudson.console.ConsoleAnnotator; | ||
import hudson.console.ConsoleNote; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* finds changeset names and marks console accordingly. | ||
*/ | ||
public class ChangeSetNote extends ConsoleNote { | ||
public static Pattern CHANGESET_PATTERN = Pattern.compile("(\\S+::)?(\\S+)::(\\S+): "); | ||
|
||
@Override | ||
public ConsoleAnnotator annotate(Object context, MarkupText text, int charPos) { | ||
Matcher matcher = CHANGESET_PATTERN.matcher(text.getText()); | ||
while (matcher.find()) { | ||
String changeSetName = matcher.group(2); | ||
int start = matcher.start(2); | ||
text.addMarkup(start, changeSetName.length() + start, "<b class=liquibase-changeset>", "</b>"); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static boolean doesLineHaveChangeset(String line) { | ||
Matcher matcher = CHANGESET_PATTERN.matcher(line); | ||
return matcher.find(); | ||
} | ||
|
||
|
||
} |
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
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