You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

436 lines
12 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. /*******************************
  2. Install Task
  3. *******************************/
  4. /*
  5. Install tasks
  6. For more notes
  7. * Runs automatically after npm update (hooks)
  8. * (NPM) Install - Will ask for where to put semantic (outside pm folder)
  9. * (NPM) Upgrade - Will look for semantic install, copy over files and update if new version
  10. * Standard installer runs asking for paths to site files etc
  11. */
  12. var
  13. gulp = require('gulp'),
  14. // node dependencies
  15. console = require('better-console'),
  16. extend = require('extend'),
  17. fs = require('fs'),
  18. mkdirp = require('mkdirp'),
  19. path = require('path'),
  20. runSequence = require('run-sequence'),
  21. // gulp dependencies
  22. chmod = require('gulp-chmod'),
  23. del = require('del'),
  24. jsonEditor = require('gulp-json-editor'),
  25. plumber = require('gulp-plumber'),
  26. prompt = require('gulp-prompt'),
  27. rename = require('gulp-rename'),
  28. replace = require('gulp-replace'),
  29. requireDotFile = require('require-dot-file'),
  30. wrench = require('wrench'),
  31. // install config
  32. install = require('./config/project/install'),
  33. // user config
  34. config = require('./config/user'),
  35. // release config (name/title/etc)
  36. release = require('./config/project/release'),
  37. // shorthand
  38. questions = install.questions,
  39. files = install.files,
  40. folders = install.folders,
  41. regExp = install.regExp,
  42. settings = install.settings,
  43. source = install.source
  44. ;
  45. // Export install task
  46. module.exports = function (callback) {
  47. var
  48. currentConfig = requireDotFile('semantic.json'),
  49. manager = install.getPackageManager(),
  50. rootQuestions = questions.root,
  51. installFolder = false,
  52. answers
  53. ;
  54. console.clear();
  55. /* Test NPM install
  56. manager = {
  57. name : 'NPM',
  58. root : path.normalize(__dirname + '/../')
  59. };
  60. */
  61. /* Don't do end user config if SUI is a sub-module */
  62. if( install.isSubModule() ) {
  63. console.info('SUI is a sub-module, skipping end-user install');
  64. return;
  65. }
  66. /*-----------------
  67. Update SUI
  68. -----------------*/
  69. // run update scripts if semantic.json exists
  70. if(currentConfig && manager.name === 'NPM') {
  71. var
  72. updateFolder = path.join(manager.root, currentConfig.base),
  73. updatePaths = {
  74. config : path.join(manager.root, files.config),
  75. tasks : path.join(updateFolder, folders.tasks),
  76. themeImport : path.join(updateFolder, folders.themeImport),
  77. definition : path.join(currentConfig.paths.source.definitions),
  78. site : path.join(currentConfig.paths.source.site),
  79. theme : path.join(currentConfig.paths.source.themes),
  80. defaultTheme : path.join(currentConfig.paths.source.themes, folders.defaultTheme)
  81. }
  82. ;
  83. // duck-type if there is a project installed
  84. if( fs.existsSync(updatePaths.definition) ) {
  85. // perform update if new version
  86. if(currentConfig.version !== release.version) {
  87. console.log('Updating Semantic UI from ' + currentConfig.version + ' to ' + release.version);
  88. console.info('Updating ui definitions...');
  89. wrench.copyDirSyncRecursive(source.definitions, updatePaths.definition, settings.wrench.overwrite);
  90. console.info('Updating default theme...');
  91. wrench.copyDirSyncRecursive(source.themes, updatePaths.theme, settings.wrench.merge);
  92. wrench.copyDirSyncRecursive(source.defaultTheme, updatePaths.defaultTheme, settings.wrench.overwrite);
  93. console.info('Updating tasks...');
  94. wrench.copyDirSyncRecursive(source.tasks, updatePaths.tasks, settings.wrench.overwrite);
  95. console.info('Updating gulpfile.js');
  96. gulp.src(source.userGulpFile)
  97. .pipe(plumber())
  98. .pipe(gulp.dest(updateFolder))
  99. ;
  100. // copy theme import
  101. console.info('Updating theme import file');
  102. gulp.src(source.themeImport)
  103. .pipe(plumber())
  104. .pipe(gulp.dest(updatePaths.themeImport))
  105. ;
  106. console.info('Adding new site theme files...');
  107. wrench.copyDirSyncRecursive(source.site, updatePaths.site, settings.wrench.merge);
  108. console.info('Updating version...');
  109. // update version number in semantic.json
  110. gulp.src(updatePaths.config)
  111. .pipe(plumber())
  112. .pipe(rename(settings.rename.json)) // preserve file extension
  113. .pipe(jsonEditor({
  114. version: release.version
  115. }))
  116. .pipe(gulp.dest(manager.root))
  117. ;
  118. console.info('Update complete! Run "\x1b[92mgulp build\x1b[0m" to rebuild dist/ files.');
  119. return;
  120. }
  121. else {
  122. console.log('Current version of Semantic UI already installed');
  123. return;
  124. }
  125. }
  126. else {
  127. console.error('Cannot locate files to update at path: ', updatePaths.definition);
  128. console.log('Running installer');
  129. }
  130. }
  131. /*--------------
  132. Determine Root
  133. ---------------*/
  134. // PM that supports Build Tools (NPM Only Now)
  135. if(manager.name == 'NPM') {
  136. rootQuestions[0].message = rootQuestions[0].message
  137. .replace('{packageMessage}', 'We detected you are using \x1b[92m' + manager.name + '\x1b[0m. Nice! ')
  138. .replace('{root}', manager.root)
  139. ;
  140. // set default path to detected PM root
  141. rootQuestions[0].default = manager.root;
  142. rootQuestions[1].default = manager.root;
  143. // insert PM questions after "Install Type" question
  144. Array.prototype.splice.apply(questions.setup, [2, 0].concat(rootQuestions));
  145. // omit cleanup questions for managed install
  146. questions.cleanup = [];
  147. }
  148. /*--------------
  149. Create SUI
  150. ---------------*/
  151. gulp.task('run setup', function() {
  152. return gulp
  153. .src('gulpfile.js')
  154. .pipe(prompt.prompt(questions.setup, function(setupAnswers) {
  155. // hoist
  156. answers = setupAnswers;
  157. }))
  158. ;
  159. });
  160. gulp.task('create install files', function(callback) {
  161. /*--------------
  162. Exit Conditions
  163. ---------------*/
  164. // if config exists and user specifies not to proceed
  165. if(answers.overwrite !== undefined && answers.overwrite == 'no') {
  166. return;
  167. }
  168. console.clear();
  169. console.log('Installing');
  170. console.log('------------------------------');
  171. /*--------------
  172. Paths
  173. ---------------*/
  174. var
  175. installPaths = {
  176. config : files.config,
  177. configFolder : folders.config,
  178. site : answers.site || folders.site,
  179. themeConfig : files.themeConfig,
  180. themeConfigFolder : folders.themeConfig
  181. }
  182. ;
  183. /*--------------
  184. NPM Install
  185. ---------------*/
  186. // Check if PM install
  187. if(answers.useRoot || answers.customRoot) {
  188. // Set root to custom root path if set
  189. if(answers.customRoot) {
  190. if(answers.customRoot === '') {
  191. console.log('Unable to proceed, invalid project root');
  192. return;
  193. }
  194. manager.root = answers.customRoot;
  195. }
  196. // special install paths only for PM install
  197. installPaths = extend(false, {}, installPaths, {
  198. definition : folders.definitions,
  199. lessImport : folders.lessImport,
  200. tasks : folders.tasks,
  201. theme : folders.themes,
  202. defaultTheme : path.join(folders.themes, folders.defaultTheme),
  203. themeImport : folders.themeImport
  204. });
  205. // add project root to semantic root
  206. installFolder = path.join(manager.root, answers.semanticRoot);
  207. // add install folder to all output paths
  208. for(var destination in installPaths) {
  209. if( installPaths.hasOwnProperty(destination) ) {
  210. // config goes in project root, rest in install folder
  211. installPaths[destination] = (destination == 'config' || destination == 'configFolder')
  212. ? path.normalize( path.join(manager.root, installPaths[destination]) )
  213. : path.normalize( path.join(installFolder, installPaths[destination]) )
  214. ;
  215. }
  216. }
  217. // create project folders
  218. try {
  219. mkdirp.sync(installFolder);
  220. mkdirp.sync(installPaths.definition);
  221. mkdirp.sync(installPaths.theme);
  222. mkdirp.sync(installPaths.tasks);
  223. }
  224. catch(error) {
  225. console.error('NPM does not have permissions to create folders at your specified path. Adjust your folders permissions and run "npm install" again');
  226. }
  227. console.log('Installing to \x1b[92m' + answers.semanticRoot + '\x1b[0m');
  228. console.info('Copying UI definitions');
  229. wrench.copyDirSyncRecursive(source.definitions, installPaths.definition, settings.wrench.overwrite);
  230. console.info('Copying UI themes');
  231. wrench.copyDirSyncRecursive(source.themes, installPaths.theme, settings.wrench.merge);
  232. wrench.copyDirSyncRecursive(source.defaultTheme, installPaths.defaultTheme, settings.wrench.overwrite);
  233. console.info('Copying gulp tasks');
  234. wrench.copyDirSyncRecursive(source.tasks, installPaths.tasks, settings.wrench.overwrite);
  235. // copy theme import
  236. console.info('Adding theme files');
  237. gulp.src(source.themeImport)
  238. .pipe(plumber())
  239. .pipe(gulp.dest(installPaths.themeImport))
  240. ;
  241. gulp.src(source.lessImport)
  242. .pipe(plumber())
  243. .pipe(gulp.dest(installPaths.lessImport))
  244. ;
  245. // create gulp file
  246. console.info('Creating gulpfile.js');
  247. gulp.src(source.userGulpFile)
  248. .pipe(plumber())
  249. .pipe(gulp.dest(installFolder))
  250. ;
  251. }
  252. /*--------------
  253. Site Theme
  254. ---------------*/
  255. // Copy _site templates folder to destination
  256. if( fs.existsSync(installPaths.site) ) {
  257. console.info('Site folder exists, merging files (no overwrite)', installPaths.site);
  258. }
  259. else {
  260. console.info('Creating site theme folder', installPaths.site);
  261. }
  262. wrench.copyDirSyncRecursive(source.site, installPaths.site, settings.wrench.merge);
  263. /*--------------
  264. Theme Config
  265. ---------------*/
  266. gulp.task('create theme.config', function() {
  267. var
  268. // determine path to site theme folder from theme config
  269. // force CSS path variable to use forward slashes for paths
  270. pathToSite = path.relative(path.resolve(installPaths.themeConfigFolder), path.resolve(installPaths.site)).replace(/\\/g,'/'),
  271. siteVariable = "@siteFolder : '" + pathToSite + "/';"
  272. ;
  273. // rewrite site variable in theme.less
  274. console.info('Adjusting @siteFolder to: ', pathToSite + '/');
  275. if(fs.existsSync(installPaths.themeConfig)) {
  276. console.info('Modifying src/theme.config (LESS config)', installPaths.themeConfig);
  277. return gulp.src(installPaths.themeConfig)
  278. .pipe(plumber())
  279. .pipe(replace(regExp.siteVariable, siteVariable))
  280. .pipe(gulp.dest(installPaths.themeConfigFolder))
  281. ;
  282. }
  283. else {
  284. console.info('Creating src/theme.config (LESS config)', installPaths.themeConfig);
  285. return gulp.src(source.themeConfig)
  286. .pipe(plumber())
  287. .pipe(rename({ extname : '' }))
  288. .pipe(replace(regExp.siteVariable, siteVariable))
  289. .pipe(gulp.dest(installPaths.themeConfigFolder))
  290. ;
  291. }
  292. });
  293. /*--------------
  294. Semantic.json
  295. ---------------*/
  296. gulp.task('create semantic.json', function() {
  297. var
  298. jsonConfig = install.createJSON(answers)
  299. ;
  300. // adjust variables in theme.less
  301. if( fs.existsSync(files.config) ) {
  302. console.info('Extending config file (semantic.json)', installPaths.config);
  303. return gulp.src(installPaths.config)
  304. .pipe(plumber())
  305. .pipe(rename(settings.rename.json)) // preserve file extension
  306. .pipe(jsonEditor(jsonConfig))
  307. .pipe(gulp.dest(installPaths.configFolder))
  308. ;
  309. }
  310. else {
  311. console.info('Creating config file (semantic.json)', installPaths.config);
  312. return gulp.src(source.config)
  313. .pipe(plumber())
  314. .pipe(rename({ extname : '' })) // remove .template from ext
  315. .pipe(jsonEditor(jsonConfig))
  316. .pipe(gulp.dest(installPaths.configFolder))
  317. ;
  318. }
  319. });
  320. runSequence(
  321. 'create theme.config',
  322. 'create semantic.json',
  323. callback
  324. );
  325. });
  326. gulp.task('clean up install', function() {
  327. // Completion Message
  328. if(installFolder) {
  329. console.log('\n Setup Complete! \n Installing Peer Dependencies. \x1b[0;31mPlease refrain from ctrl + c\x1b[0m... \n After completion navigate to \x1b[92m' + answers.semanticRoot + '\x1b[0m and run "\x1b[92mgulp build\x1b[0m" to build');
  330. process.exit(0);
  331. }
  332. else {
  333. console.log('');
  334. console.log('');
  335. }
  336. return gulp
  337. .src('gulpfile.js')
  338. .pipe(prompt.prompt(questions.cleanup, function(answers) {
  339. if(answers.cleanup == 'yes') {
  340. del(install.setupFiles);
  341. }
  342. if(answers.build == 'yes') {
  343. gulp.start('build');
  344. }
  345. }))
  346. ;
  347. });
  348. runSequence(
  349. 'run setup',
  350. 'create install files',
  351. 'clean up install',
  352. callback
  353. );
  354. };