Last active
March 29, 2016 04:49
-
-
Save cunneen/d2f3842ae0240d8ab747 to your computer and use it in GitHub Desktop.
A very rough bash script to replace meteor cordova tarball URLs with a private NPM reference. Use together with sinopia.
This file contains hidden or 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 | |
# Run this from the meteor application folder. It's assumed that the git root is somewhere | |
# ABOVE the meteor application folder. If your meteor application root *is* the git root, | |
# review this code very carefully as I've not tested it in such cases. | |
# | |
# It downloads all meteor cordova tarball and https plugins, and registers them | |
# with the npm registry. I use this with a private registry (sinopia). | |
# It then updates the meteor cordova plugin registry with a reference to the npm registry. | |
meteordir="`pwd`" | |
destfoldername="cordova-plugins" | |
gitbase="`git rev-parse --show-toplevel`" | |
destfolderpath="${gitbase}/${destfoldername}" | |
if [ ! -d .meteor ]; then | |
echo "you need to run this from your meteor project." | |
exit 1; | |
fi | |
#plugman must be installed | |
command -v plugman >/dev/null 2>&1 || { echo "I require plugman but it's not installed. Aborting." >&2; exit 1; } | |
#check that npm registry is not default | |
npmregistry=`npm config get registry` | |
if [ ${npmregistry} == "https://registry.npmjs.org/" ]; then | |
echo "Your npm registry is set to the default." | |
echo "You need to publish to your own private npm registry. Install and run Sinopia:" | |
echo -e " npm install -g sinopia\n\n" | |
echo "Then set the npm registry before running this script (and keep it set when running meteor):" | |
echo " npm config set registry http://localhost:4873/" | |
echo " npm config set ca null" | |
echo " npm adduser" | |
exit 2; | |
fi | |
if [[ ! -z $(git status -s) ]]; then | |
echo "Your git repository has changes. You MUST run this on a clean repository." | |
echo "Commit your changes first, then re-run." | |
git status -s | |
exit 2; | |
fi | |
mkdir -p "${destfolderpath}" | |
cd "$destfolderpath" | |
gitRelativePath=`pwd | sed "s|${gitbase}/||"` | |
# call like this: downloadAndPublish "${i}" "${tarball}" "${giturl}" "${giturlnaked}" "${localname}" "${pluginid}" "${sha}" | |
function downloadAndPublish() { | |
i="${1}" | |
tarball="${2}" | |
giturl="${3}" | |
giturlnaked="${4}" | |
localname="${5}" | |
pluginid="${6}" | |
sha="${7}" | |
echo -e "============\n" \ | |
"PLUGIN:$i\n" \ | |
"tarball:$tarballurl\n" \ | |
"giturl:$giturl\n" \ | |
"giturlnaked:$giturlnaked\n" \ | |
"localname:$localname\n" \ | |
"pluginid:$pluginid\n" \ | |
"sha:$sha\n" \ | |
"============\n\n" | |
# clean the destination if it already exists | |
if [ -d "${destfolderpath}/${localname}" ]; then | |
rm -rf "${destfolderpath}/${localname}" | |
fi | |
# Get the module from git and add it as a submodule of this repo | |
echo -e "adding git submodule ${localname}\n\n" | |
cd ${gitbase} | |
git submodule add "${giturl}" "./${gitRelativePath}/${localname}" && \ | |
git submodule update --init --recursive --force && \ | |
cd "${gitbase}/${gitRelativePath}/${localname}" && \ | |
git checkout "${sha}" | |
if [ $? -ne 0 ]; then | |
echo "ERROR: Git submodule / checkout didn't work" | |
cleanupAndExit 3 | |
fi | |
# Ensure we have a package.json | |
# cd "${destfoldername}/${localname}" | |
if [ ! -f package.json ]; then | |
if [ -f plugin.xml ]; then | |
echo -e "\n-------\n" | |
echo "Generating package.json from plugin.xml." | |
echo "It's a good idea to use the cordova plugin ID ${pluginid} as the package name. " | |
echo "git url is: ${giturl}" | |
echo "Stick with defaults in other cases." | |
plugman createpackagejson . | |
if [ $? -ne 0 ]; then | |
echo "ERROR: Couldn't create package.json for ./${gitRelativePath}/${localname}" | |
cleanupAndExit 4 | |
fi | |
else | |
echo "No package.json, and can't find plugin.xml to generage package.json from" | |
echo "(in folder `pwd`)" | |
cleanupAndExit 5; | |
fi | |
fi | |
# We need to discover the version number in the package.json | |
version=`node -pe 'JSON.parse(process.argv[1]).version' "$(cat package.json)"` | |
packagename=`node -pe 'JSON.parse(process.argv[1]).name' "$(cat package.json)"` | |
# Let's publish | |
echo "publishing ${packagename}@${version}" | |
npm publish . | |
retval=$? | |
if [ $retval -ne 0 ]; then | |
echo "Couldn't publish plugin for ./${gitRelativePath}/${localname}" | |
echo "when trying to publish ${packagename}@${version}" | |
echo "trying to unpublish and republish" | |
npm unpublish ${packagename}@${version} | |
npm publish . | |
retval=$? | |
fi | |
if [ $retval -ne 0 ]; then | |
echo "ERROR: Couldn't publish plugin for ./${gitRelativePath}/${localname}" | |
echo "when trying to publish ${packagename}@${version}" | |
cleanupAndExit 5 | |
fi | |
cd "${meteordir}" | |
echo "replacing meteor cordova git reference with local sinopia reference..." | |
meteor remove "cordova:${pluginid}" && \ | |
meteor add "cordova:${packagename}@${version}" | |
cd "${destfolderpath}" | |
} | |
function cleanupAndExit() { | |
# we do a hard git reset. | |
cd "${gitbase}" | |
git reset --hard | |
# cleanup the .git/modules folder | |
echo "You probably want to clean up the git modules:" | |
echo "rm -rf ${gitbase}/.git/modules/${destfoldername}" | |
echo "" | |
echo "You should also clean up the cordova plugins that were downloaded." | |
echo "rm -rf ${destfolderpath}" | |
echo "" | |
echo "Also you need to unpublish any npm publications before re-running:" | |
echo "npm unpublish package_name@version" | |
echo "see http://localhost:4873 for a list (if using sinopia)" | |
exit $1 | |
} | |
# download the plugin tarballs | |
for i in `grep tarball ${meteordir}/.meteor/cordova-plugins`; | |
#for i in "de.appplant.cordova.plugin.email-composer@https://github.com/katzer/cordova-plugin-email-composer/tarball/db9c3aa9a7923c427bb85fea82f4e95f445a28f3"; | |
do | |
tarballurl=`echo $i | cut -d@ -f2` | |
giturl=`echo $tarballurl | sed "s/\/tarball\/.*/.git/"` | |
giturlnaked=`echo $giturl | sed "s/\.git$//"` # without the .git suffix | |
localname=`echo $giturlnaked | rev | cut -d/ -f1 | rev` | |
sha=`echo $i | rev | cut -d/ -f1 | rev` | |
pluginid=`echo $i | cut -d@ -f1` | |
downloadAndPublish "${i}" "${tarball}" "${giturl}" "${giturlnaked}" "${localname}" "${pluginid}" "${sha}" | |
done | |
# Download the https cordova plugins. | |
# for i in uk.co.workingedge.phonegap.plugin.istablet@https://github.com/dpa99c/phonegap-istablet.git#8eab0629fe445df6d1d0ef834cf5cdfbf673979e | |
for i in `grep @https ${meteordir}/.meteor/cordova-plugins | grep -v tarball`; | |
do | |
tarballurl="none" | |
giturl=`echo $i | cut -f2 -d@ | sed "s/#.*//"` | |
giturlnaked=`echo $giturl | sed "s/\.git$//"` | |
localname=`echo $giturlnaked | rev | cut -d/ -f1 | rev` | |
sha=`echo $i | rev | cut -d# -f1 | rev` | |
pluginid=`echo $i | cut -d@ -f1` | |
downloadAndPublish "${i}" "${tarball}" "${giturl}" "${giturlnaked}" "${localname}" "${pluginid}" "${sha}" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment