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.

459 lines
13 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
  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('prompt-sui'),
  27. rename = require('gulp-rename'),
  28. replace = require('gulp-replace'),
  29. requireDotFile = require('require-dot-file'),
  30. wrench = require('wrench-sui'),
  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 ' + manager.name + ' 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. // If auto-install is switched on, we skip the configuration section and simply reuse the configuration from semantic.json
  153. if(install.shouldAutoInstall()) {
  154. answers = {
  155. overwrite : 'yes',
  156. install : 'auto',
  157. useRoot : true,
  158. semanticRoot : currentConfig.base
  159. };
  160. }
  161. else {
  162. return gulp
  163. .src('gulpfile.js')
  164. .pipe(prompt.prompt(questions.setup, function(setupAnswers) {
  165. // hoist
  166. answers = setupAnswers;
  167. }))
  168. ;
  169. }
  170. });
  171. gulp.task('create install files', function(callback) {
  172. /*--------------
  173. Exit Conditions
  174. ---------------*/
  175. // if config exists and user specifies not to proceed
  176. if(answers.overwrite !== undefined && answers.overwrite == 'no') {
  177. return;
  178. }
  179. console.clear();
  180. if(install.shouldAutoInstall()) {
  181. console.log('Auto-Installing (Without User Interaction)');
  182. }
  183. else {
  184. console.log('Installing');
  185. }
  186. console.log('------------------------------');
  187. /*--------------
  188. Paths
  189. ---------------*/
  190. var
  191. installPaths = {
  192. config : files.config,
  193. configFolder : folders.config,
  194. site : answers.site || folders.site,
  195. themeConfig : files.themeConfig,
  196. themeConfigFolder : folders.themeConfig
  197. }
  198. ;
  199. /*--------------
  200. NPM Install
  201. ---------------*/
  202. // Check if PM install
  203. if(manager && (answers.useRoot || answers.customRoot)) {
  204. // Set root to custom root path if set
  205. if(answers.customRoot) {
  206. if(answers.customRoot === '') {
  207. console.log('Unable to proceed, invalid project root');
  208. return;
  209. }
  210. manager.root = answers.customRoot;
  211. }
  212. // special install paths only for PM install
  213. installPaths = extend(false, {}, installPaths, {
  214. definition : folders.definitions,
  215. lessImport : folders.lessImport,
  216. tasks : folders.tasks,
  217. theme : folders.themes,
  218. defaultTheme : path.join(folders.themes, folders.defaultTheme),
  219. themeImport : folders.themeImport
  220. });
  221. // add project root to semantic root
  222. installFolder = path.join(manager.root, answers.semanticRoot);
  223. // add install folder to all output paths
  224. for(var destination in installPaths) {
  225. if( installPaths.hasOwnProperty(destination) ) {
  226. // config goes in project root, rest in install folder
  227. installPaths[destination] = (destination == 'config' || destination == 'configFolder')
  228. ? path.normalize( path.join(manager.root, installPaths[destination]) )
  229. : path.normalize( path.join(installFolder, installPaths[destination]) )
  230. ;
  231. }
  232. }
  233. // create project folders
  234. try {
  235. mkdirp.sync(installFolder);
  236. mkdirp.sync(installPaths.definition);
  237. mkdirp.sync(installPaths.theme);
  238. mkdirp.sync(installPaths.tasks);
  239. }
  240. catch(error) {
  241. console.error('NPM does not have permissions to create folders at your specified path. Adjust your folders permissions and run "npm install" again');
  242. }
  243. console.log('Installing to \x1b[92m' + answers.semanticRoot + '\x1b[0m');
  244. console.info('Copying UI definitions');
  245. wrench.copyDirSyncRecursive(source.definitions, installPaths.definition, settings.wrench.overwrite);
  246. console.info('Copying UI themes');
  247. wrench.copyDirSyncRecursive(source.themes, installPaths.theme, settings.wrench.merge);
  248. wrench.copyDirSyncRecursive(source.defaultTheme, installPaths.defaultTheme, settings.wrench.overwrite);
  249. console.info('Copying gulp tasks');
  250. wrench.copyDirSyncRecursive(source.tasks, installPaths.tasks, settings.wrench.overwrite);
  251. // copy theme import
  252. console.info('Adding theme files');
  253. gulp.src(source.themeImport)
  254. .pipe(plumber())
  255. .pipe(gulp.dest(installPaths.themeImport))
  256. ;
  257. gulp.src(source.lessImport)
  258. .pipe(plumber())
  259. .pipe(gulp.dest(installPaths.lessImport))
  260. ;
  261. // create gulp file
  262. console.info('Creating gulpfile.js');
  263. gulp.src(source.userGulpFile)
  264. .pipe(plumber())
  265. .pipe(gulp.dest(installFolder))
  266. ;
  267. }
  268. /*--------------
  269. Site Theme
  270. ---------------*/
  271. // Copy _site templates folder to destination
  272. if( fs.existsSync(installPaths.site) ) {
  273. console.info('Site folder exists, merging files (no overwrite)', installPaths.site);
  274. }
  275. else {
  276. console.info('Creating site theme folder', installPaths.site);
  277. }
  278. wrench.copyDirSyncRecursive(source.site, installPaths.site, settings.wrench.merge);
  279. /*--------------
  280. Theme Config
  281. ---------------*/
  282. gulp.task('create theme.config', function() {
  283. var
  284. // determine path to site theme folder from theme config
  285. // force CSS path variable to use forward slashes for paths
  286. pathToSite = path.relative(path.resolve(installPaths.themeConfigFolder), path.resolve(installPaths.site)).replace(/\\/g,'/'),
  287. siteVariable = "@siteFolder : '" + pathToSite + "/';"
  288. ;
  289. // rewrite site variable in theme.less
  290. console.info('Adjusting @siteFolder to: ', pathToSite + '/');
  291. if(fs.existsSync(installPaths.themeConfig)) {
  292. console.info('Modifying src/theme.config (LESS config)', installPaths.themeConfig);
  293. return gulp.src(installPaths.themeConfig)
  294. .pipe(plumber())
  295. .pipe(replace(regExp.siteVariable, siteVariable))
  296. .pipe(gulp.dest(installPaths.themeConfigFolder))
  297. ;
  298. }
  299. else {
  300. console.info('Creating src/theme.config (LESS config)', installPaths.themeConfig);
  301. return gulp.src(source.themeConfig)
  302. .pipe(plumber())
  303. .pipe(rename({ extname : '' }))
  304. .pipe(replace(regExp.siteVariable, siteVariable))
  305. .pipe(gulp.dest(installPaths.themeConfigFolder))
  306. ;
  307. }
  308. });
  309. /*--------------
  310. Semantic.json
  311. ---------------*/
  312. gulp.task('create semantic.json', function() {
  313. var
  314. jsonConfig = install.createJSON(answers)
  315. ;
  316. // adjust variables in theme.less
  317. if( fs.existsSync(installPaths.config) ) {
  318. console.info('Extending config file (semantic.json)', installPaths.config);
  319. return gulp.src(installPaths.config)
  320. .pipe(plumber())
  321. .pipe(rename(settings.rename.json)) // preserve file extension
  322. .pipe(jsonEditor(jsonConfig))
  323. .pipe(gulp.dest(installPaths.configFolder))
  324. ;
  325. }
  326. else {
  327. console.info('Creating config file (semantic.json)', installPaths.config);
  328. return gulp.src(source.config)
  329. .pipe(plumber())
  330. .pipe(rename({ extname : '' })) // remove .template from ext
  331. .pipe(jsonEditor(jsonConfig))
  332. .pipe(gulp.dest(installPaths.configFolder))
  333. ;
  334. }
  335. });
  336. runSequence(
  337. 'create theme.config',
  338. 'create semantic.json',
  339. callback
  340. );
  341. });
  342. gulp.task('clean up install', function() {
  343. // Completion Message
  344. if(installFolder && !install.shouldAutoInstall()) {
  345. 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');
  346. process.exit(0);
  347. }
  348. else {
  349. console.log('');
  350. console.log('');
  351. }
  352. // If auto-install is switched on, we skip the configuration section and simply build the dependencies
  353. if(install.shouldAutoInstall()) {
  354. return gulp.start('build');
  355. }
  356. else {
  357. return gulp
  358. .src('gulpfile.js')
  359. .pipe(prompt.prompt(questions.cleanup, function(answers) {
  360. if(answers.cleanup == 'yes') {
  361. del(install.setupFiles);
  362. }
  363. if(answers.build == 'yes') {
  364. gulp.start('build');
  365. }
  366. }))
  367. ;
  368. }
  369. });
  370. runSequence(
  371. 'run setup',
  372. 'create install files',
  373. 'clean up install',
  374. callback
  375. );
  376. };