Update Eclipse/Maven rules to be closer to Gerrit

This includes some fixes for artifact names and layout from recent
buck versions, which will fix project generation. Pulled in some
additional refactorings so the diff between this and Gerrit's
tools/eclipse/project.py is limited to Gerrit-specific things
(top-level target names, GWT, plugins, etc.).

Change-Id: I44411dbee624a68fff9900559277039b5c7b04f2
diff --git a/maven_jar.bucklet b/maven_jar.bucklet
index 0a22d6d..3a26a66 100644
--- a/maven_jar.bucklet
+++ b/maven_jar.bucklet
@@ -44,7 +44,6 @@
 
 GERRIT = 'GERRIT:'
 GERRIT_API = 'GERRIT_API:'
-ECLIPSE = 'ECLIPSE:'
 MAVEN_CENTRAL = 'MAVEN_CENTRAL:'
 MAVEN_LOCAL = 'MAVEN_LOCAL:'
 
@@ -84,7 +83,13 @@
     group, artifact, version = parts
     classifier = None
 
-  if 'SNAPSHOT' in version and repository != MAVEN_LOCAL:
+  # SNAPSHOT artifacts are handled differently on Google storage bucket:
+  # 'SNAPSHOT' is discarded from the directory name. However on other
+  # Maven repositories, most notable local repository located in
+  # ~/.m2/repository (and is supported through MAVEN_LOCAL repository)
+  # it must be preserved, otherwise the artifact wouldn't be found.
+  # Atm the SNAPSHOT part is only discarded for Google storage bucket.
+  if 'SNAPSHOT' in version and repository.startswith(GERRIT):
     file_version = version.replace('-SNAPSHOT', '')
     version = version.split('-SNAPSHOT')[0] + '-SNAPSHOT'
   else:
@@ -94,6 +99,7 @@
     file_version += '-' + classifier
 
   jar = path.join(name, artifact.lower() + '-' + file_version)
+
   url = '/'.join([
     repository,
     group.replace('.', '/'), artifact, version,
@@ -118,11 +124,10 @@
     cmd.append('--unsign')
 
   genrule(
-    name = name + '__download_bin',
+    name = '%s__download_bin' % name,
     cmd = ' '.join(cmd),
     out = binjar,
   )
-
   license = ':LICENSE-' + license
   if not local_license:
     license = '//lib' + license
@@ -133,29 +138,30 @@
     if src_sha1:
       cmd.extend(['-v', src_sha1])
     genrule(
-      name = name + '__download_src',
+      name = '%s__download_src' % name,
       cmd = ' '.join(cmd),
       out = srcjar,
     )
     prebuilt_jar(
-      name = name + '_src',
-      binary_jar = ':' + name + '__download_src',
+      name = '%s_src' % name,
+      binary_jar = ':%s__download_src' % name,
       visibility = visibility,
     )
   else:
     srcjar = None
     genrule(
-      name = name + '__download_src',
+      name = '%s_src' % name,
       cmd = ':>$OUT',
-      out = '__' + name + '__no_src',
+      out = '__%s__no_src' % name,
     )
 
   if exported_deps:
     prebuilt_jar(
-      name = name + '__jar',
+      name = '%s__jar' % name,
       deps = deps,
-      binary_jar = ':' + name + '__download_bin',
-      source_jar = ':' + name + '__download_src' if srcjar else None,
+      binary_jar = ':%s__download_bin' % name,
+      source_jar = ':%s__download_src' % name if srcjar else None,
+      visibility = visibility,
     )
     java_library(
       name = name,
@@ -166,7 +172,7 @@
     prebuilt_jar(
       name = name,
       deps = deps,
-      binary_jar = ':' + name + '__download_bin',
-      source_jar = ':' + name + '__download_src' if srcjar else None,
+      binary_jar = ':%s__download_bin' % name,
+      source_jar = ':%s__download_src' % name if srcjar else None,
       visibility = visibility,
     )
diff --git a/tools/BUCK b/tools/BUCK
index 1983929..8899928 100644
--- a/tools/BUCK
+++ b/tools/BUCK
@@ -36,10 +36,10 @@
   return environ.get('PATH')
 
 genrule(
-  name = 'buck.properties',
+  name = 'buck',
   cmd = 'echo buck=`which buck`>$OUT;' +
     ("echo PATH=\''%s'\' >>$OUT;" % shquote(os_path())),
-  out = 'buck.properties',
+  out = 'buck',
   visibility = ['PUBLIC'],
 )
 
diff --git a/tools/download_all.py b/tools/download_all.py
index 17e7ab0..0c46655 100755
--- a/tools/download_all.py
+++ b/tools/download_all.py
@@ -32,7 +32,7 @@
   if m:
     n = m.group(1)
     if args.src and n.endswith('__download_bin'):
-      n = n[:-4] + '_src'
+      n = n[:-13] + 'src'
     targets.add(n)
 r = p.wait()
 if r != 0:
diff --git a/tools/eclipse.py b/tools/eclipse.py
index 3195b2a..e62e12b 100755
--- a/tools/eclipse.py
+++ b/tools/eclipse.py
@@ -31,7 +31,7 @@
 ])
 
 ROOT = path.abspath(__file__)
-for _ in range(0, 3):
+while not path.exists(path.join(ROOT, '.buckconfig')):
   ROOT = path.dirname(ROOT)
 
 opts = OptionParser()
@@ -39,14 +39,24 @@
 opts.add_option('-n', '--name')
 args, _ = opts.parse_args()
 
-def gen_project():
-  p = path.join(ROOT, '.project')
-  name = args.name if args.name else path.basename(ROOT)
+def _query_classpath(targets):
+  deps = []
+  p = Popen(['buck', 'audit', 'classpath'] + targets, stdout=PIPE)
+  for line in p.stdout:
+    deps.append(line.strip())
+  s = p.wait()
+  if s != 0:
+    exit(s)
+  return deps
+
+
+def gen_project(name, root=ROOT):
+  p = path.join(root, '.project')
   with open(p, 'w') as fd:
     print("""\
 <?xml version="1.0" encoding="UTF-8"?>
 <projectDescription>
-  <name>%s</name>
+  <name>""" + name + """</name>
   <buildSpec>
     <buildCommand>
       <name>org.eclipse.jdt.core.javabuilder</name>
@@ -56,19 +66,9 @@
     <nature>org.eclipse.jdt.core.javanature</nature>
   </natures>
 </projectDescription>\
-""" % (name,), file=fd)
+""", file=fd)
 
 def gen_classpath():
-  def query_classpath(targets):
-    deps = []
-    p = Popen(['buck', 'audit', 'classpath'] + targets, stdout=PIPE)
-    for line in p.stdout:
-      deps.append(line.strip())
-    s = p.wait()
-    if s != 0:
-      exit(s)
-    return deps
-
   def make_classpath():
     impl = minidom.getDOMImplementation()
     return impl.createDocument(None, 'classpath', None)
@@ -87,11 +87,11 @@
   src = set()
   lib = set()
 
-  java_library = re.compile(r'[^/]+/gen(.*)/lib__[^/]+__output/[^/]+[.]jar$')
-  for p in query_classpath(MAIN):
+  java_library = re.compile(r'[^/]+/gen/(.*)/lib__[^/]+__output/[^/]+[.]jar$')
+  for p in _query_classpath(MAIN):
     m = java_library.match(p)
     if m:
-      src.add(m.group(1).lstrip('/'))
+      src.add(m.group(1))
     else:
       lib.add(p)
 
@@ -124,7 +124,7 @@
     for j in sorted(libs):
       s = None
       if j.endswith('.jar'):
-        s = j[:-4] + '-src.jar'
+        s = j[:-4] + '_src.jar'
         if not path.exists(s):
           s = None
       classpathentry('lib', j, s)
@@ -143,11 +143,12 @@
     except CalledProcessError as err:
       exit(1)
 
-  gen_project()
+  name = args.name if args.name else path.basename(ROOT)
+  gen_project(name)
   gen_classpath()
 
   try:
-    targets = ['//bucklets/tools:buck.properties'] + MAIN
+    targets = ['//bucklets/tools:buck'] + MAIN
     check_call(['buck', 'build', '--deep'] + targets)
   except CalledProcessError as err:
     exit(1)