Skip to content

Commit ca1315b

Browse files
committed
Improved cheerpj publish to github pages action
1 parent 4a48a5c commit ca1315b

File tree

3 files changed

+123
-80
lines changed

3 files changed

+123
-80
lines changed

cli/src/main/java/ca/weblite/jdeploy/services/CheerpjService.java

Lines changed: 13 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
package ca.weblite.jdeploy.services;
22

33
import ca.weblite.jdeploy.cheerpj.services.BuildCheerpjAppService;
4-
import ca.weblite.tools.io.FileUtil;
54
import org.json.JSONObject;
6-
75
import java.io.*;
8-
import java.util.Arrays;
9-
import java.util.Scanner;
106

117
public class CheerpjService extends BaseService {
128
private BuildCheerpjAppService buildCheerpjAppService;
139

1410
public CheerpjService(File packageJSONFile, JSONObject packageJSON) throws IOException {
1511
super(packageJSONFile, packageJSON);
1612
buildCheerpjAppService = new BuildCheerpjAppService();
17-
1813
}
1914

20-
2115
public boolean isEnabled() {
2216
return getJDeployObject().has("cheerpj");
2317
}
@@ -138,10 +132,16 @@ private String getGithubPagesPublishPath() throws IOException, InterruptedExcept
138132
}
139133

140134
public boolean isGithubPagesEnabled() {
135+
if (!getGithubPagesConfig().has("enabled")) {
136+
return false;
137+
}
141138
return getGithubPagesConfig().getBoolean("enabled");
142139
}
143140

144141
private String getGithubPagesBranch() {
142+
if (!getGithubPagesConfig().has("branch")) {
143+
return null;
144+
}
145145
return getGithubPagesConfig().getString("branch");
146146
}
147147

@@ -161,20 +161,6 @@ private String getCurrentGitBranch() throws IOException, InterruptedException {
161161
return branchName;
162162
}
163163

164-
private void checkoutBranch(String branch, boolean fetch, boolean pull) throws IOException, InterruptedException {
165-
if (fetch) {
166-
executeGitCommand("git", "fetch", "--all");
167-
}
168-
169-
// Checkout gh-pages branch
170-
executeGitCommand("git", "checkout", branch);
171-
172-
// Pull the latest
173-
if (pull) {
174-
executeGitCommand("git", "pull");
175-
}
176-
}
177-
178164
public static void generateGitignoreFile(String directoryPath) throws IOException {
179165
File gitignore = new File(directoryPath, ".gitignore");
180166
try (BufferedWriter writer = new BufferedWriter(new FileWriter(gitignore))) {
@@ -186,70 +172,17 @@ public static void generateGitignoreFile(String directoryPath) throws IOExceptio
186172
// Do not ignore directories, so we can traverse them
187173
writer.write("\n");
188174
}
189-
System.out.println(".gitignore file created at: " + gitignore.getAbsolutePath());
190175
}
191176

192-
193177
private void publishToGithubPages() throws IOException, InterruptedException {
194-
String currentBranch = getCurrentGitBranch();
195-
String githubPagesBranch = getGithubPagesBranch();
196-
String githubPagesPublishPath = getGithubPagesPublishPath();
197-
if (githubPagesPublishPath == null) {
198-
throw new IOException("Github pages publish path is null.");
178+
if (getCurrentGitBranch() != null && getCurrentGitBranch().equals(getGithubPagesBranch())) {
179+
System.out.println("Cannot publish to github pages from the same branch");
180+
return;
199181
}
200-
File publishPath = new File(packageJSONFile.getParentFile(), githubPagesPublishPath);
201-
boolean stashed = false;
202-
if (!currentBranch.equals(githubPagesBranch)) {
203-
executeGitCommand("git", "stash");
204-
stashed = true;
205-
checkoutBranch(githubPagesBranch, true, true);
206-
}
207-
try {
208-
if (publishPath.exists()) {
209-
FileUtil.delTree(publishPath);
210-
}
211-
publishPath.getParentFile().mkdirs();
212-
publishToCurrentBranch(publishPath);
213-
} finally {
214-
if (stashed) {
215-
executeGitCommand("git", "stash", "pop");
216-
}
217-
if (!currentBranch.equals(githubPagesBranch)) {
218-
checkoutBranch(currentBranch, false, false);
219-
}
220-
}
221-
222-
223-
}
224-
225-
private void executeGitCommand(String... commands) throws IOException, InterruptedException {
226-
System.out.println("Running git command: " + Arrays.toString(commands));
227-
ProcessBuilder processBuilder = new ProcessBuilder(commands);
228-
processBuilder.inheritIO();
229-
Process process = processBuilder.start();
230-
int result = process.waitFor();
231-
232-
// Output the command result for debugging purpose
233-
try (InputStream inputStream = process.getInputStream();
234-
Scanner scanner = new Scanner(inputStream)) {
235-
while (scanner.hasNextLine()) {
236-
System.out.println(scanner.nextLine());
237-
}
238-
}
239-
if (result != 0) {
240-
throw new IOException("Error while executing git command: " + Arrays.toString(commands));
241-
}
242-
}
243-
244-
private void publishToCurrentBranch(File publishPath) throws IOException, InterruptedException {
245-
246-
FileUtil.copy(getDestDirectory(), publishPath);
247-
generateGitignoreFile(publishPath.getAbsolutePath());
248-
249-
executeGitCommand("git", "add", getGithubPagesPublishPath());
250-
executeGitCommand("git", "commit", "-m", "Publish to github pages.");
251-
executeGitCommand("git", "push", "origin", getCurrentGitBranch());
182+
GithubPagesPublisher publisher = new GithubPagesPublisher();
183+
generateGitignoreFile(getDestDirectory().getAbsolutePath());
184+
System.out.println("Publishing to github pages");
185+
publisher.publishToGithubPages(getDestDirectory(), null, getGithubPagesBranch(), getGithubPagesPublishPath());
252186

253187
}
254-
255188
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package ca.weblite.jdeploy.services;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.nio.file.*;
6+
import java.util.Scanner;
7+
8+
public class GithubPagesPublisher {
9+
private GithubService githubService = new GithubService();
10+
/**
11+
* Publishes the contents of a directory to a github pages branch.
12+
*
13+
* @param sourceDirectory The directory containing the files to publish.
14+
* @param repoUrl The URL of the github repository
15+
* @param branchName The branch-name to publish to
16+
* @param destPath The destination path within the repo where the files should be published.
17+
* @throws IOException, InterruptedException
18+
*/
19+
public void publishToGithubPages(File sourceDirectory, String repoUrl, String branchName, String destPath) throws IOException, InterruptedException {
20+
if (repoUrl == null) {
21+
repoUrl = githubService.getRepoURL(sourceDirectory);
22+
}
23+
File tempDir = null;
24+
try {
25+
// Create a temporary directory
26+
tempDir = Files.createTempDirectory("tempRepoDir").toFile();
27+
28+
// Clone the repository to a temporary directory
29+
ProcessBuilder builder = new ProcessBuilder();
30+
builder.command("git", "clone", "--depth", "1", "-b", branchName, repoUrl, tempDir.getAbsolutePath())
31+
.inheritIO()
32+
.start()
33+
.waitFor();
34+
35+
// Copy the contents of sourceDirectory to the temporary directory at destPath
36+
File destDir = new File(tempDir, destPath);
37+
copyDirectory(sourceDirectory, destDir);
38+
39+
// Add, commit and push the changes
40+
builder.command("git", "add", ".")
41+
.directory(tempDir)
42+
.inheritIO()
43+
.start()
44+
.waitFor();
45+
builder.command("git", "commit", "-m", "Update GitHub Pages")
46+
.directory(tempDir)
47+
.inheritIO()
48+
.start()
49+
.waitFor();
50+
builder.command("git", "push")
51+
.directory(tempDir)
52+
.inheritIO()
53+
.start()
54+
.waitFor();
55+
} finally {
56+
// Delete the temporary directory
57+
if (tempDir != null) {
58+
deleteDirectory(tempDir);
59+
}
60+
}
61+
}
62+
63+
// Copy the source directory to the destination directory
64+
private void copyDirectory(File source, File dest) throws IOException {
65+
if (source.isDirectory()) {
66+
if (!dest.exists()) {
67+
dest.mkdir();
68+
}
69+
for (String child : source.list()) {
70+
copyDirectory(new File(source, child), new File(dest, child));
71+
}
72+
} else {
73+
Files.copy(source.toPath(), dest.toPath(), StandardCopyOption.REPLACE_EXISTING);
74+
}
75+
}
76+
77+
// Delete a directory recursively
78+
private void deleteDirectory(File directoryToBeDeleted) {
79+
File[] allContents = directoryToBeDeleted.listFiles();
80+
if (allContents != null) {
81+
for (File file : allContents) {
82+
deleteDirectory(file);
83+
}
84+
}
85+
directoryToBeDeleted.delete();
86+
}
87+
88+
public static void main(String[] args) {
89+
GithubPagesPublisher publisher = new GithubPagesPublisher();
90+
try {
91+
publisher.publishToGithubPages(new File("sourceDirectory"), "https://github.com/user/repo.git", "gh-pages", "destPath");
92+
} catch (IOException | InterruptedException e) {
93+
e.printStackTrace();
94+
}
95+
}
96+
}

cli/src/main/java/ca/weblite/jdeploy/services/GithubService.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
import ca.weblite.jdeploy.helpers.PackageInfoBuilder;
44

5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.util.Scanner;
8+
59
public class GithubService {
610

711
private String token;
@@ -13,5 +17,15 @@ public PackageInfoBuilder createPackageInfoBuilder(String source) {
1317
return builder;
1418
}
1519

20+
public String getRepoURL(File repoDir) throws IOException {
21+
ProcessBuilder builder = new ProcessBuilder();
22+
builder.command("git", "config", "--get", "remote.origin.url")
23+
.directory(repoDir);
24+
Process process = builder.start();
25+
Scanner scanner = new Scanner(process.getInputStream());
26+
String url = scanner.nextLine();
27+
scanner.close();
28+
return url;
29+
}
1630

1731
}

0 commit comments

Comments
 (0)