Package org.bladerunnerjs.model

Source Code of org.bladerunnerjs.model.ThirdpartyLibManifest

package org.bladerunnerjs.model;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.bladerunnerjs.model.exception.ConfigException;
import org.bladerunnerjs.utility.RelativePathUtility;

public class ThirdpartyLibManifest extends ConfFile<ThirdpartyLibYamlManifest>
{
  public static final String LIBRARY_MANIFEST_FILENAME = "thirdparty-lib.manifest";
 
  public static final String commaWithOptionalSpacesSeparator = "[\\s]*,[\\s]*";
 
  private final FileInfo fileInfo;
  private final File assetLocationDir;
  private final AssetLocation assetLocation;
 
  public ThirdpartyLibManifest(AssetLocation assetLocation) throws ConfigException {
    super(assetLocation, ThirdpartyLibYamlManifest.class, assetLocation.file(LIBRARY_MANIFEST_FILENAME));
    fileInfo = assetLocation.root().getFileInfo(assetLocation.dir());
    assetLocationDir = assetLocation.dir();
    this.assetLocation = assetLocation;
  }
 
  public List<String> getDepends() throws ConfigException
  {
    return listify(getConf().depends);
  }
 
  public List<File> getJsFiles() throws ConfigException {
    return getFilesForConfigPaths(getJs(), ".js");
  }
 
  public List<File> getCssFiles() throws ConfigException {
    return getFilesForConfigPaths(getCss(), ".css");
  }

  public String getExports() throws ConfigException
  {
    return StringUtils.replaceChars(getConf().exports, " ", "");
  }
 
  public boolean getCommonjsDefinition() throws ConfigException
  {
    return getConf().commonjsDefinition;
  }
 
 
  private List<String> getJs() throws ConfigException
  {
    return listify(getConf().js); // TODO: see if we should also limit to only loading js in the root directory by default too
  }
 
  private List<String> getCss() throws ConfigException
  {
    return listify(getConf().css);
 
 
  private List<String> listify(String value)
  {
    if (value != null && !value.equals(""))
    {
      return Arrays.asList(value.split(commaWithOptionalSpacesSeparator));
    }
    return Collections.emptyList();
  }
 
  private List<File> getFilesForConfigPaths(List<String> configPaths, String fileExtension) throws ConfigException
  {
    if (configPaths.isEmpty())
    {
      return findAllFilesWithExtension(fileExtension, false);
    }
    else if (configPaths.size() == 1)
    {
      String firstConfigPath = configPaths.get(0);
      if (firstConfigPath.equals("*"+fileExtension))
      {
        return findAllFilesWithExtension(fileExtension, false);
      }
      else if (firstConfigPath.equals("**/*"+fileExtension))
      {
        return findAllFilesWithExtension(fileExtension, true);
      }
    }
    return getFilesWithPaths(configPaths);
  }
 
  private List<File> getFilesWithPaths(List<String> filePaths) throws ConfigException
  {
    List<File> foundFiles = new ArrayList<File>();
    String assetLocationDirPath = assetLocationDir.getAbsolutePath();
   
    for (String filePath : filePaths)
    {
      String fullFilePath = assetLocationDirPath + File.separator + filePath;
      File file = new File(fullFilePath);
     
      if(file.exists()){
        foundFiles.add(file);
      }else{
        String relativeManifestPath = RelativePathUtility.get(assetLocation.root().getFileInfoAccessor(), assetLocation.assetContainer().root().dir(), assetLocation.file(LIBRARY_MANIFEST_FILENAME));
        throw new ConfigException("Unable to find the file '" + filePath + "' required in the manifest at '" + relativeManifestPath + "'.");
      }
    }
    return foundFiles;
  }
 
  private List<File> findAllFilesWithExtension(String extension, boolean includeNestedDirs)
  {
    List<File> foundFiles = new ArrayList<File>();
    List<File> files = (includeNestedDirs) ? fileInfo.nestedFiles() : fileInfo.filesAndDirs();
   
    for (File f : files)
    {
      if (f.getName().endsWith(extension))
      {
        foundFiles.add(f);
      }
    }
    return foundFiles;
  }
 
}
TOP

Related Classes of org.bladerunnerjs.model.ThirdpartyLibManifest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.