summaryrefslogtreecommitdiffstats
path: root/modules/orionode/Gruntfile.js
blob: a30473d1002dc1bc2524a59859a06ff33a36987a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*eslint-env node*/
var _path = require("path"),
    utilFactory = require("./build/utils");
module.exports = function(grunt) {
	var util = utilFactory(grunt),
	    BADDIR = "This Gruntfile must be run from the modules/orionode folder in the Orion client repository.",
	    SOURCE_GLOB = ["**", "!**/node_modules/**", "!**/built/**", "!**/builder/**", "!**/target/**"],
	    // All paths here are relative to Gruntfile.js
	    configPath = "../../releng/org.eclipse.orion.client.releng/builder/scripts/orion.build.js",
	    clientPath = "../../",
	    staging = "target/staging/",
	    optimized = "target/optimized/";

	var orionBuildConfig = util.loadBuildConfig(configPath),
	    bundles = util.parseBundles(orionBuildConfig, {
			buildDirectory: staging,
			orionClient: clientPath
		});

	grunt.initConfig({
		pkg: grunt.file.readJSON("package.json"),
		clientPath: clientPath,
		configPath: configPath,
		staging: staging,
		optimized: optimized,
		nodeBuildConfig: util.filterBuildConfig(orionBuildConfig, "<% requirejsExcludeModules %>"),
		checkDirs: {
			orion: {
				src: ["<%= clientPath %>/bundles", "<%= configPath %>"],
				options: {
					expand: true
				}
			}
		},
		clean: {
			outdirs: {
				src: [staging + "/**" , optimized + "/**", "lib/orion.client/**"]
			}
		},
		copy: {
			// Copy each client {bundle} to lib/orion.client/bundles/{bundle}
			orionclient: {
				files: bundles.map(function(bundle) {
					return {
						expand: true,
						cwd: bundle.path,
						src: SOURCE_GLOB,
						dest: "lib/orion.client/bundles/" + bundle.name + "/"
					};
				})
			},
			// Copy each client bundle's web folder to staging dir for optimization
			stage: {
				files: bundles.map(function(bundle) {
					return {
						expand: true,
						cwd: bundle.web,
						src: SOURCE_GLOB,
						dest: staging
					};
				}).concat([
					// Copy orionode.client last since it must overwrite some files
					{
						expand: true,
						cwd: "lib/orionode.client/**",
						src: SOURCE_GLOB,
						dest: staging
					}
				])
			},
			// Copy optimized js files, source maps, and css, back to the bundles they originated from in lib/
			unstage: {
				files: [{
					expand: true,
					cwd: optimized,
					dest: "lib/",
					src: ["**/*.js", "**/*.css", "**/*.html", "**/*.map"], // includes .src.js
					rename: function(dest, src) {
						// Determine the actual destination. This is either a bundle in lib/orion.client/bundles/
						// or the Orionode client code lib/orionode.client/.
						var bundlefile, match;
						if ((match = /(.*)(\.src\.js|\.map)$/.exec(src)) != null) {
							// Source map file; use the associated .js file to decide where we go
							bundlefile = match[1];
						} else {
							bundlefile = src;
						}
						var newDest;
						// Reverse order here so later bundle in list wins over earlier one, if both contain a bundlefile
						grunt.verbose.write("Finding origin bundle for " + src + "... ");
						bundles.slice().reverse().some(function(bundle) {
							if (grunt.file.exists(_path.join(bundle.web, bundlefile))) {
								// This file originated from bundle
								newDest = _path.join(dest, "orion.client/bundles/", bundle.name, "web/", src);
								return true;
							}
						});
						// Check orionode.client last, since it overrides orion.client bundles
						if (grunt.file.exists(_path.join("lib/orionode.client/", bundlefile)))
							newDest = _path.join(dest, "orionode.client/", src);

						if (newDest)
							grunt.verbose.ok();
						else
							grunt.fail.warn("Could not determine origin bundle for " + src + ".");
						return newDest;
					}
				}]
			}
		},
		requirejs: {
			compile: {} // .options is set later
		},
		"string-replace": {
			// Ensure optimized files use the minified copy of requirejs
			requiremin: {
				files: [{
					expand: true,
					cwd: optimized,
					dest: optimized,
					src: ["**/*.js", "!**/*.src.js", "**/*.html", "!**/node_modules/**"]
				}],
				options: {
					replacements: [{
						pattern: "requirejs/require.js",
						replacement: "requirejs/require.min.js"
					}]
				}
			}
		},
		simplemocha: {
			options: {
				reporter: "dot"
			},
			all: { src: "test/*.js" }
		}
	});

	// Dynamic configuration
	grunt.config("requirejs.compile.options", util.mixin(grunt.config("nodeBuildConfig"), {
		optimize: "uglify2",
		generateSourceMaps: false, // Turn off source maps to reduce download size
		appDir: staging,
		baseUrl: "./",
		dir: optimized // TODO <% optimized %> ?
	}));

	// Task definitions
	grunt.loadNpmTasks("grunt-contrib-clean");
	grunt.loadNpmTasks("grunt-contrib-copy");
	grunt.loadNpmTasks("grunt-contrib-requirejs");
	grunt.loadNpmTasks("grunt-simple-mocha");
	grunt.loadNpmTasks("grunt-string-replace");

	grunt.registerTask("printBuild", function() {
		grunt.log.writeln("Using build file", JSON.stringify(grunt.config("requirejs.compile.options"), null, 2));
	});

	grunt.registerMultiTask("checkDirs", "Check files/dirs exist", function() {
		this.filesSrc.forEach(function(filepath) {
			grunt.verbose.write("Checking existence of " + filepath + "...");
			if (grunt.file.exists(filepath))
				grunt.verbose.ok();
			else grunt.fatal(BADDIR);
		});
	});

	grunt.registerTask("test", ["simplemocha"]);
	grunt.registerTask("optimize", ["printBuild", "copy:stage", "requirejs", "string-replace", "copy:unstage"]);
	grunt.registerTask("default", ["checkDirs", "clean", "copy:orionclient", "optimize", "test"]);
	grunt.registerTask("notest", ["checkDirs", "clean", "copy:orionclient", "optimize"]);
	grunt.registerTask("nomin",   ["checkDirs", "clean", "copy:orionclient", "string-replace:orionclient", "test"]);
};