system/src/sc/lang/js/JSRuntimeProcessor.java
SrcEntry saveJSTypeToFile(BodyTypeDeclaration td, Layer genLayer, String buildSrcDir, ArrayList<SrcEntry> resFiles) {
      //if (td.toString().equals("SyncLayer.SyncChange (layer:sys.sccore) (runtime: js) (transformed)"))
      //   System.out.println("***");
      StringBuilder result = new StringBuilder();

      // First look for an existing one in the build-path - if not found, then create a new one for this buildDir
      SrcEntry srcEnt = findJSSrcEntry(genLayer, td);
      SrcEntry resEnt = srcEnt;
      boolean needsSave = false;
      // No existing entry for this source file in this layer or previous
      if (srcEnt == null) {
         srcEnt = getJSSrcEntry(genLayer, buildSrcDir, td);
         resEnt = srcEnt;
         needsSave = true;
      }
      else {
         if (srcEnt.layer == genLayer) {
            needsSave = true;
            // TODO: is there any other way to ell if this type has changed?
         }
         else {
            // This type has been changed since the layer we are inheriting
            needsSave = td.getLayer().getLayerPosition() > srcEnt.layer.getLayerPosition();
            resEnt = getJSSrcEntryFromTypeName(genLayer, buildSrcDir, td.getFullTypeName());
         }
      }

      String resultStr = null;
      // The theory here is that we transform all of the types that should have changed and so if this type is not
      // transformed, it hasn't changed so there's no need to save it.
      // TODO: Unfortunately we are not always transforming the Anon classes and those that come from the external source path.
      // Those types won't get updated on an incremental build as it stands now.   We should replace this with a more reliable change
      // detection algorithm, like compare the hashes or register changed types in the detection phase.
      if (!needsSave) {
         File typeFile = new File(srcEnt.absFileName);
         if (typeFile.canRead())
            resultStr = FileUtil.getFileAsString(srcEnt.absFileName);
      }

      if (resultStr == null) {
         GenFileLineIndex lineIndex = system.options.genDebugInfo ? new GenFileLineIndex(resEnt.absFileName) : null;
         // convert the type declaration into JS and append it to result
         if (appendJSType(td, result, lineIndex)) {
            resultStr = result.toString();
            FileUtil.saveStringAsFile(resEnt.absFileName, resultStr, true);

            if (lineIndex != null) {
               File lineIndexFile = GenFileLineIndex.getLineIndexFile(resEnt.absFileName);
               lineIndexCache.put(resEnt.absFileName, lineIndex);
               lineIndex.numLines = ParseUtil.countLinesInNode(resultStr) + 1;
               lineIndex.saveLineIndexFile(lineIndexFile);
            }
         }
      }
      else if (resEnt != srcEnt) {
         copyFileAndIndex(srcEnt.absFileName, resEnt.absFileName);
      }

      if (resultStr != null) {
         resEnt.hash = StringUtil.computeHash(resultStr);

      // Note: this is done in LayeredSystem as this srcEnt gets returned via getProcessedFiles
      //genLayer.addSrcFileIndex(srcEnt.relFileName, );

         if (resFiles != null)
            resFiles.add(resEnt);

         List<Object> innerTypes = td.getAllInnerTypes(null, true, false);
         if (innerTypes != null) {
            for (Object innerTypeObj:innerTypes) {
               if (innerTypeObj instanceof BodyTypeDeclaration) {
                  if (innerTypeObj instanceof EnumConstant)
                     continue;
                  BodyTypeDeclaration innerType = (BodyTypeDeclaration) innerTypeObj;

                  SrcEntry innerSrcEnt = saveJSTypeToFile(innerType, genLayer, buildSrcDir, resFiles);
                  // We only really have to update the srcFileIndex if we saved a new version above - in which case the hash is filled in.
                  if (innerSrcEnt != null && innerSrcEnt.hash != null)
                     genLayer.addSrcFileIndex(innerSrcEnt.relFileName, innerSrcEnt.hash, innerSrcEnt.getExtension(), innerSrcEnt.absFileName);
               }
            }
         }
      }
      else if (resEnt != null)
         resFiles.add(resEnt);

      JavaModel javaModel = td.getJavaModel();
      // For Java types specified in a final/compiled layer, we will never transform - need to set this here to enable incremental JS builds.
      if (javaModel.transformedInLayer == null)
         javaModel.transformedInLayer = system.currentBuildLayer;

      //if (td.getEnclosingType() == null)
      //   javaModel.clearParseNode();

      return resEnt;
   }
/**
    * If necessary, saves the js/types/typeName.js file.  If this type has not been transformed,
    * this means the Java file did not change so we don't have to append it.  If we tried, it would fail
    * cause we only convert Java, not SC definitions to Javascript.    If that types file is gone
    * we should be doing a rebuild anyway?
    *
    * Note that this also returns the src entry to get processed files and must have the hash code filled in.
    */
If necessary, saves the js/types/typeName.js file.  If this type has not been transformed,
this means the Java file did not change so we don't have to append it.  If we tried, it would fail
cause we only convert Java, not SC definitions to Javascript.

Note that this also returns the src entry to get processed files and must have the hash code filled in.
If necessary saves the js / types / typeName . js file . If this type has not been transformed this means the Java file did not change so we don ' t have to append it . If we tried it would fail cause we only convert Java not SC definitions to Javascript . Note that this also returns the src entry to get processed files and must have the hash code filled in .
SrcEntry saveJSTypeToFile(BodyTypeDeclaration td, Layer genLayer, String buildSrcDir, ArrayList<SrcEntry> resFiles) {
      StringBuilder result = new StringBuilder();
      SrcEntry srcEnt = findJSSrcEntry(genLayer, td);
      SrcEntry resEnt = srcEnt;
      boolean needsSave = false;
      if (srcEnt == null) {
         srcEnt = getJSSrcEntry(genLayer, buildSrcDir, td);
         resEnt = srcEnt;
         needsSave = true;
      }
      else {
         if (srcEnt.layer == genLayer) {
            needsSave = true;
         }
         else {
            needsSave = td.getLayer().getLayerPosition() > srcEnt.layer.getLayerPosition();
            resEnt = getJSSrcEntryFromTypeName(genLayer, buildSrcDir, td.getFullTypeName());
         }
      }
      String resultStr = null;
      if (!needsSave) {
         File typeFile = new File(srcEnt.absFileName);
         if (typeFile.canRead())
            resultStr = FileUtil.getFileAsString(srcEnt.absFileName);
      }
      if (resultStr == null) {
         GenFileLineIndex lineIndex = system.options.genDebugInfo ? new GenFileLineIndex(resEnt.absFileName) : null;
         if (appendJSType(td, result, lineIndex)) {
            resultStr = result.toString();
            FileUtil.saveStringAsFile(resEnt.absFileName, resultStr, true);
            if (lineIndex != null) {
               File lineIndexFile = GenFileLineIndex.getLineIndexFile(resEnt.absFileName);
               lineIndexCache.put(resEnt.absFileName, lineIndex);
               lineIndex.numLines = ParseUtil.countLinesInNode(resultStr) + 1;
               lineIndex.saveLineIndexFile(lineIndexFile);
            }
         }
      }
      else if (resEnt != srcEnt) {
         copyFileAndIndex(srcEnt.absFileName, resEnt.absFileName);
      }
      if (resultStr != null) {
         resEnt.hash = StringUtil.computeHash(resultStr);
         if (resFiles != null)
            resFiles.add(resEnt);
         List<Object> innerTypes = td.getAllInnerTypes(null, true, false);
         if (innerTypes != null) {
            for (Object innerTypeObj:innerTypes) {
               if (innerTypeObj instanceof BodyTypeDeclaration) {
                  if (innerTypeObj instanceof EnumConstant)
                     continue;
                  BodyTypeDeclaration innerType = (BodyTypeDeclaration) innerTypeObj;
                  SrcEntry innerSrcEnt = saveJSTypeToFile(innerType, genLayer, buildSrcDir, resFiles);
                  if (innerSrcEnt != null && innerSrcEnt.hash != null)
                     genLayer.addSrcFileIndex(innerSrcEnt.relFileName, innerSrcEnt.hash, innerSrcEnt.getExtension(), innerSrcEnt.absFileName);
               }
            }
         }
      }
      else if (resEnt != null)
         resFiles.add(resEnt);
      JavaModel javaModel = td.getJavaModel();
      if (javaModel.transformedInLayer == null)
         javaModel.transformedInLayer = system.currentBuildLayer;
      return resEnt;
   }
SrcEntry saveJSTypeToFile ( BodyTypeDeclaration td , Layer genLayer , String buildSrcDir , ArrayList < SrcEntry > resFiles ) { StringBuilder result = new StringBuilder ( ) ; SrcEntry srcEnt = findJSSrcEntry ( genLayer , td ) ; SrcEntry resEnt = srcEnt ; boolean needsSave = false ; if ( srcEnt == null ) { srcEnt = getJSSrcEntry ( genLayer , buildSrcDir , td ) ; resEnt = srcEnt ; needsSave = true ; } else { if ( srcEnt . layer == genLayer ) { needsSave = true ; } else { needsSave = td . getLayer ( ) . getLayerPosition ( ) > srcEnt . layer . getLayerPosition ( ) ; resEnt = getJSSrcEntryFromTypeName ( genLayer , buildSrcDir , td . getFullTypeName ( ) ) ; } } String resultStr = null ; if ( ! needsSave ) { File typeFile = new File ( srcEnt . absFileName ) ; if ( typeFile . canRead ( ) ) resultStr = FileUtil . getFileAsString ( srcEnt . absFileName ) ; } if ( resultStr == null ) { GenFileLineIndex lineIndex = system . options . genDebugInfo ? new GenFileLineIndex ( resEnt . absFileName ) : null ; if ( appendJSType ( td , result , lineIndex ) ) { resultStr = result . toString ( ) ; FileUtil . saveStringAsFile ( resEnt . absFileName , resultStr , true ) ; if ( lineIndex != null ) { File lineIndexFile = GenFileLineIndex . getLineIndexFile ( resEnt . absFileName ) ; lineIndexCache . put ( resEnt . absFileName , lineIndex ) ; lineIndex . numLines = ParseUtil . countLinesInNode ( resultStr ) + 1 ; lineIndex . saveLineIndexFile ( lineIndexFile ) ; } } } else if ( resEnt != srcEnt ) { copyFileAndIndex ( srcEnt . absFileName , resEnt . absFileName ) ; } if ( resultStr != null ) { resEnt . hash = StringUtil . computeHash ( resultStr ) ; if ( resFiles != null ) resFiles . add ( resEnt ) ; List < Object > innerTypes = td . getAllInnerTypes ( null , true , false ) ; if ( innerTypes != null ) { for ( Object innerTypeObj : innerTypes ) { if ( innerTypeObj instanceof BodyTypeDeclaration ) { if ( innerTypeObj instanceof EnumConstant ) continue ; BodyTypeDeclaration innerType = ( BodyTypeDeclaration ) innerTypeObj ; SrcEntry innerSrcEnt = saveJSTypeToFile ( innerType , genLayer , buildSrcDir , resFiles ) ; if ( innerSrcEnt != null && innerSrcEnt . hash != null ) genLayer . addSrcFileIndex ( innerSrcEnt . relFileName , innerSrcEnt . hash , innerSrcEnt . getExtension ( ) , innerSrcEnt . absFileName ) ; } } } } else if ( resEnt != null ) resFiles . add ( resEnt ) ; JavaModel javaModel = td . getJavaModel ( ) ; if ( javaModel . transformedInLayer == null ) javaModel . transformedInLayer = system . currentBuildLayer ; return resEnt ; }
If necessary, saves the js/types/typeName.js file.
If necessary saves the js / types / typeName . js file .
//if (td.toString().equals("SyncLayer.SyncChange (layer:sys.sccore) (runtime: js) (transformed)")) //   System.out.println("***"); // First look for an existing one in the build-path - if not found, then create a new one for this buildDir // No existing entry for this source file in this layer or previous // TODO: is there any other way to ell if this type has changed? // This type has been changed since the layer we are inheriting // The theory here is that we transform all of the types that should have changed and so if this type is not // transformed, it hasn't changed so there's no need to save it. // TODO: Unfortunately we are not always transforming the Anon classes and those that come from the external source path. // Those types won't get updated on an incremental build as it stands now.   We should replace this with a more reliable change // detection algorithm, like compare the hashes or register changed types in the detection phase. // convert the type declaration into JS and append it to result // Note: this is done in LayeredSystem as this srcEnt gets returned via getProcessedFiles //genLayer.addSrcFileIndex(srcEnt.relFileName, ); // We only really have to update the srcFileIndex if we saved a new version above - in which case the hash is filled in. // For Java types specified in a final/compiled layer, we will never transform - need to set this here to enable incremental JS builds. //if (td.getEnclosingType() == null) //   javaModel.clearParseNode();