| /* |
| * Copyright 2016 The Bazel Authors. All rights reserved. |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| package com.google.idea.blaze.golang.sync; |
| |
| import com.google.common.collect.ImmutableSet; |
| import com.google.common.collect.Lists; |
| import com.google.idea.blaze.base.io.VirtualFileSystemProvider; |
| import com.google.idea.blaze.base.model.BlazeProjectData; |
| import com.google.idea.blaze.base.model.primitives.LanguageClass; |
| import com.google.idea.blaze.base.model.primitives.WorkspaceRoot; |
| import com.google.idea.blaze.base.model.primitives.WorkspaceType; |
| import com.google.idea.blaze.base.projectview.ProjectViewSet; |
| import com.google.idea.blaze.base.scope.BlazeContext; |
| import com.google.idea.blaze.base.sync.BlazeSyncPlugin; |
| import com.google.idea.blaze.base.sync.libraries.LibrarySource; |
| import com.google.idea.common.experiments.BoolExperiment; |
| import com.intellij.openapi.diagnostic.Logger; |
| import com.intellij.openapi.module.Module; |
| import com.intellij.openapi.project.Project; |
| import com.intellij.openapi.roots.ModifiableRootModel; |
| import com.intellij.openapi.roots.libraries.Library; |
| import com.intellij.openapi.roots.libraries.LibraryTablesRegistrar; |
| import com.intellij.openapi.vfs.VfsUtil; |
| import com.intellij.openapi.vfs.VirtualFile; |
| import java.util.List; |
| import java.util.Set; |
| import javax.annotation.Nullable; |
| |
| /** Supports golang. */ |
| public class BlazeGoSyncPlugin extends BlazeSyncPlugin.Adapter { |
| |
| private static final Logger logger = Logger.getInstance(BlazeGoSyncPlugin.class); |
| |
| private static final BoolExperiment refreshExecRoot = |
| new BoolExperiment("refresh.exec.root.golang", true); |
| |
| static final ImmutableSet<String> GO_LIBRARY_PREFIXES = ImmutableSet.of("GOPATH", "Go SDK"); |
| |
| @Override |
| public Set<LanguageClass> getSupportedLanguagesInWorkspace(WorkspaceType workspaceType) { |
| return ImmutableSet.of(LanguageClass.GO); |
| } |
| |
| @Override |
| public void updateProjectStructure( |
| Project project, |
| BlazeContext context, |
| WorkspaceRoot workspaceRoot, |
| ProjectViewSet projectViewSet, |
| BlazeProjectData blazeProjectData, |
| @Nullable BlazeProjectData oldBlazeProjectData, |
| ModuleEditor moduleEditor, |
| Module workspaceModule, |
| ModifiableRootModel workspaceModifiableModel) { |
| if (!blazeProjectData.workspaceLanguageSettings.isLanguageActive(LanguageClass.GO)) { |
| return; |
| } |
| for (Library lib : getGoLibraries(project)) { |
| if (workspaceModifiableModel.findLibraryOrderEntry(lib) == null) { |
| workspaceModifiableModel.addLibraryEntry(lib); |
| } |
| } |
| } |
| |
| private static List<Library> getGoLibraries(Project project) { |
| List<Library> libraries = Lists.newArrayList(); |
| LibraryTablesRegistrar registrar = LibraryTablesRegistrar.getInstance(); |
| for (Library lib : registrar.getLibraryTable().getLibraries()) { |
| if (BlazeGoLibrarySource.isGoLibrary(lib)) { |
| libraries.add(lib); |
| } |
| } |
| |
| for (Library lib : registrar.getLibraryTable(project).getLibraries()) { |
| if (BlazeGoLibrarySource.isGoLibrary(lib)) { |
| libraries.add(lib); |
| } |
| } |
| return libraries; |
| } |
| |
| @Nullable |
| @Override |
| public LibrarySource getLibrarySource( |
| ProjectViewSet projectViewSet, BlazeProjectData blazeProjectData) { |
| if (!blazeProjectData.workspaceLanguageSettings.isLanguageActive(LanguageClass.GO)) { |
| return null; |
| } |
| return BlazeGoLibrarySource.INSTANCE; |
| } |
| |
| @Override |
| public void refreshVirtualFileSystem(BlazeProjectData blazeProjectData) { |
| if (!blazeProjectData.workspaceLanguageSettings.isLanguageActive(LanguageClass.GO)) { |
| return; |
| } |
| if (!refreshExecRoot.getValue()) { |
| return; |
| } |
| long start = System.currentTimeMillis(); |
| refreshExecRoot(blazeProjectData); |
| long end = System.currentTimeMillis(); |
| logger.info(String.format("Refreshing execution root took: %d ms", (end - start))); |
| } |
| |
| private static void refreshExecRoot(BlazeProjectData blazeProjectData) { |
| // recursive refresh of the blaze execution root. This is required because our blaze aspect |
| // can't yet tell us exactly which genfiles are required to resolve the project. |
| VirtualFile execRoot = |
| VirtualFileSystemProvider.getInstance() |
| .getSystem() |
| .refreshAndFindFileByIoFile(blazeProjectData.blazeInfo.getExecutionRoot()); |
| if (execRoot != null) { |
| VfsUtil.markDirtyAndRefresh(false, true, true, execRoot); |
| } |
| } |
| } |