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.

454 lines
13 KiB

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