11package ca .weblite .jdeploy .services ;
22
33import ca .weblite .jdeploy .cheerpj .services .BuildCheerpjAppService ;
4- import ca .weblite .tools .io .FileUtil ;
54import org .json .JSONObject ;
6-
75import java .io .*;
8- import java .util .Arrays ;
9- import java .util .Scanner ;
106
117public 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}
0 commit comments