Skip to content

Commit

Permalink
Add shebang lookup.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbt committed Aug 22, 2012
1 parent 2252cee commit 758aed0
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/docker.js
Expand Up @@ -514,15 +514,36 @@ Docker.prototype.parseSections = function(data, language){
* Provides language-specific params for a given file name.
*
* @param {string} filename The name of the file to test
* @param {string} filedata The contents of the file (to check for shebang)
* @return {object} Object containing all of the language-specific params
*/
Docker.prototype.languageParams = function(filename){
Docker.prototype.languageParams = function(filename, filedata){

// First try to detect the language from the file extension
var ext = path.extname(filename);
ext = ext.replace(/^\./, '');

// Bit of a hacky way of incorporating .C for C++
if(ext === '.C') return 'cpp';
ext = ext.toLowerCase();

for(var i in this.languages){
if(!this.languages.hasOwnProperty(i)) continue;
if(this.languages[i].extensions.indexOf(ext) !== -1) return i;
}

// If that doesn't work, see if we can grab a shebang

var shebangRegex = /^\n*#!\s*(?:\/usr\/bin\/env)?\s*(?:[^\n]*\/)*([^\/\n]+)(?:\n|$)/;
var match = shebangRegex.exec(filedata);
if(match){
for(var j in this.languages){
if(!this.languages.hasOwnProperty(j)) continue;
if(this.languages[j].executables && this.languages[j].executables.indexOf(match[1]) !== -1) return j;
}
}

// If we still can't figure it out, give up and return false.
return false;
};

Expand All @@ -539,30 +560,37 @@ Docker.prototype.languageParams = function(filename){
Docker.prototype.languages = {
javascript: {
extensions: [ 'js' ],
executables: [ 'node' ],
comment: '//', multiLine: [ /\/\*\*?/, /\*\// ], commentsIgnore: /^\s*\/\/=/, dox: true
},
coffeescript: {
extensions: [ 'coffee' ],
executables: [ 'coffee' ],
comment: '#', multiLine: [ /^#{3}\s*$/m, /^#{3}\s*$/m ], dox: true
},
ruby: {
extensions: [ 'rb' ],
executables: [ 'ruby' ],
comment: '#', multiLine: [ /\=begin/, /\=end/ ]
},
python: {
extensions: [ 'py' ],
executables: [ 'python' ],
comment: '#' // Python has no block commments :-(
},
perl: {
extensions: [ 'pl', 'pm' ],
executables: [ 'perl' ],
comment: '#' // Nor (really) does perl.
},
c: {
extensions: [ 'c', 'h' ],
executables: [ 'gcc' ],
comment: '//', multiLine: [ /\/\*/, /\*\// ]
},
cpp: { // TODO get this to pick up .C
extensions: [ 'cc', 'cpp' ],
executables: [ 'g++' ],
comment: '//', multiLine: [ /\/\*/, /\*\// ]
},
csharp: {
Expand All @@ -575,6 +603,7 @@ Docker.prototype.languages = {
},
php: {
extensions: [ 'php', 'php3', 'php4', 'php5' ],
executables: [ 'php' ],
comment: '//', multiLine: [ /\/\*/, /\*\// ], dox: true
},
actionscript: {
Expand All @@ -583,6 +612,7 @@ Docker.prototype.languages = {
},
sh: {
extensions: [ 'sh' ],
executables: [ 'bash', 'sh', 'zsh' ],
comment: '#'
},
yaml: {
Expand Down

4 comments on commit 758aed0

@peterhost
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfecto :) Works great !

I'd just add (not enough change for a pull request), just before line 357 :

//Don't process symbolic links
var isSymLink = fs.lstatSync(filename).isSymbolicLink();
if (isSymLink) return callback(false);

// If we should be processing all files, then yes, we should process this one
...

(because I just noticed an unhandled exception in this case)

Exception which is :

/usr/local/lib/node_modules/docker/src/docker.js:322
      if(err) throw err;
                    ^          

@jbt
Copy link
Owner Author

@jbt jbt commented on 758aed0 Aug 22, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unhandled exception eh? Oops, that doesn't sound good. I'll check it out and push a fix.

@peterhost
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i happen to have lots of code which reside in apache-like : script-available and script-enabled directories, the latter symlinking the former, and as soon as you pushed the shebang detection feature, I couldn't resist testing Docker on a huge script repo of mine :) That's how I just came about that.

Only exception I had. Once fixed, works like a charm

@jbt
Copy link
Owner Author

@jbt jbt commented on 758aed0 Aug 22, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, after testing a bit, I don't seem to be able to reproduce the linking bug. Tried various combinations of linking directories / files and can't get it to break. Any chance you could add any useful info (node version / exactly how the symlinks are laid out / anything else you think might be useful) over on #5 please?

Please sign in to comment.