Last active
January 16, 2017 21:36
-
-
Save jart/41bfd977b913c2301627162f1c038e55 to your computer and use it in GitHub Desktop.
Bazel Maven Repository Naming Algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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