git.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. const tools = require( './tools' );
  7. module.exports = {
  8. /**
  9. * Holds boilerplate repository Git URL.
  10. *
  11. * @private
  12. * @readonly
  13. * @type {String}
  14. */
  15. BOILERPLATE_REPOSITORY: 'git@github.com:ckeditor/ckeditor-boilerplate.git',
  16. /**
  17. * Holds boilerplate branch used in CKEditor5 projects.
  18. *
  19. * @private
  20. * @readonly
  21. * @type {String}
  22. */
  23. BOILERPLATE_BRANCH: 'ckeditor5',
  24. /**
  25. * Parses GitHub URL. Extracts used server, repository and branch.
  26. *
  27. * @param {String} url GitHub URL from package.json file.
  28. * @returns {Object} urlInfo
  29. * @returns {String} urlInfo.server
  30. * @returns {String} urlInfo.repository
  31. * @returns {String} urlInfo.user
  32. * @returns {String} urlInfo.name
  33. * @returns {String} urlInfo.branch
  34. */
  35. parseRepositoryUrl( url ) {
  36. const regexp = /^((?:git@|http[s]?:\/\/)github\.com(?:\/|:))?(([\w-]+)\/([\w-]+(?:\.git)?))(?:#([\w-/]+))?$/;
  37. const match = url.match( regexp );
  38. let server;
  39. let repository;
  40. let branch;
  41. let name;
  42. let user;
  43. if ( !match ) {
  44. return null;
  45. }
  46. server = match[ 1 ] || 'https://github.com/';
  47. repository = match[ 2 ];
  48. user = match[ 3 ] || '';
  49. name = match[ 4 ] || '';
  50. branch = match[ 5 ] || 'master';
  51. if ( !repository || !user || !name ) {
  52. return null;
  53. }
  54. name = /\.git$/.test( name ) ? name.slice( 0, -4 ) : name;
  55. return {
  56. server: server,
  57. repository: repository,
  58. branch: branch,
  59. user: user,
  60. name: name
  61. };
  62. },
  63. /**
  64. * Clones repository to workspace.
  65. *
  66. * @param {Object} urlInfo Parsed URL object from {@link #parseRepositoryUrl}.
  67. * @param {String} workspacePath Path to the workspace location where repository will be cloned.
  68. */
  69. cloneRepository( urlInfo, workspacePath ) {
  70. const cloneCommands = [
  71. `cd ${ workspacePath }`,
  72. `git clone ${ urlInfo.server + urlInfo.repository }`
  73. ];
  74. tools.shExec( cloneCommands.join( ' && ' ) );
  75. },
  76. /**
  77. * Checks out branch on selected repository.
  78. *
  79. * @param {String} repositoryLocation Absolute path to repository.
  80. * @param {String} branchName Name of the branch to checkout.
  81. */
  82. checkout( repositoryLocation, branchName ) {
  83. const checkoutCommands = [
  84. `cd ${ repositoryLocation }`,
  85. `git checkout ${ branchName }`
  86. ];
  87. tools.shExec( checkoutCommands.join( ' && ' ) );
  88. },
  89. /**
  90. * Pulls specified branch from origin.
  91. *
  92. * @param {String} repositoryLocation Absolute path to repository.
  93. * @param {String} branchName Branch name to pull.
  94. */
  95. pull( repositoryLocation, branchName ) {
  96. const checkoutCommands = [
  97. `cd ${ repositoryLocation }`,
  98. `git pull origin ${ branchName }`
  99. ];
  100. tools.shExec( checkoutCommands.join( ' && ' ) );
  101. },
  102. /**
  103. * Initializes new repository, adds and merges CKEditor5 boilerplate project.
  104. *
  105. * @param {String} repositoryPath Absolute path where repository should be created.
  106. */
  107. initializeRepository( repositoryPath ) {
  108. const initializeCommands = [
  109. `git init ${ repositoryPath }`,
  110. `cd ${ repositoryPath }`,
  111. `git remote add boilerplate ${ this.BOILERPLATE_REPOSITORY }`,
  112. `git fetch boilerplate ${ this.BOILERPLATE_BRANCH }`,
  113. `git merge boilerplate/${ this.BOILERPLATE_BRANCH }`
  114. ];
  115. tools.shExec( initializeCommands.join( ' && ' ) );
  116. },
  117. /**
  118. * Returns Git status of repository stored under specified path. It runs `git status --porcelain -sb` command.
  119. *
  120. * @param {String} repositoryPath Absolute path to repository.
  121. * @returns {String} Executed command's result.
  122. */
  123. getStatus( repositoryPath ) {
  124. return tools.shExec( `cd ${ repositoryPath } && git status --porcelain -sb` );
  125. },
  126. /**
  127. * Updates boilerplate project in specified repository.
  128. * @param {String} repositoryPath Absolute path to repository.
  129. */
  130. updateBoilerplate( repositoryPath ) {
  131. const regexp = /boilerplate(\n|$)/;
  132. // Try to add boilerplate remote if one is not already added.
  133. if ( !regexp.test( tools.shExec( `cd ${ repositoryPath } && git remote` ) ) ) {
  134. tools.shExec( `cd ${ repositoryPath } && git remote add boilerplate ${ this.BOILERPLATE_REPOSITORY }` );
  135. }
  136. const updateCommands = [
  137. `cd ${ repositoryPath }`,
  138. `git fetch boilerplate ${ this.BOILERPLATE_BRANCH }`,
  139. `git merge boilerplate/${ this.BOILERPLATE_BRANCH }`
  140. ];
  141. tools.shExec( updateCommands.join( ' && ' ) );
  142. },
  143. /**
  144. * Creates initial commit on repository under specified path.
  145. *
  146. * @param {String} pluginName
  147. * @param {String} repositoryPath
  148. */
  149. initialCommit( pluginName, repositoryPath ) {
  150. const commitCommands = [
  151. `cd ${ repositoryPath }`,
  152. `git add .`,
  153. `git commit -m "Initial commit for ${ pluginName }."`
  154. ];
  155. tools.shExec( commitCommands.join( ' && ' ) );
  156. }
  157. };