def _rollup(ctx):

  baseFolder = "{0}/rollup.runfiles/{1}".format(ctx.executable.rollup.dirname, ctx.workspace_name)
  rollupConfig = "{0}/rollup.config.js".format(baseFolder)

  es2105SourceFolder = "bazel-out/host/bin/{0}prod_source.es6".format(ctx.build_file_path.replace("BUILD.bazel", ""))
  sourceRoot = ctx.build_file_path.split("/")[0]

  args = ["--config", rollupConfig]
  args += ["--output.dir", ctx.outputs.bundle_es6_main.path.replace("main.js", "")]
  args += ["--output.format", "cjs"]
  args += ["--es6", es2105SourceFolder]
  args += ["--workspace", ctx.workspace_name]
  args += ["--sourceRoot", sourceRoot]


  print(ctx.build_file_path)

  ctx.action(
      executable = ctx.executable.rollup,
      outputs = [ctx.outputs.bundle_es6_main, 
                 ctx.outputs.bundle_es6_spreadsheet,
                 ctx.outputs.bundle_es6_treeview,
                 ctx.outputs.bundle_es6_form,
                 ctx.outputs.bundle_es6_treeview,
                 ctx.outputs.bundle_es6_lazy_treeview,

                 ctx.outputs.bundle_es6_chunk1,
                 ctx.outputs.bundle_es6_chunk2
                 ],
      arguments = args
  )

  return struct()

rollup = rule(
    implementation = _rollup,
    attrs = {
        "rollup": attr.label(default=Label("//src/apps/demo:rollup"), executable=True, cfg="host", allow_files=True)
    },
    outputs = {
        "bundle_es6_main": "main.js",
        "bundle_es6_spreadsheet": "spreadsheet.module.ngfactory.js",
        "bundle_es6_form": "survey.module.ngfactory.js",
        "bundle_es6_treeview": "tree-view.module.ngfactory.js",
        "bundle_es6_lazy_treeview": "lazy-loaded-tree-view.module.ngfactory.js",

        #Chunks generated by Rollup
        "bundle_es6_chunk1": "chunk1.js",
        "bundle_es6_chunk2": "chunk2.js"
    }
)
def _rollup(ctx):

  baseFolder = "{0}/rollup.runfiles/{1}".format(ctx.executable.rollup.dirname, ctx.workspace_name)
  rollupConfig = "{0}/rollup.config.js".format(baseFolder)

  es2105SourceFolder = "bazel-out/host/bin/{0}prod_source.es6".format(ctx.build_file_path.replace("BUILD.bazel", ""))
  sourceRoot = ctx.build_file_path.split("/")[0]

  args = ["--config", rollupConfig]
  args += ["--output.dir", ctx.outputs.bundle_es6_main.path.replace("main.js", "")]
  args += ["--output.format", "cjs"]
  args += ["--es6", es2105SourceFolder]
  args += ["--workspace", ctx.workspace_name]
  args += ["--sourceRoot", sourceRoot]


  print(ctx.build_file_path)

  ctx.action(
      executable = ctx.executable.rollup,
      outputs = [ctx.outputs.bundle_es6_main, 
                 ctx.outputs.bundle_es6_spreadsheet,
                 ctx.outputs.bundle_es6_treeview,
                 ctx.outputs.bundle_es6_form,
                 ctx.outputs.bundle_es6_treeview,
                 ctx.outputs.bundle_es6_lazy_treeview,

                 ctx.outputs.bundle_es6_chunk1,
                 ctx.outputs.bundle_es6_chunk2
                 ],
      arguments = args
  )

  return struct()

rollup = rule(
    implementation = _rollup,
    attrs = {
        "rollup": attr.label(default=Label("//src/apps/demo:rollup"), executable=True, cfg="host", allow_files=True)
    },
    outputs = {
        "bundle_es6_main": "main.js",
        "bundle_es6_spreadsheet": "spreadsheet.module.ngfactory.js",
        "bundle_es6_form": "survey.module.ngfactory.js",
        "bundle_es6_treeview": "tree-view.module.ngfactory.js",
        "bundle_es6_lazy_treeview": "lazy-loaded-tree-view.module.ngfactory.js",

        #Chunks generated by Rollup
        "bundle_es6_chunk1": "chunk1.js",
        "bundle_es6_chunk2": "chunk2.js"
    }
)
#Chunks generated by Rollup