upgrade.sh 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. #!/usr/bin/env bash
  2. ##
  3. # tightenco/collect upgrader script
  4. #
  5. # usage
  6. # ./upgrade.sh [laravel framework version]
  7. #
  8. # or
  9. #
  10. # bash upgrade.sh [laravel framework version]
  11. #
  12. ##
  13. # Include dotfiles on file operations
  14. #
  15. shopt -s dotglob
  16. ##
  17. # App
  18. #
  19. function main()
  20. {
  21. echo "Upgrading..."
  22. checkDependencies
  23. prepareEnvironment $1
  24. displayVariables
  25. cleanDirectories
  26. downloadRepository
  27. extractZip
  28. copyClasses
  29. copyContracts
  30. copyTraits
  31. copyStubs
  32. downloadTests
  33. renameNamespace
  34. fillAliases
  35. cleanupDir
  36. runTests
  37. }
  38. ##
  39. # Check if all dependencies are available
  40. #
  41. function checkDependencies()
  42. {
  43. for dependency in ${dependencies[@]}; do
  44. if ! [ -x "$(command -v ${dependency})" ]; then
  45. echo "Error: ${dependency} is not installed." >&2
  46. exit 1
  47. fi
  48. done
  49. }
  50. ##
  51. # Prepare the environment
  52. #
  53. function prepareEnvironment()
  54. {
  55. ##
  56. # Define all variables
  57. #
  58. requestedVersion=$1
  59. rootDir=.
  60. baseDir=${rootDir}/src
  61. vendor=laravel
  62. project=framework
  63. oldNamespace='Illuminate'
  64. newNamespace='Tightenco\\Collect'
  65. newDir='Collect'
  66. logFile=$(mktemp /tmp/collect-log.XXXXXX)
  67. repository=https://github.com/$vendor/$project.git
  68. getCurrentVersionFromGitHub
  69. repositoryDir=${rootDir}/$project-${collectionVersion}
  70. repositorySrcDir=${repositoryDir}/src
  71. collectionZip=${rootDir}/$project-${collectionVersion}.zip
  72. collectionZipUrl=https://github.com/$vendor/$project/archive/v${collectionVersion}.zip
  73. oldNamespaceDir=${repositorySrcDir}/${oldNamespace}
  74. newNamespaceDir=${baseDir}/${newDir}
  75. testsDir=${rootDir}/tests
  76. testsBaseUrl=https://raw.githubusercontent.com/${vendor}/${project}/v${collectionVersion}/tests
  77. stubsDir=${rootDir}/stubs
  78. aliasFile=${baseDir}/${newDir}/Support/alias.php
  79. carriageReturn="
  80. "
  81. classes=(
  82. 'Support/Collection'
  83. 'Support/Arr'
  84. 'Support/Carbon'
  85. 'Support/HigherOrderCollectionProxy'
  86. 'Support/HtmlString'
  87. )
  88. excludeFromAliases=(
  89. 'Support/Carbon'
  90. )
  91. traits=(
  92. 'Support/Traits/Macroable'
  93. )
  94. contracts=(
  95. 'Contracts/Support/Arrayable'
  96. 'Contracts/Support/Jsonable'
  97. 'Contracts/Support/Htmlable'
  98. )
  99. tests=(
  100. 'Support/SupportCollectionTest.php'
  101. 'Support/SupportArrTest.php'
  102. 'Support/SupportMacroableTest.php'
  103. 'Support/SupportCarbonTest.php'
  104. )
  105. stubs=(
  106. 'src/Collect/Support/helpers.php'
  107. 'src/Collect/Support/alias.php'
  108. 'tests/bootstrap.php'
  109. )
  110. dependencies=(
  111. 'wget'
  112. 'unzip'
  113. 'mktemp'
  114. )
  115. }
  116. ##
  117. # Display all variables
  118. #
  119. function displayVariables()
  120. {
  121. echo
  122. echo "-- Variables"
  123. echo "---------------------------------------------"
  124. echo baseDir = ${baseDir}
  125. echo collectionVersion = ${collectionVersion}
  126. echo repositoryDir = ${repositoryDir}
  127. echo repositorySrcDir = ${repositorySrcDir}
  128. echo collectionZip = ${collectionZip}
  129. echo baseDir = ${baseDir}
  130. echo oldNamespace = ${oldNamespace}
  131. echo newNamespace = ${newNamespace}
  132. echo oldNamespaceDir = ${oldNamespaceDir}
  133. echo newNamespaceDir = ${newNamespaceDir}
  134. echo testsDir = ${testsDir}
  135. echo testsBaseUrl = ${testsBaseUrl}
  136. echo "---------------------------------------------"
  137. }
  138. ##
  139. # Clean the destination directory
  140. #
  141. function cleanDirectories()
  142. {
  143. echo "Cleaning ${baseDir} and ${testsDir}/Support..."
  144. if [ -d ${baseDir} ]; then
  145. rm -rf ${baseDir}
  146. fi
  147. if [ -d ${testsDir}/Support ]; then
  148. rm -rf ${testsDir}
  149. fi
  150. if [ -d ${repositoryDir} ]; then
  151. rm -rf ${repositoryDir}
  152. fi
  153. }
  154. ##
  155. # Download a new version
  156. #
  157. function downloadRepository()
  158. {
  159. echo "-- Downloading ${collectionZipUrl} to ${baseDir}"
  160. wget ${collectionZipUrl} -O ${collectionZip} >${logFile} 2>&1
  161. handleErrors
  162. }
  163. ##
  164. # Extract from compressed file
  165. #
  166. function extractZip()
  167. {
  168. echo "-- Extracting $project.zip..."
  169. unzip ${collectionZip} -d ${rootDir} >${logFile} 2>&1
  170. rm ${collectionZip}
  171. handleErrors
  172. }
  173. ##
  174. # Copy classes
  175. #
  176. function copyClasses()
  177. {
  178. echo "-- Copying classes and contracts..."
  179. for class in ${classes[@]}; do
  180. echo "Copying ${oldNamespaceDir}.php/${class}.php..."
  181. mkdir -p $(dirname ${newNamespaceDir}/${class})
  182. cp ${oldNamespaceDir}/${class}.php ${newNamespaceDir}/${class}.php
  183. done
  184. }
  185. ##
  186. # Move contracts
  187. #
  188. function copyContracts()
  189. {
  190. echo "-- Copying contracts..."
  191. for contract in ${contracts[@]}; do
  192. echo "Copying ${oldNamespaceDir}/${contract}.php..."
  193. mkdir -p $(dirname ${newNamespaceDir}/${contract})
  194. cp ${oldNamespaceDir}/${contract}.php ${newNamespaceDir}/${contract}.php
  195. done
  196. }
  197. ##
  198. # Move traits
  199. #
  200. function copyTraits()
  201. {
  202. echo "-- Copying traits..."
  203. for trait in ${traits[@]}; do
  204. echo "Copying ${oldNamespaceDir}/${trait}.php..."
  205. mkdir -p $(dirname ${newNamespaceDir}/${trait})
  206. cp ${oldNamespaceDir}/${trait}.php ${newNamespaceDir}/${trait}.php
  207. done
  208. }
  209. ##
  210. # Copy classes and contracts
  211. #
  212. function copyStubs()
  213. {
  214. echo "-- Copying stubs..."
  215. for stub in ${stubs[@]}; do
  216. echo "Copying ${stubsDir}/${stub} to ${rootDir}/${stub}..."
  217. mkdir -p $(dirname ${rootDir}/${stub})
  218. cp ${stubsDir}/${stub} ${rootDir}/${stub}
  219. done
  220. }
  221. ##
  222. # Fill the alias.php file with the list of aliases
  223. #
  224. function fillAliases()
  225. {
  226. echo "-- Filling aliases.php..."
  227. indent=' '
  228. aliases='CARRIAGERETURN'
  229. for contract in ${contracts[@]}; do
  230. aliases="${aliases}${indent}${newNamespace}/${contract}::class => ${oldNamespace}/${contract}::class,CARRIAGERETURN"
  231. done
  232. for class in ${classes[@]}; do
  233. if [[ ! " ${excludeFromAliases[@]} " =~ " ${class} " ]]; then
  234. aliases="${aliases}${indent}${newNamespace}/${class}::class => ${oldNamespace}/${class}::class,CARRIAGERETURN"
  235. fi
  236. done
  237. for trait in ${traits[@]}; do
  238. aliases="${aliases}${indent}${newNamespace}/${trait}::class => ${oldNamespace}/${trait}::class,CARRIAGERETURN"
  239. done
  240. aliases=${aliases//\//\\\\}
  241. sed -i "" -e "s|/\*--- ALIASES ---\*/|${aliases}|g" $aliasFile
  242. sed -i "" -e "s|CARRIAGERETURN|\\${carriageReturn}|g" $aliasFile
  243. }
  244. ##
  245. # Copy tests to our tests dir
  246. #
  247. function getCurrentVersionFromGitHub()
  248. {
  249. echo Getting current version from $repository...
  250. if [ -z "$requestedVersion" ]; then
  251. collectionVersion=$(git ls-remote $repository | grep tags/ | grep -v {} | cut -d \/ -f 3 | cut -d v -f 2 | grep -v RC | grep -vi beta | sort -t. -k 1,1n -k 2,2n -k 3,3n| tail -1)
  252. else
  253. collectionVersion=$requestedVersion
  254. fi
  255. echo Upgrading to $vendor/$project $collectionVersion
  256. }
  257. ##
  258. # Download tests to tests dir
  259. #
  260. function downloadTests()
  261. {
  262. echo "-- Copying tests..."
  263. for test in ${tests[@]}; do
  264. echo "---- Downloading test ${testsBaseUrl}/${test} to ${testsDir}/${test}..."
  265. mkdir -p $(dirname ${testsDir}/${test})
  266. wget ${testsBaseUrl}/${test} -O ${testsDir}/${test} >/dev/null 2>&1
  267. done
  268. }
  269. ##
  270. # Rename namespace on all files
  271. #
  272. function renameNamespace()
  273. {
  274. echo "-- Renaming namespace from $oldNamespace to $newNamespace..."
  275. find ${newNamespaceDir} -name "*.php" -exec sed -i "" -e "s|${oldNamespace}|${newNamespace}|g" {} \;
  276. find ${testsDir} -name "*.php" -exec sed -i "" -e "s|${oldNamespace}|${newNamespace}|g" {} \;
  277. find ${newNamespaceDir} -name "*.php" -exec sed -i "" -e "s|/\*--- OLDNAMESPACE ---\*/|${oldNamespace}|g" {} \;
  278. }
  279. ##
  280. # Clean up dir
  281. #
  282. function cleanupDir()
  283. {
  284. echo "-- Cleaning up ${repositoryDir}..."
  285. rm -rf ${repositoryDir}
  286. }
  287. ##
  288. # Run tests
  289. #
  290. function runTests()
  291. {
  292. echo "-- Running tests..."
  293. if [ -f ${rootDir}/composer.lock ]; then
  294. rm ${rootDir}/composer.lock
  295. fi
  296. if [ -d ${rootDir}/vendor ]; then
  297. rm -rf ${rootDir}/vendor
  298. fi
  299. composer install
  300. vendor/bin/phpunit
  301. }
  302. ##
  303. # Handle command errors
  304. #
  305. function handleErrors()
  306. {
  307. if [[ $? -ne 0 ]]; then
  308. echo "FATAL ERROR occurred during command execution:"
  309. cat ${logFile}
  310. exit 1
  311. fi
  312. }
  313. ##
  314. # Run the app
  315. #
  316. main $@