Refactoring features rule and generation

Change-Id: Id3eb8b595f858c7d362414c7b7ff5db0ae269c1d
diff --git a/buck-tools/onos_app.py b/buck-tools/onos_app.py
index 30f6edf..dc2e5fa 100755
--- a/buck-tools/onos_app.py
+++ b/buck-tools/onos_app.py
@@ -51,31 +51,47 @@
                 parts[4] = parts[4][len(NON_OSGI_TAG):]
     return prefix + '/'.join(parts) + suffix
 
-def generateFeatureFile(feature_name,
-                        version,
-                        title,
-                        feature_repo_name,
+def generateFeatureFile(feature_repo_name,
                         features = [],
-                        bundles = [],
                         **kwargs):
     values = {
+        'feature_repo_name' : '-'.join(feature_repo_name.split(':')[1:3]),
+    }
+
+    output = FEATURES_HEADER % values
+
+    for feature in features:
+        output += feature
+
+    output += FEATURES_FOOTER
+    return output
+
+def generateFeature(feature_name,
+                    version,
+                    title,
+                    features = [],
+                    bundles = [],
+                    **kwargs):
+    values = {
         'feature_name' : feature_name,
         'version' : version,
         'title' : title,
-        'feature_repo_name' : '-'.join(feature_repo_name.split(':')[1:3]),
     }
 
-    output = FEATURES_HEADER % values + FEATURE_HEADER % values
+    output = FEATURE_HEADER % values
 
-    for feature in features:
-        output += EXISTING_FEATURE % feature
+    if features:
+        for feature in features:
+            output += EXISTING_FEATURE % feature
 
-    for bundle in bundles:
-        output += BUNDLE % mvnUrl(bundle)
+    if bundles:
+        for bundle in bundles:
+            output += BUNDLE % mvnUrl(bundle)
 
-    output += FEATURE_FOOTER + FEATURES_FOOTER
+    output += FEATURE_FOOTER
     return output
 
+
 def generateAppFile(app_name,
                     origin,
                     version,
@@ -139,12 +155,14 @@
 
     parser.add_option("-A", "--write-app", dest="write_app", action="store_true")
     parser.add_option("-F", "--write-features", dest="write_features", action="store_true")
+    parser.add_option("-E", "--write-feature", dest="write_feature", action="store_true")
 
     (options, args) = parser.parse_args()
 
     values = {}
     if options.feature_coords and options.version and options.title:
-        values['feature_name'] = options.feature_coords.split(':')[1]
+        parts = options.feature_coords.split(':')
+        values['feature_name'] = parts[1] if len(parts) > 1 else parts[0]
         values['version'] = options.version
         values['title'] = options.title
     else:
@@ -165,15 +183,22 @@
     values['feature_repo_name'] = options.repo_name if options.repo_name \
                                     else options.feature_coords
 
+    bundles = []
+    if options.included_bundles:
+        bundles += options.included_bundles
+    if options.excluded_bundles:
+        bundles += options.excluded_bundles
+    feature = generateFeature(bundles=bundles,
+                              features=options.features,
+                              **values)
+
+    if options.write_feature:
+        print feature
+
     if options.write_features:
-        bundles = []
-        if options.included_bundles:
-            bundles += options.included_bundles
-        if options.excluded_bundles:
-            bundles += options.excluded_bundles
-        print generateFeatureFile(bundles=bundles,
-                                  features=options.features,
+        print generateFeatureFile(features=[ feature ],
                                   **values)
+
     if options.write_app:
         print generateAppFile(artifacts=options.included_bundles,
                               apps=options.apps,
diff --git a/bucklets/onos_app.bucklet b/bucklets/onos_app.bucklet
index 476bdac..8d0eca3 100644
--- a/bucklets/onos_app.bucklet
+++ b/bucklets/onos_app.bucklet
@@ -16,6 +16,107 @@
     base_path = get_base_path()
     return APP_PREFIX + os.path.basename(base_path)
 
+def osgi_feature(
+        name,
+        title,
+        feature_coords = None,
+        version = ONOS_VERSION,
+        required_features = [ 'onos-api' ],
+        required_apps = [],
+        included_bundles = None,
+        excluded_bundles = [],
+        generate_file = False,
+        visibility = [ 'PUBLIC' ],
+        stage_repo = True,
+    ):
+
+    if not feature_coords:
+        feature_coords = name
+    args = [ '-n %s' % feature_coords,
+             '-v %s' % version,
+             '-t "%s"' % title,
+             ]
+    args += [ '-f %s' % f for f in required_features ]
+    args += [ '-b $(maven_coords %s)' % b for b in included_bundles ]
+    args += [ '-e $(maven_coords %s)' % b for b in excluded_bundles ]
+    args += [ '-d %s' % a for a in required_apps ]
+
+    feature_cmd = '-F' if generate_file else '-E'
+
+    cmd = '$(exe //buck-tools:onos-app-writer) %s ' % feature_cmd
+    cmd += ' '.join(args) + ' > $OUT'
+    genrule(
+            name = name + '-feature',
+            bash = cmd,
+            out = 'features.xml',
+            visibility = visibility,
+    )
+
+    if stage_repo:
+        sources = ['$(location %s) $(maven_coords %s)' % (i, i) for i in included_bundles]
+        genrule(
+                name = name + '-repo',
+                out = name + '-repo.zip.part',
+                bash = '$(exe //buck-tools:onos-feature) $OUT ' + ' '.join(sources),
+                visibility = visibility,
+        )
+
+FEATURES_HEADER = '''\
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<features xmlns="http://karaf.apache.org/xmlns/features/v1.2.0"
+          name="onos-%s">
+    <repository>mvn:org.apache.karaf.features/standard/3.0.5/xml/features</repository>
+
+''' % ONOS_VERSION
+
+FEATURES_FOOTER = '</features>'
+
+def compile_features(
+        name,
+        features = [],
+        visibility = [ 'PUBLIC' ],
+    ):
+
+    cmd = "(echo '%s'; " % FEATURES_HEADER
+    cmd += ''.join(['cat $(location %s-feature); ' % s for s in features])
+    cmd += "echo '%s') > $OUT" % FEATURES_FOOTER
+
+    genrule(
+            name = name,
+            bash = cmd,
+            visibility = visibility,
+            out = 'features.xml',
+    )
+
+
+#TODO rename this
+def osgi_feature_group(
+        name,
+        description = 'TEST',
+        version = ONOS_VERSION,
+        exported_deps = [],
+        visibility = ['PUBLIC'],
+        **kwargs
+    ):
+    java_library(
+            name = name,
+            exported_deps = exported_deps, #compile only
+            visibility = visibility,
+    )
+
+    osgi_feature(
+            name = name,
+            feature_coords = name,
+            version = version,
+            title = description,
+            required_features = [],
+            included_bundles = exported_deps,
+            generate_file = False,
+            visibility = visibility,
+    )
+
+
+
 def onos_app(
         app_name = None,
         name = None,
@@ -66,13 +167,26 @@
     args += [ '-e $(maven_coords %s)' % b for b in excluded_bundles ]
     args += [ '-d %s' % a for a in required_apps ]
 
-    cmd = '$(exe //buck-tools:onos-app-writer) -F ' + ' '.join(args) + ' > $OUT'
-    genrule(
-        name = name + '-features',
-        bash = cmd,
-        out = 'features.xml',
+    # cmd = '$(exe //buck-tools:onos-app-writer) -F ' + ' '.join(args) + ' > $OUT'
+    # genrule(
+    #     name = name + '-feature',
+    #     bash = cmd,
+    #     out = 'features.xml',
+    #     visibility = [],
+    # )
+    osgi_feature(
+        name = name,
+        feature_coords = feature_coords,
+        version = version,
+        title = title,
+        required_features = required_features,
+        included_bundles = included_bundles,
+        excluded_bundles = excluded_bundles,
+        generate_file = True,
         visibility = [],
+        stage_repo = False,
     )
+
     cmd = '$(exe //buck-tools:onos-app-writer) -A ' + ' '.join(args) + ' > $OUT'
     genrule(
         name = name + '-app-xml',
@@ -82,7 +196,7 @@
     )
 
     sources = [
-        '$(location :%s-features) %s' % (name, feature_coords),
+        '$(location :%s-feature) %s' % (name, feature_coords),
         '$(location :%s-app-xml) NONE' % name,
     ]
     sources += ['$(location %s) $(maven_coords %s)' % (i, i) for i in included_bundles]