Esempio n. 1
0
  protected final void collectModels(
      IFile dir,
      String package_,
      String relativePath,
      Map<String, String> options,
      Collection<SModel> models) {
    if (FileSystem.getInstance().isFileIgnored(dir.getName())) return;
    if (!dir.isDirectory()) return;

    List<IFile> files = dir.getChildren();
    options.put(ModelFactory.OPTION_PACKAGE, package_);
    for (IFile file : files) {
      String fileName = file.getName();
      String extension = FileUtil.getExtension(fileName);

      if (extension == null) continue;
      ModelFactory modelFactory = PersistenceFacade.getInstance().getModelFactory(extension);
      if (modelFactory == null || file.isDirectory()) continue;

      FileDataSource source = new FileDataSource(file, this);
      options.put(ModelFactory.OPTION_RELPATH, combinePath(relativePath, fileName));
      String fileNameWE = FileUtil.getNameWithoutExtension(fileName);
      options.put(
          ModelFactory.OPTION_MODELNAME,
          package_ != null
              ? (package_.isEmpty() ? fileNameWE : package_ + "." + fileNameWE)
              : null);
      try {
        SModel model = modelFactory.load(source, Collections.unmodifiableMap(options));
        ((SModelBase) model).setModelRoot(this);
        models.add(model);
      } catch (UnsupportedDataSourceException ex) {
        /* model factory registration problem: ignore */
      }
    }

    options.put(ModelFactory.OPTION_RELPATH, relativePath);
    for (FolderModelFactory factory : PersistenceRegistry.getInstance().getFolderModelFactories()) {
      for (DataSource dataSource : factory.createDataSources(this, dir)) {
        SModel model = factory.load(dataSource, Collections.unmodifiableMap(options));
        ((SModelBase) model).setModelRoot(this);
        models.add(model);
      }
    }

    for (IFile childDir : files) {
      if (childDir.isDirectory()) {
        String name = childDir.getName();
        String innerPackage =
            package_ != null && JavaNameUtil.isJavaIdentifier(name)
                ? (package_.isEmpty() ? name : package_ + "." + name)
                : null;
        String innerPath = combinePath(relativePath, name);
        collectModels(childDir, innerPackage, innerPath, options, models);
      }
    }
  }
Esempio n. 2
0
  private <T> void readModuleDescriptorsFromFolder(
      IFile file,
      Set<IFile> excludes,
      List<T> result,
      boolean refreshFiles,
      DescriptorReader<T> reader) {
    if (!needProcess(file, excludes)) return;
    if (refreshFiles) {
      FileSystem.getInstance().refresh(file);
    }

    // if this is a jar dir, we need to go to modules sub dir or check for META-INF/module.xml
    // if this is just good old plain directory, we check every file in it

    if (file.getPath().endsWith(JAR + JAR_SEPARATOR)) {

      IFile moduleXml = file.getDescendant(META_INF).getDescendant(MODULE_XML);
      if (moduleXml.exists() && !moduleXml.isDirectory()) {
        ModuleDescriptor moduleDescriptor = loadModuleDescriptor(moduleXml);
        if (moduleDescriptor != null) {
          T descriptor = reader.read(new ModuleHandle(moduleXml, moduleDescriptor));
          if (descriptor != null) {
            result.add(descriptor);
          }
        }
      } else {
        IFile dirInJar = file.getDescendant(MODULES_DIR);
        if (dirInJar.exists() && dirInJar.isDirectory()) {
          readModuleDescriptorsFromFolder(dirInJar, excludes, result, refreshFiles, reader);
        }
      }

    } else {

      // first, we read from files
      // this way all modules roots, sources/classes folders are in excludes and we do not even go
      // into them
      List<IFile> children = file.getChildren();
      ArrayList<IFile> folders = new ArrayList<IFile>();
      for (IFile child : children) {
        if (!child.isDirectory()) {
          readModuleDescriptorsFromFile(child, excludes, result, refreshFiles, reader);
        } else {
          folders.add(child);
        }
      }

      // now read from folders
      for (IFile child : folders) {
        readModuleDescriptorsFromFolder(child, excludes, result, refreshFiles, reader);
      }
    }
  }
  private Set<SModel> getModels(IFile dir, String pkg) {

    Set<SModel> models = SetSequence.fromSet(new HashSet<SModel>());

    MultiStreamDataSource dataSource = newDataSource(dir);
    boolean thereAreJavaFiles = dataSource.getAvailableStreams().iterator().hasNext();

    if (thereAreJavaFiles) {
      SModelReference modelRef = Util.makeModelReference(pkg, getModule());
      // it can be a default package
      if (modelRef != null) {
        JavaSourceStubModelDescriptor model =
            new JavaSourceStubModelDescriptor(modelRef, dataSource, pkg);
        SetSequence.fromSet(models).addElement(model);

        SetSequence.fromSet(myDataSources).addElement(dataSource);
      }
    }

    // should be one line: dir.getChildren().where cannot be entered...
    Iterable<IFile> children = dir.getChildren();
    Iterable<IFile> subDirs =
        Sequence.fromIterable(children)
            .where(
                new IWhereFilter<IFile>() {
                  public boolean accept(IFile it) {
                    return it.isDirectory();
                  }
                });

    for (IFile subDir : Sequence.fromIterable(subDirs)) {
      String subPkg = (pkg.equals("") ? subDir.getName() : pkg + "." + subDir.getName());
      Set<SModel> set = getModels(subDir, subPkg);
      SetSequence.fromSet(models).addSequence(SetSequence.fromSet(set));
    }

    return models;
  }
  @Override
  public void dispose() {
    CodeOrchestraGenerateManager codeOrchestraGenerateManager =
        myProcessor.getProject().getComponent(CodeOrchestraGenerateManager.class);
    CodeOrchestraGenerationContext currentContext =
        codeOrchestraGenerateManager.getCurrentContext();

    List<String> relativeAssetsPaths;
    if (currentContext == null) {
      relativeAssetsPaths = Collections.emptyList();
    } else {
      relativeAssetsPaths = currentContext.getRelativeAssetsPaths();
    }

    Set<IFile> directories = new HashSet<IFile>();

    // RF-826
    // directories.add(myOutputDir);

    directories.add(myCachesOutputDir);
    for (IFile f : mySavedFiles) {
      // RE-3231
      if (f instanceof IdeaFile) {
        IdeaFile ideaFile = (IdeaFile) f;
        // RE-3635
        if (ideaFile.getName().endsWith(TraceInfoCache.TRACE_FILE_NAME)) {
          continue;
        }
      }
      directories.add(f.getParent());
    }

    // clear garbage
    final List<IFile> filesToDelete = new ArrayList<IFile>();
    for (IFile dir : directories) {
      for (IFile outputDirectoryFile : dir.getChildren()) {
        if (outputDirectoryFile.isDirectory()) continue;
        if (mySavedFiles.contains(outputDirectoryFile)) continue;

        if (outputDirectoryFile instanceof IdeaFile) {
          IdeaFile ideaFile = (IdeaFile) outputDirectoryFile;

          // Ignore assets
          for (String relativeAssetsPath : relativeAssetsPaths) {
            if (ideaFile.getPath().endsWith(relativeAssetsPath)) {
              continue;
            }
          }

          // Ignore debug file
          if (ASDebugGenFile.FILE_NAME.equals(ideaFile.getName())) {
            continue;
          }

          // Ignore generated model digest file
          if (ideaFile.getName().endsWith(".generated")) {
            continue;
          }
        }

        filesToDelete.add(outputDirectoryFile);
      }
    }

    myProcessor.filesToDelete(filesToDelete);
    myProcessor.invalidateModel(myModelDescriptor);
  }