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.

365 lines
11 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 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('gulp-prompt'),
  26. rename = require('gulp-rename'),
  27. replace = require('gulp-replace'),
  28. requireDotFile = require('require-dot-file'),
  29. wrench = require('wrench'),
  30. // user config
  31. config = require('./config/user'),
  32. // install config
  33. install = require('./config/project/install'),
  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 () {
  46. var
  47. currentConfig = requireDotFile('semantic.json'),
  48. manager = install.getPackageManager(),
  49. rootQuestions = questions.root
  50. ;
  51. //console.clear();
  52. /* use to debug NPM install from standard git clone
  53. manager = {
  54. name : 'NPM',
  55. root : path.normalize(__dirname + '/../')
  56. };
  57. */
  58. /*--------------
  59. PM Config
  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. // run update scripts if semantic.json exists
  67. if(currentConfig && manager.name === 'NPM') {
  68. var
  69. updateFolder = manager.root,
  70. updatePaths = {
  71. config : path.join(updateFolder, files.config),
  72. modules : path.join(updateFolder, currentConfig.base, folders.modules),
  73. tasks : path.join(updateFolder, currentConfig.base, folders.tasks),
  74. definition : path.join(updateFolder, currentConfig.paths.source.definitions),
  75. site : path.join(updateFolder, currentConfig.paths.source.site),
  76. theme : path.join(updateFolder, currentConfig.paths.source.themes)
  77. }
  78. ;
  79. // duck-type if there is a project installed
  80. if( fs.existsSync(updatePaths.definition) ) {
  81. // perform update if new version
  82. if(currentConfig.version !== release.version) {
  83. console.log('Updating Semantic UI from ' + currentConfig.version + ' to ' + release.version);
  84. console.info('Updating ui definitions...');
  85. // fs.renameSync(oldPath, newPath); swap to move before debut
  86. wrench.copyDirSyncRecursive(source.definitions, updatePaths.definition, settings.wrench.update);
  87. console.info('Updating default theme...');
  88. wrench.copyDirSyncRecursive(source.themes, updatePaths.theme, settings.wrench.update);
  89. console.info('Updating gulp tasks...');
  90. wrench.copyDirSyncRecursive(source.modules, updatePaths.modules, settings.wrench.update);
  91. wrench.copyDirSyncRecursive(source.tasks, updatePaths.tasks, settings.wrench.update);
  92. wrench.copyDirSyncRecursive(source.site, updatePaths.site, settings.wrench.site);
  93. console.info('Updating version...');
  94. // update version number in semantic.json
  95. gulp.src(updatePaths.config)
  96. .pipe(plumber())
  97. .pipe(rename(settings.rename.json)) // preserve file extension
  98. .pipe(jsonEditor({
  99. version: release.version
  100. }))
  101. .pipe(gulp.dest(updateFolder))
  102. ;
  103. return;
  104. }
  105. else {
  106. console.log('Current version of Semantic UI already installed, skipping set-up');
  107. return;
  108. }
  109. }
  110. }
  111. /*--------------
  112. Determine Root
  113. ---------------*/
  114. // PM that supports Build Tools (NPM Only Now)
  115. if(manager.name == 'NPM') {
  116. rootQuestions[0].message = rootQuestions[0].message
  117. .replace('{packageMessage}', 'We detected you are using \033[92m' + manager.name + '\033[0m. Nice! ')
  118. .replace('{root}', manager.root)
  119. ;
  120. // set default path to detected PM root
  121. rootQuestions[0].default = manager.root;
  122. rootQuestions[1].default = manager.root;
  123. // insert PM questions after "Install Type" question
  124. Array.prototype.splice.apply(questions.setup, [2, 0].concat(rootQuestions));
  125. }
  126. /*--------------
  127. Set-up
  128. ---------------*/
  129. return gulp
  130. .src('gulpfile.js')
  131. .pipe(prompt.prompt(questions.setup, function(answers) {
  132. /*--------------
  133. Exit Conditions
  134. ---------------*/
  135. // if config exists and user specifies not to proceed
  136. if(answers.overwrite !== undefined && answers.overwrite == 'no') {
  137. return;
  138. }
  139. //console.clear();
  140. console.log('Installing');
  141. console.log('------------------------------');
  142. /*--------------
  143. Paths
  144. ---------------*/
  145. var
  146. installPaths = {
  147. config : files.config,
  148. configFolder : folders.config,
  149. site : answers.site || folders.site,
  150. themeConfig : files.themeConfig,
  151. themeConfigFolder : folders.themeConfig
  152. },
  153. installFolder = false
  154. ;
  155. /*--------------
  156. PM Install
  157. ---------------*/
  158. // Check if PM install
  159. if(answers.useRoot || answers.customRoot) {
  160. // Set root to custom root path if set
  161. if(answers.customRoot) {
  162. manager.root = answers.customRoot;
  163. }
  164. // special install paths only for PM install
  165. installPaths = extend(false, {}, installPaths, {
  166. definition : folders.definitions,
  167. theme : folders.themes,
  168. modules : folders.modules,
  169. tasks : folders.tasks,
  170. themeImport : folders.themeImport
  171. });
  172. // add project root to semantic root
  173. installFolder = path.join(manager.root, answers.semanticRoot);
  174. // add install folder to all output paths
  175. for(var destination in installPaths) {
  176. if(installPaths.hasOwnProperty(destination)) {
  177. if(destination == 'config' || destination == 'configFolder') {
  178. // semantic config goes in project root
  179. installPaths[destination] = path.normalize( path.join(manager.root, installPaths[destination]) );
  180. }
  181. else {
  182. // all other paths go in semantic root
  183. installPaths[destination] = path.normalize( path.join(installFolder, installPaths[destination]) );
  184. }
  185. }
  186. }
  187. // create project folders
  188. try {
  189. mkdirp.sync(installFolder);
  190. mkdirp.sync(installPaths.definition);
  191. mkdirp.sync(installPaths.theme);
  192. mkdirp.sync(installPaths.modules);
  193. mkdirp.sync(installPaths.tasks);
  194. }
  195. catch(error) {
  196. console.error('NPM does not have permissions to create folders at your specified path. Adjust your folders permissions and run "npm install" again');
  197. }
  198. // fs.renameSync(oldPath, newPath); swap to move before debut
  199. // copy gulp node_modules
  200. console.info('Copying definitions to ', installPaths.definition);
  201. wrench.copyDirSyncRecursive(source.definitions, installPaths.definition, settings.wrench.install);
  202. wrench.copyDirSyncRecursive(source.themes, installPaths.theme, settings.wrench.install);
  203. console.info('Copying build tools', installPaths.tasks);
  204. //wrench.copyDirSyncRecursive(source.modules, installPaths.modules, settings.wrench.install);
  205. wrench.copyDirSyncRecursive(source.tasks, installPaths.tasks, settings.wrench.install);
  206. // copy theme import
  207. console.info('Adding theme import file');
  208. gulp.src(source.themeImport)
  209. .pipe(plumber())
  210. .pipe(gulp.dest(installPaths.themeImport))
  211. ;
  212. // create gulp file
  213. console.info('Creating gulp-file.js');
  214. gulp.src(source.userGulpFile)
  215. .pipe(plumber())
  216. .pipe(gulp.dest(installFolder))
  217. ;
  218. }
  219. /*--------------
  220. Site Theme
  221. ---------------*/
  222. // Copy _site templates folder to destination
  223. if( fs.existsSync(installPaths.site) ) {
  224. console.info('Site folder exists, merging files (no overwrite)', installPaths.site);
  225. }
  226. else {
  227. console.info('Creating site theme folder', installPaths.site);
  228. }
  229. wrench.copyDirSyncRecursive(source.site, installPaths.site, settings.wrench.site);
  230. /*--------------
  231. Theme Config
  232. ---------------*/
  233. var
  234. // determine path to site theme folder from theme config
  235. // force CSS path variable to use forward slashes for paths
  236. pathToSite = path.relative(path.resolve(installPaths.themeConfigFolder), path.resolve(installPaths.site)).replace(/\\/g,'/'),
  237. siteVariable = "@siteFolder : '" + pathToSite + "/';"
  238. ;
  239. // rewrite site variable in theme.less
  240. console.info('Adjusting @siteFolder to: ', pathToSite + '/');
  241. if(fs.existsSync(installPaths.themeConfig)) {
  242. console.info('Modifying src/theme.config (LESS config)', installPaths.themeConfig);
  243. gulp.src(installPaths.themeConfig)
  244. .pipe(plumber())
  245. .pipe(replace(regExp.siteVariable, siteVariable))
  246. .pipe(gulp.dest(installPaths.themeConfigFolder))
  247. ;
  248. }
  249. else {
  250. console.info('Creating src/theme.config (LESS config)', installPaths.themeConfig);
  251. gulp.src(source.themeConfig)
  252. .pipe(plumber())
  253. .pipe(rename({ extname : '' }))
  254. .pipe(replace(regExp.siteVariable, siteVariable))
  255. .pipe(gulp.dest(installPaths.themeConfigFolder))
  256. ;
  257. }
  258. /*--------------
  259. Semantic.json
  260. ---------------*/
  261. var
  262. jsonConfig = install.createJSON(answers)
  263. ;
  264. // adjust variables in theme.less
  265. if( fs.existsSync(files.config) ) {
  266. console.info('Extending config file (semantic.json)', installPaths.config);
  267. gulp.src(installPaths.config)
  268. .pipe(plumber())
  269. .pipe(rename(settings.rename.json)) // preserve file extension
  270. .pipe(jsonEditor(jsonConfig))
  271. .pipe(gulp.dest(installPaths.configFolder))
  272. ;
  273. }
  274. else {
  275. console.info('Creating config file (semantic.json)', installPaths.config);
  276. gulp.src(source.config)
  277. .pipe(plumber())
  278. .pipe(rename({ extname : '' })) // remove .template from ext
  279. .pipe(jsonEditor(jsonConfig))
  280. .pipe(gulp.dest(installPaths.configFolder))
  281. ;
  282. }
  283. // omit cleanup questions for managed install
  284. if(installFolder) {
  285. questions.cleanup = [];
  286. }
  287. console.log('');
  288. console.log('');
  289. }))
  290. .pipe(prompt.prompt(questions.cleanup, function(answers) {
  291. if(answers.cleanup == 'yes') {
  292. del(install.setupFiles);
  293. }
  294. if(answers.build == 'yes') {
  295. gulp.start('build');
  296. }
  297. }))
  298. ;
  299. };