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,183 @@ 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
+ System .out .println (new String ("You can find the source online at: " + gist .getHtmlUrl ()));
2387
+ foundMatchingGist =true ;
2388
+ }
2389
+ }
2390
+ }
2391
+ if (foundMatchingGist ==false ){ //if no gist exists for the board
2392
+ gist = new Gist ().setDescription (new String ("The file that is currently on an " +base .getCurrentBoard () + " with a serial number of " +serialNumber ));
2393
+ gist .setPublic (true ); //this should be an option in the future, but keep in mind these cannot be edited
2394
+ file .setContent (sketch .getCurrentCode ().getProgram ());
2395
+ gist .setFiles (Collections .singletonMap (new String (sketch .getCurrentCode ().getPrettyName ()+".pde" ), file ));
2396
+ gist = service .createGist (gist );
2397
+ System .out .println (new String ("You can find the source online at: " + gist .getHtmlUrl ()));
2398
+
2399
+ }
2400
+ }catch (IOException e ){
2401
+ System .out .println (e .getMessage ());
2402
+ }
2403
+ }
2404
+ }
2405
+
2406
+ class DefaultRetrieveFromGitHubHandler implements Runnable {
2407
+ public void run () {
2408
+ String serialNumber ;
2409
+ try {
2410
+ int timeout = 2000 ;
2411
+ InetAddress address = InetAddress .getByName ("api.github.com" );
2412
+ if (address .isReachable (timeout )){
2413
+ serialNumber =findSerialNumber ();
2414
+ if (!serialNumber .isEmpty ()){
2415
+ retrieveFromGitHub (serialNumber );
2416
+ }else {
2417
+ System .out .println ("Could not find your board, make sure it's plugged into USB." );
2418
+ statusNotice ("" );
2419
+ }
2420
+ }else {
2421
+ System .out .println ("github service is unavailable, cannot retrieve source." );
2422
+ statusNotice ("" );
2423
+ }
2424
+ } catch (Exception e ) {
2425
+ System .out .println ("You are not connected to the internet, cannot retrieve source." );
2426
+ statusNotice ("" );
2427
+ }
2428
+ toolbar .deactivate (EditorToolbar .RETRIEVE );
2429
+ }
2430
+
2431
+ private void retrieveFromGitHub (String serialNumber ) {
2432
+ GitHubClient client = new GitHubClient ().setCredentials ("arduinoboard" , "1knowmysource" );
2433
+ GistService service = new GistService (client );
2434
+ GistFile file = new GistFile ();
2435
+ Gist gist = new Gist ();
2436
+
2437
+ try {
2438
+ List <Gist > gists = service .getGists ("arduinoboard" );
2439
+ Boolean foundMatchingGist =false ;
2440
+ //for (Gist gist : gists) {
2441
+ for (int i = gists .size (); --i >= 0 ;){ //backwards so the first one found is the oldest one
2442
+ gist = (Gist )gists .get (i );
2443
+ if (gist .getDescription ().contains (serialNumber )){ //found the last matching gist
2444
+ if (foundMatchingGist ==true ){ //if one has already been found then an extra was made in error and needs to be cleaned up
2445
+ //delete the spurious gist
2446
+ service .deleteGist (gist .getId ());
2447
+ }else {
2448
+ statusNotice ("Found Source" );
2449
+ gist =service .getGist (gist .getId ());//get it again because the other capture only gets the meta-data
2450
+ setText (gist .getFiles ().get (gist .getFiles ().keySet ().iterator ().next ()).getContent ()); //gets the first sketch, puts it in the window
2451
+ foundMatchingGist =true ;
2452
+ }
2453
+ }
2454
+ }
2455
+ if (foundMatchingGist ==false ){ //if no gist exists for the board
2456
+ System .out .println ("No source was found for this board." );
2457
+ statusNotice ("" );
2458
+ }
2459
+ }catch (IOException e ){
2460
+ System .out .println (e .getMessage ());
2461
+ statusNotice ("" );
2462
+ }
2463
+ }
2464
+ }
2282
2465
2466
+
2467
+
2283
2468
// DAM: in Arduino, this is upload
2284
2469
class DefaultExportHandler implements Runnable {
2285
2470
public void run () {
@@ -2292,7 +2477,23 @@ public void run() {
2292
2477
2293
2478
boolean success = sketch .exportApplet (false );
2294
2479
if (success ) {
2295
- statusNotice ("Done uploading." );
2480
+ //this is where the github code starts
2481
+ try {
2482
+ int timeout = 2000 ;
2483
+ InetAddress address = InetAddress .getByName ("api.github.com" );
2484
+ if (address .isReachable (timeout )){
2485
+ statusNotice ("Done uploading, sending source to github..." );
2486
+ new Thread (sendToGitHubHandler ).start ();
2487
+ }else {
2488
+ statusNotice ("Done uploading." );
2489
+ System .out .println ("github service is unavailable, source will not be sent." );
2490
+ System .out .println ("Make sure to save locally!" );
2491
+ }
2492
+ } catch (Exception e ) {
2493
+ statusNotice ("Done uploading." );
2494
+ System .out .println ("You are not connected to the internet, source will not be sent." );
2495
+ System .out .println ("Make sure to save locally!" );
2496
+ }
2296
2497
} else {
2297
2498
// error message will already be visible
2298
2499
}
0 commit comments