Skip to content

Commit

Permalink
Added support for generating projects and publishing directly to github
Browse files Browse the repository at this point in the history
  • Loading branch information
shannah committed Oct 22, 2023
1 parent a75058b commit 9f0e61c
Show file tree
Hide file tree
Showing 20 changed files with 937 additions and 40 deletions.
22 changes: 20 additions & 2 deletions cli/src/main/java/ca/weblite/jdeploy/JDeploy.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import ca.weblite.jdeploy.appbundler.BundlerSettings;
import ca.weblite.jdeploy.cheerpj.services.BuildCheerpjAppService;
import ca.weblite.jdeploy.cli.controllers.CheerpjController;
import ca.weblite.jdeploy.cli.controllers.GitHubRepositoryInitializerCLIController;
import ca.weblite.jdeploy.cli.controllers.JPackageController;
import ca.weblite.jdeploy.cli.controllers.ProjectGeneratorCLIController;
import ca.weblite.jdeploy.gui.JDeployMainMenu;
Expand Down Expand Up @@ -2340,7 +2341,8 @@ private void help(Options opts) {
+ " package : Prepare for install. This copies necessary files into bin directory.\n"
+ " install : Installs the app locally (links to PATH)\n"
+ " publish : Publishes to NPM\n"
+ " generate: Generates a new project\n",
+ " generate: Generates a new project\n"
+ " github init -n <repo-name>: Initializes commits, and pushes to github\n",
opts);

}
Expand All @@ -2355,12 +2357,19 @@ private void _run() {
public static void main(String[] args) {
try {
JDeploy prog = new JDeploy(new File(".").getAbsoluteFile());
if ("generate".equals(args[0])) {
if (args.length > 0 && "generate".equals(args[0])) {
String[] generateArgs = new String[args.length-1];
System.arraycopy(args, 1, generateArgs, 0, generateArgs.length);
prog.generate(generateArgs);
return;
}
if (args.length > 0 && "github".equals(args[0]) && args.length> 1 && "init".equals(args[1])) {
String[] githubInitArgs = new String[args.length-2];
System.arraycopy(args, 2, githubInitArgs, 0, githubInitArgs.length);
prog.githubInit(githubInitArgs);
return;
}

Options opts = new Options();
opts.addOption("y", "no-prompt", false,"Indicates not to prompt user ");
opts.addOption("W", "no-workflow", false,"Indicates not to create a github workflow if true");
Expand Down Expand Up @@ -2429,6 +2438,7 @@ public static void main(String[] args) {
System.exit(0);
}
}

if ("cheerpj".equals(args[0])) {
String[] cheerpjArgs = new String[args.length-1];
System.arraycopy(args, 1, cheerpjArgs, 0, cheerpjArgs.length);
Expand Down Expand Up @@ -2477,6 +2487,14 @@ public static void main(String[] args) {
}
}

private void githubInit(String[] githubInitArgs) {
GitHubRepositoryInitializerCLIController controller = new GitHubRepositoryInitializerCLIController(getPackageJsonFile(), githubInitArgs);
controller.run();
if (controller.getExitCode() != 0) {
fail("Failed to initialize github repository", controller.getExitCode());
}
}

private void generate(String[] args) {
ProjectGeneratorCLIController controller = new ProjectGeneratorCLIController(args);
controller.run();
Expand Down
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public void parseArgsToParams(Object params, String[] args) {
String property = parts[0];
String value = parts[1];
setProperty(params, property, value);
} else {
String property = parts[0];
setProperty(params, property, "true");
}
} else if (arg.startsWith("-")) {
String alias = arg.substring(1);
Expand Down Expand Up @@ -76,6 +79,8 @@ private void setProperty(Object object, String property, String value) {
field.set(object, new File(value));
} else if (type == int.class || type == Integer.class) {
field.set(object, Integer.parseInt(value));
} else if (type == boolean.class || type == Boolean.class) {
field.set(object, Boolean.parseBoolean(value));
} else {
field.set(object, value);
}
Expand Down
47 changes: 47 additions & 0 deletions cli/src/main/java/ca/weblite/jdeploy/dtos/GithubRepositoryDto.java
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;
}
}
8 changes: 8 additions & 0 deletions cli/src/main/java/ca/weblite/jdeploy/helpers/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,12 @@ public int countCharInstances(String input, char target) {
}
return count;
}

public String ucFirst(String lowerCaseWithSeparatorToCamelCase) {
if (lowerCaseWithSeparatorToCamelCase == null || lowerCaseWithSeparatorToCamelCase.isEmpty()) {
return lowerCaseWithSeparatorToCamelCase;
}

return Character.toUpperCase(lowerCaseWithSeparatorToCamelCase.charAt(0)) + lowerCaseWithSeparatorToCamelCase.substring(1);
}
}
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);
}
}
}
}
}
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);
}
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));
}
}
}
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();
}
}
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));
}
}
}
}
}
Loading

0 comments on commit 9f0e61c

Please sign in to comment.