Search in sources :

Example 21 with ModifiableModuleModel

use of com.intellij.openapi.module.ModifiableModuleModel in project intellij-plugins by JetBrains.

the class FlexTestUtils method createModule.

public static Module createModule(Project project, final String moduleName, final VirtualFile moduleContent) throws IOException {
    return WriteAction.compute(() -> {
        final ModifiableModuleModel m1 = ModuleManager.getInstance(project).getModifiableModel();
        final VirtualFile moduleDir = project.getBaseDir().createChildDirectory(JSTestUtils.class, moduleName);
        final Module result = m1.newModule(moduleDir.getPath() + "/" + moduleName + ".iml", FlexModuleType.getInstance().getId());
        m1.commit();
        if (moduleContent != null) {
            VfsUtil.copyDirectory(JSTestUtils.class, moduleContent, moduleDir, null);
            PsiTestUtil.addSourceRoot(result, moduleDir);
        }
        return result;
    });
}
Also used : ModifiableModuleModel(com.intellij.openapi.module.ModifiableModuleModel) Module(com.intellij.openapi.module.Module)

Example 22 with ModifiableModuleModel

use of com.intellij.openapi.module.ModifiableModuleModel in project intellij-plugins by JetBrains.

the class FlexScopeTest method testCircularDependency.

public void testCircularDependency() throws Exception {
    final Sdk sdk = FlexTestUtils.createSdk(FlexTestUtils.getPathToCompleteFlexSdk("4.6"), null, true);
    WriteAction.run(() -> {
        final ModifiableModuleModel m1 = ModuleManager.getInstance(myProject).getModifiableModel();
        final VirtualFile moduleDir = myProject.getBaseDir().createChildDirectory(this, "module2");
        final Module module2 = m1.newModule(moduleDir.getPath(), FlexModuleType.getInstance().getId());
        m1.commit();
        PsiTestUtil.addSourceRoot(module2, moduleDir);
        FlexTestUtils.modifyConfigs(myProject, editor -> {
            final ModifiableFlexBuildConfiguration app1 = editor.getConfigurations(myModule)[0];
            app1.setName("app1");
            FlexTestUtils.setSdk(app1, sdk);
            final ModifiableFlexBuildConfiguration lib1 = editor.getConfigurations(module2)[0];
            lib1.setNature(new BuildConfigurationNature(TargetPlatform.Web, false, OutputType.Library));
            app1.setName("lib1");
            FlexTestUtils.setSdk(lib1, sdk);
            final ModifiableFlexBuildConfiguration lib2 = editor.createConfiguration(myModule);
            lib2.setName("lib2");
            lib2.setNature(new BuildConfigurationNature(TargetPlatform.Web, false, OutputType.Library));
            FlexTestUtils.setSdk(lib2, sdk);
            final ModifiableBuildConfigurationEntry dep1 = editor.createBcEntry(app1.getDependencies(), lib1, null);
            app1.getDependencies().getModifiableEntries().add(dep1);
            final ModifiableBuildConfigurationEntry dep2 = editor.createBcEntry(lib1.getDependencies(), lib2, null);
            lib1.getDependencies().getModifiableEntries().add(dep2);
        });
    });
    doHighlightingTest("");
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) BuildConfigurationNature(com.intellij.flex.model.bc.BuildConfigurationNature) ModifiableModuleModel(com.intellij.openapi.module.ModifiableModuleModel) Sdk(com.intellij.openapi.projectRoots.Sdk) Module(com.intellij.openapi.module.Module)

Example 23 with ModifiableModuleModel

use of com.intellij.openapi.module.ModifiableModuleModel in project intellij-plugins by JetBrains.

the class FlashBuilderImportTest method importProject.

protected void importProject(final String dotProjectFileContent, final String dotActionScriptPropertiesFileContent, final Collection<String> otherProjectNames, @Nullable final String flashBuilderWorkspacePath) throws IOException, ConfigurationException {
    final VirtualFile dotProjectFile = addFileWithContent(FlashBuilderImporter.DOT_PROJECT, dotProjectFileContent, myFlashBuilderProjectDir);
    addFileWithContent(FlashBuilderImporter.DOT_ACTION_SCRIPT_PROPERTIES, dotActionScriptPropertiesFileContent, myFlashBuilderProjectDir);
    final FlashBuilderProject flashBuilderProject = FlashBuilderProjectLoadUtil.loadProject(dotProjectFile, false);
    final Collection<FlashBuilderProject> allFBProjects = new ArrayList<>();
    allFBProjects.add(flashBuilderProject);
    for (String name : otherProjectNames) {
        allFBProjects.add(FlashBuilderProjectLoadUtil.getDummyFBProject(name));
    }
    final String moduleFilePath = myFlashBuilderProjectDir.getPath() + "/" + flashBuilderProject.getName() + ModuleFileType.DOT_DEFAULT_EXTENSION;
    final ModifiableModuleModel moduleModel = ModuleManager.getInstance(myProject).getModifiableModel();
    myModule = moduleModel.newModule(moduleFilePath, FlexModuleType.getInstance().getId());
    final ModifiableRootModel rootModel = ModuleRootManager.getInstance(myModule).getModifiableModel();
    final FlashBuilderSdkFinder sdkFinder = new FlashBuilderSdkFinder(myProject, StringUtil.notNullize(flashBuilderWorkspacePath), Collections.singletonList(flashBuilderProject));
    final FlexProjectConfigurationEditor flexEditor = FlexProjectConfigurationEditor.createEditor(myProject, Collections.singletonMap(myModule, rootModel), null, null);
    new FlashBuilderModuleImporter(myProject, flexEditor, allFBProjects, sdkFinder).setupModule(rootModel, flashBuilderProject);
    flexEditor.commit();
    ApplicationManager.getApplication().runWriteAction(() -> ModifiableModelCommitter.multiCommit(Collections.singletonList(rootModel), moduleModel));
}
Also used : FlexProjectConfigurationEditor(com.intellij.lang.javascript.flex.projectStructure.model.impl.FlexProjectConfigurationEditor) ModifiableModuleModel(com.intellij.openapi.module.ModifiableModuleModel)

Example 24 with ModifiableModuleModel

use of com.intellij.openapi.module.ModifiableModuleModel in project intellij-plugins by JetBrains.

the class FlashBuilderImporter method commit.

public List<Module> commit(final Project project, final ModifiableModuleModel model, final ModulesProvider modulesProvider, final ModifiableArtifactModel artifactModel) {
    //FlexModuleBuilder.setupResourceFilePatterns(project);
    final boolean needToCommit = model == null;
    final ModifiableModuleModel moduleModel = model != null ? model : ModuleManager.getInstance(project).getModifiableModel();
    final List<String> paths = getList();
    final boolean isArchive = paths.size() == 1 && FlashBuilderProjectFinder.hasArchiveExtension(paths.get(0));
    final List<String> dotProjectPaths = getDotProjectPaths(project);
    final List<FlashBuilderProject> flashBuilderProjects = FlashBuilderProjectLoadUtil.loadProjects(dotProjectPaths, isArchive);
    final Map<FlashBuilderProject, ModifiableRootModel> flashBuilderProjectToModifiableModelMap = new THashMap<>();
    final Map<Module, ModifiableRootModel> moduleToModifiableModelMap = new THashMap<>();
    final Set<String> moduleNames = new THashSet<>(flashBuilderProjects.size());
    final FlexProjectConfigurationEditor currentFlexEditor = FlexBuildConfigurationsExtension.getInstance().getConfigurator().getConfigEditor();
    assert needToCommit == (currentFlexEditor == null);
    for (FlashBuilderProject flashBuilderProject : flashBuilderProjects) {
        final String moduleName = makeUnique(flashBuilderProject.getName(), moduleNames);
        moduleNames.add(moduleName);
        final String moduleFilePath = flashBuilderProject.getProjectRootPath() + "/" + moduleName + ModuleFileType.DOT_DEFAULT_EXTENSION;
        if (LocalFileSystem.getInstance().findFileByPath(moduleFilePath) != null) {
            ApplicationManager.getApplication().runWriteAction(() -> ModuleBuilder.deleteModuleFile(moduleFilePath));
        }
        final Module module = moduleModel.newModule(moduleFilePath, FlexModuleType.getInstance().getId());
        final ModifiableRootModel rootModel = currentFlexEditor != null ? currentFlexEditor.getModifiableRootModel(module) : ModuleRootManager.getInstance(module).getModifiableModel();
        flashBuilderProjectToModifiableModelMap.put(flashBuilderProject, rootModel);
        moduleToModifiableModelMap.put(module, rootModel);
    }
    final FlexProjectConfigurationEditor flexConfigEditor = currentFlexEditor != null ? currentFlexEditor : FlexProjectConfigurationEditor.createEditor(project, moduleToModifiableModelMap, null, null);
    final FlashBuilderSdkFinder sdkFinder = new FlashBuilderSdkFinder(project, getParameters().myInitiallySelectedPath, flashBuilderProjects);
    final FlashBuilderModuleImporter flashBuilderModuleImporter = new FlashBuilderModuleImporter(project, flexConfigEditor, flashBuilderProjects, sdkFinder);
    for (final FlashBuilderProject flashBuilderProject : flashBuilderProjects) {
        flashBuilderModuleImporter.setupModule(flashBuilderProjectToModifiableModelMap.get(flashBuilderProject), flashBuilderProject);
    }
    if (needToCommit) {
        try {
            flexConfigEditor.commit();
        } catch (ConfigurationException e) {
            Logger.getInstance(FlashBuilderImporter.class).error(e);
        }
        ApplicationManager.getApplication().runWriteAction(() -> {
            ModifiableModelCommitter.multiCommit(moduleToModifiableModelMap.values(), moduleModel);
        });
    }
    return new ArrayList<>(moduleToModifiableModelMap.keySet());
}
Also used : THashSet(gnu.trove.THashSet) ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) FlexProjectConfigurationEditor(com.intellij.lang.javascript.flex.projectStructure.model.impl.FlexProjectConfigurationEditor) ModifiableModuleModel(com.intellij.openapi.module.ModifiableModuleModel) THashMap(gnu.trove.THashMap) ConfigurationException(com.intellij.openapi.options.ConfigurationException) Module(com.intellij.openapi.module.Module)

Example 25 with ModifiableModuleModel

use of com.intellij.openapi.module.ModifiableModuleModel in project intellij-leiningen-plugin by derkork.

the class ModuleCreationUtils method importModule.

/**
     * This method imports a leiningen module from a leiningen project file and imports it into the idea project.
     * <p/>
     * Notes:
     * <p/>
     * Each of the IDEA components has a getModifiableModel on it.  This method returns a new instance each time you
     * invoke it.  Once you have a modifiable model of the component you wish to update, you mutate it to the state
     * you wish.  Once you're done, you call commit() on the modifiable model and it updates the component it came from.
     * <p/>
     * Since a lot of the components are persisted in files, commit() updates these files as well.  Therefore you need
     * to make any calls to commit() from within a WriteAction.
     *
     * @param ideaProject The IDEA project to add the leiningen module to.
     * @param leinProjectFile  The leiningen project file
     * @return The leiningen project map.
     */
public Map importModule(Project ideaProject, VirtualFile leinProjectFile) {
    ClassPathUtils.getInstance().switchToPluginClassLoader();
    Map projectMap = LeiningenAPI.loadProject(leinProjectFile.getPath());
    String name = (String) projectMap.get(LEIN_PROJECT_NAME);
    final ModifiableModuleModel moduleManager = createModuleManager(ideaProject);
    final ModifiableRootModel module = createModule(moduleManager, leinProjectFile.getParent().getPath(), name);
    initializeModulePaths(projectMap, module, leinProjectFile.getParent());
    ProjectRootManagerEx rootManager = ProjectRootManagerEx.getInstanceEx(ideaProject);
    module.setSdk(rootManager.getProjectSdk());
    //Setup the dependencies
    // Based loosely on org.jetbrains.idea.maven.importing.MavenRootModelAdapter#addLibraryDependency
    //We could use the module table here, but then the libraries wouldn't be shared across modules.
    final LibraryTable.ModifiableModel projectLibraries = ProjectLibraryTable.getInstance(ideaProject).getModifiableModel();
    //Load all the dependencies from the project file
    List dependencyMaps = LeiningenAPI.loadDependencies(leinProjectFile.getCanonicalPath());
    final List<LibraryInfo> dependencies = initializeDependencies(module, projectLibraries, dependencyMaps);
    new WriteAction() {

        @Override
        protected void run(Result result) throws Throwable {
            for (LibraryInfo library : dependencies) {
                library.modifiableModel.commit();
            }
            //Save the project libraries
            projectLibraries.commit();
            //Save the module itself to the module file.
            module.commit();
            //Save the list of modules that are in this project to the IDEA project file
            moduleManager.commit();
        }
    }.execute();
    return projectMap;
}
Also used : WriteAction(com.intellij.openapi.application.WriteAction) Result(com.intellij.openapi.application.Result) ProjectRootManagerEx(com.intellij.openapi.roots.ex.ProjectRootManagerEx) ProjectLibraryTable(com.intellij.openapi.roots.impl.libraries.ProjectLibraryTable) LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) ModifiableModuleModel(com.intellij.openapi.module.ModifiableModuleModel) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Aggregations

ModifiableModuleModel (com.intellij.openapi.module.ModifiableModuleModel)44 Module (com.intellij.openapi.module.Module)29 VirtualFile (com.intellij.openapi.vfs.VirtualFile)13 ModuleManager (com.intellij.openapi.module.ModuleManager)10 Project (com.intellij.openapi.project.Project)9 NotNull (org.jetbrains.annotations.NotNull)8 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)5 File (java.io.File)5 Result (com.intellij.openapi.application.Result)4 LibraryTable (com.intellij.openapi.roots.libraries.LibraryTable)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 StdModuleTypes (com.intellij.openapi.module.StdModuleTypes)3 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)3 Task (com.intellij.openapi.progress.Task)3 ProjectLibraryTable (com.intellij.openapi.roots.impl.libraries.ProjectLibraryTable)3 Nullable (org.jetbrains.annotations.Nullable)3 Project (aQute.bnd.build.Project)2 MoveModulesToSubGroupAction (com.intellij.ide.projectView.actions.MoveModulesToSubGroupAction)2 FlexProjectConfigurationEditor (com.intellij.lang.javascript.flex.projectStructure.model.impl.FlexProjectConfigurationEditor)2