Skip to content

Resolve diagnostics in sdk version file #7701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 63 additions & 49 deletions flutter-idea/src/io/flutter/sdk/FlutterSdkVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

public class FlutterSdkVersion implements Comparable<FlutterSdkVersion> {
public final class FlutterSdkVersion implements Comparable<FlutterSdkVersion> {
/**
* The version for which the distributed icons can be used.
*/
@VisibleForTesting
@NotNull
public static final FlutterSdkVersion DISTRIBUTED_ICONS = new FlutterSdkVersion("3.1.0");

/**
* The minimum version we suggest people use.
*/
@NotNull
private static final FlutterSdkVersion MIN_SUPPORTED_SDK = new FlutterSdkVersion("0.0.12");

/**
Expand All @@ -31,31 +35,37 @@ public class FlutterSdkVersion implements Comparable<FlutterSdkVersion> {
* Before this version there were issues if you ran the app from the command
* line without the flag after running
*/
@NotNull
private static final FlutterSdkVersion MIN_SAFE_TRACK_WIDGET_CREATION_SDK = new FlutterSdkVersion("0.10.2");

/**
* The version that supports --dart-define in the run command.
*/
@NotNull
private static final FlutterSdkVersion MIN_DART_DEFINE_SDK = new FlutterSdkVersion("1.12.0");

/**
* The version of the stable channel that supports --androidx in the create command.
*/
@NotNull
private static final FlutterSdkVersion MIN_PUB_OUTDATED_SDK = new FlutterSdkVersion("1.16.4");

/**
* The version that supports --platform in flutter create.
*/
@NotNull
private static final FlutterSdkVersion MIN_CREATE_PLATFORMS_SDK = new FlutterSdkVersion("1.20.0");

/**
* The version that supports --config-only when configuring Xcode for opening a project.
*/
@NotNull
private static final FlutterSdkVersion MIN_XCODE_CONFIG_ONLY = new FlutterSdkVersion("1.22.0-12.0.pre");

/**
* The last version of stable that does not support --platform in flutter create.
*/
@NotNull
private static final FlutterSdkVersion MAX_STABLE_NO_PLATFORMS_SDK = new FlutterSdkVersion("1.22.6");

/**
Expand All @@ -75,16 +85,19 @@ public class FlutterSdkVersion implements Comparable<FlutterSdkVersion> {
/**
* The version that includes the skeleton template.
*/
@NotNull
private static final FlutterSdkVersion MIN_SKELETON_TEMPLATE = new FlutterSdkVersion("2.5.0");

/**
* The version that includes the skeleton template.
*/
@NotNull
private static final FlutterSdkVersion MIN_PLUGIN_FFI_TEMPLATE = new FlutterSdkVersion("3.0.0");

/**
* The version that includes the skeleton template.
*/
@NotNull
private static final FlutterSdkVersion MIN_EMPTY_PROJECT = new FlutterSdkVersion("3.6.0-0.1.pre");

/**
Expand Down Expand Up @@ -127,29 +140,29 @@ public class FlutterSdkVersion implements Comparable<FlutterSdkVersion> {
private final String versionText;
@Nullable
private final Version betaVersion;
private final int masterVersion;
private final int mainVersion;

@VisibleForTesting
public FlutterSdkVersion(@Nullable String versionString) {
versionText = versionString;
if (versionString != null) {
final String[] split = versionString.split("-");
final @NotNull String[] split = versionString.split("-");
version = Version.parseVersion(split[0]);

if (split.length > 1) {
betaVersion = Version.parseVersion(split[1]);
final String[] parts = split[1].split("\\.");
masterVersion = parts.length > 3 ? Integer.parseInt(parts[3]) : 0;
final @NotNull String[] parts = split[1].split("\\.");
mainVersion = parts.length > 3 ? Integer.parseInt(parts[3]) : 0;
}
else {
betaVersion = null;
masterVersion = 0;
mainVersion = 0;
}
}
else {
version = null;
betaVersion = null;
masterVersion = 0;
mainVersion = 0;
}
}

Expand All @@ -172,7 +185,8 @@ private static FlutterSdkVersion readFromFile(@Nullable VirtualFile file) {
return new FlutterSdkVersion(versionString);
}

private static String readVersionString(VirtualFile file) {
@Nullable
private static String readVersionString(@NotNull VirtualFile file) {
try {
final String data = new String(file.contentsToByteArray(), StandardCharsets.UTF_8);
for (String line : data.split("\n")) {
Expand All @@ -191,42 +205,40 @@ private static String readVersionString(VirtualFile file) {
}
}

private boolean supportsVersion(@NotNull FlutterSdkVersion otherVersion) {
return this.compareTo(otherVersion) >= 0;
}

public boolean isMinRecommendedSupported() {
assert (MIN_SUPPORTED_SDK.version != null);
return version != null && version.compareTo(MIN_SUPPORTED_SDK.version) >= 0;
return supportsVersion(MIN_SUPPORTED_SDK);
}

public boolean isTrackWidgetCreationRecommended() {
assert (MIN_SAFE_TRACK_WIDGET_CREATION_SDK.version != null);
return version != null && version.compareTo(MIN_SAFE_TRACK_WIDGET_CREATION_SDK.version) >= 0;
return supportsVersion(MIN_SAFE_TRACK_WIDGET_CREATION_SDK);
}

public boolean isDartDefineSupported() {
//noinspection ConstantConditions
return version != null && version.compareTo(MIN_DART_DEFINE_SDK.version) >= 0;
return supportsVersion(MIN_DART_DEFINE_SDK);
}

public boolean isXcodeConfigOnlySupported() {
//noinspection ConstantConditions
return version != null && version.compareTo(MIN_XCODE_CONFIG_ONLY.version) >= 0;
return supportsVersion(MIN_XCODE_CONFIG_ONLY);
}

public boolean flutterCreateSupportsPlatforms() {
//noinspection ConstantConditions
return version != null && version.compareTo(MIN_CREATE_PLATFORMS_SDK.version) >= 0;
return supportsVersion(MIN_CREATE_PLATFORMS_SDK);
}

public boolean stableChannelSupportsPlatforms() {
//noinspection ConstantConditions
return version != null && version.compareTo(MAX_STABLE_NO_PLATFORMS_SDK.version) > 0;
return supportsVersion(MAX_STABLE_NO_PLATFORMS_SDK);
}

public boolean flutterRunSupportsDevToolsUrl() {
return version != null && this.compareTo(MIN_PASS_DEVTOOLS_SDK) >= 0 && this.compareTo(MIN_OPTIONAL_PASS_DEVTOOLS_SDK) < 0;
return this.compareTo(MIN_PASS_DEVTOOLS_SDK) >= 0 && this.compareTo(MIN_OPTIONAL_PASS_DEVTOOLS_SDK) < 0;
}

public boolean useDaemonForDevTools() {
return version != null && this.compareTo(MIN_PASS_DEVTOOLS_SDK) >= 0;
return supportsVersion(MIN_PASS_DEVTOOLS_SDK);
}

public boolean flutterTestSupportsMachineMode() {
Expand All @@ -238,76 +250,77 @@ public boolean flutterTestSupportsFiltering() {
}

public boolean isPubOutdatedSupported() {
//noinspection ConstantConditions
return version != null && version.compareTo(MIN_PUB_OUTDATED_SDK.version) >= 0;
return supportsVersion(MIN_PUB_OUTDATED_SDK);
}

public boolean isSkeletonTemplateAvailable() {
return version != null && version.compareTo(MIN_SKELETON_TEMPLATE.version) >= 0;
return supportsVersion(MIN_SKELETON_TEMPLATE);
}

public boolean isPluginFfiTemplateAvailable() {
return version != null && version.compareTo(MIN_PLUGIN_FFI_TEMPLATE.version) >= 0;
return supportsVersion(MIN_PLUGIN_FFI_TEMPLATE);
}

public boolean isEmptyProjectAvailable() {
return version != null && version.compareTo(MIN_EMPTY_PROJECT.version) >= 0;
return supportsVersion(MIN_EMPTY_PROJECT);
}

public boolean isUriMappingSupportedForWeb() {
return version != null && this.compareTo(MIN_URI_MAPPING_FOR_WEB) >= 0;
return supportsVersion(MIN_URI_MAPPING_FOR_WEB);
}

public boolean isWebPlatformStable() {
return version != null && this.compareTo(MIN_STABLE_WEB_PLATFORM) >= 0;
return supportsVersion(MIN_STABLE_WEB_PLATFORM);
}

public boolean isWindowsPlatformStable() {
return version != null && this.compareTo(MIN_STABLE_WINDOWS_PLATFORM) >= 0;
return supportsVersion(MIN_STABLE_WINDOWS_PLATFORM);
}

public boolean isLinuxPlatformStable() {
return version != null && this.compareTo(MIN_STABLE_LINUX_PLATFORM) >= 0;
return supportsVersion(MIN_STABLE_LINUX_PLATFORM);
}

public boolean isMacOSPlatformStable() {
return version != null && this.compareTo(MIN_STABLE_MACOS_PLATFORM) >= 0;
return supportsVersion(MIN_STABLE_MACOS_PLATFORM);
}

public boolean canUseDistributedIcons() {
return version != null && this.compareTo(DISTRIBUTED_ICONS) >= 0;
return supportsVersion(DISTRIBUTED_ICONS);
}

public boolean canUseDevToolsPathUrls() {
return version != null && this.compareTo(MIN_SUPPORTS_DEVTOOLS_PATH_URLS) >= 0;
return supportsVersion(MIN_SUPPORTS_DEVTOOLS_PATH_URLS);
}

public boolean canUseToolEventStream() {
return version != null && this.compareTo(MIN_SUPPORTS_TOOL_EVENT_STREAM) >= 0;
return supportsVersion(MIN_SUPPORTS_TOOL_EVENT_STREAM);
}

public boolean canUseDeepLinksTool() {
return version != null && this.compareTo(MIN_SUPPORTS_DEEP_LINKS_TOOL) >= 0;
return supportsVersion(MIN_SUPPORTS_DEEP_LINKS_TOOL);
}

public boolean canUseDevToolsMultiEmbed() {
return version != null && this.compareTo(MIN_SUPPORTS_DEVTOOLS_MULTI_EMBED) >= 0;
return supportsVersion(MIN_SUPPORTS_DEVTOOLS_MULTI_EMBED);
}

@SuppressWarnings("BooleanMethodIsAlwaysInverted")
public boolean canUseDtd() {
return version != null && this.compareTo(MIN_SUPPORTS_DTD) >= 0;
return supportsVersion(MIN_SUPPORTS_DTD);
}

public boolean sdkIsSupported() {
return version != null && this.compareTo(MIN_SDK_SUPPORTED) >= 0;
return supportsVersion(MIN_SDK_SUPPORTED);
}

public boolean isValid() {
return version != null;
}

@NotNull
public String fullVersion() {
return version == null ? "unknown version" : version.toString();
return version == null ? "unknown version" : Objects.requireNonNull(version.toString());
}

/**
Expand All @@ -319,21 +332,21 @@ public String getVersionText() {
}

@Override
@NotNull
public String toString() {
return version == null ? "unknown version" : version.toCompactString();
return version == null ? "unknown version" : Objects.requireNonNull(version.toCompactString());
}

@Override
public int compareTo(@NotNull FlutterSdkVersion otherVersion) {
// TODO(messick) Remove "version != null" prior to calling this method everywhere in this file.
if (version == null) return -1;
if (otherVersion.version == null) return 1;
final int standardComparisonResult = version.compareTo(otherVersion.version);
if (standardComparisonResult != 0) {
return standardComparisonResult;
}

// Check for beta version strings if standard versions are equivalent.
// If standard versions are equivalent, check for beta version strings.
if (betaVersion == null && otherVersion.betaVersion == null) {
return 0;
}
Expand All @@ -350,15 +363,16 @@ else if (otherVersion.betaVersion == null) {
return betaComparisonResult;
}

// Check master version ints if both have master version ints and versions are otherwise equivalent.
// Otherwise, the version without a master version is further ahead.
if (masterVersion != 0 && otherVersion.masterVersion != 0) {
return Integer.compare(masterVersion, otherVersion.masterVersion);
// If both have main version ints and the versions are otherwise equivalent,
// compare the main version ints.
// Otherwise, the version without a main version is further ahead.
if (mainVersion != 0 && otherVersion.mainVersion != 0) {
return Integer.compare(mainVersion, otherVersion.mainVersion);
}
else if (masterVersion != 0) {
else if (mainVersion != 0) {
return -1;
}
else if (otherVersion.masterVersion != 0) {
else if (otherVersion.mainVersion != 0) {
return 1;
}
else {
Expand Down