Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/bin/kbuild.js
blob: e1623cf8631251248d2bdeafee3a6b1173326be3 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//narwhal ~/karma/mainline/bin/kbuild.js --karma-src-dir karmaSrcDir --build-dir build --lessons-src-dir lessonsSrcDir --tag stable --bundle-type xo


//requires narwhal
var file = require('file');
var os = require('os');
var args = require('args');

//list of repos for lessons
var LESSONS = [ [" git://git.sugarlabs.org/karma_adding_up_to_10_svg/mainline.git", 
		 "karma_adding_up_to_10_svg"], 
	     // ["gitorious@git.sugarlabs.org:karma_conozco-uruguay/mainline.git", 
	     //  'karma_Conozco-Uruguay'],
	     // ["~/karma/lessons/English_Alphabet_Puzzle_Solving", 
	     //  'karma_English_Alphabet_Puzzle_Solving'] 
	    ];

var KARMA_REPO = "~/karma/mainline/";
var buildDir = "./build/";
var buildHtmlDir = './';
var bundleSrcDir = "tools/";
var bundleType = 'xo';
var tag = "master";
var lessonsSrcDir = buildDir + '/lessons/';
var lessonSrcDir = lessonsSrcDir;
var includedLessons = [];

var parseOptions = function(){
    //parse args
    var parser = new args.Parser();
    parser.help('Builds and distributes Karma bundle to different type of targets');

    parser.option('-t', '--tag', 'tag')
	.help("which tag to checkout for all lessons")
	.set();

    parser.option('--build-dir', 'buildDir')
	.help("directory where bundle is built")
        .def('build/')
	.set();    
    
    parser.option('--bundle-type', 'bundleType')
	.help("type of bundle to be built. Examples: 'web' for use on a website" +
	     " 'xo' to create a .xo bundle for the Sugar environment")
	.def('xo')
	.choices(['xo', 'web'])
	.set();
	
    parser.helpful();

    var options = parser.parse(system.args);

    tag = options.tag || tag;
    KARMA_REPO = options.KARMA_REPO || KARMA_REPO;
    buildDir = options.buildDir || buildDir;
    lessonsSrcDir = options.lessonsSrcDir || lessonsSrcDir;
    bundleType = options.bundleType || bundleType;

    if(bundleType === 'xo'){
	bundleSrcDir = bundleSrcDir + '/xo_bundle';
    } 


    if (buildDir === bundleSrcDir || buildDir === lessonsSrcDir || 
       bundleSrcDir === lessonsSrcDir){
	throw new Error("The arguments you supplied for KARMASRCDIR, BUILDDIR, and " +
			"LESSONSSRCDIR are not unique. These arguments must be unique");
	exit();
    }
};


var prepareKarmaSrcDir = function(){
    
    //check that the gitdir exists
    //if not create it
    var cmd = '';
    var proc = '';
    var exitCode;

    if(!file.exists(bundleSrcDir)){
	file.mkdir(bundleSrcDir);   
    } 

    if (!file.exists(bundleSrcDir + '/.git')){
	cmd = "git clone " + KARMA_REPO + " " + bundleSrcDir;
	proc = os.popen(cmd);
	exitCode = proc.wait();
	if (exitCode !== 0){
	    throw new Error("Could not clone the bundle repository: " +
			    KARMA_REPO + "\nResults of shell commands \n" + 
			    proc.stderr.read());
	} 
    } else {
	//pull newest version
	cmd = "cd " + bundleSrcDir + ";git pull origin master ";
	proc = os.popen(cmd);
	exitCode = proc.wait();
	if (exitCode !== 0){
	    throw new Error("Could not update the bundle repository: " +
			    KARMA_REPO + "\nResults of shell commands \n" + 
			    proc.stderr.read());
	}
    }
};

var prepareLessonsSrcDir = function(){
    if(!file.exists(lessonsSrcDir)){
	file.mkdir(lessonsSrcDir);   
    } 
};


var prepareEachLessonSrcDir = function(repo){
    var cmd = '';
    var proc = '';
    var exitCode = '';
    var lessonRepo = repo[0];
    var lessonName = repo[1];
    lessonSrcDir = lessonsSrcDir +'/' + lessonName;
    
    if(!file.exists(lessonSrcDir)){
	file.mkdir(lessonSrcDir);   
    } 
    
    if (!file.exists(lessonSrcDir + '/.git')){
	cmd = "git clone " + lessonRepo + " " + lessonSrcDir;
	proc = os.popen(cmd);
	exitCode = proc.wait();
	if (exitCode !== 0){
	    throw new Error("Could not clone the lesson repository: " +
			    lessonRepo + "\nResults of shell commands \n" + 
			    proc.stderr.read());
	}
    } 

    //pull version that matches tag supplied by user
    cmd = "cd " + lessonSrcDir + ";git pull -t origin master; git checkout " + tag;
    proc = os.popen(cmd);
    exitCode = proc.wait();
    if (exitCode !== 0){
	print("There isn't a commit in that lesson repository " +
	      lessonRepo + "\n that matches the tag " + tag + 
	      "\nResults of shell commands \n" + 
	      proc.stderr.read());
    } else{
	includedLessons.push(lessonName);

    }
 };


//check that build dir exists, if not create it
var prepareBuildDir = function(){
    var cmd = '';
    var proc = '';
    var exitCode = '';

    var copyLesson2BuildDir = function(lesson){
	    cmd = "cp -r " + lessonsSrcDir + "/" + lesson + 
		" " + buildHtmlDir + "/lessons/;" +
		"find " + buildDir + " -d -name '.git' " +
		"-exec rm -rf {} \\; ";
	    proc = os.popen(cmd);
	    print(proc.stdout.read());
	    exitCode = proc.wait();
	    if (exitCode !== 0){
		throw new Error("Couldn't copy lesson from " + lesson + " to " +
				buildDir + "\nStderr " + proc.stderr.read());	
	    }
	    delete proc;
    };

    if(file.exists(buildDir)){
	proc = os.popen("rm -rf " + buildDir);
	exitCode = proc.wait();
	if (exitCode !== 0){
	    throw new Error("Couldn't remove directory " + buildDir);
	}
	delete proc;
    }
    
    file.mkdir(buildDir);    

    if (bundleType === "web"){
	//copy over the web bundle
	buildHtmlDir = buildDir;

	var copyWebBundle2BuildDir = function(){
	    cmd = "cp -r " + bundleSrcDir + "/* " + buildDir +
		"/; rm -rf " + buildDir + "/bundles" +
		"; rm -rf " + buildDir + "/docs" +
		"; rm -rf " + buildDir + "/tests" +
		"; rm -rf " + buildDir + "/bin" +
		"; rm -rf " + buildDir + "/lessons" +
		"; mkdir -p " + buildDir + "/lessons" +
		"; find " + buildDir + " -d -name '.git' " +
		"-exec rm -rf {} \\; ";
	    proc = os.popen(cmd);
	    exitCode = proc.wait();
	    if (exitCode !== 0){
		throw new Error("Couldn't copy from directory " + bundleSrcDir + " to " +
				buildDir + "\nStderr " + proc.stderr.read());
	    }

	    delete proc;
	};
	
	copyWebBundle2BuildDir();
	
    } else if (bundleType === "xo"){
	buildHtmlDir = buildDir + "/karma";
	//copy over XO bundle and lesson stuff
	var copyXoBundle2BuildDir = function(){
	    cmd = "cp -r " + bundleSrcDir + "/bundles/xo/* " + buildDir +
		"/; rm -rf " + buildHtmlDir +
		"; mkdir -p " + buildHtmlDir + "/lessons" +
		"; cp -r " + bundleSrcDir + "/assets " + buildHtmlDir +
		"; cp -r " + bundleSrcDir + "/css " + buildHtmlDir  +
		"; cp -r " + bundleSrcDir + "/index* " + buildHtmlDir +
		"; cp -r " + bundleSrcDir + "/js " + buildHtmlDir + 
		"; find " + buildDir + " -d -name '.git' " +
		"-exec rm -rf {} \\; ";
	    proc = os.popen(cmd);
	    exitCode = proc.wait();
	    if (exitCode !== 0){
		throw new Error("Couldn't copy from directory " + bundleSrcDir + " to " +
				buildDir + "\nStderr " + proc.stderr.read());
	    }
	    delete proc;
	};    
	
	copyXoBundle2BuildDir();
    }    
	includedLessons.forEach(copyLesson2BuildDir);

};

var moveJsLibraries = function() {
    var cmd = '';
    var proc;
    var exitCode;
    
    //move all the js libraries to the common js folder except for lesson.js
    var moveCommonJsFiles = function(){
	cmd = "find " + buildHtmlDir + "/lessons -name '*.js' \\! -name 'lesson.js'" +
	    " -exec mv -n {} " + buildHtmlDir + "/js  \\;;" +
            "find " + buildHtmlDir + "/lessons -name '*.js' \\! -name 'lesson.js'" +
	    " -exec rm {} \\;";

	proc = os.popen(cmd);
	exitCode = proc.wait();
	if (exitCode !== 0){
	    throw new Error("Couldn't move directories to " +
			    buildHtmlDir + "\nStderr: " + proc.stderr.read());
	}
	delete proc;
    };

    var changeHtmlScriptPaths = function(){
	/*	sed -n 's/src="\.*\/*\(js\/.*\.js\)"/src="..\/..\/\1"/p' index.html
	# fix lesson.js so its path is unchanged
	sed -n 's/src="\.*\/*\.*\/*\(js\/lesson.js\)"/src=".\/\1"/p' index.html
	 */
	var cmdFindIndexFiles = 
	    "find " + buildHtmlDir + "/lessons -name '*.html' -exec ";
	var cmdChangePaths = 
	    "sed -i 's/src=\"\\.*\\/*\\(js\\/.*.js\\)\"/src=\"..\\/..\\/\\1\"/g' {} \\;";
	var cmdFixLessonJs = 
	    "sed -i 's/src=\"\\.*\\/*\\.*\\/*\\(js\\/lesson.js\\)\"/src=\"\\.\\/\\1\"/g'" +
	    " {} \\;";
	cmd = cmdFindIndexFiles + cmdChangePaths + ';' +
	      cmdFindIndexFiles + cmdFixLessonJs;
	print(cmd);
	proc = os.popen(cmd);
	exitCode = proc.wait();
	if (exitCode !== 0){
	    throw new Error("Couldn't change js references " +
			    "\nStderr: " + proc.stderr.read());
	}
	print(proc.stderr.read());
	delete proc;
    };
    

    moveCommonJsFiles();
    changeHtmlScriptPaths();
    
};



parseOptions();
prepareKarmaSrcDir();
prepareLessonsSrcDir();
LESSONS.forEach(prepareEachLessonSrcDir);
prepareBuildDir();
moveJsLibraries();


//command for appending text to part of an html file
// sed -i '/<some html>/a\foo' index.html