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.

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