Skip to content

Use Build Variants view to control Flutter build mode #4134

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2019 The Chromium Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
package io.flutter.android;

import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import org.jetbrains.android.facet.AndroidFacet;

public class AndroidBuildVariantDetector implements BuildVariantDetector {

@Override
public String selectedBuildVariant(Project project) {
Module flutterModule = ModuleManager.getInstance(project).findModuleByName("flutter");
if (flutterModule == null) {
return null;
}
AndroidFacet facet = AndroidFacet.getInstance(flutterModule);
if (facet == null) {
return null;
}
String variant = facet.getProperties().SELECTED_BUILD_VARIANT;
if (variant == null || variant.isEmpty()) {
return null;
}
return variant;
}
}
2 changes: 2 additions & 0 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1027,10 +1027,12 @@

<extensionPoints>
<extensionPoint name="gradleSyncProvider" interface="io.flutter.android.GradleSyncProvider"/>
<extensionPoint name="buildVariantDetector" interface="io.flutter.android.BuildVariantDetector"/>
</extensionPoints>

<extensions defaultExtensionNs="io.flutter">
<gradleSyncProvider implementation="io.flutter.android.IntellijGradleSyncProvider" order="last"/>
<buildVariantDetector implementation="io.flutter.android.IntellijBuildVariantDetector" order="last"/>
</extensions>

<extensions defaultExtensionNs="com.intellij">
Expand Down
2 changes: 2 additions & 0 deletions resources/META-INF/plugin_template.xml
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,12 @@

<extensionPoints>
<extensionPoint name="gradleSyncProvider" interface="io.flutter.android.GradleSyncProvider"/>
<extensionPoint name="buildVariantDetector" interface="io.flutter.android.BuildVariantDetector"/>
</extensionPoints>

<extensions defaultExtensionNs="io.flutter">
<gradleSyncProvider implementation="io.flutter.android.IntellijGradleSyncProvider" order="last"/>
<buildVariantDetector implementation="io.flutter.android.IntellijBuildVariantDetector" order="last"/>
</extensions>

<extensions defaultExtensionNs="com.intellij">
Expand Down
1 change: 1 addition & 0 deletions resources/META-INF/studio-contribs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

<extensions defaultExtensionNs="io.flutter">
<gradleSyncProvider implementation="io.flutter.android.AndroidStudioGradleSyncProvider" order="first"/>
<buildVariantDetector implementation="io.flutter.android.AndroidBuildVariantDetector" order="first"/>
</extensions>

<extensions defaultExtensionNs="com.intellij">
Expand Down
1 change: 1 addition & 0 deletions resources/META-INF/studio-contribs_template.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

<extensions defaultExtensionNs="io.flutter">
<gradleSyncProvider implementation="io.flutter.android.AndroidStudioGradleSyncProvider" order="first"/>
<buildVariantDetector implementation="io.flutter.android.AndroidBuildVariantDetector" order="first"/>
</extensions>

<extensions defaultExtensionNs="com.intellij">
Expand Down
4 changes: 2 additions & 2 deletions src/io/flutter/actions/FlutterBuildActionGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@

public class FlutterBuildActionGroup extends DefaultActionGroup {

public static OSProcessHandler build(Project project, @NotNull PubRoot pubRoot, FlutterSdk sdk, BuildType buildType, String desc) {
public static OSProcessHandler build(@NotNull Project project, @NotNull PubRoot pubRoot, FlutterSdk sdk, BuildType buildType, String desc) {
ProgressHelper progressHelper = new ProgressHelper(project);
progressHelper.start(desc);
OSProcessHandler processHandler = sdk.flutterBuild(pubRoot, buildType.type).startInConsole(project);
OSProcessHandler processHandler = sdk.flutterBuild(project, pubRoot, buildType.type).startInConsole(project);
if (processHandler == null) {
progressHelper.done();
}
Expand Down
2 changes: 1 addition & 1 deletion src/io/flutter/actions/OpenInXcodeAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private static void openFile(@NotNull VirtualFile file) {

// TODO(pq): consider a popup explaining why we're doing a build.
// Note: we build only for the simulator to bypass device provisioning issues.
final OSProcessHandler processHandler = sdk.flutterBuild(pubRoot, "ios", "--simulator").startInConsole(project);
final OSProcessHandler processHandler = sdk.flutterBuild(project, pubRoot, "ios", "--simulator").startInConsole(project);
if (processHandler == null) {
progressHelper.done();
FlutterMessages.showError("Error Opening Xcode", "unable to run `flutter build`");
Expand Down
16 changes: 16 additions & 0 deletions src/io/flutter/android/BuildVariantDetector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright 2019 The Chromium Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
package io.flutter.android;

import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.project.Project;

/// Extension point to determine the selected build variant for the :flutter module of a <code>project</code>.
public interface BuildVariantDetector {
ExtensionPointName<BuildVariantDetector> EP_NAME = ExtensionPointName.create("io.flutter.buildVariantDetector");

String selectedBuildVariant(Project project);
}
16 changes: 16 additions & 0 deletions src/io/flutter/android/IntellijBuildVariantDetector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright 2019 The Chromium Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
package io.flutter.android;

import com.intellij.openapi.project.Project;

public class IntellijBuildVariantDetector implements BuildVariantDetector {

@Override
public String selectedBuildVariant(Project project) {
return null;
}
}
72 changes: 58 additions & 14 deletions src/io/flutter/sdk/FlutterSdk.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@
*/
package io.flutter.sdk;

import com.google.gson.*;
import static java.util.Arrays.asList;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSyntaxException;
import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.execution.process.*;
import com.intellij.execution.process.OSProcessHandler;
import com.intellij.execution.process.ProcessAdapter;
import com.intellij.execution.process.ProcessEvent;
import com.intellij.execution.process.ProcessListener;
import com.intellij.execution.process.ProcessOutputTypes;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.externalSystem.service.execution.InvalidSdkException;
import com.intellij.openapi.fileEditor.FileDocumentManager;
Expand All @@ -19,11 +29,16 @@
import com.intellij.openapi.roots.libraries.LibraryTablesRegistrar;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.*;
import com.intellij.openapi.vfs.CharsetToolkit;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.util.ui.EdtInvocationManager;
import com.jetbrains.lang.dart.sdk.DartSdk;
import com.jetbrains.lang.dart.sdk.DartSdkUtil;
import io.flutter.FlutterUtils;
import io.flutter.android.BuildVariantDetector;
import io.flutter.bazel.Workspace;
import io.flutter.dart.DartPlugin;
import io.flutter.pub.PubRoot;
Expand All @@ -33,14 +48,16 @@
import io.flutter.samples.FlutterSample;
import io.flutter.samples.FlutterSampleManager;
import io.flutter.settings.FlutterSettings;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.util.*;

import static java.util.Arrays.asList;

public class FlutterSdk {
public static final String FLUTTER_SDK_GLOBAL_LIB_NAME = "Flutter SDK";

Expand Down Expand Up @@ -224,8 +241,30 @@ public FlutterCommand flutterMakeHostAppEditable(@NotNull PubRoot root) {
return new FlutterCommand(this, root.getRoot(), FlutterCommand.Type.MAKE_HOST_APP_EDITABLE);
}

public FlutterCommand flutterBuild(@NotNull PubRoot root, String... additionalArgs) {
return new FlutterCommand(this, root.getRoot(), FlutterCommand.Type.BUILD, additionalArgs);
public FlutterCommand flutterBuild(@NotNull Project project, @NotNull PubRoot root, String... additionalArgs) {
String variant = getBuildVariant(project);
String[] args;
if (variant != null) {
args = new String[additionalArgs.length + 1];
System.arraycopy(additionalArgs, 0, args, 0, additionalArgs.length);
String mode;
switch (variant) {
case "release":
mode = "--release";
break;
case "profile":
mode = "--profile";
break;
default:
mode = "--debug";
break;
}
args[args.length - 1] = mode;
}
else {
args = additionalArgs;
}
return new FlutterCommand(this, root.getRoot(), FlutterCommand.Type.BUILD, args);
}

public FlutterCommand flutterConfig(String... additionalArgs) {
Expand Down Expand Up @@ -279,7 +318,7 @@ else if (flutterLaunchMode == FlutterLaunchMode.RELEASE) {
}
args.add(FileUtil.toSystemDependentName(mainPath));

return new FlutterCommand(this, root.getRoot(), FlutterCommand.Type.RUN, args.toArray(new String[]{ }));
return new FlutterCommand(this, root.getRoot(), FlutterCommand.Type.RUN, args.toArray(new String[]{}));
}

public FlutterCommand flutterAttach(@NotNull PubRoot root, @NotNull VirtualFile main, @Nullable FlutterDevice device,
Expand Down Expand Up @@ -312,15 +351,15 @@ else if (flutterLaunchMode == FlutterLaunchMode.RELEASE) {
}
args.add(FileUtil.toSystemDependentName(mainPath));

return new FlutterCommand(this, root.getRoot(), FlutterCommand.Type.ATTACH, args.toArray(new String[]{ }));
return new FlutterCommand(this, root.getRoot(), FlutterCommand.Type.ATTACH, args.toArray(new String[]{}));
}

public FlutterCommand flutterRunOnTester(@NotNull PubRoot root, @NotNull String mainPath) {
final List<String> args = new ArrayList<>();
args.add("--machine");
args.add("--device-id=flutter-tester");
args.add(mainPath);
return new FlutterCommand(this, root.getRoot(), FlutterCommand.Type.RUN, args.toArray(new String[]{ }));
return new FlutterCommand(this, root.getRoot(), FlutterCommand.Type.RUN, args.toArray(new String[]{}));
}

public FlutterCommand flutterTest(@NotNull PubRoot root, @NotNull VirtualFile fileOrDir, @Nullable String testNameSubstring,
Expand Down Expand Up @@ -357,7 +396,7 @@ public FlutterCommand flutterTest(@NotNull PubRoot root, @NotNull VirtualFile fi
args.add(FileUtil.toSystemDependentName(mainPath));
}

return new FlutterCommand(this, root.getRoot(), FlutterCommand.Type.TEST, args.toArray(new String[]{ }));
return new FlutterCommand(this, root.getRoot(), FlutterCommand.Type.TEST, args.toArray(new String[]{}));
}

/**
Expand Down Expand Up @@ -586,6 +625,11 @@ public void onTextAvailable(@NotNull ProcessEvent event, @NotNull Key outputType
return null;
}

private static String getBuildVariant(@NotNull Project project) {
BuildVariantDetector extension = BuildVariantDetector.EP_NAME.getExtensionList().get(0);
return extension.selectedBuildVariant(project);
}

/**
* A {@link FlutterSdk} that is compatible with the Bazel version of the Dart and Flutter SDK.
*/
Expand Down