Skip to content

Commit

Permalink
Add buildship offline mode preference
Browse files Browse the repository at this point in the history
Signed-off-by: Snjezana Peco <[email protected]>
  • Loading branch information
snjeza committed Dec 18, 2019
1 parent dd830b5 commit eb36213
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,15 @@ public static BuildConfiguration getBuildConfiguration(Path rootFolder) {
File javaHome = javaHomeStr == null ? null : new File(javaHomeStr);
List<String> gradleArguments = JavaLanguageServerPlugin.getPreferencesManager() != null ? JavaLanguageServerPlugin.getPreferencesManager().getPreferences().getGradleArguments() : new ArrayList<>();
List<String> gradleJvmArguments = JavaLanguageServerPlugin.getPreferencesManager() != null ? JavaLanguageServerPlugin.getPreferencesManager().getPreferences().getGradleJvmArguments() : new ArrayList<>();
boolean offlineMode = JavaLanguageServerPlugin.getPreferencesManager() != null ? JavaLanguageServerPlugin.getPreferencesManager().getPreferences().isImportGradleOfflineEnabled() : false;
// @formatter:off
BuildConfiguration build = BuildConfiguration.forRootProjectDirectory(rootFolder.toFile())
.overrideWorkspaceConfiguration(overrideWorkspaceConfiguration)
.gradleDistribution(distribution)
.javaHome(javaHome)
.arguments(gradleArguments)
.jvmArguments(gradleJvmArguments)
.offlineMode(offlineMode)
.build();
// @formatter:on
return build;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.Map;
import java.util.UUID;

import org.eclipse.buildship.core.internal.CorePlugin;
import org.eclipse.buildship.core.internal.configuration.WorkspaceConfiguration;
import org.eclipse.core.internal.resources.PreferenceInitializer;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
Expand Down Expand Up @@ -65,6 +67,10 @@ public class Preferences {
* Preference key to enable/disable gradle importer.
*/
public static final String IMPORT_GRADLE_ENABLED = "java.import.gradle.enabled";
/**
* Preference key to enable/disable gradle offline mode.
*/
public static final String IMPORT_GRADLE_OFFLINE_ENABLED = "java.import.gradle.offline.enabled";
/**
* Preference key to enable/disable gradle wrapper.
*/
Expand Down Expand Up @@ -323,6 +329,7 @@ public class Preferences {
private FeatureStatus updateBuildConfigurationStatus;
private boolean referencesCodeLensEnabled;
private boolean importGradleEnabled;
private boolean importGradleOfflineEnabled;
private boolean gradleWrapperEnabled;
private String gradleVersion;
private List<String> gradleArguments;
Expand Down Expand Up @@ -441,6 +448,7 @@ public Preferences() {
incompleteClasspathSeverity = Severity.warning;
updateBuildConfigurationStatus = FeatureStatus.interactive;
importGradleEnabled = true;
importGradleOfflineEnabled = false;
gradleWrapperEnabled = true;
gradleVersion = null;
gradleArguments = new ArrayList<>();
Expand Down Expand Up @@ -502,6 +510,8 @@ public static Preferences createFrom(Map<String, Object> configuration) {

boolean importGradleEnabled = getBoolean(configuration, IMPORT_GRADLE_ENABLED, true);
prefs.setImportGradleEnabled(importGradleEnabled);
boolean importGradleOfflineEnabled = getBoolean(configuration, IMPORT_GRADLE_OFFLINE_ENABLED, false);
prefs.setImportGradleOfflineEnabled(importGradleOfflineEnabled);
boolean gradleWrapperEnabled = getBoolean(configuration, GRADLE_WRAPPER_ENABLED, true);
prefs.setGradleWrapperEnabled(gradleWrapperEnabled);
String gradleVersion = getString(configuration, GRADLE_VERSION);
Expand Down Expand Up @@ -703,6 +713,19 @@ public Preferences setImportGradleEnabled(boolean enabled) {
return this;
}

@SuppressWarnings("restriction")
public Preferences setImportGradleOfflineEnabled(boolean enabled) {
this.importGradleOfflineEnabled = enabled;
WorkspaceConfiguration workspaceConfiguration = CorePlugin.configurationManager().loadWorkspaceConfiguration();
if (workspaceConfiguration.isOffline() != enabled) {
WorkspaceConfiguration newConfig = new WorkspaceConfiguration(workspaceConfiguration.getGradleDistribution(), workspaceConfiguration.getGradleUserHome(), workspaceConfiguration.getJavaHome(), enabled,
workspaceConfiguration.isBuildScansEnabled(), workspaceConfiguration.isAutoSync(), workspaceConfiguration.getArguments(), workspaceConfiguration.getJvmArguments(), workspaceConfiguration.isShowConsoleView(),
workspaceConfiguration.isShowExecutionsView());
CorePlugin.configurationManager().saveWorkspaceConfiguration(newConfig);
}
return this;
}

public Preferences setGradleWrapperEnabled(boolean enabled) {
this.gradleWrapperEnabled = enabled;
return this;
Expand Down Expand Up @@ -914,6 +937,10 @@ public boolean isImportGradleEnabled() {
return importGradleEnabled;
}

public boolean isImportGradleOfflineEnabled() {
return importGradleOfflineEnabled;
}

public boolean isGradleWrapperEnabled() {
return gradleWrapperEnabled;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,21 @@ public void testGradleArguments() {
}
}

@Test
public void testGradleOfflineMode() {
boolean offlineMode = JavaLanguageServerPlugin.getPreferencesManager().getPreferences().isImportGradleOfflineEnabled();
try {
Path rootPath = ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile().toPath();
BuildConfiguration build = GradleProjectImporter.getBuildConfiguration(rootPath);
assertFalse(build.isOfflineMode());
JavaLanguageServerPlugin.getPreferencesManager().getPreferences().setImportGradleOfflineEnabled(true);
build = GradleProjectImporter.getBuildConfiguration(rootPath);
assertTrue(build.isOfflineMode());
} finally {
JavaLanguageServerPlugin.getPreferencesManager().getPreferences().setImportGradleOfflineEnabled(offlineMode);
}
}

@Test
public void testGradleJvmArguments() {
List<String> jvmArguments = JavaLanguageServerPlugin.getPreferencesManager().getPreferences().getGradleJvmArguments();
Expand Down

0 comments on commit eb36213

Please sign in to comment.