1
1
package ca .weblite .jdeploy .services ;
2
2
3
3
import ca .weblite .jdeploy .cheerpj .services .BuildCheerpjAppService ;
4
- import ca .weblite .tools .io .FileUtil ;
5
4
import org .json .JSONObject ;
6
-
7
5
import java .io .*;
8
- import java .util .Arrays ;
9
- import java .util .Scanner ;
10
6
11
7
public class CheerpjService extends BaseService {
12
8
private BuildCheerpjAppService buildCheerpjAppService ;
13
9
14
10
public CheerpjService (File packageJSONFile , JSONObject packageJSON ) throws IOException {
15
11
super (packageJSONFile , packageJSON );
16
12
buildCheerpjAppService = new BuildCheerpjAppService ();
17
-
18
13
}
19
14
20
-
21
15
public boolean isEnabled () {
22
16
return getJDeployObject ().has ("cheerpj" );
23
17
}
@@ -138,10 +132,16 @@ private String getGithubPagesPublishPath() throws IOException, InterruptedExcept
138
132
}
139
133
140
134
public boolean isGithubPagesEnabled () {
135
+ if (!getGithubPagesConfig ().has ("enabled" )) {
136
+ return false ;
137
+ }
141
138
return getGithubPagesConfig ().getBoolean ("enabled" );
142
139
}
143
140
144
141
private String getGithubPagesBranch () {
142
+ if (!getGithubPagesConfig ().has ("branch" )) {
143
+ return null ;
144
+ }
145
145
return getGithubPagesConfig ().getString ("branch" );
146
146
}
147
147
@@ -161,20 +161,6 @@ private String getCurrentGitBranch() throws IOException, InterruptedException {
161
161
return branchName ;
162
162
}
163
163
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
-
178
164
public static void generateGitignoreFile (String directoryPath ) throws IOException {
179
165
File gitignore = new File (directoryPath , ".gitignore" );
180
166
try (BufferedWriter writer = new BufferedWriter (new FileWriter (gitignore ))) {
@@ -186,70 +172,17 @@ public static void generateGitignoreFile(String directoryPath) throws IOExceptio
186
172
// Do not ignore directories, so we can traverse them
187
173
writer .write ("\n " );
188
174
}
189
- System .out .println (".gitignore file created at: " + gitignore .getAbsolutePath ());
190
175
}
191
176
192
-
193
177
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 ;
199
181
}
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 ());
252
186
253
187
}
254
-
255
188
}
0 commit comments