Last active
March 9, 2021 15:36
-
-
Save satmandu/18f926a905b28576f88775b1a8e79df4 to your computer and use it in GitHub Desktop.
Script to help with creating Chromebrew package files from Arch Linux PKGBUILD files
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
#!/bin/bash -x | |
# arch2crew | |
# Script to help with creating Chromebrew package files from Arch Linux PKGBUILD files | |
# Assumes rubocop is installed via "gem install rubocop" | |
# Usage: arch2crew <Arch Linux package name> | |
# Satadru Pramanik <satadru@gmail.com> | |
# | |
archpkgname="${1}" | |
# Initial setup | |
dir="$(pwd)" | |
if | |
curl -Lf https://github.com/archlinux/svntogit-packages/raw/packages/${archpkgname}/trunk/PKGBUILD > /tmp/"${archpkgname}" ; then | |
archurl="https://github.com/archlinux/svntogit-packages/raw/packages/${archpkgname}/trunk/PKGBUILD" | |
elif | |
curl -Lf https://github.com/archlinux/svntogit-community/raw/packages/${archpkgname}/trunk/PKGBUILD > /tmp/"${archpkgname}" ; then | |
archurl="https://github.com/archlinux/svntogit-community/raw/packages/${archpkgname}/trunk/PKGBUILD" | |
elif | |
curl -Lf https://aur.archlinux.org/cgit/aur.git/plain/PKGBUILD?h="${archpkgname}" > /tmp/"${archpkgname}" ; then | |
archurl="https://aur.archlinux.org/cgit/aur.git/plain/PKGBUILD?h=${archpkgname}" | |
else | |
exit 1 | |
fi | |
CleanBuildVars () { | |
local var="${1}" | |
[[ -n ${pkgver} ]] && var="${var//\$\{pkgver\}/$pkgver}" | |
[[ -n ${pkgbase} ]] && var="${var//\$\{pkgbase\}/$pkgbase}" | |
[[ -n ${pkgname} ]] && var=$(echo "${var}" | sed '/cd \$pkgname/d' ) | |
[[ -n ${pkgver} ]] && var=${var//\$pkgver/$pkgver} | |
[[ -n ${pkgname} ]] && var=${var//\$\{pkgname\}/$archpkgname} | |
[[ -n ${pkgname} ]] && var=${var//\$pkgname/$archpkgname} | |
[[ -n ${version} ]] && var=$(echo "${var}" | sed 's/#{version}/'"$version"'/g') | |
[[ -n ${_hkgname} ]] && var="${var//\$_hkgname/$_hkgname}" | |
var=${var//\$\{pkgdir\}/#\{CREW_DEST_DIR\}} | |
var=$(echo "${var}" | sed 's/"\$pkgdir"/#{CREW_DEST_DIR}/g' ) | |
var=$(echo "${var}" | sed 's/\$pkgdir/#{CREW_DEST_DIR}/g' ) | |
var=$(echo "${var}" | sed 's/^ *//g') | |
[[ "$var" == *"--prefix"* ]] && var=$(echo "${var}" | sed 's/--prefix=\/usr /#{CREW_OPTIONS} /g') | |
[[ "$var" == *"--mandir"* ]] && var=$(echo "${var}" | sed 's/\/usr/#{CREW_PREFIX}/g') | |
var=${var//arch-meson/meson} | |
var=$(echo "${var}" | sed 's/\/usr\/lib/#{CREW_LIB_PREFIX}/g') ; | |
#var=${var//\/usr\/lib/#{CREW_LIB_PREFIX}} | |
var=$(echo "${var}" | sed 's/DESTDIR="\${pkgdir}"/DESTDIR=#{CREW_DEST_DIR}/g'| sed 's/"//g') | |
var=$(echo "${var}" | sed 's/.*:://') | |
#[[ "$var" == *"#\{CREW"* ]] && var=$(echo $var | sed 's/"//g') | |
#[[ "$var" == *"#\{CREW"* ]] && var=${var//\"/} | |
echo "$var" | |
} | |
get () { | |
field= | |
local field=$(grep -m 1 "${1}=" /tmp/"${archpkgname}" | awk -F "=" '{print $2}'| tr -d \' | tr -d \( | tr -d \) | tr -d \"| sed 's/{,.sig}//') | |
CleanBuildVars "${field}" | |
} | |
get_new () { | |
field= | |
if ! grep -q "^[[:space:]]\+${1} =" "$crewpath/${crewname}.rb"; then | |
local field="$(grep -m 1 "^[[:space:]]\+${1}" "$crewpath/${crewname}.rb" | awk '{print $2}'| tr -d \' | tr -d \( | tr -d \) | tr -d \"| sed 's/{,.sig}//')" | |
else | |
local field="$(grep -m 1 "^[[:space:]]\+${1} '\|^[[:space:]]\+${1} \"" "$crewpath/${crewname}.rb")" | |
field=$(echo ${field#*=} | tr -d \' | tr -d \( | tr -d \) | tr -d \"| sed 's/{,.sig}//') | |
fi | |
CleanBuildVars "${field}" | |
} | |
CleanNameVars () { | |
local var="${1}" | |
var=$(echo "${var}" | sed -r 's/-/_/g' ) | |
echo "$var" | |
} | |
ToFile () { | |
local line="${1}" | |
if [[ "$line" == *"#\{CREW"* ]]; then | |
# line=${line//\"/} | |
echo "${line//\'/\"}" >> "${crewpkgname}.rb" | |
# echo "${line}" >> "${crewpkgname}.rb" | |
else | |
echo "${line}" >> "${crewpkgname}.rb" | |
fi | |
} | |
ToFile2 () { | |
local line="${1}" | |
local match="#{CREW" | |
if [[ "$line" == *"$match"* ]]; then | |
echo "${line//£/\"}" >> "${crewpkgname}.rb" | |
else | |
echo "${line//£/\'}" >> "${crewpkgname}.rb" | |
fi | |
} | |
VartoFile () { | |
local indent="${1}" | |
local crewvar="${2}" | |
local archvar=$(get "${3}") | |
[[ -n "$archvar" ]] && echo "${indent}${crewvar} '${archvar}'" >> "${crewpkgname}.rb" | |
} | |
GetSHA256 () { | |
local url=${1} | |
local tmpfile=$(mktemp /tmp/sha_XXX) | |
if (curl -Lf "$url" > "$tmpfile") &>/dev/null; then | |
sha256sum "$tmpfile" | awk '{print $1}' | |
rm -f "$tmpfile" | |
else | |
echo "-1" | |
fi | |
} | |
pkgbase=$(get pkgbase) | |
pkgname=$(get pkgname) | |
crewpkgname=$(CleanNameVars "$pkgname") | |
echo "# Adapted from Arch Linux $archpkgname PKGBUILD at:" > "${crewpkgname}.rb" | |
_hkgname=$(get _hkgname) | |
ToFile "# $archurl" | |
ToFile "" | |
ToFile "require 'package'" | |
# Insertions | |
ToFile | |
class="$(tr '[:lower:]' '[:upper:]' <<< "${crewpkgname:0:1}")${crewpkgname:1}" | |
ToFile "class ${class} < Package" | |
VartoFile " " "description" "pkgdesc" | |
VartoFile " " "homepage" "url" | |
pkgrel=$(get pkgrel) | |
pkgver=$(get pkgver) | |
version="${pkgver}" | |
[[ "$pkgrel" != "1" ]] && version="${pkgver}-${pkgrel}" | |
ToFile " version '${version}'" | |
Archarch=$(get arch) | |
#echo "arch: $Archarch" | |
if [[ "$Archarch" == "any" ]]; then ToFile " compatibility 'all'" else VartoFile " " "compatibility" "arch"; fi | |
source_url=$(get source) | |
ToFile " source_url '$source_url'" | |
#VartoFile " " "source_url" "source" | |
source_sha256=$(get sha256sums) | |
#VartoFile " " "source_sha256" "sha256sums" | |
if [[ -z $source_sha256 ]]; then | |
ToFile " source_sha256 '$(GetSHA256 $source_url)'" | |
else | |
ToFile " source_sha256 '$source_sha256'" | |
fi | |
#VartoFile " " "source_sha512" "sha512sums" | |
ToFile "" | |
depends=$(get depends) | |
#echo "depends: $depends" | |
for i in $depends; do | |
i=$(CleanNameVars "$i") | |
ToFile " depends_on '$i'" | |
done | |
makedepends=$(get makedepends) | |
#echo "makedepends: $makedepends" | |
for i in $makedepends; do | |
i=$(CleanNameVars "$i") | |
ToFile " depends_on '$i' => ':build'" | |
done | |
if grep -q "prepare()" /tmp/"${archpkgname}" ; then | |
getprepare=1 | |
fi | |
ToFile "" | |
ToFile "def self.build" | |
if [[ $getprepare == 1 ]]; then | |
#preparesection=$(sed -n '/prepare()/,/'"}"'/{//!p}' /tmp/"${archpkgname}") | |
preparesection=$(sed -n '/^$/d;/prepare()/,/^}/p; /^}/d' /tmp/"${archpkgname}" | sed '$d' | sed '1d' | sed '1d' ) | |
preparesection=$(CleanBuildVars "$preparesection") | |
while IFS= read -r line | |
do | |
if echo $line | grep '^[[:blank:]]*--*\|^[[:blank:]]*-D*' ; then | |
echo "\"$line\"" | sed "s/[[:blank:]]\\\'/',/" >> "${crewpkgname}.rb" | |
elif echo "$line" | grep -E '\.*do |\.*do$' ; then | |
let "inwhile=inwhile+1" | |
echo "in while loop: $inwhile" | |
echo "system '$line'" | sed "s/[[:blank:]]\\\'/',/" >> "${crewpkgname}.rb" | |
elif echo "$line" | grep -E '\.*done |\.*done$' ; then | |
let "inwhile=inwhile-1" | |
echo "\"$line\"" | sed "s/[[:blank:]]\\\'/',/" >> "${crewpkgname}.rb" | |
elif [[ "$inwhile" != "0" ]] ; then | |
echo "\"$line\"" | sed "s/[[:blank:]]\\\'/',/" >> "${crewpkgname}.rb" | |
else | |
echo "system \"$line\"" | sed "s/[[:blank:]]\\\'/',/" >> "${crewpkgname}.rb" | |
fi | |
done < <(printf '%s\n' "$preparesection") | |
fi | |
#buildsection=$(sed -n '/build()/,/'"}"'/{//!p}' /tmp/"${archpkgname}" ) | |
buildsection=$(sed -n '/^$/d;/build()/,/^}/p; /^}/d' /tmp/"${archpkgname}" | sed '$d' | sed '1d' | sed '1d' ) | |
buildsection=$(CleanBuildVars "$buildsection") | |
inwhile=0 | |
linecontinue=0 | |
while IFS= read -r line | |
do | |
if echo $line | grep '^[[:blank:]]*--*\|^[[:blank:]]*-D*' ; then | |
echo "\"$line\"" | sed "s/[[:blank:]]\\\'/',/" >> "${crewpkgname}.rb" | |
elif echo "$line" | grep -E '\.*do |\.*do$' ; then | |
let "inwhile=inwhile+1" | |
echo "in while loop: $inwhile" | |
echo "system '$line'" | sed "s/[[:blank:]]\\\'/',/" >> "${crewpkgname}.rb" | |
elif echo "$line" | grep -E '\.*done |\.*done$' ; then | |
let "inwhile=inwhile-1" | |
echo "\"$line\"" | sed "s/[[:blank:]]\\\'/',/" >> "${crewpkgname}.rb" | |
elif [[ "$inwhile" != "0" ]] ; then | |
echo "\"$line\"" | sed "s/[[:blank:]]\\\'/',/" >> "${crewpkgname}.rb" | |
else | |
echo "system \"$line\"" | sed "s/[[:blank:]]\\\'/',/" >> "${crewpkgname}.rb" | |
fi | |
done < <(printf '%s\n' "$buildsection") | |
ToFile "end" | |
ToFile "def self.install" | |
#packagesection=$(sed -n '/package()/,/'"}"'\b/{//!p}' /tmp/"${archpkgname}" | sed '$d' | sed '1d') | |
packagesection=$(sed -n '/^$/d;/package()/,/^}/p; /^}/d' /tmp/"${archpkgname}" | sed '$d' | sed '1d' | sed '1d' ) | |
packagesection=$(CleanBuildVars "$packagesection") | |
while IFS= read -r line | |
do | |
if [[ "$linecontinue" == "0" ]] ; then | |
if echo $line | grep -E '\.*\\ |\.*\\$' ; then | |
linecontinue=1 | |
fi | |
else | |
let "linecontinue=linecontinue+1" | |
echo "linecontinue is $linecontinue" | |
fi | |
if echo $line | grep '^[[:blank:]]*--*\|^[[:blank:]]*-D*' ; then | |
# configure option | |
#echo "$line" | sed "s/[[:blank:]]\\\'/',/" >> "${crewpkgname}.rb" | |
ToFile2 "$(echo "£$line£" | sed "s/[[:blank:]]\\\'/',/")" | |
elif echo "$line" | grep -E '\.*do |\.*do$' ; then | |
# a while loop start | |
let "inwhile=inwhile+1" | |
#echo "system '$line'," | sed "s/[[:blank:]]\\\'/'\,/" >> "${crewpkgname}.rb" | |
ToFile2 "$(echo "system £$line£," | sed "s/[[:blank:]]\\\'/'\,/")" | |
elif echo "$line" | grep -E '\.*done |\.*done$' ; then | |
# a while loop is done | |
let "inwhile=inwhile-1" | |
if [[ "$inwhile" != "0" ]] ; then | |
# not last while loop exit | |
#echo "'$line'" | sed "s/[[:blank:]]\\\'/',/" >> "${crewpkgname}.rb" | |
ToFile $(echo "'$line'" | sed "s/[[:blank:]]\\\'/',/") | |
else | |
# last while loop exit | |
#echo "'$line'" >> "${crewpkgname}.rb" | |
ToFile $(echo "'$line'" >> "${crewpkgname}.rb") | |
fi | |
elif [[ "$inwhile" != "0" ]] && [[ "$linecontinue" == "0" ]] ; then | |
# while loop, not 1st or 2nd continue line | |
#echo "'$line'," | sed "s/[[:blank:]]\\\'/',/" >> "${crewpkgname}.rb" | |
ToFile $(echo "'$line'," | sed "s/[[:blank:]]\\\'/',/") | |
elif [[ "$inwhile" != "0" ]] && [[ "$linecontinue" == "1" ]] ; then | |
# while loop, 1st continue line | |
#echo "'$line'," >> "${crewpkgname}.rb" | |
ToFile $(echo "\"$line\",") | |
elif [[ "$inwhile" != "0" ]] && [[ "$linecontinue" == "2" ]] ; then | |
# while loop, 2nd continue line | |
echo "\"$line\"," >> "${crewpkgname}.rb" | |
linecontinue=0 | |
elif [[ "$inwhile" == "0" ]] && [[ "$linecontinue" == "0" ]] ; then | |
# not while loop, not 1st or 2nd continue line | |
echo "system \"$line\"" >> "${crewpkgname}.rb" | |
elif [[ "$inwhile" == "0" ]] && [[ "$linecontinue" == "1" ]] ; then | |
# not while loop, 1st continue line | |
echo "system \"$line\"," >> "${crewpkgname}.rb" | |
elif [[ "$inwhile" == "0" ]] && [[ "$linecontinue" == "2" ]] ; then | |
# not while loop, 2nd continue line | |
echo "\"$line\"" >> "${crewpkgname}.rb" | |
linecontinue=0 | |
else | |
# not in a while loop or contine line | |
echo "system \"$line\"" | |
exit 1 | |
#echo "linecontinue is $linecontinue" | |
#echo "system '$line'" | sed "s/[[:blank:]]\\\'/',/" >> "${crewpkgname}.rb" | |
fi | |
done < <(printf '%s\n' "$packagesection") | |
ToFile "end" | |
ToFile "end" | |
#Final Checks | |
# Add compatibility line: | |
sed -i "/^[[:blank:]]*compatibility/d" "${crewpkgname}.rb" | |
sed -i "/^[[:blank:]]*version/ a compatibility 'all'" "${crewpkgname}.rb" | |
cat <<'EOF'> ~/.rubocop.yml | |
# The behavior of RuboCop can be controlled via the .rubocop.yml | |
# configuration file. It makes it possible to enable/disable | |
# certain cops (checks) and to alter their behavior if they accept | |
# any parameters. The file can be placed either in your home | |
# directory or in some project directory. | |
# | |
# RuboCop will start looking for the configuration file in the directory | |
# where the inspected file is and continue its way up to the root directory. | |
# | |
# See https://docs.rubocop.org/rubocop/configuration | |
AllCops: | |
NewCops: enable | |
Layout/HashAlignment: | |
EnforcedHashRocketStyle: | |
- separator | |
EnforcedColonStyle: | |
- separator | |
Layout/FirstHashElementIndentation: | |
EnforcedStyle: consistent | |
Style/FrozenStringLiteralComment: | |
Enabled: false | |
Style/Documentation: | |
Enabled: false | |
Naming/VariableNumber: | |
Enabled: false | |
Naming/ClassAndModuleCamelCase: | |
Enabled: false | |
Metrics/MethodLength: | |
Enabled: false | |
Layout/LineLength: | |
IgnoredPatterns: ['description'] | |
EOF | |
rubocop -A "${crewpkgname}.rb" | |
# Cleanup | |
echo -e "\nOriginal Arch Linux version:\n" | |
cat /tmp/"${archpkgname}" | |
rm /tmp/"${archpkgname}" | |
echo -e "\nChromebrew version ready to be edited at ""$dir""/${crewpkgname}.rb:\n" | |
cat "${crewpkgname}.rb" | |
ge "${crewpkgname}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment