Skip to content

Commit

Permalink
Initial creation of airfield
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamBien committed Oct 13, 2015
1 parent fd59ffd commit c0a6230
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 0 deletions.
77 changes: 77 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.airhacks</groupId>
<artifactId>airfield</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>4.1.0.201509280440-r</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<excludeGroupIds>junit,org.mockito,org.hamcrest</excludeGroupIds>
<outputDirectory>${project.build.directory}/app</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<finalName>airfield</finalName>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>airfield.App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>package-everything</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<finalName>airfield</finalName>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
25 changes: 25 additions & 0 deletions src/main/java/airfield/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package airfield;

import com.airhacks.airfield.TakeDown;

/**
*
* @author airhacks.com
*/
public class App {

public final static void main(String args[]) {
if (args.length != 2) {
usage();
return;
}
String local = args[0];
String remote = args[1];
TakeDown installer = new TakeDown(local, remote);
installer.installOrUpdate();
}

static void usage() {
System.out.println("Use: java -jar airfield.App [PATH_TO_LOCAL_APP] [PATH_TO_REMOTE_GIT_REPO]");
}
}
71 changes: 71 additions & 0 deletions src/main/java/com/airhacks/airfield/TakeDown.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
*/
package com.airhacks.airfield;

import java.io.File;
import java.io.IOException;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ResetCommand;
import org.eclipse.jgit.api.errors.GitAPIException;

/**
*
* @author adam-bien.com
*/
public class TakeDown {

private final String remotePath;
private final String localPath;
private Git git;

public TakeDown(String localPath, String remotePath) {
this.remotePath = remotePath;
this.localPath = localPath;
}

void initialDownload() {
try {
this.git = Git.cloneRepository()
.setURI(remotePath)
.setDirectory(new File(localPath))
.call();
System.out.println("+App installed into: " + this.localPath);
} catch (GitAPIException ex) {
System.err.println("--Cannot download files: " + ex.getMessage());
}

}

void update() {
try {
this.git.reset().setMode(ResetCommand.ResetType.HARD).call();
System.out.println("+Changed files removed");
} catch (GitAPIException ex) {
throw new IllegalStateException("Cannot reset local repository", ex);
}
this.git.pull();
System.out.println("+Files updated, ready to start!");
}

boolean openLocal() {
File localRepo = new File(this.localPath);
try {
this.git = Git.open(localRepo);
} catch (IOException ex) {
System.out.println("-" + ex.getMessage());
return false;
}
System.out.println("+Application already installed at: " + this.localPath);
return true;
}

public void installOrUpdate() {
boolean alreadyInstalled = openLocal();
if (alreadyInstalled) {
update();
} else {
initialDownload();
}
}

}
63 changes: 63 additions & 0 deletions src/test/java/com/airhacks/airfield/TakeDownIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.airhacks.airfield;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;

/**
*
* @author adam-bien.com
*/
public class TakeDownIT {

private TakeDown cut;
static final String LOCAL_REPO = "target/repo";

@Before
public void init() throws IOException {
Path directory = Paths.get(LOCAL_REPO);
if (Files.exists(directory)) {
Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}

});
}
this.cut = new TakeDown(LOCAL_REPO, "git://localhost:4242/");
}

@Test
public void initialDownload() {
Path file = Paths.get(LOCAL_REPO + "/app.txt");
assertFalse(Files.exists(file));
this.cut.initialDownload();
assertTrue(Files.exists(file));
}

@Test
public void openLocal() {
boolean repoExists = this.cut.openLocal();
assertFalse(repoExists);
this.cut.initialDownload();
repoExists = this.cut.openLocal();
assertTrue(repoExists);
}

}

0 comments on commit c0a6230

Please sign in to comment.