-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for generating projects and publishing directly to github
- Loading branch information
Showing
20 changed files
with
937 additions
and
40 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
39 changes: 39 additions & 0 deletions
39
...ain/java/ca/weblite/jdeploy/cli/controllers/GitHubRepositoryInitializerCLIController.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,39 @@ | ||
package ca.weblite.jdeploy.cli.controllers; | ||
|
||
import ca.weblite.jdeploy.cli.util.CommandLineParser; | ||
import ca.weblite.jdeploy.services.GitHubRepositoryInitializer; | ||
import ca.weblite.jdeploy.services.GitHubUsernameService; | ||
import ca.weblite.jdeploy.services.GithubTokenService; | ||
|
||
import java.io.File; | ||
|
||
public class GitHubRepositoryInitializerCLIController extends BaseController { | ||
|
||
private GithubTokenService githubTokenService = new GithubTokenService(); | ||
private GitHubUsernameService gitHubUsernameService = new GitHubUsernameService(githubTokenService); | ||
|
||
private int exitCode = 0; | ||
|
||
public GitHubRepositoryInitializerCLIController(File packageJSONFile, String[] args) { | ||
super(packageJSONFile, args); | ||
} | ||
|
||
public void run() { | ||
CommandLineParser parser = new CommandLineParser(); | ||
GitHubRepositoryInitializer.Params params = new GitHubRepositoryInitializer.Params(); | ||
parser.parseArgsToParams(params, args); | ||
try { | ||
new GitHubRepositoryInitializer(packageJSONFile, null, githubTokenService, gitHubUsernameService).initAndPublish(params); | ||
} catch (Exception e) { | ||
System.err.println("Failed to initialize GitHub repository"); | ||
e.printStackTrace(System.err); | ||
parser.printHelp(params); | ||
exitCode = 1; | ||
} | ||
|
||
} | ||
|
||
public int getExitCode() { | ||
return exitCode; | ||
} | ||
} |
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
47 changes: 47 additions & 0 deletions
47
cli/src/main/java/ca/weblite/jdeploy/dtos/GithubRepositoryDto.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,47 @@ | ||
package ca.weblite.jdeploy.dtos; | ||
|
||
import ca.weblite.jdeploy.services.GitHubUsernameService; | ||
|
||
import java.io.IOException; | ||
|
||
public class GithubRepositoryDto { | ||
private String fullRepositoryName; | ||
private boolean isPrivate; | ||
|
||
public GithubRepositoryDto(String repositoryName, boolean isPrivate) { | ||
this.fullRepositoryName = repositoryName; | ||
this.isPrivate = isPrivate; | ||
} | ||
|
||
public String getFullRepositoryName() { | ||
return fullRepositoryName; | ||
} | ||
|
||
public String getRepositoryName() { | ||
if (fullRepositoryName != null && fullRepositoryName.contains("/") && fullRepositoryName.split("/").length > 1) { | ||
return fullRepositoryName.split("/")[1]; | ||
} | ||
|
||
return fullRepositoryName; | ||
} | ||
|
||
public String getRepositoryUsername(GitHubUsernameService gitHubUsernameService) throws IOException { | ||
if (fullRepositoryName != null && fullRepositoryName.contains("/") && fullRepositoryName.split("/").length > 1) { | ||
return fullRepositoryName.split("/")[0]; | ||
} | ||
|
||
return gitHubUsernameService.getGitHubUsername(); | ||
} | ||
|
||
public void setFullRepositoryName(String fullRepositoryName) { | ||
this.fullRepositoryName = fullRepositoryName; | ||
} | ||
|
||
public boolean isPrivate() { | ||
return isPrivate; | ||
} | ||
|
||
public void setPrivate(boolean aPrivate) { | ||
isPrivate = aPrivate; | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
cli/src/main/java/ca/weblite/jdeploy/helpers/filemergers/DirectoryMerger.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,51 @@ | ||
package ca.weblite.jdeploy.helpers.filemergers; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
public class DirectoryMerger { | ||
|
||
private FileMerger[] fileMergers; | ||
|
||
public DirectoryMerger(FileMerger... fileMergers) { | ||
this.fileMergers = fileMergers; | ||
} | ||
|
||
public void merge(File directory, File patch) throws Exception { | ||
if (!directory.isDirectory()) { | ||
throw new IllegalArgumentException("directory must be a directory"); | ||
} | ||
if (!patch.isDirectory()) { | ||
throw new IllegalArgumentException("patch must be a directory"); | ||
} | ||
|
||
for (File patchFile : patch.listFiles()) { | ||
File directoryFile = new File(directory, patchFile.getName()); | ||
if (patchFile.isDirectory()) { | ||
if (directoryFile.exists()) { | ||
merge(directoryFile, patchFile); | ||
} else { | ||
FileUtils.copyDirectory(patchFile, directoryFile); | ||
} | ||
} else { | ||
if (directoryFile.exists()) { | ||
boolean merged = false; | ||
for (FileMerger merger : fileMergers) { | ||
if (merger.isApplicableTo(directoryFile, patchFile)) { | ||
merger.merge(directoryFile, patchFile); | ||
merged = true; | ||
break; | ||
} | ||
} | ||
if (!merged) { | ||
FileUtils.copyFile(patchFile, directoryFile); | ||
} | ||
} else { | ||
FileUtils.copyFile(patchFile, directoryFile); | ||
} | ||
} | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
cli/src/main/java/ca/weblite/jdeploy/helpers/filemergers/FileMerger.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,9 @@ | ||
package ca.weblite.jdeploy.helpers.filemergers; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
public interface FileMerger { | ||
public void merge(File base, File patch) throws Exception; | ||
public boolean isApplicableTo(File base, File patch); | ||
} |
48 changes: 48 additions & 0 deletions
48
cli/src/main/java/ca/weblite/jdeploy/helpers/filemergers/JSONFileMerger.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,48 @@ | ||
package ca.weblite.jdeploy.helpers.filemergers; | ||
|
||
import org.json.JSONObject; | ||
import org.json.JSONTokener; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
|
||
public class JSONFileMerger implements FileMerger { | ||
|
||
public void merge(File packageJson, File patch) throws Exception { | ||
JSONObject baseJson = readJsonFromFile(packageJson); | ||
JSONObject patchJson = readJsonFromFile(patch); | ||
|
||
JSONObject mergedJson = mergeRecursively(baseJson, patchJson); | ||
|
||
// Write the merged JSON back to the original file | ||
try (FileOutputStream fos = new FileOutputStream(packageJson)) { | ||
fos.write(mergedJson.toString(4).getBytes()); // Indented with 4 spaces | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isApplicableTo(File base, File patch) { | ||
return base.getName().endsWith(".json") && patch.getName().equals(base.getName()); | ||
} | ||
|
||
private JSONObject mergeRecursively(JSONObject baseJson, JSONObject patchJson) { | ||
for (String key : JSONObject.getNames(patchJson)) { | ||
if (baseJson.has(key) && baseJson.get(key) instanceof JSONObject && patchJson.get(key) instanceof JSONObject) { | ||
// If the key leads to another JSONObject in both baseJson and patchJson, merge those JSONObjects | ||
baseJson.put(key, mergeRecursively(baseJson.getJSONObject(key), patchJson.getJSONObject(key))); | ||
} else { | ||
// Otherwise, just replace the value in baseJson with the one from patchJson | ||
baseJson.put(key, patchJson.get(key)); | ||
} | ||
} | ||
return baseJson; | ||
} | ||
|
||
private JSONObject readJsonFromFile(File file) throws IOException { | ||
try (FileInputStream fis = new FileInputStream(file)) { | ||
return new JSONObject(new JSONTokener(fis)); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
cli/src/main/java/ca/weblite/jdeploy/helpers/filemergers/PackageJsonFileMerger.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,13 @@ | ||
package ca.weblite.jdeploy.helpers.filemergers; | ||
|
||
import java.io.File; | ||
|
||
public class PackageJsonFileMerger extends JSONFileMerger { | ||
@Override | ||
public boolean isApplicableTo(File base, File patch) { | ||
return base.getName().equals("package.json") | ||
&& patch.getName().equals(base.getName()) | ||
&& base.isFile() | ||
&& patch.isFile(); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
cli/src/main/java/ca/weblite/jdeploy/helpers/filemergers/PomFileMerger.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,64 @@ | ||
package ca.weblite.jdeploy.helpers.filemergers; | ||
|
||
import org.w3c.dom.Document; | ||
import org.w3c.dom.Element; | ||
import org.w3c.dom.Node; | ||
import org.w3c.dom.NodeList; | ||
|
||
import javax.xml.parsers.DocumentBuilder; | ||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import javax.xml.transform.Transformer; | ||
import javax.xml.transform.TransformerFactory; | ||
import javax.xml.transform.dom.DOMSource; | ||
import javax.xml.transform.stream.StreamResult; | ||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
|
||
public class PomFileMerger implements FileMerger { | ||
public void merge(File pomXml, File pomXmlPatch) throws Exception { | ||
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); | ||
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); | ||
Document doc = dBuilder.parse(pomXml); | ||
doc.getDocumentElement().normalize(); | ||
|
||
Document patchDoc = dBuilder.parse(pomXmlPatch); | ||
patchDoc.getDocumentElement().normalize(); | ||
|
||
mergeRecursively(doc.getDocumentElement(), patchDoc.getDocumentElement()); | ||
|
||
TransformerFactory transformerFactory = TransformerFactory.newInstance(); | ||
Transformer transformer = transformerFactory.newTransformer(); | ||
DOMSource source = new DOMSource(doc); | ||
|
||
try (FileOutputStream fos = new FileOutputStream(pomXml)) { | ||
StreamResult result = new StreamResult(fos); | ||
transformer.transform(source, result); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isApplicableTo(File base, File patch) { | ||
return base.getName().equals("pom.xml") | ||
&& patch.getName().equals(base.getName()) | ||
&& base.isFile() | ||
&& patch.isFile(); | ||
} | ||
|
||
private void mergeRecursively(Element baseElement, Element patchElement) { | ||
NodeList patchChildren = patchElement.getChildNodes(); | ||
for (int i = 0; i < patchChildren.getLength(); i++) { | ||
Node patchChild = patchChildren.item(i); | ||
if (patchChild instanceof Element) { | ||
Element patchChildElement = (Element) patchChild; | ||
String tagName = patchChildElement.getTagName(); | ||
if (baseElement.getElementsByTagName(tagName).getLength() > 0) { | ||
// If the baseElement already has a child with the same tag name, merge the children | ||
mergeRecursively((Element) baseElement.getElementsByTagName(tagName).item(0), patchChildElement); | ||
} else { | ||
// Otherwise, append the patchChildElement to the baseElement | ||
baseElement.appendChild(baseElement.getOwnerDocument().importNode(patchChildElement, true)); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.