Example #1
0
  public void run() {
    long start = System.currentTimeMillis();

    combineRequire(require);

    ArrayList<String> combinedFiles = new ArrayList<String>();
    StringBuilder finalCodes = new StringBuilder();

    for (Module m : processedModules) {
      combinedFiles.add(m.getName());
      finalCodes.append(m.getCode());
    }

    String re =
        "/*\n Combined processedModules by KISSY Module Compiler: \n\n "
            + ArrayUtils.join(combinedFiles.toArray(new String[combinedFiles.size()]), "\n ")
            + "\n*/\n\n"
            + finalCodes.toString();

    if (output != null) {
      FileUtils.outputContent(re, output, outputEncoding);
      System.out.println("success generated: " + output);
    } else {
      System.out.println(re);
    }

    if (outputDependency != null && dependencies.size() != 0) {
      String allRs = "";
      for (String r : dependencies) {
        if (r.startsWith("#")) {
          allRs += "," + r.substring(1);
        } else {
          allRs += ",'" + r + "'";
        }
      }
      re = DEP_PREFIX + "'" + require + "': {requires: [" + allRs.substring(1) + "]}" + DEP_SUFFIX;
      FileUtils.outputContent(re, outputDependency, outputEncoding);
      System.out.println("success generated: " + outputDependency);
    }

    System.out.print("duration: " + (System.currentTimeMillis() - start));
  }
Example #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);
  }