use of com.intellij.openapi.vfs.LocalFileSystem in project intellij-community by JetBrains.
the class ProjectFromSourcesBuilderImpl method setupRootModel.
private static void setupRootModel(ProjectDescriptor projectDescriptor, final ModuleDescriptor descriptor, final ModifiableRootModel rootModel, final Map<LibraryDescriptor, Library> projectLibs) {
final CompilerModuleExtension compilerModuleExtension = rootModel.getModuleExtension(CompilerModuleExtension.class);
compilerModuleExtension.setExcludeOutput(true);
rootModel.inheritSdk();
final Set<File> contentRoots = descriptor.getContentRoots();
for (File contentRoot : contentRoots) {
final LocalFileSystem lfs = LocalFileSystem.getInstance();
VirtualFile moduleContentRoot = lfs.refreshAndFindFileByPath(FileUtil.toSystemIndependentName(contentRoot.getPath()));
if (moduleContentRoot != null) {
final ContentEntry contentEntry = rootModel.addContentEntry(moduleContentRoot);
final Collection<DetectedSourceRoot> sourceRoots = descriptor.getSourceRoots(contentRoot);
for (DetectedSourceRoot srcRoot : sourceRoots) {
final String srcpath = FileUtil.toSystemIndependentName(srcRoot.getDirectory().getPath());
final VirtualFile sourceRoot = lfs.refreshAndFindFileByPath(srcpath);
if (sourceRoot != null) {
contentEntry.addSourceFolder(sourceRoot, shouldBeTestRoot(srcRoot.getDirectory()), getPackagePrefix(srcRoot));
}
}
}
}
compilerModuleExtension.inheritCompilerOutputPath(true);
final LibraryTable moduleLibraryTable = rootModel.getModuleLibraryTable();
for (LibraryDescriptor libDescriptor : ModuleInsight.getLibraryDependencies(descriptor, projectDescriptor.getLibraries())) {
final Library projectLib = projectLibs.get(libDescriptor);
if (projectLib != null) {
rootModel.addLibraryEntry(projectLib);
} else {
// add as module library
final Collection<File> jars = libDescriptor.getJars();
for (File file : jars) {
Library library = moduleLibraryTable.createLibrary();
Library.ModifiableModel modifiableModel = library.getModifiableModel();
modifiableModel.addRoot(VfsUtil.getUrlForLibraryRoot(file), OrderRootType.CLASSES);
modifiableModel.commit();
}
}
}
}
use of com.intellij.openapi.vfs.LocalFileSystem in project intellij-community by JetBrains.
the class ExternalSystemVcsRegistrar method handle.
@SuppressWarnings("unchecked")
public static void handle(@NotNull final Project project) {
for (final ExternalSystemManager<?, ?, ?, ?, ?> manager : ExternalSystemApiUtil.getAllManagers()) {
final AbstractExternalSystemSettings settings = manager.getSettingsProvider().fun(project);
settings.subscribe(new ExternalSystemSettingsListenerAdapter() {
@Override
public void onProjectsLinked(@NotNull final Collection linked) {
List<VcsDirectoryMapping> newMappings = ContainerUtilRt.newArrayList();
final LocalFileSystem fileSystem = LocalFileSystem.getInstance();
ProjectLevelVcsManager vcsManager = ProjectLevelVcsManager.getInstance(project);
for (Object o : linked) {
final ExternalProjectSettings settings = (ExternalProjectSettings) o;
VirtualFile dir = fileSystem.refreshAndFindFileByPath(settings.getExternalProjectPath());
if (dir == null) {
continue;
}
if (!dir.isDirectory()) {
dir = dir.getParent();
}
newMappings.addAll(VcsUtil.findRoots(dir, project));
}
// There is a possible case that no VCS mappings are configured for the current project. There is a single
// mapping like <Project> - <No VCS> then. We want to replace it if only one mapping to the project root dir
// has been detected then.
List<VcsDirectoryMapping> oldMappings = vcsManager.getDirectoryMappings();
if (oldMappings.size() == 1 && newMappings.size() == 1 && StringUtil.isEmpty(oldMappings.get(0).getVcs())) {
VcsDirectoryMapping newMapping = newMappings.iterator().next();
String detectedDirPath = newMapping.getDirectory();
VirtualFile detectedDir = fileSystem.findFileByPath(detectedDirPath);
if (detectedDir != null && detectedDir.equals(project.getBaseDir())) {
newMappings.clear();
newMappings.add(new VcsDirectoryMapping("", newMapping.getVcs()));
vcsManager.setDirectoryMappings(newMappings);
return;
}
}
newMappings.addAll(oldMappings);
vcsManager.setDirectoryMappings(newMappings);
}
});
}
}
use of com.intellij.openapi.vfs.LocalFileSystem in project intellij-community by JetBrains.
the class ManagingContentRootsTest method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
ApplicationManager.getApplication().runWriteAction(() -> {
try {
LocalFileSystem fs = LocalFileSystem.getInstance();
dir = fs.refreshAndFindFileByIoFile(createTempDirectory());
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
use of com.intellij.openapi.vfs.LocalFileSystem in project intellij-community by JetBrains.
the class NewFilesProcessor method addToVcsIfNeeded.
/**
* @param localFileNames names of local files to add to VCS
*/
private static void addToVcsIfNeeded(@NotNull final Module module, @NotNull final String... localFileNames) {
final LocalFileSystem fs = LocalFileSystem.getInstance();
fs.refresh(false);
final Project project = module.getProject();
Arrays.stream(localFileNames).map(o -> fs.findFileByPath(o)).filter(o -> o != null).forEach(file -> {
final AbstractVcs<?> vcs = VcsUtil.getVcsFor(project, file);
if (vcs == null) {
return;
}
final CheckinEnvironment environment = vcs.getCheckinEnvironment();
if (environment != null) {
environment.scheduleUnversionedFilesForAddition(Collections.singletonList(file));
}
});
}
use of com.intellij.openapi.vfs.LocalFileSystem in project intellij-community by JetBrains.
the class PyExtractSuperclassHelper method createDirectories.
/**
* Create all intermediate dirs with inits from one of roots up to target dir.
*
* @param project
* @param target a full path to target dir
* @return deepest child directory, or null if target is not in roots or process fails at some point.
*/
@Nullable
private static PsiDirectory createDirectories(Project project, String target) throws IOException {
String the_rest = null;
VirtualFile the_root = null;
PsiDirectory ret = null;
// NOTE: we don't canonicalize target; must be ok in reasonable cases, and is far easier in unit test mode
target = FileUtil.toSystemIndependentName(target);
for (VirtualFile file : ProjectRootManager.getInstance(project).getContentRoots()) {
final String root_path = file.getPath();
if (target.startsWith(root_path)) {
the_rest = target.substring(root_path.length());
the_root = file;
break;
}
}
if (the_root == null) {
throw new IOException("Can't find '" + target + "' among roots");
}
if (the_rest != null) {
final LocalFileSystem lfs = LocalFileSystem.getInstance();
final PsiManager psi_mgr = PsiManager.getInstance(project);
String[] dirs = the_rest.split("/");
int i = 0;
if ("".equals(dirs[0]))
i = 1;
while (i < dirs.length) {
VirtualFile subdir = the_root.findChild(dirs[i]);
if (subdir != null) {
if (!subdir.isDirectory()) {
throw new IOException("Expected dir, but got non-dir: " + subdir.getPath());
}
} else {
subdir = the_root.createChildDirectory(lfs, dirs[i]);
}
VirtualFile init_vfile = subdir.findChild(PyNames.INIT_DOT_PY);
if (init_vfile == null)
init_vfile = subdir.createChildData(lfs, PyNames.INIT_DOT_PY);
/*
// here we could add an __all__ clause to the __init__.py.
// * there's no point to do so; we import the class directly;
// * we can't do this consistently since __init__.py may already exist and be nontrivial.
if (i == dirs.length - 1) {
PsiFile init_file = psi_mgr.findFile(init_vfile);
LOG.assertTrue(init_file != null);
final PyElementGenerator gen = PyElementGenerator.getInstance(project);
final PyStatement statement = gen.createFromText(LanguageLevel.getDefault(), PyStatement.class, PyNames.ALL + " = [\"" + lastName + "\"]");
init_file.add(statement);
}
*/
the_root = subdir;
i += 1;
}
ret = psi_mgr.findDirectory(the_root);
}
return ret;
}
Aggregations