Skip to content
This repository has been archived by the owner on Jun 11, 2021. It is now read-only.

Commit

Permalink
issue #4 Take supported SDK versions from bin/compilerInfo.xml (2.2.2+)
Browse files Browse the repository at this point in the history
  • Loading branch information
liias committed Feb 22, 2017
1 parent 739bad4 commit 03b7c03
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 13 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#### 0.3.4
* Avoid dependency on gradle plugin (issue #5) - Thanks to grego33
* Highlight constant name in constant declaration
* Add 2.2.1 base version (issue #4)
* Add target versions from bin/compilerInfo.xml (adds 2.2.0 base version) (issue #4)

#### 0.3.3
* Fix adding SDK 2.1.5 (issue #3)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package io.github.liias.monkey.project.runconfig;

import org.jetbrains.annotations.NotNull;

public class TargetSdkVersion {
public static final TargetSdkVersion VERSION_1_2_X = new TargetSdkVersion("1.2.1", "1.2.x");
public static final TargetSdkVersion VERSION_1_3_X = new TargetSdkVersion("1.3.1", "1.3.x");
public static final TargetSdkVersion VERSION_2_1_X = new TargetSdkVersion("2.1.1", "2.1.x");
public static final TargetSdkVersion VERSION_2_2_X = new TargetSdkVersion("2.2.1", "2.2.x");
public static final TargetSdkVersion VERSION_2_2_X = new TargetSdkVersion("2.2.0", "2.2.x");

private String id;
private String name;

public TargetSdkVersion(String id, String name) {
public TargetSdkVersion(@NotNull String id, String name) {
this.id = id;
this.name = name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import com.intellij.util.containers.HashMap;
import icons.MonkeyIcons;
import io.github.liias.monkey.project.module.MonkeyConstants;
import io.github.liias.monkey.project.runconfig.TargetSdkVersion;
import io.github.liias.monkey.project.sdk.skeleton.SdkSkeleton;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.swing.*;
Expand All @@ -20,14 +22,21 @@
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import java.util.*;

import static io.github.liias.monkey.Utils.getForWinLinOrMac;

public class MonkeySdkType extends SdkType {
public static String COMPILER_INFO_XML = "compilerInfo.xml";

// major compiler versions come from bin/compilerInfo.xml (provided since SDK 2.2.2)
private static final List<TargetSdkVersion> OLD_TARGET_SDK_VERSIONS = Arrays.asList(
TargetSdkVersion.VERSION_1_2_X,
TargetSdkVersion.VERSION_1_3_X,
TargetSdkVersion.VERSION_2_1_X,
TargetSdkVersion.VERSION_2_2_X
);

public MonkeySdkType() {
super(MonkeyConstants.SDK_TYPE_ID);
}
Expand Down Expand Up @@ -127,6 +136,55 @@ private String detectCompilerVersion(@NotNull String homePath) {
return version;
}

public static List<TargetSdkVersion> getTargetSdkVersions(@Nullable String sdkBinPath) {
if (sdkBinPath == null) {
return OLD_TARGET_SDK_VERSIONS;
}

final File compilerInfoXml = new File(sdkBinPath, COMPILER_INFO_XML);
if (!compilerInfoXml.exists()) {
return null;
}

try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(compilerInfoXml);
//optional, but recommended
//read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
doc.getDocumentElement().normalize();

NodeList targetSdkVersions1 = doc.getElementsByTagName("targetSdkVersions");

if (targetSdkVersions1.getLength() == 0) {
return OLD_TARGET_SDK_VERSIONS;
}

Node targetSdkVersionsNode = targetSdkVersions1.item(0);
NodeList childNodes = targetSdkVersionsNode.getChildNodes();

List<TargetSdkVersion> targetSdkVersions = new ArrayList<>();
for (int i = 0; i < childNodes.getLength(); i++) {
Node targetVersion = childNodes.item(i);
if (targetVersion.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
String targetVersionString = targetVersion.getTextContent();

String[] number = targetVersionString.split("\\.");
int major = Short.parseShort(number[0]);
int minor = Short.parseShort(number[1]);
String versionWildcard = major + "." + minor + ".x";
TargetSdkVersion targetSdkVersion = new TargetSdkVersion(targetVersionString, versionWildcard);
targetSdkVersions.add(targetSdkVersion);
}
return targetSdkVersions;

} catch (SAXException | ParserConfigurationException | IOException e) {
e.printStackTrace();
}
return OLD_TARGET_SDK_VERSIONS;
}

@Nullable
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.intellij.openapi.diagnostic.Logger;
import io.github.liias.monkey.project.runconfig.TargetDevice;
import io.github.liias.monkey.project.runconfig.TargetSdkVersion;
import io.github.liias.monkey.project.sdk.MonkeySdkType;
import org.jetbrains.annotations.Nullable;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
Expand All @@ -16,7 +17,6 @@
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class DevicesReader {
Expand All @@ -39,12 +39,7 @@ public DevicesReader(@Nullable String sdkBinPath) {
}

public List<TargetSdkVersion> getTargetSdkVersions() {
return Arrays.asList(
TargetSdkVersion.VERSION_1_2_X,
TargetSdkVersion.VERSION_1_3_X,
TargetSdkVersion.VERSION_2_1_X,
TargetSdkVersion.VERSION_2_2_X
);
return MonkeySdkType.getTargetSdkVersions(sdkBinPath);
}

public List<TargetDevice> parseDevicesXml() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<ul>
<li>Avoid dependency on gradle plugin (issue #5) - Thanks to grego33</li>
<li>Highlight constant name in constant declaration</li>
<li>Add 2.2.1 base version (issue #4)</li>
<li>Add target versions from bin/compilerInfo.xml (adds 2.2.0 base version) (issue #4)</li>
</ul>
<a href="https://github.com/liias/monkey/blob/master/CHANGELOG.md#changelog">Full change log...</a>
Expand Down

0 comments on commit 03b7c03

Please sign in to comment.