Skip to content

Instantly share code, notes, and snippets.

@jart
Last active January 16, 2017 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jart/41bfd977b913c2301627162f1c038e55 to your computer and use it in GitHub Desktop.
Save jart/41bfd977b913c2301627162f1c038e55 to your computer and use it in GitHub Desktop.
Bazel Maven Repository Naming Algorithm
/**
* @fileoverview Bazel external repository naming algorithm for Maven.
*/
var CLEANSE_CHARS_ = new RegExp('[^_0-9A-Za-z]', 'g');
/**
* Turns Maven group and artifact into Bazel repository name.
*
* <p>This algorithm works by turning illegal characters into underscores and
* then eliminating redundancy. For example:
*
* <ul>
* <li>com.google.guava:guava becomes com_google_guava
* <li>commons-logging:commons-logging becomes commons_logging
* <li>junit:junit becomes junit
* </ul>
*
* @param {string} group Maven group ID.
* @param {string} artifact Maven artifact ID.
* @return {string} Recommended name for Bazel external repository.
*/
function getName(group, artifact) {
var left = group.replace(CLEANSE_CHARS_, '_');
var right = artifact.replace(CLEANSE_CHARS_, '_');
var p = -1;
while (p < right.length) {
p = right.indexOf('_', p + 1);
if (p == -1) {
p = right.length;
}
var chunk = right.slice(0, p);
if (left == chunk) {
return right;
}
chunk = '_' + chunk;
if (left.slice(-chunk.length) == chunk) {
left = left.slice(0, -chunk.length);
break;
}
}
return left + '_' + right;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment