Last active
December 28, 2020 02:12
-
-
Save tuaris/428591f9a112116477b7bee601f36b50 to your computer and use it in GitHub Desktop.
Extract Github Tupples from a composer.json using AWK
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
BEGIN { | |
current_dimension = 0 | |
gh_tuple_len = 0 | |
gl_tuple_len = 0 | |
packages_len = 0 | |
gitlab_sites["https://gitlab.com"] = 1 | |
gitlab_sites["https://gitlab.freedesktop.org"] = 1 | |
gitlab_sites["https://gitlab.gnome.org"] = 1 | |
gitlab_sites["https://gitlab.redox-os.org"] = 1 | |
} | |
# Logic to help navigate the JSON array | |
match($1, /\".+\":/) { | |
current_field = substr($1,RSTART + 1,RLENGTH - 3) | |
fields[current_dimension] = current_field | |
} | |
/\{/ { | |
current_dimension++ | |
} | |
/\}/ { | |
current_dimension-- | |
} | |
# Look for the fields we want | |
current_field == "name" && fields[current_dimension - 1] == "packages" { | |
package_name = $2 | |
gsub(/"/, "", package_name) | |
gsub(",", "", package_name) | |
} | |
current_field == "version" && fields[current_dimension - 1] == "packages" { | |
package_version = $2 | |
gsub(/"/, "", package_version) | |
gsub(",", "", package_version) | |
} | |
current_field == "reference" && fields[current_dimension - 1] == "source" && fields[current_dimension - 2] == "packages" { | |
package_reference = $2 | |
gsub(/"/, "", package_reference) | |
gsub(",", "", package_reference) | |
} | |
current_field == "url" && fields[current_dimension - 1] == "source" && fields[current_dimension - 2] == "packages" { | |
package_source = $2 | |
gsub(/"/, "", package_source) | |
gsub(",", "", package_source) | |
} | |
function split_url(s) { | |
# scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment] | |
split(s, url_scheme, "://") | |
url["scheme"] = url_scheme[1] | |
split(url_scheme[2], url_fragment, "#") | |
url["fragment"] = url_fragment[2] | |
split(url_fragment[1], url_query, "?") | |
url["query"] = url_query[2] | |
split(url_query[1], url_authority, "/") | |
url["path"] = substr(url_query[1], length(url_authority[1]) + 1) | |
split(url_authority[1], url_auth, "@") | |
if (length(url_auth) == 2) { | |
split(url_auth[1], url_user, ":") | |
url["user"] = url_user[1] | |
url["password"] = url_user[2] | |
split(url_auth[2], url_host, ":") | |
} else { | |
url["user"] = "" | |
url["password"] = "" | |
split(url_auth[1], url_host, ":") | |
} | |
url["host"] = url_host[1] | |
url["port"] = url_host[2] | |
} | |
function print_array(start, arr, arrlen) { | |
end = " \\\n" | |
for (i = 0; i < arrlen; i++) { | |
if (i == arrlen - 1) { | |
end = "\n" | |
} | |
printf "%s\t%s%s", start, arr[i], end | |
start = "\t" | |
} | |
} | |
function record_added_package() { | |
package_added[package_name] = 1 | |
package_name = "" | |
package_version = "" | |
package_source = "" | |
package_reference = "" | |
} | |
!package_added[package_name] && package_source && package_reference && package_name { | |
split_url(package_source) | |
split(url["path"], path, "/") | |
account = path[2] | |
project = path[3] | |
gsub("\.git$", "", project) | |
tag = package_reference; | |
added = 0 | |
if (url["host"] == "github.com") { | |
added = 1 | |
gh_tuple[gh_tuple_len++] = sprintf(\ | |
"%s:%s:%s:%s", account, project, tag, package_name) | |
} else { | |
repo_site = sprintf("%s://%s", url["scheme"], url["host"]) | |
for (site in gitlab_sites) { | |
if (repo_site != site) { | |
continue | |
} | |
if (ENVIRON["GL_SITE"] == site) { | |
gl_tuple[gl_tuple_len++] = sprintf(\ | |
"%s:%s:%s:%s", account, project, tag, package_name) | |
} else { | |
gl_tuple[gl_tuple_len++] = sprintf(\ | |
"%s:%s:%s:%s:%s", site, account, project, tag, package_name) | |
} | |
added = 1 | |
break | |
} | |
} | |
if (!added) { | |
printf "Warning: Ignoring git source on line %d: %s\n", NR, package_source > "/dev/stderr" | |
} | |
record_added_package() | |
} | |
END { | |
if (gh_tuple_len > 0 && ENVIRON["USE_GITHUB"] == "") { | |
printf "USE_GITHUB=\tnodefault\n" | |
} | |
print_array("GH_TUPLE=", gh_tuple, gh_tuple_len) | |
if (gl_tuple_len > 0 && ENVIRON["USE_GITLAB"] == "") { | |
printf "USE_GITLAB=\tnodefault\n" | |
} | |
print_array("GL_TUPLE=", gl_tuple, gl_tuple_len) | |
if (gh_tuple_len > 0) { | |
printf "COMPOSER_USE_GITHUB=\tyes\n" | |
} | |
if (gl_tuple_len > 0) { | |
printf "COMPOSER_USE_GITLAB=\tyes\n" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
New revision is much more polished and easier to follow.