tools/maven: generate Maven install scripts

Build actions can be cached, so they can't be used for Maven
interactions.  Instead, generate a script that calls buck and then
runs the upload process.

This obviates tools/maven/api.py.

The script tools/maven/api.sh runs all deployment steps combined
(generate script, generate artifacts, upload to maven.)

Tested:
  Ran "python tools/maven/api.py -n deploy" before and "buck
build //tools/maven:gen_api_deploy" afterwards. The old command line
and generated script differ as expected:

 * paths in the script are absolute, because Buck expands $(location)
   to absolute paths.

 * the ordering of arguments differs, because Python dict iteration
   was random beforehand.

 * "-o" missing afterward, as expected.

The same check was performed for gen_api_install.

Change-Id: Ia246000f8b59e881c53265751e2ebcfe8ec7d433
diff --git a/tools/maven/package.defs b/tools/maven/package.defs
index 8fe9a13..c3e5595 100644
--- a/tools/maven/package.defs
+++ b/tools/maven/package.defs
@@ -12,6 +12,19 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+sh_bang_template = (' && '.join([
+  "echo '#!/bin/sh -eu' > $OUT",
+  'echo "# this script should run from the root of your workspace." >> $OUT',
+  'echo "" >> $OUT',
+  "echo 'if [[ -n \"$${VERBOSE:-}\" ]]; then set -x ; fi' >> $OUT", 
+  'echo "" >> $OUT',
+  'echo %s >> $OUT',
+  'echo "" >> $OUT',
+  'echo %s >> $OUT',
+  # This is supposed to be handled by executable=True, but it doesn't
+  # work. Bug?
+  'chmod +x $OUT' ]))
+
 def maven_package(
     version,
     repository = None,
@@ -20,44 +33,61 @@
     src = {},
     doc = {},
     war = {}):
-  cmd = ['$(exe //tools/maven:mvn)', '-v', version, '-o', '$OUT']
-  api_cmd = []
+
+  build_cmd = ['buck', 'build']
+
+  mvn_cmd = ['$(exe //tools/maven:mvn)', '-v', version]
+  api_cmd = mvn_cmd[:]
+  api_targets = [ '//tools/maven:mvn' ]
   for type,d in [('jar', jar), ('java-source', src), ('javadoc', doc)]:
-    for a,t in d.iteritems():
+    for a,t in sorted(d.iteritems()):
       api_cmd.append('-s %s:%s:$(location %s)' % (a,type,t))
+      api_targets.append(t)
 
   genrule(
-    name = 'api_install',
-    cmd = ' '.join(cmd + api_cmd + ['-a', 'install']),
-    out = 'api_install.info',
+    name = 'gen_api_install',
+    cmd = sh_bang_template % (
+      ' '.join(build_cmd + api_targets),
+      ' '.join(api_cmd + ['-a', 'install'])),
+    out = 'api_install.sh',
+    executable = True,
   )
 
   if repository and url:
     genrule(
-      name = 'api_deploy',
-      cmd = ' '.join(cmd + api_cmd + [
-        '-a', 'deploy',
-        '--repository', repository,
-        '--url', url]),
-      out = 'api_deploy.info',
+      name = 'gen_api_deploy',
+      cmd = sh_bang_template % (
+        ' '.join(build_cmd + api_targets),
+        ' '.join(api_cmd + ['-a', 'deploy',
+                            '--repository', repository,
+                            '--url', url])),
+      out = 'api_deploy.sh',
+      executable = True,
     )
 
-  war_cmd = []
-  for a,t in war.iteritems():
+  war_cmd = mvn_cmd[:]
+  war_targets = [ '//tools/maven:mvn' ]
+  for a,t in sorted(war.iteritems()):
     war_cmd.append('-s %s:war:$(location %s)' % (a,t))
+    war_targets.append(t)
 
   genrule(
-    name = 'war_install',
-    cmd = ' '.join(cmd + war_cmd + ['-a', 'install']),
-    out = 'war_install.info',
+    name = 'gen_war_install',
+    cmd = sh_bang_template % (' '.join(build_cmd + war_targets),
+                              ' '.join(war_cmd + ['-a', 'install'])),
+    out = 'war_install.sh',
+    executable = True,
   )
 
   if repository and url:
     genrule(
-      name = 'war_deploy',
-      cmd = ' '.join(cmd + war_cmd + [
+      name = 'gen_war_deploy',
+      cmd = sh_bang_template % (
+          ' '.join(build_cmd + war_targets),
+          ' '.join(war_cmd + [
         '-a', 'deploy',
         '--repository', repository,
-        '--url', url]),
-      out = 'war_deploy.info',
+        '--url', url])),
+      out = 'war_deploy.sh',
+      executable = True,
     )