-
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.
Improved cheerpj publish to github pages action
- Loading branch information
Showing
3 changed files
with
123 additions
and
80 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
96 changes: 96 additions & 0 deletions
96
cli/src/main/java/ca/weblite/jdeploy/services/GithubPagesPublisher.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,96 @@ | ||
package ca.weblite.jdeploy.services; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.*; | ||
import java.util.Scanner; | ||
|
||
public class GithubPagesPublisher { | ||
private GithubService githubService = new GithubService(); | ||
/** | ||
* Publishes the contents of a directory to a github pages branch. | ||
* | ||
* @param sourceDirectory The directory containing the files to publish. | ||
* @param repoUrl The URL of the github repository | ||
* @param branchName The branch-name to publish to | ||
* @param destPath The destination path within the repo where the files should be published. | ||
* @throws IOException, InterruptedException | ||
*/ | ||
public void publishToGithubPages(File sourceDirectory, String repoUrl, String branchName, String destPath) throws IOException, InterruptedException { | ||
if (repoUrl == null) { | ||
repoUrl = githubService.getRepoURL(sourceDirectory); | ||
} | ||
File tempDir = null; | ||
try { | ||
// Create a temporary directory | ||
tempDir = Files.createTempDirectory("tempRepoDir").toFile(); | ||
|
||
// Clone the repository to a temporary directory | ||
ProcessBuilder builder = new ProcessBuilder(); | ||
builder.command("git", "clone", "--depth", "1", "-b", branchName, repoUrl, tempDir.getAbsolutePath()) | ||
.inheritIO() | ||
.start() | ||
.waitFor(); | ||
|
||
// Copy the contents of sourceDirectory to the temporary directory at destPath | ||
File destDir = new File(tempDir, destPath); | ||
copyDirectory(sourceDirectory, destDir); | ||
|
||
// Add, commit and push the changes | ||
builder.command("git", "add", ".") | ||
.directory(tempDir) | ||
.inheritIO() | ||
.start() | ||
.waitFor(); | ||
builder.command("git", "commit", "-m", "Update GitHub Pages") | ||
.directory(tempDir) | ||
.inheritIO() | ||
.start() | ||
.waitFor(); | ||
builder.command("git", "push") | ||
.directory(tempDir) | ||
.inheritIO() | ||
.start() | ||
.waitFor(); | ||
} finally { | ||
// Delete the temporary directory | ||
if (tempDir != null) { | ||
deleteDirectory(tempDir); | ||
} | ||
} | ||
} | ||
|
||
// Copy the source directory to the destination directory | ||
private void copyDirectory(File source, File dest) throws IOException { | ||
if (source.isDirectory()) { | ||
if (!dest.exists()) { | ||
dest.mkdir(); | ||
} | ||
for (String child : source.list()) { | ||
copyDirectory(new File(source, child), new File(dest, child)); | ||
} | ||
} else { | ||
Files.copy(source.toPath(), dest.toPath(), StandardCopyOption.REPLACE_EXISTING); | ||
} | ||
} | ||
|
||
// Delete a directory recursively | ||
private void deleteDirectory(File directoryToBeDeleted) { | ||
File[] allContents = directoryToBeDeleted.listFiles(); | ||
if (allContents != null) { | ||
for (File file : allContents) { | ||
deleteDirectory(file); | ||
} | ||
} | ||
directoryToBeDeleted.delete(); | ||
} | ||
|
||
public static void main(String[] args) { | ||
GithubPagesPublisher publisher = new GithubPagesPublisher(); | ||
try { | ||
publisher.publishToGithubPages(new File("sourceDirectory"), "https://github.com/user/repo.git", "gh-pages", "destPath"); | ||
} catch (IOException | InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
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