示例#1
0
  public static void commandRunnerCLI(String[] args) throws Exception {

    Options options = new Options();
    options.addOption("encodings", true, "baseUrls 's encodings");
    options.addOption("baseUrls", true, "baseUrls");
    options.addOption("require", true, "require");
    options.addOption("excludeReg", true, "excludeReg");
    options.addOption("output", true, "output");
    options.addOption("v", "version", false, "version");
    options.addOption("outputEncoding", true, "outputEncoding");
    options.addOption("outputDependency", true, "outputDependency");
    options.addOption("fixModuleName", true, "fixModuleName");

    // create the command line parser
    CommandLineParser parser = new GnuParser();
    CommandLine line;

    try {
      // parse the command line arguments
      line = parser.parse(options, args);
    } catch (ParseException exp) {
      System.out.println("Unexpected exception:" + exp.getMessage());
      return;
    }

    if (line.hasOption("v")) {
      System.out.println("KISSY Module Compiler 1.3.1");
      return;
    }

    Main builder = new Main();

    Packages packages = builder.getPackages();

    String encodingStr = line.getOptionValue("encodings");
    if (encodingStr != null) {
      packages.setEncodings(encodingStr.split(","));
    }

    String baseUrlStr = line.getOptionValue("baseUrls");
    if (baseUrlStr != null) {
      packages.setBaseUrls(baseUrlStr.split(","));
    }

    String fixModuleName = line.getOptionValue("fixModuleName");
    if (fixModuleName != null) {
      builder.setFixModuleName(true);
    }

    builder.setRequire(line.getOptionValue("require"));

    String excludeReg = line.getOptionValue("excludeReg");
    if (excludeReg != null) {
      builder.setExcludePattern(Pattern.compile(excludeReg));
    }

    builder.setOutput(line.getOptionValue("output"));

    String outputEncoding = line.getOptionValue("outputEncoding");
    if (outputEncoding != null) {
      builder.setOutputEncoding(outputEncoding);
    }

    builder.setOutputDependency(line.getOptionValue("outputDependency"));

    builder.run();
  }
示例#2
0
  /**
   * x -> a,b,c : x depends on a,b,c add a,b,c then add x to final code buffer
   *
   * @param requiredModuleName module name required
   */
  private void combineRequire(String requiredModuleName) {

    // if css file, do not combine with js files
    if (requiredModuleName.endsWith(".css")
        ||
        // conditional loader
        requiredModuleName.startsWith("#")) {
      this.addDependency(requiredModuleName);
      return;
    }

    // if specify exclude this module, just return
    if (excludePattern != null && excludePattern.matcher(requiredModuleName).matches()) {
      this.addDependency(requiredModuleName);
      return;
    }

    if (!packages.isModuleExists(requiredModuleName)) {
      System.out.println("warning: module not found: " + requiredModuleName);
      this.addDependency(requiredModuleName);
      return;
    }

    Module requiredModule = packages.getModuleFromName(requiredModuleName);

    if (requiredModule == null) {
      System.err.println("error: invalid module: " + requiredModuleName);
      System.exit(1);
    }

    // x -> a,b,c
    // a -> b
    // reduce redundant parse and recursive
    if (processedModules.contains(requiredModule)) {
      return;
    }

    if (modulesVisited.contains(requiredModuleName)) {
      String error =
          "cyclic dependence: "
              + ArrayUtils.join(modulesVisited.toArray(new String[modulesVisited.size()]), ",")
              + ","
              + requiredModuleName;
      // if silence ,just return
      System.err.println(error);
      System.exit(1);
      return;
    }

    // mark as start for cyclic detection
    modulesVisited.add(requiredModuleName);

    requiredModule.completeModuleName(fixModuleName);

    String[] requires = requiredModule.getRequires();

    for (String require : requires) {
      combineRequire(require);
    }

    // remove mark for cyclic detection
    modulesVisited.remove(modulesVisited.size() - 1);

    processedModules.add(requiredModule);
  }