Skip to content

Add a flutter pub add action #7924

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 5 commits into from
Feb 4, 2025
Merged
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
32 changes: 32 additions & 0 deletions flutter-idea/src/io/flutter/actions/FlutterPackagesAddAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2025 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.actions;

import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.project.Project;
import io.flutter.FlutterMessages;
import io.flutter.pub.PubRoot;
import io.flutter.sdk.FlutterSdk;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class FlutterPackagesAddAction extends FlutterSdkAction {
@Override
public void startCommand(@NotNull Project project, @NotNull FlutterSdk sdk, @Nullable PubRoot root, @NotNull DataContext context) {
if (root == null) {
FlutterMessages.showError(
"Cannot Find Pub Root",
"Flutter pub add can only be run within a directory with a pubspec.yaml file",
project);
return;
}
PackageDialogWrapper dialog = new PackageDialogWrapper();
if (dialog.showAndGet()) {
sdk.startPubAdd(root, project, dialog.getPackageName(), dialog.getDevOnly());
}
}
}

73 changes: 73 additions & 0 deletions flutter-idea/src/io/flutter/actions/PackageDialogWrapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2025 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.actions;

import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.ValidationInfo;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;

public class PackageDialogWrapper extends DialogWrapper {

@NotNull
private JTextField packageInput = new JTextField("", 16);

@NotNull
private JCheckBox devOnlyCheckBox = new JCheckBox();

public PackageDialogWrapper() {
super(true); // use current window as parent
setTitle("Add Package");
init();
}

@Nullable
@Override
protected JComponent createCenterPanel() {
JPanel dialogPanel = new JPanel();

JLabel packageNameLabel = new JLabel("Package name:");
dialogPanel.add(packageNameLabel, BorderLayout.LINE_START);
dialogPanel.add(packageInput, BorderLayout.CENTER);

dialogPanel.add(new JSeparator());

JLabel devOnlyLabel = new JLabel("Dev only:");
dialogPanel.add(devOnlyLabel, BorderLayout.LINE_START);
dialogPanel.add(devOnlyCheckBox, BorderLayout.CENTER);

return dialogPanel;
}

@Nullable
@Override
protected ValidationInfo doValidate() {
if (getPackageName().isEmpty()) {
return new ValidationInfo("A package name is required", packageInput);
}
return null;
}

@Override
public JComponent getPreferredFocusedComponent() {
return packageInput;
}

@NotNull
public String getPackageName() {
String text = packageInput.getText();
return text == null ? "" : text;
}

public boolean getDevOnly() {
Object[] selectedObjects = devOnlyCheckBox.getSelectedObjects();
if (selectedObjects == null) return false;
return selectedObjects.length > 0;
}
}
1 change: 1 addition & 0 deletions flutter-idea/src/io/flutter/sdk/FlutterCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ enum Type {
CONFIG("Flutter config", "config"),
CREATE("Flutter create", "create"),
DOCTOR("Flutter doctor", "doctor", "--verbose"),
PUB_ADD("Flutter pub add", "pub", "add"),
PUB_GET("Flutter pub get", "pub", "get"),
PUB_UPGRADE("Flutter pub upgrade", "pub", "upgrade"),
PUB_OUTDATED("Flutter pub outdated", "pub", "outdated"),
Expand Down
21 changes: 21 additions & 0 deletions flutter-idea/src/io/flutter/sdk/FlutterSdk.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ public FlutterCommand flutterCreate(@NotNull VirtualFile appDir, @Nullable Flutt
return new FlutterCommand(this, appDir.getParent(), FlutterCommand.Type.CREATE, vargs);
}

public FlutterCommand flutterPackagesAdd(@NotNull PubRoot root, @NotNull String name, boolean devOnly) {
if (devOnly) name = "dev:" + name;
return new FlutterCommand(this, root.getRoot(), FlutterCommand.Type.PUB_ADD, name);
}

public FlutterCommand flutterPackagesGet(@NotNull PubRoot root) {
return new FlutterCommand(this, root.getRoot(), FlutterCommand.Type.PUB_GET);
}
Expand Down Expand Up @@ -441,6 +446,22 @@ public PubRoot createFiles(@NotNull VirtualFile baseDir, @Nullable Module module
return PubRoot.forDirectory(baseDir);
}

/**
* Starts running 'flutter pub add' on the given pub root provided it's in one of this project's modules.
* <p>
* Shows output in the console associated with the given module.
* <p>
* Returns the process if successfully started.
*/
public Process startPubAdd(@NotNull PubRoot root, @NotNull Project project, @NotNull String pkg, boolean devOnly) {
final Module module = root.getModule(project);
if (module == null) return null;
// Ensure pubspec is saved.
FileDocumentManager.getInstance().saveAllDocuments();
// Refresh afterwards to ensure Dart Plugin doesn't mistakenly nag to run pub.
return flutterPackagesAdd(root, pkg, devOnly).startInModuleConsole(module, root::refresh, null);
}

/**
* Starts running 'flutter pub get' on the given pub root provided it's in one of this project's modules.
* <p>
Expand Down
3 changes: 3 additions & 0 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@
text="Flutter Doctor"
description="Run 'flutter doctor'"/>
<separator/>
<action id="flutter.pub.add" class="io.flutter.actions.FlutterPackagesAddAction"
text="Flutter Pub Add"
description="Run 'flutter pub add'"/>
<action id="flutter.pub.get" class="io.flutter.actions.FlutterPackagesGetAction"
text="Flutter Pub Get"
description="Run 'flutter pub get'"/>
Expand Down
3 changes: 3 additions & 0 deletions resources/META-INF/plugin_template.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@
text="Flutter Doctor"
description="Run 'flutter doctor'"/>
<separator/>
<action id="flutter.pub.add" class="io.flutter.actions.FlutterPackagesAddAction"
text="Flutter Pub Add"
description="Run 'flutter pub add'"/>
<action id="flutter.pub.get" class="io.flutter.actions.FlutterPackagesGetAction"
text="Flutter Pub Get"
description="Run 'flutter pub get'"/>
Expand Down