34
34
import java .io .*;
35
35
import java .net .*;
36
36
import java .util .*;
37
+ import java .util .List ;
37
38
import java .util .zip .*;
38
39
39
40
import javax .swing .*;
40
41
import javax .swing .event .*;
41
42
import javax .swing .text .*;
42
43
import javax .swing .undo .*;
43
44
45
+ import org .eclipse .egit .github .core .Gist ;
46
+ import org .eclipse .egit .github .core .GistFile ;
47
+ import org .eclipse .egit .github .core .client .GitHubClient ;
48
+ import org .eclipse .egit .github .core .service .GistService ;
49
+
44
50
import gnu .io .*;
45
51
46
52
/**
@@ -145,6 +151,8 @@ public class Editor extends JFrame implements RunnerListener {
145
151
Runnable stopHandler ;
146
152
Runnable exportHandler ;
147
153
Runnable exportAppHandler ;
154
+ Runnable sendToGitHubHandler ;
155
+ Runnable retrieveFromGitHubHandler ;
148
156
149
157
150
158
public Editor (Base ibase , String path , int [] location ) {
@@ -1362,13 +1370,16 @@ protected void updateRedoState() {
1362
1370
1363
1371
1364
1372
public void setHandlers (Runnable runHandler , Runnable presentHandler ,
1365
- Runnable stopHandler ,
1366
- Runnable exportHandler , Runnable exportAppHandler ) {
1373
+ Runnable stopHandler , Runnable exportHandler ,
1374
+ Runnable exportAppHandler , Runnable sendToGitHubHandler ,
1375
+ Runnable retrieveFromGitHubHandler ) {
1367
1376
this .runHandler = runHandler ;
1368
1377
this .presentHandler = presentHandler ;
1369
1378
this .stopHandler = stopHandler ;
1370
1379
this .exportHandler = exportHandler ;
1371
1380
this .exportAppHandler = exportAppHandler ;
1381
+ this .sendToGitHubHandler = sendToGitHubHandler ;
1382
+ this .retrieveFromGitHubHandler = retrieveFromGitHubHandler ;
1372
1383
}
1373
1384
1374
1385
@@ -1378,6 +1389,8 @@ public void resetHandlers() {
1378
1389
stopHandler = new DefaultStopHandler ();
1379
1390
exportHandler = new DefaultExportHandler ();
1380
1391
exportAppHandler = new DefaultExportAppHandler ();
1392
+ sendToGitHubHandler = new DefaultSendToGitHubHandler ();
1393
+ retrieveFromGitHubHandler = new DefaultRetrieveFromGitHubHandler ();
1381
1394
}
1382
1395
1383
1396
@@ -2275,11 +2288,182 @@ synchronized public void handleExport(final boolean verbose) {
2275
2288
//if (!handleExportCheckModified()) return;
2276
2289
toolbar .activate (EditorToolbar .EXPORT );
2277
2290
console .clear ();
2291
+
2278
2292
statusNotice ("Uploading to I/O Board..." );
2279
-
2280
2293
new Thread (verbose ? exportAppHandler : exportHandler ).start ();
2281
2294
}
2295
+
2296
+ // DV: this is the added code for sending to a github gist
2297
+
2298
+ synchronized public void handleRetrieve () {
2299
+ toolbar .activate (EditorToolbar .RETRIEVE );
2300
+ console .clear ();
2301
+
2302
+ statusNotice ("Retrieving Source" );
2303
+ new Thread (retrieveFromGitHubHandler ).start ();
2304
+ }
2305
+
2306
+
2307
+ public String findSerialNumber () {
2308
+ if (Base .isMacOS ()) {
2309
+ String getUsbArgs [] = new String [2 ];
2310
+ getUsbArgs [0 ]="system_profiler" ;
2311
+ getUsbArgs [1 ]="SPUSBDataType" ;
2312
+ try {
2313
+ Process process = new ProcessBuilder (getUsbArgs ).start ();
2314
+ InputStream is = process .getInputStream ();
2315
+ InputStreamReader isr = new InputStreamReader (is );
2316
+ BufferedReader br = new BufferedReader (isr );
2317
+ String line ;
2318
+
2319
+ boolean foundArduino =false ;
2320
+ boolean foundSerial =false ;
2321
+ int serialNumPosition ;
2322
+ while ((line = br .readLine ()) != null && !foundSerial ) {
2323
+ if (line .indexOf ("Arduino" ) > 0 || line .indexOf ("FT232R" ) > 0 ){
2324
+ foundArduino =true ;
2325
+ }
2326
+ if (foundArduino ){
2327
+ serialNumPosition = line .indexOf ("Serial Number" );
2328
+ if (serialNumPosition > 0 ){
2329
+ foundSerial =true ;
2330
+ return line .substring ((serialNumPosition +15 ));
2331
+ }
2332
+ }
2333
+ }
2334
+ if (foundSerial ==false ){
2335
+ return "" ;
2336
+ }
2337
+ }
2338
+ catch (IOException e ){
2339
+ System .out .println (e .getMessage ());
2340
+ }
2341
+ }
2342
+ return "" ;
2343
+ }
2344
+
2345
+ class DefaultSendToGitHubHandler implements Runnable {
2346
+ public void run () {
2347
+ String serialNumber =findSerialNumber ();
2348
+ if (!serialNumber .isEmpty ()){
2349
+ uploadToGitHub (serialNumber );
2350
+ statusNotice ("Source sent to github" );
2351
+ }else {
2352
+ System .out .println ("Could not find your board, make sure it's plugged into USB." );
2353
+ statusNotice ("" );
2354
+ }
2355
+ }
2356
+
2357
+ private void uploadToGitHub (String serialNumber ) {
2358
+ GitHubClient client = new GitHubClient ().setCredentials ("arduinoboard" , "1knowmysource" );
2359
+ GistService service = new GistService (client );
2360
+ GistFile file = new GistFile ();
2361
+ Gist gist = new Gist ();
2362
+
2363
+ try {
2364
+ List <Gist > gists = service .getGists ("arduinoboard" );
2365
+ Boolean foundMatchingGist =false ;
2366
+ //for (Gist gist : gists) {
2367
+ for (int i = gists .size (); --i >= 0 ;){ //backwards so the first one found is the oldest one
2368
+ gist = (Gist )gists .get (i );
2369
+ if (gist .getDescription ().contains (serialNumber )){ //found the last matching gist
2370
+ if (foundMatchingGist ==true ){ //if one has already been found then an extra was made in error and needs to be cleaned up
2371
+ //delete the spurious gist
2372
+ service .deleteGist (gist .getId ());
2373
+ }else {
2374
+ //edit the current gist
2375
+ file .setContent (sketch .getCurrentCode ().getProgram ());
2376
+ String filename = new String (sketch .getCurrentCode ().getPrettyName ()+".pde" );
2377
+ //remove old files
2378
+ for (String key : gist .getFiles ().keySet ()) {
2379
+ if (!key .equals (filename )){
2380
+ service .updateGist (gist .setFiles (Collections .singletonMap (key , new GistFile ())));
2381
+ }
2382
+ }
2383
+
2384
+ gist .setFiles (Collections .singletonMap (filename , file ));
2385
+ service .updateGist (gist );
2386
+ foundMatchingGist =true ;
2387
+ }
2388
+ }
2389
+ }
2390
+ if (foundMatchingGist ==false ){ //if no gist exists for the board
2391
+ gist = new Gist ().setDescription (new String ("The file that is currently on an " +base .getCurrentBoard () + " with a serial number of " +serialNumber ));
2392
+ gist .setPublic (true ); //this should be an option in the future, but keep in mind these cannot be edited
2393
+ file .setContent (sketch .getCurrentCode ().getProgram ());
2394
+ gist .setFiles (Collections .singletonMap (new String (sketch .getCurrentCode ().getPrettyName ()+".pde" ), file ));
2395
+ gist = service .createGist (gist );
2396
+
2397
+ }
2398
+ System .out .println (new String ("You can find the source online at: " + gist .getHtmlUrl ()));
2399
+ }catch (IOException e ){
2400
+ System .out .println (e .getMessage ());
2401
+ }
2402
+ }
2403
+ }
2404
+
2405
+ class DefaultRetrieveFromGitHubHandler implements Runnable {
2406
+ public void run () {
2407
+ String serialNumber ;
2408
+ try {
2409
+ int timeout = 2000 ;
2410
+ InetAddress address = InetAddress .getByName ("api.github.com" );
2411
+ if (address .isReachable (timeout )){
2412
+ serialNumber =findSerialNumber ();
2413
+ if (!serialNumber .isEmpty ()){
2414
+ retrieveFromGitHub (serialNumber );
2415
+ }else {
2416
+ System .out .println ("Could not find your board, make sure it's plugged into USB." );
2417
+ statusNotice ("" );
2418
+ }
2419
+ }else {
2420
+ System .out .println ("github service is unavailable, cannot retrieve source." );
2421
+ statusNotice ("" );
2422
+ }
2423
+ } catch (Exception e ) {
2424
+ System .out .println ("You are not connected to the internet, cannot retrieve source." );
2425
+ statusNotice ("" );
2426
+ }
2427
+ toolbar .deactivate (EditorToolbar .RETRIEVE );
2428
+ }
2429
+
2430
+ private void retrieveFromGitHub (String serialNumber ) {
2431
+ GitHubClient client = new GitHubClient ().setCredentials ("arduinoboard" , "1knowmysource" );
2432
+ GistService service = new GistService (client );
2433
+ GistFile file = new GistFile ();
2434
+ Gist gist = new Gist ();
2435
+
2436
+ try {
2437
+ List <Gist > gists = service .getGists ("arduinoboard" );
2438
+ Boolean foundMatchingGist =false ;
2439
+ //for (Gist gist : gists) {
2440
+ for (int i = gists .size (); --i >= 0 ;){ //backwards so the first one found is the oldest one
2441
+ gist = (Gist )gists .get (i );
2442
+ if (gist .getDescription ().contains (serialNumber )){ //found the last matching gist
2443
+ if (foundMatchingGist ==true ){ //if one has already been found then an extra was made in error and needs to be cleaned up
2444
+ //delete the spurious gist
2445
+ service .deleteGist (gist .getId ());
2446
+ }else {
2447
+ statusNotice ("Found Source" );
2448
+ gist =service .getGist (gist .getId ());//get it again because the other capture only gets the meta-data
2449
+ setText (gist .getFiles ().get (gist .getFiles ().keySet ().iterator ().next ()).getContent ()); //gets the first sketch, puts it in the window
2450
+ foundMatchingGist =true ;
2451
+ }
2452
+ }
2453
+ }
2454
+ if (foundMatchingGist ==false ){ //if no gist exists for the board
2455
+ System .out .println ("No source was found for this board." );
2456
+ statusNotice ("" );
2457
+ }
2458
+ }catch (IOException e ){
2459
+ System .out .println (e .getMessage ());
2460
+ statusNotice ("" );
2461
+ }
2462
+ }
2463
+ }
2282
2464
2465
+
2466
+
2283
2467
// DAM: in Arduino, this is upload
2284
2468
class DefaultExportHandler implements Runnable {
2285
2469
public void run () {
@@ -2292,7 +2476,23 @@ public void run() {
2292
2476
2293
2477
boolean success = sketch .exportApplet (false );
2294
2478
if (success ) {
2295
- statusNotice ("Done uploading." );
2479
+ //this is where the github code starts
2480
+ try {
2481
+ int timeout = 2000 ;
2482
+ InetAddress address = InetAddress .getByName ("api.github.com" );
2483
+ if (address .isReachable (timeout )){
2484
+ statusNotice ("Done uploading, sending source to github..." );
2485
+ new Thread (sendToGitHubHandler ).start ();
2486
+ }else {
2487
+ statusNotice ("Done uploading." );
2488
+ System .out .println ("github service is unavailable, source will not be sent." );
2489
+ System .out .println ("Make sure to save locally!" );
2490
+ }
2491
+ } catch (Exception e ) {
2492
+ statusNotice ("Done uploading." );
2493
+ System .out .println ("You are not connected to the internet, source will not be sent." );
2494
+ System .out .println ("Make sure to save locally!" );
2495
+ }
2296
2496
} else {
2297
2497
// error message will already be visible
2298
2498
}
0 commit comments