Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Create fsExplorer to simplift the large amount of path wrangling that…

… needs to be done.
  • Loading branch information...
commit 4fd841e7acc1dc7a8d0bd1813ecede4c3e50d671 1 parent a6a35b3
@Benvie Benvie authored
Showing with 316 additions and 88 deletions.
  1. +30 −88 build-win32.js
  2. +286 −0 fsExplorer.js
View
118 build-win32.js
@@ -4,16 +4,20 @@ var child_process = require('child_process');
var Builder = require('mnm');
+var fsExplorer = require('./fsExplorer');
-var exists = fs.existsSync || path.existsSync;
-var resolve = path.resolve;
+var Path = fsExplorer.Path;
+var File = fsExplorer.File;
+var Directory = fsExplorer.Directory;
+var exists = fsExplorer.existsSync;
+var resolve = fsExplorer.resolve;
function wxBuilder(o){
o = Object.keys(o || {}).reduce(function(r,s){ r[s]=o[s]; return r; }, {
wxBase: '/wxWidgets',
- wxConfig: resolve('build', 'config-'+process.platform+'.json'),
+ wxConfig: resolve('config-'+process.platform+'.json'),
useDll: false,
verbose: false,
name: 'wxnode',
@@ -21,12 +25,12 @@ function wxBuilder(o){
this.wxConfig = new Config(o.wxConfig);
this.name = this.wxConfig.CFG = o.name;
- this.wxBase = resolve(o.wxBase);
- this.wxLibDir = resolve(o.wxBase, 'lib/vc_'+(o.useDll ? 'dll' : 'lib')+o.name);
- this.wxBuildDir = resolve(o.wxBase, 'build/msw');
- this.wxIncludeDir = resolve(o.wxBase, 'include');
- this.srcDir = resolve(__dirname, 'src');
- this.buildDir = resolve(__dirname, 'build');
+ this.wxBase = new Directory(o.wxBase);
+ this.wxLibDir = new Directory(o.wxBase+'/lib/vc_'+(o.useDll ? 'dll' : 'lib')+o.name);
+ this.wxBuildDir = new Directory(o.wxBase+'/build/msw');
+ this.wxIncludeDir = new Directory(o.wxBase+'/include');
+ this.srcDir = new Directory(__dirname+'/src');
+ this.buildDir = new Directory(__dirname+'/build');
}
wxBuilder.msLibs = [ 'kernel32', 'user32', 'gdi32', 'comdlg32',
@@ -36,7 +40,7 @@ wxBuilder.msLibs = [ 'kernel32', 'user32', 'gdi32', 'comdlg32',
wxBuilder.prototype = {
build: function build(callback){
var self = this;
- if (!exists(this.wxLibDir)) {
+ if (!this.wxLibDir.exists()) {
this.buildWX(function(e){
self.buildBindings(callback);
});
@@ -50,17 +54,14 @@ wxBuilder.prototype = {
callback = callback || function(){};
var configname = 'config_'+this.name;
- var config = new File(this.wxBuildDir + '/'+configname);
- var makefile = new File(this.wxBuildDir + '/makefile_'+this.name);
-
- this.wxConfig.save(config);
- new File(this.wxBuildDir + '/makefile.vc').copy(makefile, function(s){
+ var config = this.wxConfig.save(this.wxBuildDir.resolve(configname));
+ var makefile = this.wxBuildDir.resolve('makefile.vc').copy(this.wxBuildDir.resolve('makefile_'+this.name), function(s){
return s.replace('config.vc', configname);
});
- new File(this.wxIncludeDir + '/wx/msw/setup0.h').copy(this.wxIncludeDir + '/wx/msw/setup.h');
+ this.wxIncludeDir.resolve('wx/msw/setup0.h').copy(this.wxIncludeDir.resolve('wx/msw/setup.h'));
child_process.spawn('nmake', [ makefile.path ], {
- cwd: this.wxBuildDir,
+ cwd: this.wxBuildDir.path,
env: process.env,
customFds: [0,1,2]
}).on('exit', function(code) {
@@ -83,20 +84,20 @@ wxBuilder.prototype = {
if (this.dll)
builder.appendUnique('CXXFLAGS', '-DWXUSINGDLL');
- builder.appendLinkerSearchDir(this.wxLibDir);
+ builder.appendLinkerSearchDir(this.wxLibDir.path);
- builder.appendLinkerLibrary(dir(this.wxLibDir, '.lib'));
+ builder.appendLinkerLibrary(this.wxLibDir.filter('.lib'));
builder.appendLinkerLibrary(wxBuilder.msLibs);
builder.appendIncludeDir([
- this.srcDir,
+ this.srcDir+'',
this.srcDir+'-dummy',
this.srcDir+'-generated',
this.wxIncludeDir
]);
builder.appendSourceDir([
- this.srcDir,
+ this.srcDir+'',
this.srcDir+'-generated'
]);
@@ -104,7 +105,9 @@ wxBuilder.prototype = {
},
vcVars: function vcVars(){
- var vcpath = [10,9,8].reduce(function(r,s){ return r ? r : (s = 'VS'+s+'0COMNTOOLS') in process.env ? process.env[s] : r; }, null);
+ var vcpath = [10,9,8].reduce(function(r,s){
+ return r ? r : (s = 'VS'+s+'0COMNTOOLS') in process.env ? process.env[s] : r;
+ }, null);
var vars;
if (!vcpath || !exists(vcpath = resolve(vcpath, '../../vc/vcvarsall.bat'))) {
vars = null;
@@ -126,6 +129,7 @@ wxBuilder.prototype = {
function Config(o){
+ if (Path.isPath(o)) o = o.path;
if (typeof o === 'string' && path.extname(o) === '.json' && exists(o)) {
o = require(o);
}
@@ -151,7 +155,7 @@ Config.prototype = {
}, this).join('\r\n')+'\r\n';
},
save: function save(path){
- new File(path).write(this.serialize());
+ return new File(path).write(this.serialize());
},
load: function load(path){
return this.parse(new File(path).read());
@@ -159,79 +163,17 @@ Config.prototype = {
}
-function File(name){
- if (File.prototype.isPrototypeOf(name)) return name;
- if (!File.prototype.isPrototypeOf(this)) return new File(name);
- this.path = path.resolve(name);
-}
-
-File.prototype = {
- constructor: File,
- encoding: 'utf8',
- chunkSize: 2 << 15,
- exists: function exists(){
- return fs.existsSync(this.path);
- },
- read: function read(){
- return fs.readFileSync(this.path, this.encoding);
- },
- write: function write(content){
- fs.writeFileSync(this.path, content);
- },
- delete: function delete_(){
- fs.unlinkSync(this.path);
- },
- open: function open(flag){
- this.fd && this.close();
- return this.fd = fs.openSync(this.path, flag);
- },
- close: function close(){
- this.fd && fs.closeSync(this.fd);
- delete this.fd;
- },
- copy: function copy(to, processor){
- to = new File(to);
- to.write(processor ? processor(this.read()) : this.read());
- return to.path;
- }
-};
- // copy: function copy(to){
- // var buffer = new Buffer(this.chunkSize);
- // var dest = new File(to);
- // var read = 1, offset = 0;
-
- // this.open('r');
- // dest.open('w');
- // while (read > 0) {
- // read = fs.readSync(this.fd, buffer, 0, this.chunkSize, offset);
- // fs.writeSync(dest.fd, buffer, 0, read);
- // offset += read;
- // }
- // this.close();
- // dest.close();
- // }
-
-
-
-
-function dir(name, filter){
- if (!exists(name))
- throw new Error('Path not found: '+pnameath);
- var files = fs.readdirSync(name);
- return filter ? files.filter(function(s){ return path.extname(s) === filter }) : files;
-}
-
function execSync(params){
var f = Math.random().toString(36).slice(2)+'.temp';
child_process.exec(params+' 1>'+f+' 2>&1 & ren '+f+' _'+f);
f = new File('_'+f);
- while (!f.exists());
+ while (!exists(f.path));
var output = f.read();
f.delete();
return output;
}
-
var builder = new wxBuilder;
-builder.build();
+console.log(builder.build());
+//builder.build();
View
286 fsExplorer.js
@@ -0,0 +1,286 @@
+var path = require('path');
+var fs = require('fs');
+
+
+
+var exists = fs.exists || path.exists;
+var existsSync = fs.existsSync || path.existsSync;
+var resolve = path.resolve;
+
+var isWin = process.platform === 'win32';
+var slice = Function.call.bind([].slice);
+
+
+
+
+module.exports = {
+ explore: function explore(path){
+ return new Path(path);
+ },
+
+ Path: Path,
+ File: File,
+ Directory: Directory,
+
+ existsSync: existsSync,
+ exists: exists,
+ resolve: resolve
+};
+
+
+
+
+var cache = {};
+var existCache = {};
+
+
+function Path(request, type){
+ if (type) {
+ return Path.isPath(request) ? new type(request.path) : new type(request);
+ }
+ if (Path.isPath(request)) return request;
+ if (!Path.isPath(this)) return new Path(request);
+
+ this.path = Array.isArray(request) ? resolve.apply(null, request) : resolve(request);
+ if (this.path in cache) return cache[this.path];
+ return cache[this.path] = this;
+}
+
+Path.separator = isWin ? [/[\/\\]/, '\\'] : [/\//, '/'];
+
+Path.isPath = function isPath(o){
+ return Path.prototype.isPrototypeOf(o);
+};
+
+
+Path.prototype = {
+ constructor: Path,
+
+ getExtname: function getExtname(){
+ return this.define('extname', path.extname(this.path));
+ },
+ get extname(){ return this.getExtname() },
+
+ getDirname: function getDirname(){
+ return this.define('dirname', path.dirname(this.path));
+ },
+ get dirname(){ return this.getDirname() },
+
+ getBasename: function getBasename(){
+ return this.define('basename', path.basename(this.path));
+ },
+ get basename(){ return this.getBasename() },
+
+ getName: function getName(){
+ return this.define('name', this.basename.slice(0, -this.extname.length));
+ },
+ get name(){ return this.getName() },
+
+
+ getStats: function getStats(){
+ return this.define('stats', fs.statSync(this.path));
+ },
+ get stats(){ return this.getStats() },
+
+ getParts: function getParts(){
+ this.define('parts', this.path.split(Path.separator[0]));
+ if (isWin) {
+ this.define('drive', this.parts.shift());
+ }
+ return this.parts;
+ },
+ get parts(){ return this.getParts() },
+
+ getType: function getType(){
+ if (!this.exists()) return null;
+ var stats = fs.statSync(this.path);
+ if (stats.isFile()) {
+ this.__proto__ = File.prototype;
+ return 'File';
+ }
+ if (stats.isDirectory()) {
+ this.__proto__ = Directory.prototype;
+ return 'Directory';
+ }
+ return null;
+ },
+ get type(){ return this.getType() },
+
+ getParent: function getParent(){
+ if (this.root) return
+ this.define('parent', new Directory([this.path, '..']));
+ if (this.parent === this) {
+ this.define('root', true);
+ }
+ return this.parent;
+ },
+ get parent(){ return this.getParent() },
+
+ resolve: function resolve(request, type){
+ type = type || File;
+ return new type([this.path].concat(request));
+ },
+
+ relative: function relative(to){
+ return path.relative(this.path, to);
+ },
+
+ toUnix: function toUnix(){
+ return '/'+this.split.join('/');
+ },
+
+ toWin: function toWin(){
+ return [this.drive ? this.drive : 'C:'].concat(this.split).join('\\');
+ },
+
+ toIdentifier: function toIdentifier(){
+ return this.basename.slice(0,-this.extname.length).replace(/[^\w]+(.)?/g, function(m,c){
+ return c ? c.toUpperCase() : '';
+ });
+ },
+
+ toString: function toString(){
+ return this.path;
+ },
+
+ exists: function exists(){
+ if (existCache[this.path]) return existCache[this.path][0];
+ existCache[this.path] = [
+ existsSync(this.path),
+ setTimeout(function(){ delete existCache[this.path] }.bind(this), 10000)
+ ];
+ return existCache[this.path][0];
+ },
+
+ define: function define(name, value, hidden, readonly){
+ if (arguments.length === 2) {
+ hidden = this.constructor.prototype === this;
+ }
+ Object.defineProperty(this, name, {
+ value: value,
+ enumerable: !hidden,
+ writable: !readonly,
+ configurable: true
+ });
+ return value;
+ }
+};
+
+
+
+function File(name){
+ if (!Path.isPath(this)) return new File(name);
+ if (Path.call(this, name) === name) return name;
+}
+
+File.prototype = {
+ __proto__: Path.prototype,
+ constructor: File,
+ encoding: 'utf8',
+ chunkSize: 2 << 15,
+ isFile: true,
+ type: 'File',
+ read: function read(){
+ return this.contents = fs.readFileSync(this.path, this.encoding);
+ },
+ write: function write(content){
+ this.contents = content || this.contents || '';
+ fs.writeFileSync(this.path, this.contents);
+ return this;
+ },
+ delete: function delete_(){
+ fs.unlinkSync(this.path);
+ delete this.contents;
+ return this;
+ },
+ move: function move(to){
+ to = new File(to);
+ fs.renameSync(this.path, to.path);
+ return to;
+ },
+ copy: function copy(to, processor){
+ to = new File(to);
+ to.write(processor ? processor(this.read()) : this.read());
+ return to;
+ },
+ open: function open(flag){
+ this.fd && this.close();
+ this.fd = fs.openSync(this.path, flag);
+ return this;
+ },
+ close: function close(){
+ this.fd && fs.closeSync(this.fd);
+ delete this.fd;
+ return this;
+ },
+ streamCopy: function streamCopy(to){
+ var buffer = new Buffer(this.chunkSize);
+ var dest = new File(to);
+ var read = 1, offset = 0;
+
+ this.open('r');
+ dest.open('w');
+ while (read > 0) {
+ read = fs.readSync(this.fd, buffer, 0, this.chunkSize, offset);
+ fs.writeSync(dest.fd, buffer, 0, read);
+ offset += read;
+ }
+ this.close();
+ dest.close();
+ return dest;
+ }
+};
+
+
+
+function Directory(name){
+ if (!Path.isPath(this)) return new Directory(name);
+ if (Path.call(this, name) === name) return name;
+}
+
+Directory.filters = {
+ ext: function ext(ext){
+ return function(f){ return path.extname(f) === ext }
+ },
+ prop: function prop(n, to, op){
+ switch (op) {
+ default:
+ case '===': return function(f){ return f[n] === to };
+ case '!==': return function(f){ return f[n] !== to };
+ case '>': return function(f){ return f[n] > to };
+ case '<': return function(f){ return f[n] < to };
+ }
+ }
+};
+
+Directory.prototype = {
+ __proto__: Path.prototype,
+ constructor: Directory,
+ isDir: true,
+ type: 'Directory',
+ map: function map(names){
+ return (Array.isArray(names) ? names : [names]).map(resolve.bind(null, this.path));
+ },
+ read: function read(unmapped){
+ var names = fs.readdirSync(this.path);
+ return unmapped ? names : this.map(names);
+ },
+ filter: function filter(filterBy){
+ var callback;
+ if (typeof filtberBy === 'function') {
+ callback = filterBy;
+ } else if (typeof filterBy === 'string') {
+ if (filterBy[0] === '.') {
+ callback = Directory.filters.ext(filterBy)
+ }
+ }
+ return this.read().filter(callback);
+ }
+};
+
+
+
+// function filesize(a){
+// for (var b=0; a>=1024; b++) a /= 1024;
+// return (b ? a.toFixed(2)+' '+' kmgt'[b] : a+' ')+'b';
+// }
Please sign in to comment.
Something went wrong with that request. Please try again.