1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-17 22:23:10 +03:00

Removed lots of calls to BaseNoGui.getPlatform(): static is evil

This commit is contained in:
Federico Fissore
2015-05-22 15:42:05 +02:00
parent 46e065b76d
commit ab7b7351f5
11 changed files with 84 additions and 136 deletions

View File

@ -33,8 +33,8 @@ import cc.arduino.contributions.GZippedJsonDownloader;
import cc.arduino.utils.ArchiveExtractor;
import cc.arduino.utils.MultiStepProgress;
import cc.arduino.utils.Progress;
import processing.app.BaseNoGui;
import processing.app.I18n;
import processing.app.Platform;
import processing.app.helpers.FileUtils;
import java.io.File;
@ -60,10 +60,12 @@ public class LibraryInstaller {
private final LibrariesIndexer indexer;
private final DownloadableContributionsDownloader downloader;
private final Platform platform;
public LibraryInstaller(LibrariesIndexer _indexer) {
indexer = _indexer;
File stagingFolder = _indexer.getStagingFolder();
public LibraryInstaller(LibrariesIndexer indexer, Platform platform) {
this.indexer = indexer;
this.platform = platform;
File stagingFolder = indexer.getStagingFolder();
downloader = new DownloadableContributionsDownloader(stagingFolder) {
@Override
protected void onProgress(Progress progress) {
@ -126,7 +128,7 @@ public class LibraryInstaller {
File libsFolder = indexer.getSketchbookLibrariesFolder();
File tmpFolder = FileUtils.createTempFolderIn(libsFolder);
try {
new ArchiveExtractor(BaseNoGui.getPlatform()).extract(lib.getDownloadedFile(), tmpFolder, 1);
new ArchiveExtractor(platform).extract(lib.getDownloadedFile(), tmpFolder, 1);
} catch (Exception e) {
if (tmpFolder.exists())
FileUtils.recursiveDelete(tmpFolder);

View File

@ -29,7 +29,7 @@
package cc.arduino.contributions.packages;
import cc.arduino.contributions.DownloadableContribution;
import processing.app.BaseNoGui;
import processing.app.Platform;
import java.util.List;
@ -41,9 +41,9 @@ public abstract class ContributedTool {
public abstract List<HostDependentDownloadableContribution> getSystems();
public DownloadableContribution getDownloadableContribution() {
public DownloadableContribution getDownloadableContribution(Platform platform) {
for (HostDependentDownloadableContribution c : getSystems()) {
if (c.isCompatible(BaseNoGui.getPlatform()))
if (c.isCompatible(platform))
return c;
}
return null;
@ -51,11 +51,17 @@ public abstract class ContributedTool {
@Override
public String toString() {
return toString(null);
}
public String toString(Platform platform) {
String res;
res = "Tool name : " + getName() + " " + getVersion() + "\n";
for (HostDependentDownloadableContribution sys : getSystems()) {
res += " sys";
res += sys.isCompatible(BaseNoGui.getPlatform()) ? "*" : " ";
if (platform != null) {
res += sys.isCompatible(platform) ? "*" : " ";
}
res += " : " + sys + "\n";
}
return res;

View File

@ -40,6 +40,7 @@ import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.Executor;
import processing.app.BaseNoGui;
import processing.app.I18n;
import processing.app.Platform;
import processing.app.PreferencesData;
import processing.app.helpers.FileUtils;
import processing.app.helpers.filefilters.OnlyDirs;
@ -58,8 +59,10 @@ public class ContributionInstaller {
private final ContributionsIndexer indexer;
private final DownloadableContributionsDownloader downloader;
private final Platform platform;
public ContributionInstaller(ContributionsIndexer contributionsIndexer) {
public ContributionInstaller(ContributionsIndexer contributionsIndexer, Platform platform) {
this.platform = platform;
File stagingFolder = contributionsIndexer.getStagingFolder();
indexer = contributionsIndexer;
downloader = new DownloadableContributionsDownloader(stagingFolder) {
@ -70,18 +73,18 @@ public class ContributionInstaller {
};
}
public List<String> install(ContributedPlatform platform) throws Exception {
public List<String> install(ContributedPlatform contributedPlatform) throws Exception {
List<String> errors = new LinkedList<String>();
if (platform.isInstalled()) {
if (contributedPlatform.isInstalled()) {
throw new Exception("Platform is already installed!");
}
// Do not download already installed tools
List<ContributedTool> tools = new LinkedList<ContributedTool>(platform.getResolvedTools());
List<ContributedTool> tools = new LinkedList<ContributedTool>(contributedPlatform.getResolvedTools());
Iterator<ContributedTool> toolsIterator = tools.iterator();
while (toolsIterator.hasNext()) {
ContributedTool tool = toolsIterator.next();
DownloadableContribution downloadable = tool.getDownloadableContribution();
DownloadableContribution downloadable = tool.getDownloadableContribution(platform);
if (downloadable == null) {
throw new Exception(format(_("Tool {0} is not available for your operating system."), tool.getName()));
}
@ -96,7 +99,7 @@ public class ContributionInstaller {
// Download all
try {
// Download platform
downloader.download(platform, progress, _("Downloading boards definitions."));
downloader.download(contributedPlatform, progress, _("Downloading boards definitions."));
progress.stepDone();
// Download tools
@ -104,7 +107,7 @@ public class ContributionInstaller {
for (ContributedTool tool : tools) {
String msg = format(_("Downloading tools ({0}/{1})."), i, tools.size());
i++;
downloader.download(tool.getDownloadableContribution(), progress, msg);
downloader.download(tool.getDownloadableContribution(platform), progress, msg);
progress.stepDone();
}
} catch (InterruptedException e) {
@ -112,7 +115,7 @@ public class ContributionInstaller {
return errors;
}
ContributedPackage pack = platform.getParentPackage();
ContributedPackage pack = contributedPlatform.getParentPackage();
File packageFolder = new File(indexer.getPackagesFolder(), pack.getName());
// TODO: Extract to temporary folders and move to the final destination only
@ -126,12 +129,12 @@ public class ContributionInstaller {
progress.setStatus(format(_("Installing tools ({0}/{1})..."), i, tools.size()));
onProgress(progress);
i++;
DownloadableContribution toolContrib = tool.getDownloadableContribution();
DownloadableContribution toolContrib = tool.getDownloadableContribution(platform);
File destFolder = new File(toolsFolder, tool.getName() + File.separator + tool.getVersion());
destFolder.mkdirs();
assert toolContrib.getDownloadedFile() != null;
new ArchiveExtractor(BaseNoGui.getPlatform()).extract(toolContrib.getDownloadedFile(), destFolder, 1);
new ArchiveExtractor(platform).extract(toolContrib.getDownloadedFile(), destFolder, 1);
try {
executePostInstallScriptIfAny(destFolder);
} catch (IOException e) {
@ -145,12 +148,12 @@ public class ContributionInstaller {
// Unpack platform on the correct location
progress.setStatus(_("Installing boards..."));
onProgress(progress);
File platformFolder = new File(packageFolder, "hardware" + File.separator + platform.getArchitecture());
File destFolder = new File(platformFolder, platform.getParsedVersion());
File platformFolder = new File(packageFolder, "hardware" + File.separator + contributedPlatform.getArchitecture());
File destFolder = new File(platformFolder, contributedPlatform.getParsedVersion());
destFolder.mkdirs();
new ArchiveExtractor(BaseNoGui.getPlatform()).extract(platform.getDownloadedFile(), destFolder, 1);
platform.setInstalled(true);
platform.setInstalledFolder(destFolder);
new ArchiveExtractor(platform).extract(contributedPlatform.getDownloadedFile(), destFolder, 1);
contributedPlatform.setInstalled(true);
contributedPlatform.setInstalledFolder(destFolder);
progress.stepDone();
progress.setStatus(_("Installation completed!"));
@ -160,7 +163,7 @@ public class ContributionInstaller {
}
private void executePostInstallScriptIfAny(File folder) throws IOException {
Collection<File> postInstallScripts = Collections2.filter(BaseNoGui.getPlatform().postInstallScripts(folder), new FileExecutablePredicate());
Collection<File> postInstallScripts = Collections2.filter(platform.postInstallScripts(folder), new FileExecutablePredicate());
if (postInstallScripts.isEmpty()) {
String[] subfolders = folder.list(new OnlyDirs());
@ -190,22 +193,22 @@ public class ContributionInstaller {
}
}
public List<String> remove(ContributedPlatform platform) {
if (platform == null || platform.isReadOnly()) {
public List<String> remove(ContributedPlatform contributedPlatform) {
if (contributedPlatform == null || contributedPlatform.isReadOnly()) {
return new LinkedList<String>();
}
List<String> errors = new LinkedList<String>();
FileUtils.recursiveDelete(platform.getInstalledFolder());
platform.setInstalled(false);
platform.setInstalledFolder(null);
FileUtils.recursiveDelete(contributedPlatform.getInstalledFolder());
contributedPlatform.setInstalled(false);
contributedPlatform.setInstalledFolder(null);
// Check if the tools are no longer needed
for (ContributedTool tool : platform.getResolvedTools()) {
for (ContributedTool tool : contributedPlatform.getResolvedTools()) {
if (indexer.isContributedToolUsed(tool)) {
continue;
}
DownloadableContribution toolContrib = tool.getDownloadableContribution();
DownloadableContribution toolContrib = tool.getDownloadableContribution(platform);
File destFolder = toolContrib.getInstalledFolder();
FileUtils.recursiveDelete(destFolder);
toolContrib.setInstalled(false);

View File

@ -46,6 +46,7 @@ import com.google.common.collect.Iterables;
import com.google.common.collect.Multimaps;
import org.apache.commons.compress.utils.IOUtils;
import processing.app.BaseNoGui;
import processing.app.Platform;
import processing.app.debug.TargetPackage;
import processing.app.debug.TargetPlatform;
import processing.app.debug.TargetPlatformException;
@ -65,10 +66,12 @@ public class ContributionsIndexer {
private final File packagesFolder;
private final File stagingFolder;
private final File preferencesFolder;
private final Platform platform;
private ContributionsIndex index;
public ContributionsIndexer(File preferencesFolder) {
public ContributionsIndexer(File preferencesFolder, Platform platform) {
this.preferencesFolder = preferencesFolder;
this.platform = platform;
packagesFolder = new File(preferencesFolder, "packages");
stagingFolder = new File(preferencesFolder, "staging" + File.separator + "packages");
}
@ -266,7 +269,7 @@ public class ContributionsIndexer {
if (tool == null) {
return;
}
DownloadableContribution contrib = tool.getDownloadableContribution();
DownloadableContribution contrib = tool.getDownloadableContribution(platform);
if (contrib == null) {
System.err.println(tool + " seems to have no downloadable contributions for your operating system, but it is installed in\n" + installationFolder);
return;

View File

@ -586,7 +586,7 @@ public class BaseNoGui {
}
static public void initPackages() throws Exception {
indexer = new ContributionsIndexer(BaseNoGui.getSettingsFolder());
indexer = new ContributionsIndexer(BaseNoGui.getSettingsFolder(), BaseNoGui.getPlatform());
File indexFile = indexer.getIndexFile("package_index.json");
File defaultPackageJsonFile = new File(getContentFile("dist"), "package_index.json");
if (!indexFile.isFile() || (defaultPackageJsonFile.isFile() && defaultPackageJsonFile.lastModified() > indexFile.lastModified())) {
@ -797,7 +797,7 @@ public class BaseNoGui {
PreferencesData.removeAllKeysWithPrefix(prefix);
for (ContributedTool tool : indexer.getInstalledTools()) {
File installedFolder = tool.getDownloadableContribution().getInstalledFolder();
File installedFolder = tool.getDownloadableContribution(getPlatform()).getInstalledFolder();
if (installedFolder != null) {
PreferencesData.set(prefix + tool.getName() + ".path", installedFolder.getAbsolutePath());
PreferencesData.set(prefix + tool.getName() + "-" + tool.getVersion() + ".path", installedFolder.getAbsolutePath());