1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-15 21:15:23 +02:00

Add support for multiple board packages suppliers.

Default URLs for Arduino and the ESP8266 community. Preference page
to allow user to add their own.

Change-Id: Ia40d2729d6b5e26225dbecf85a845322b4a71f5f
This commit is contained in:
Doug Schaefer 2015-09-01 22:39:10 -04:00
parent 0e382c064b
commit 40ff5f2a66
12 changed files with 188 additions and 141 deletions

View file

@ -1,6 +1,6 @@
package org.eclipse.cdt.arduino.core.tests;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotEquals;
import org.eclipse.cdt.arduino.core.internal.board.ArduinoManager;
import org.junit.Test;
@ -9,7 +9,7 @@ public class BoardManagerTests {
@Test
public void loadPackagesTest() throws Exception {
assertNotNull(ArduinoManager.instance.getPackageIndex());
assertNotEquals(0, ArduinoManager.instance.getPackageIndices().size());
}
}

View file

@ -10,19 +10,42 @@ package org.eclipse.cdt.arduino.core.internal;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.osgi.service.prefs.BackingStoreException;
public class ArduinoPreferences {
public static String ARDUINO_HOME = "arduinoHome"; //$NON-NLS-1$
private static final String ARDUINO_HOME = "arduinoHome"; //$NON-NLS-1$
private static final String BOARD_URLS = "boardUrls"; //$NON-NLS-1$
private static final String defaultHome = Paths.get(System.getProperty("user.home"), ".arduinocdt").toString(); //$NON-NLS-1$ //$NON-NLS-2$
private static final String defaultBoardUrls = "http://downloads.arduino.cc/packages/package_index.json" //$NON-NLS-1$
+ "\nhttp://arduino.esp8266.com/stable/package_esp8266com_index.json"; //$NON-NLS-1$
private static IEclipsePreferences getPrefs() {
return InstanceScope.INSTANCE.getNode(Activator.getId());
}
public static Path getArduinoHome() {
String pathStr = Platform.getPreferencesService().getString(Activator.getId(), ARDUINO_HOME, null, null);
return pathStr != null ? Paths.get(pathStr) : getDefaultArduinoHome();
return Paths.get(getPrefs().get(ARDUINO_HOME, defaultHome));
}
public static Path getDefaultArduinoHome() {
return Paths.get(System.getProperty("user.home"), ".arduinocdt"); //$NON-NLS-1$ //$NON-NLS-2$
public static String getBoardUrls() {
return getPrefs().get(BOARD_URLS, defaultBoardUrls);
}
public static void setBoardUrls(String boardUrls) {
IEclipsePreferences prefs = getPrefs();
prefs.put(BOARD_URLS, boardUrls);
try {
prefs.flush();
} catch (BackingStoreException e) {
Activator.log(e);
}
}
public static String getDefaultBoardUrls() {
return defaultBoardUrls;
}
}

View file

@ -16,10 +16,12 @@ import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.lang.reflect.Type;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.PosixFilePermission;
import java.util.ArrayList;
@ -65,69 +67,107 @@ public class ArduinoManager {
public static final String PACKAGE_OPTION_ID = "org.eclipse.cdt.arduino.option.package"; //$NON-NLS-1$
public static final String AVR_TOOLCHAIN_ID = "org.eclipse.cdt.arduino.toolChain.avr"; //$NON-NLS-1$
private Path packageIndexPath = ArduinoPreferences.getArduinoHome().resolve("package_index.json"); //$NON-NLS-1$
private PackageIndex packageIndex;
private Path libraryIndexPath = ArduinoPreferences.getArduinoHome().resolve("library_index.json"); //$NON-NLS-1$
public static final String LIBRARIES_URL = "http://downloads.arduino.cc/libraries/library_index.json"; //$NON-NLS-1$
private List<PackageIndex> packageIndices;
private LibraryIndex libraryIndex;
public ArduinoManager() {
public void loadIndices() {
new Job(Messages.ArduinoBoardManager_0) {
protected IStatus run(IProgressMonitor monitor) {
try {
// library index has the same parent right now
Files.createDirectories(packageIndexPath.getParent());
URL packageUrl = new URL("http://downloads.arduino.cc/packages/package_index.json"); //$NON-NLS-1$
try (InputStream in = packageUrl.openStream()) {
Files.copy(in, packageIndexPath, StandardCopyOption.REPLACE_EXISTING);
}
URL libraryUrl = new URL("http://downloads.arduino.cc/libraries/library_index.json"); //$NON-NLS-1$
try (InputStream in = libraryUrl.openStream()) {
Files.copy(in, libraryIndexPath, StandardCopyOption.REPLACE_EXISTING);
}
} catch (IOException e) {
return new Status(IStatus.ERROR, Activator.getId(), e.getLocalizedMessage(), e);
String[] boardUrls = ArduinoPreferences.getBoardUrls().split("\n"); //$NON-NLS-1$
packageIndices = new ArrayList<>(boardUrls.length);
for (String boardUrl : boardUrls) {
loadPackageIndex(boardUrl, true);
}
loadLibraryIndex(true);
return Status.OK_STATUS;
}
}.schedule();
}
public PackageIndex getPackageIndex() throws CoreException {
if (packageIndex == null) {
try (FileReader reader = new FileReader(packageIndexPath.toFile())) {
packageIndex = new Gson().fromJson(reader, PackageIndex.class);
packageIndex.setOwners(this);
} catch (IOException e) {
throw new CoreException(new Status(IStatus.ERROR, Activator.getId(), "Reading package index", e));
private void loadPackageIndex(String url, boolean download) {
try {
URL packageUrl = new URL(url.trim());
Path packagePath = ArduinoPreferences.getArduinoHome()
.resolve(Paths.get(packageUrl.getPath()).getFileName());
File packageFile = packagePath.toFile();
if (download) {
Files.copy(packageUrl.openStream(), packagePath, StandardCopyOption.REPLACE_EXISTING);
}
if (packageFile.exists()) {
try (Reader reader = new FileReader(packageFile)) {
PackageIndex index = new Gson().fromJson(reader, PackageIndex.class);
index.setOwners(ArduinoManager.this);
packageIndices.add(index);
}
}
} catch (IOException e) {
Activator.log(e);
}
}
public List<PackageIndex> getPackageIndices() throws CoreException {
if (packageIndices == null) {
String[] boardUrls = ArduinoPreferences.getBoardUrls().split("\n"); //$NON-NLS-1$
packageIndices = new ArrayList<>(boardUrls.length);
for (String boardUrl : boardUrls) {
loadPackageIndex(boardUrl, false);
}
}
return packageIndex;
return packageIndices;
}
private void loadLibraryIndex(boolean download) {
try {
URL librariesUrl = new URL(LIBRARIES_URL);
Path librariesPath = ArduinoPreferences.getArduinoHome()
.resolve(Paths.get(librariesUrl.getPath()).getFileName());
File librariesFile = librariesPath.toFile();
if (download) {
Files.copy(librariesUrl.openStream(), librariesPath, StandardCopyOption.REPLACE_EXISTING);
}
if (librariesFile.exists()) {
try (Reader reader = new FileReader(librariesFile)) {
libraryIndex = new Gson().fromJson(reader, LibraryIndex.class);
}
}
} catch (IOException e) {
Activator.log(e);
}
}
public LibraryIndex getLibraryIndex() throws CoreException {
if (libraryIndex == null) {
try (FileReader reader = new FileReader(libraryIndexPath.toFile())) {
libraryIndex = new Gson().fromJson(reader, LibraryIndex.class);
libraryIndex.resolve();
} catch (IOException e) {
throw new CoreException(new Status(IStatus.ERROR, Activator.getId(), "Reading library index", e));
}
loadLibraryIndex(false);
}
return libraryIndex;
}
public ArduinoBoard getBoard(String boardName, String platformName, String packageName) throws CoreException {
return getPackageIndex().getPackage(packageName).getPlatform(platformName).getBoard(boardName);
for (PackageIndex index : packageIndices) {
ArduinoPackage pkg = index.getPackage(packageName);
if (pkg != null) {
ArduinoPlatform platform = pkg.getPlatform(platformName);
if (platform != null) {
ArduinoBoard board = platform.getBoard(boardName);
if (board != null) {
return board;
}
}
}
}
return null;
}
public List<ArduinoBoard> getBoards() throws CoreException {
List<ArduinoBoard> boards = new ArrayList<>();
for (ArduinoPackage pkg : getPackageIndex().getPackages()) {
for (ArduinoPlatform platform : pkg.getLatestPlatforms()) {
boards.addAll(platform.getBoards());
for (PackageIndex index : packageIndices) {
for (ArduinoPackage pkg : index.getPackages()) {
for (ArduinoPlatform platform : pkg.getLatestPlatforms()) {
boards.addAll(platform.getBoards());
}
}
}
return boards;
@ -135,17 +175,37 @@ public class ArduinoManager {
public List<ArduinoBoard> getInstalledBoards() throws CoreException {
List<ArduinoBoard> boards = new ArrayList<>();
for (ArduinoPackage pkg : getPackageIndex().getPackages()) {
for (ArduinoPlatform platform : pkg.getInstalledPlatforms()) {
boards.addAll(platform.getBoards());
for (PackageIndex index : packageIndices) {
for (ArduinoPackage pkg : index.getPackages()) {
for (ArduinoPlatform platform : pkg.getInstalledPlatforms()) {
boards.addAll(platform.getBoards());
}
}
}
return boards;
}
public ArduinoPackage getPackage(String packageName) {
for (PackageIndex index : packageIndices) {
ArduinoPackage pkg = index.getPackage(packageName);
if (pkg != null) {
return pkg;
}
}
return null;
}
public ArduinoTool getTool(String packageName, String toolName, String version) {
ArduinoPackage pkg = packageIndex.getPackage(packageName);
return pkg != null ? pkg.getTool(toolName, version) : null;
for (PackageIndex index : packageIndices) {
ArduinoPackage pkg = index.getPackage(packageName);
if (pkg != null) {
ArduinoTool tool = pkg.getTool(toolName, version);
if (tool != null) {
return tool;
}
}
}
return null;
}
private static final String LIBRARIES = "libraries"; //$NON-NLS-1$
@ -178,7 +238,7 @@ public class ArduinoManager {
try {
settings.flush();
} catch (BackingStoreException e) {
throw new CoreException(new Status(IStatus.ERROR, Activator.getId(), "Saving preferences", e));
Activator.log(e);
}
new Job("Install libraries") {
@ -209,7 +269,7 @@ public class ArduinoManager {
IProgressMonitor monitor) {
try {
URL dl = new URL(url);
Path dlDir = ArduinoPreferences.getArduinoHome().resolve("downloads");
Path dlDir = ArduinoPreferences.getArduinoHome().resolve("downloads"); //$NON-NLS-1$
Files.createDirectories(dlDir);
Path archivePath = dlDir.resolve(archiveFileName);
Files.copy(dl.openStream(), archivePath, StandardCopyOption.REPLACE_EXISTING);

View file

@ -52,7 +52,7 @@ public class ArduinoToolSystem {
case Platform.OS_MACOSX:
switch (host) {
case "i386-apple-darwin11": //$NON-NLS-1$
case "x86_64-apple-darwin:": //$NON-NLS-1$
case "x86_64-apple-darwin": //$NON-NLS-1$
return true;
default:
return false;

View file

@ -40,7 +40,7 @@ public class ToolDependency {
public ArduinoTool getTool() throws CoreException {
ArduinoPackage pkg = platform.getPackage();
if (!pkg.getName().equals(packager)) {
pkg = pkg.getManager().getPackageIndex().getPackage(packager);
pkg = pkg.getManager().getPackage(packager);
}
return pkg.getTool(name, version);

View file

@ -90,10 +90,4 @@
name="Boards">
</page>
</extension>
<extension
point="org.eclipse.core.runtime.preferences">
<initializer
class="org.eclipse.cdt.arduino.ui.internal.preferences.ArduinoPreferenceInitializer">
</initializer>
</extension>
</plugin>

View file

@ -10,14 +10,12 @@
*******************************************************************************/
package org.eclipse.cdt.arduino.ui.internal;
import org.eclipse.cdt.arduino.core.internal.board.ArduinoManager;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.eclipse.ui.preferences.ScopedPreferenceStore;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
@ -26,8 +24,6 @@ import org.osgi.framework.ServiceReference;
*/
public class Activator extends AbstractUIPlugin {
private IPreferenceStore corePreferenceStore;
// The plug-in ID
public static final String PLUGIN_ID = "org.eclipse.cdt.arduino.ui"; //$NON-NLS-1$
@ -40,6 +36,8 @@ public class Activator extends AbstractUIPlugin {
public void start(BundleContext context) throws Exception {
super.start(context);
plugin = this;
// Load up the Arduino indices
ArduinoManager.instance.loadIndices();
}
public void stop(BundleContext context) throws Exception {
@ -86,11 +84,4 @@ public class Activator extends AbstractUIPlugin {
return ref != null ? context.getService(ref) : null;
}
public IPreferenceStore getCorePreferenceStore() {
if (corePreferenceStore == null) {
corePreferenceStore = new ScopedPreferenceStore(InstanceScope.INSTANCE, "org.eclipse.cdt.qrduino.core"); //$NON-NLS-1$
}
return corePreferenceStore;
}
}

View file

@ -14,8 +14,6 @@ public class Messages extends NLS {
public static String ArduinoLaunchConsole_0;
public static String ArduinoLaunchConsole_1;
public static String ArduinoLaunchConsole_2;
public static String ArduinoPreferencePage_0;
public static String ArduinoPreferencePage_1;
public static String ArduinoTargetPropertyPage_0;
public static String ArduinoTargetPropertyPage_1;
public static String ArduinoTargetPropertyPage_2;
@ -28,6 +26,7 @@ public class Messages extends NLS {
public static String NewArduinoTargetWizardPage_5;
public static String ArduinoBoardsPreferencePage_desc;
public static String LibrariesPropertyPage_desc;
public static String ArduinoPreferencePage_desc;
static {
// initialize resource bundle

View file

@ -8,8 +8,6 @@
ArduinoLaunchConsole_0=Arduino
ArduinoLaunchConsole_1=Start Arduino Console
ArduinoLaunchConsole_2=Arduino Console Output
ArduinoPreferencePage_0=Arduino IDE Install Location
ArduinoPreferencePage_1=Arduino C++ Preferences
ArduinoTargetPropertyPage_0=Serial Port:
ArduinoTargetPropertyPage_1=No serial ports
ArduinoTargetPropertyPage_2=Board type:
@ -26,3 +24,4 @@ libraries and tools. For more information, see http://arduino.cc.
LibrariesPropertyPage_desc=Select libraries to use in your project and click OK or Apply. \
If necessary the library will be installed. By adding libraries you agree to the licenses of those \
libraries. For more information, see http://arduino.cc
ArduinoPreferencePage_desc=Enter URLs for package_index.json files one per line.

View file

@ -7,7 +7,6 @@
*******************************************************************************/
package org.eclipse.cdt.arduino.ui.internal.preferences;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
@ -16,9 +15,7 @@ import java.util.Set;
import org.eclipse.cdt.arduino.core.internal.board.ArduinoBoard;
import org.eclipse.cdt.arduino.core.internal.board.ArduinoManager;
import org.eclipse.cdt.arduino.core.internal.board.ArduinoPackage;
import org.eclipse.cdt.arduino.core.internal.board.ArduinoPlatform;
import org.eclipse.cdt.arduino.core.internal.board.PackageIndex;
import org.eclipse.cdt.arduino.ui.internal.Activator;
import org.eclipse.cdt.arduino.ui.internal.Messages;
import org.eclipse.core.runtime.CoreException;
@ -52,7 +49,6 @@ public class ArduinoBoardsPreferencePage extends PreferencePage implements IWork
@Override
public void init(IWorkbench workbench) {
setPreferenceStore(Activator.getDefault().getCorePreferenceStore());
}
@Override
@ -136,20 +132,7 @@ public class ArduinoBoardsPreferencePage extends PreferencePage implements IWork
table.removeAll();
try {
PackageIndex packageIndex = ArduinoManager.instance.getPackageIndex();
List<ArduinoBoard> boards = new ArrayList<>();
for (ArduinoPackage pkg : packageIndex.getPackages()) {
for (ArduinoPlatform platform : pkg.getLatestPlatforms()) {
try {
for (ArduinoBoard board : platform.getBoards()) {
boards.add(board);
}
} catch (CoreException e) {
Activator.log(e);
}
}
}
List<ArduinoBoard> boards = ArduinoManager.instance.getBoards();
Collections.sort(boards, new Comparator<ArduinoBoard>() {
public int compare(ArduinoBoard o1, ArduinoBoard o2) {
return o1.getName().compareTo(o2.getName());

View file

@ -1,23 +0,0 @@
/*******************************************************************************
* 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.ui.internal.preferences;
import org.eclipse.cdt.arduino.core.internal.ArduinoPreferences;
import org.eclipse.cdt.arduino.ui.internal.Activator;
import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;
import org.eclipse.jface.preference.IPreferenceStore;
public class ArduinoPreferenceInitializer extends AbstractPreferenceInitializer {
@Override
public void initializeDefaultPreferences() {
IPreferenceStore store = Activator.getDefault().getCorePreferenceStore();
store.setDefault(ArduinoPreferences.ARDUINO_HOME, ArduinoPreferences.getDefaultArduinoHome().toString());
}
}

View file

@ -11,37 +11,58 @@
package org.eclipse.cdt.arduino.ui.internal.preferences;
import org.eclipse.cdt.arduino.core.internal.ArduinoPreferences;
import org.eclipse.cdt.arduino.ui.internal.Activator;
import org.eclipse.cdt.arduino.core.internal.board.ArduinoManager;
import org.eclipse.cdt.arduino.ui.internal.Messages;
import org.eclipse.jface.preference.DirectoryFieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
public class ArduinoPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {
public class ArduinoPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
public ArduinoPreferencePage() {
super(GRID);
}
@Override
public IPreferenceStore getPreferenceStore() {
// TODO Auto-generated method stub
return super.getPreferenceStore();
}
@Override
protected void createFieldEditors() {
addField(new DirectoryFieldEditor(ArduinoPreferences.ARDUINO_HOME, Messages.ArduinoPreferencePage_0,
getFieldEditorParent()));
}
private Text urlsText;
@Override
public void init(IWorkbench workbench) {
setDescription(Messages.ArduinoPreferencePage_1);
// Preferences are stored in core
setPreferenceStore(Activator.getDefault().getCorePreferenceStore());
}
@Override
protected Control createContents(Composite parent) {
Composite control = new Composite(parent, SWT.NONE);
control.setLayout(new GridLayout());
Text desc = new Text(control, SWT.READ_ONLY | SWT.WRAP);
GridData layoutData = new GridData(SWT.FILL, SWT.FILL, true, false);
layoutData.widthHint = 500;
desc.setLayoutData(layoutData);
desc.setBackground(parent.getBackground());
desc.setText(Messages.ArduinoPreferencePage_desc);
urlsText = new Text(control, SWT.BORDER);
urlsText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
urlsText.setText(ArduinoPreferences.getBoardUrls());
return control;
}
@Override
public boolean performOk() {
ArduinoPreferences.setBoardUrls(urlsText.getText());
ArduinoManager.instance.loadIndices();
return true;
}
@Override
protected void performDefaults() {
String defaultBoardUrl = ArduinoPreferences.getDefaultBoardUrls();
urlsText.setText(defaultBoardUrl);
ArduinoPreferences.setBoardUrls(defaultBoardUrl);
super.performDefaults();
}
}