From df39cb4b09953536ed32667f23560b56ac3f11a4 Mon Sep 17 00:00:00 2001 From: Doug Schaefer Date: Mon, 20 Jul 2015 09:36:12 -0400 Subject: [PATCH] Start of using Arduino package index for board packages. Added dependency to Gson for JSON parsing and Apache Http Client for getting the index files from the Arduino site. Change-Id: I3d265c1037e7f74e082f36c751f5eaf780e96684 --- releng/org.eclipse.cdt.target/cdt-e4.5.target | 3 ++ .../org.eclipse.cdt.arduino.core.tests/.classpath | 7 +++ .../org.eclipse.cdt.arduino.core.tests/.gitignore | 1 + .../org.eclipse.cdt.arduino.core.tests/.project | 28 +++++++++++ .../.settings/org.eclipse.jdt.core.prefs | 7 +++ .../META-INF/MANIFEST.MF | 8 ++++ .../build.properties | 4 ++ .../cdt/arduino/core/tests/BoardManagerTests.java | 27 +++++++++++ .../META-INF/MANIFEST.MF | 5 +- .../cdt/arduino/core/IArduinoBoardManager.java | 14 ++++++ .../cdt/arduino/core/internal/Activator.java | 2 +- .../{remote => board}/ArduinoBoardManager.java | 56 +++++++++++++++++++++- .../arduino/core/internal/board/PackageIndex.java | 12 +++++ 13 files changed, 171 insertions(+), 3 deletions(-) create mode 100644 toolchains/arduino/org.eclipse.cdt.arduino.core.tests/.classpath create mode 100644 toolchains/arduino/org.eclipse.cdt.arduino.core.tests/.gitignore create mode 100644 toolchains/arduino/org.eclipse.cdt.arduino.core.tests/.project create mode 100644 toolchains/arduino/org.eclipse.cdt.arduino.core.tests/.settings/org.eclipse.jdt.core.prefs create mode 100644 toolchains/arduino/org.eclipse.cdt.arduino.core.tests/META-INF/MANIFEST.MF create mode 100644 toolchains/arduino/org.eclipse.cdt.arduino.core.tests/build.properties create mode 100644 toolchains/arduino/org.eclipse.cdt.arduino.core.tests/src/org/eclipse/cdt/arduino/core/tests/BoardManagerTests.java rename toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/{remote => board}/ArduinoBoardManager.java (52%) create mode 100644 toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/board/PackageIndex.java diff --git a/releng/org.eclipse.cdt.target/cdt-e4.5.target b/releng/org.eclipse.cdt.target/cdt-e4.5.target index c951ce1..18cd732 100644 --- a/releng/org.eclipse.cdt.target/cdt-e4.5.target +++ b/releng/org.eclipse.cdt.target/cdt-e4.5.target @@ -10,6 +10,9 @@ + + + diff --git a/toolchains/arduino/org.eclipse.cdt.arduino.core.tests/.classpath b/toolchains/arduino/org.eclipse.cdt.arduino.core.tests/.classpath new file mode 100644 index 0000000..eca7bdb --- /dev/null +++ b/toolchains/arduino/org.eclipse.cdt.arduino.core.tests/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/toolchains/arduino/org.eclipse.cdt.arduino.core.tests/.gitignore b/toolchains/arduino/org.eclipse.cdt.arduino.core.tests/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/toolchains/arduino/org.eclipse.cdt.arduino.core.tests/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/toolchains/arduino/org.eclipse.cdt.arduino.core.tests/.project b/toolchains/arduino/org.eclipse.cdt.arduino.core.tests/.project new file mode 100644 index 0000000..c1028c6 --- /dev/null +++ b/toolchains/arduino/org.eclipse.cdt.arduino.core.tests/.project @@ -0,0 +1,28 @@ + + + org.eclipse.cdt.arduino.core.tests + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.pde.ManifestBuilder + + + + + org.eclipse.pde.SchemaBuilder + + + + + + org.eclipse.pde.PluginNature + org.eclipse.jdt.core.javanature + + diff --git a/toolchains/arduino/org.eclipse.cdt.arduino.core.tests/.settings/org.eclipse.jdt.core.prefs b/toolchains/arduino/org.eclipse.cdt.arduino.core.tests/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..0c68a61 --- /dev/null +++ b/toolchains/arduino/org.eclipse.cdt.arduino.core.tests/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,7 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/toolchains/arduino/org.eclipse.cdt.arduino.core.tests/META-INF/MANIFEST.MF b/toolchains/arduino/org.eclipse.cdt.arduino.core.tests/META-INF/MANIFEST.MF new file mode 100644 index 0000000..e5eb180 --- /dev/null +++ b/toolchains/arduino/org.eclipse.cdt.arduino.core.tests/META-INF/MANIFEST.MF @@ -0,0 +1,8 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: Tests +Bundle-SymbolicName: org.eclipse.cdt.arduino.core.tests +Bundle-Version: 1.0.0.qualifier +Fragment-Host: org.eclipse.cdt.arduino.core;bundle-version="1.0.0" +Bundle-RequiredExecutionEnvironment: JavaSE-1.8 +Require-Bundle: org.junit;bundle-version="4.12.0" diff --git a/toolchains/arduino/org.eclipse.cdt.arduino.core.tests/build.properties b/toolchains/arduino/org.eclipse.cdt.arduino.core.tests/build.properties new file mode 100644 index 0000000..34d2e4d --- /dev/null +++ b/toolchains/arduino/org.eclipse.cdt.arduino.core.tests/build.properties @@ -0,0 +1,4 @@ +source.. = src/ +output.. = bin/ +bin.includes = META-INF/,\ + . diff --git a/toolchains/arduino/org.eclipse.cdt.arduino.core.tests/src/org/eclipse/cdt/arduino/core/tests/BoardManagerTests.java b/toolchains/arduino/org.eclipse.cdt.arduino.core.tests/src/org/eclipse/cdt/arduino/core/tests/BoardManagerTests.java new file mode 100644 index 0000000..58f2dc1 --- /dev/null +++ b/toolchains/arduino/org.eclipse.cdt.arduino.core.tests/src/org/eclipse/cdt/arduino/core/tests/BoardManagerTests.java @@ -0,0 +1,27 @@ +package org.eclipse.cdt.arduino.core.tests; + +import static org.junit.Assert.assertNotNull; + +import java.util.concurrent.Semaphore; + +import org.eclipse.cdt.arduino.core.IArduinoBoardManager.Handler; +import org.eclipse.cdt.arduino.core.internal.board.ArduinoBoardManager; +import org.eclipse.cdt.arduino.core.internal.board.PackageIndex; +import org.junit.Test; + +public class BoardManagerTests { + + @Test + public void loadPackagesTest() throws Exception { + Semaphore semaphore = new Semaphore(0); + new ArduinoBoardManager().getPackageIndex(new Handler() { + @Override + public void handle(PackageIndex result) { + assertNotNull(result); + semaphore.release(); + } + }); + semaphore.acquire(); + } + +} diff --git a/toolchains/arduino/org.eclipse.cdt.arduino.core/META-INF/MANIFEST.MF b/toolchains/arduino/org.eclipse.cdt.arduino.core/META-INF/MANIFEST.MF index 74ddfb8..fae3615 100644 --- a/toolchains/arduino/org.eclipse.cdt.arduino.core/META-INF/MANIFEST.MF +++ b/toolchains/arduino/org.eclipse.cdt.arduino.core/META-INF/MANIFEST.MF @@ -14,7 +14,10 @@ Require-Bundle: org.eclipse.core.runtime, org.eclipse.launchbar.core, org.eclipse.remote.core;bundle-version="2.0.0", org.eclipse.cdt.native.serial;bundle-version="1.0.0", - org.eclipse.remote.serial.core;bundle-version="1.0.0" + org.eclipse.remote.serial.core;bundle-version="1.0.0", + com.google.gson;bundle-version="2.2.4", + org.apache.httpcomponents.httpclient;bundle-version="4.3.6", + org.apache.httpcomponents.httpcore;bundle-version="4.3.3" Bundle-RequiredExecutionEnvironment: JavaSE-1.7 Bundle-ActivationPolicy: lazy Bundle-ClassPath: libs/freemarker-2.3.22.jar, diff --git a/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/IArduinoBoardManager.java b/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/IArduinoBoardManager.java index 8930f73..ecba578 100644 --- a/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/IArduinoBoardManager.java +++ b/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/IArduinoBoardManager.java @@ -12,8 +12,22 @@ package org.eclipse.cdt.arduino.core; import java.util.Collection; +/** + * Interface into the board package data. + */ public interface IArduinoBoardManager { + /** + * Many of the calls into the board manager require reaching out to the web. + * In order to not block UI, these calls are asynchronous. This handler + * interface is how the results of the call are returned. + * + * @param + */ + public interface Handler { + void handle(T result); + } + Board getBoard(String id); Collection getBoards(); diff --git a/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/Activator.java b/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/Activator.java index 1dbe641..9fadccb 100644 --- a/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/Activator.java +++ b/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/Activator.java @@ -11,7 +11,7 @@ package org.eclipse.cdt.arduino.core.internal; import org.eclipse.cdt.arduino.core.IArduinoBoardManager; -import org.eclipse.cdt.arduino.core.internal.remote.ArduinoBoardManager; +import org.eclipse.cdt.arduino.core.internal.board.ArduinoBoardManager; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Plugin; diff --git a/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/remote/ArduinoBoardManager.java b/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/board/ArduinoBoardManager.java similarity index 52% rename from toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/remote/ArduinoBoardManager.java rename to toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/board/ArduinoBoardManager.java index 7806fc2..b36dbf7 100644 --- a/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/remote/ArduinoBoardManager.java +++ b/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/board/ArduinoBoardManager.java @@ -8,11 +8,16 @@ * Contributors: * QNX Software Systems - Initial API and implementation *******************************************************************************/ -package org.eclipse.cdt.arduino.core.internal.remote; +package org.eclipse.cdt.arduino.core.internal.board; import java.io.File; import java.io.FileInputStream; +import java.io.FileReader; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -23,14 +28,63 @@ import java.util.List; import java.util.Map; import java.util.Properties; +import org.apache.http.HttpEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; import org.eclipse.cdt.arduino.core.ArduinoHome; import org.eclipse.cdt.arduino.core.Board; import org.eclipse.cdt.arduino.core.IArduinoBoardManager; +import org.eclipse.cdt.arduino.core.internal.Activator; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.jobs.Job; + +import com.google.gson.Gson; public class ArduinoBoardManager implements IArduinoBoardManager { private Map boards; + // TODO make this a preference + private Path arduinoHome = Paths.get(System.getProperty("user.home"), ".arduinocdt"); //$NON-NLS-1$ //$NON-NLS-2$ + + public void getPackageIndex(final Handler handler) { + new Job("Fetching package index") { + // Closeable isn't API yet but it's recommended. + @SuppressWarnings("restriction") + protected IStatus run(IProgressMonitor monitor) { + try (CloseableHttpClient client = HttpClients.createDefault()) { + HttpGet get = new HttpGet("http://downloads.arduino.cc/packages/package_index.json"); //$NON-NLS-1$ + try (CloseableHttpResponse response = client.execute(get)) { + if (response.getStatusLine().getStatusCode() >= 400) { + return new Status(IStatus.ERROR, Activator.getId(), + response.getStatusLine().getReasonPhrase()); + } else { + HttpEntity entity = response.getEntity(); + if (entity == null) { + return new Status(IStatus.ERROR, Activator.getId(), + "Package index missing from response"); + } + Files.createDirectories(arduinoHome); + Path indexPath = arduinoHome.resolve("package_index.json"); //$NON-NLS-1$ + Files.copy(entity.getContent(), indexPath, StandardCopyOption.REPLACE_EXISTING); + try (FileReader reader = new FileReader(indexPath.toFile())) { + PackageIndex index = new Gson().fromJson(reader, PackageIndex.class); + handler.handle(index); + } + } + } + } catch (IOException e) { + return new Status(IStatus.ERROR, Activator.getId(), e.getLocalizedMessage(), e); + } + return Status.OK_STATUS; + } + }.schedule(); + } + @Override public Board getBoard(String id) { init(); diff --git a/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/board/PackageIndex.java b/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/board/PackageIndex.java new file mode 100644 index 0000000..cdce67d --- /dev/null +++ b/toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/board/PackageIndex.java @@ -0,0 +1,12 @@ +/******************************************************************************* + * Copyright (c) 2015 QNX Software Systems and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + *******************************************************************************/ +package org.eclipse.cdt.arduino.core.internal.board; + +public class PackageIndex { + +} -- 2.7.4